#! /usr/bin/bash

hostname="$1"
fs="$2"
shift 2

cmd="$1"
shift
echo -n "cmd: $cmd"
for word in "$@"; do
	echo -n " ! $word"
done
echo

# HOSTNAME
hostname "$hostname"

# MOUNT
# Change current directory to the container's filesystem
cd "$fs"
# Mount special filesystems
mount -t proc proc proc
mount -t sysfs none sys
mount -t tmpfs none tmp

# Block waiting for veth pair end and IP address
read vethitf ipaddr <&3

# Notice that sudo is not needed here: we are mapped to the root user
ip link set "$vethitf" up
ip addr add "$ipaddr" dev "$vethitf"
# Set the default route through the veth device
ip route add default dev "$vethitf"

# Execute chroot and make it execute the container's command
exec chroot . "$cmd" "$@"

