/usr/bin/lxc-start-ephemeral is in lxc 0.7.5-3ubuntu52.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | #!/bin/bash
# (C) Copyright Canonical 2011,2012
# What lxc container to clone
LXC_BASE=""
# $2 is a path to bind mount e.g. /tmp/foo.
LXC_BIND=""
UNION="overlayfs"
usage() {
echo "usage: lxc-start-ephemeral [-U uniontype] [-d|--daemon] [-h] [-b bdir] [-u user] [-S key] -o orig -- [COMMAND [ARGS...]]"
}
help() {
usage
echo
echo "Runs an ephemeral (one-off) container"
echo
echo "Options:"
echo "orig : name of the original container"
echo "bdir : directory to bind mount into container"
echo "user : the user to connect to the container as"
echo "key : the path to the SSH key to use to connect"
echo "-d : run in the background"
echo "-U : type of union (aufs or overlayfs)"
echo " Default is overlayfs"
echo
echo "if a COMMAND is given, then the container will run only as long"
echo "as the command runs. If no COMMAND is given, this command will"
echo "wait until the container is shut down"
}
shortoptions='hb:o:u:S:dU:'
longoptions='help,orig:,bdir:,user:,ssh-key:,daemon,union:'
LXC_RUNNING=0
LXC_MOUNTED=0
DAEMON=0
cleanup() {
if [ $LXC_RUNNING -eq 1 ]; then
sudo lxc-stop -n $LXC_NAME
fi
if [ $LXC_MOUNTED -eq 1 ]; then
sudo umount $EPHEMERAL_BIND_DIR
sudo umount $LXC_DIR
sudo umount $OVERLAY_DIR
sudo rmdir $LXC_DIR
sudo rmdir $OVERLAY_DIR
fi
exit 1
}
do_mount() {
lower=$1
upper=$2
target=$3
if [ $UNION = "aufs" ]; then
sudo mount -t aufs -o br=${upper}=rw:${lower}=ro,noplink none ${target}
else
sudo mount -t overlayfs -oupperdir=${upper},lowerdir=${lower} none ${target}
fi
}
trap cleanup SIGTERM SIGINT SIGQUIT
getopt=$(getopt -o $shortoptions --longoptions $longoptions -- "$@")
if [ $? != 0 ]; then
usage
exit 1;
fi
eval set -- "$getopt"
while true; do
case "$1" in
-h|--help)
help
exit 1
;;
-o|--orig)
shift
LXC_BASE=$1
shift
;;
-b|--bdir)
shift
LXC_BIND=$1
shift
;;
-u|--user)
shift
LXC_USER=$1
shift
;;
-S|--ssh-key)
shift
LXC_KEY="-i $1"
shift
;;
-d|--detach)
DAEMON=1
shift
;;
-U|--union)
shift
UNION=$1
shift
;;
--)
shift
break;;
*)
echo $1
usage
exit 1
;;
esac
done
COMMAND=$@
COMMAND_LENGTH=$#
LXC_USER=${LXC_USER:-`id -un`}
# validation
if [ -z $LXC_BASE ]; then
echo "original container must be specified"
usage
exit 1
fi
if [ ! -d /var/lib/lxc/$LXC_BASE ] ; then
echo 'no such lxc container $LXC_BASE'
exit 1
fi
if [ "$UNION" != "overlayfs" -a "$UNION" != "aufs" ]; then
echo "Invalid option for union: choose overlayfs or aufs."
exit 1
fi
setup_container()
{
echo "Setting up ephemeral container..."
OVERLAY_DIR=`mktemp -d /tmp/lxc-lp-XXXXXXX`
sudo mount -t tmpfs none $OVERLAY_DIR
LXC_DIR=`sudo mktemp -d --tmpdir=/var/lib/lxc $LXC_BASE-temp-XXXXXXX`
LXC_NAME=`basename $LXC_DIR`
do_mount "/var/lib/lxc/$LXC_BASE" "${OVERLAY_DIR}" $LXC_DIR
EPHEMERAL_BIND_DIR=$LXC_DIR/ephemeralbind
sudo mkdir $EPHEMERAL_BIND_DIR
sudo mount -t tmpfs none $EPHEMERAL_BIND_DIR
LXC_MOUNTED=1
# Update the ephemeral lxc's configuration to reflect the new container name.
sudo sed -i -e "s/$LXC_BASE/$LXC_NAME/" $LXC_DIR/fstab $LXC_DIR/config $LXC_DIR/rootfs/etc/hostname $LXC_DIR/rootfs/etc/hosts
# Update the fstab to have all bind mounts be ephemeral.
sudo cp $LXC_DIR/fstab $LXC_DIR/fstab.old
while read line; do
# Pull out the second field of the current line of fstab info
path=`echo -n $line | awk '{print $2}'`
# If LXC_BIND is not set, or the mount destination of this line is not
# LXC_BIND...
if [ -z "$LXC_BIND" -o "`readlink -f $path`" != "`readlink -f $LXC_DIR/rootfs$LXC_BIND`" ];
then
# ...then we should write some form of this line.
# If this line is a bind...
if [ `echo -n $line | awk '{print $4}'` == "bind" ]; then
# ...we should rewrite it as an overlay.
source=`echo -n $line | awk '{print $1}'`
upperdir=$EPHEMERAL_BIND_DIR$source
sudo mkdir -p $upperdir
sudo chown `sudo stat -c '%U.%G' $source` $upperdir
if [ $UNION = "overlayfs" ]; then
echo "none $path overlayfs upperdir=$upperdir,lowerdir=$source 0 0";
else
echo "none $path aufs br=${upperdir}=rw:${lowerdir}=ro,noplink 0 0";
fi
else
# Otherwise, we can pass it through unchanged.
echo $line;
fi
fi
done < $LXC_DIR/fstab.old > $LXC_DIR/fstab
# If LXC_BIND is defined, add it to fstab.
if [ -n "$LXC_BIND" ]; then
sudo mkdir -p $LXC_DIR/rootfs$LXC_BIND
echo "$LXC_BIND $LXC_DIR/rootfs$LXC_BIND none bind 0 0" >> $LXC_DIR/fstab
fi
# update the ephemeral container's MAC address (lifted from lxc-clone)
c=$LXC_DIR/config
# change hwaddrs
mv ${c} ${c}.old
(
while read line; do
if [ "${line:0:18}" = "lxc.network.hwaddr" ]; then
echo "lxc.network.hwaddr= 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')"
else
echo $line
fi
done
) < ${c}.old > ${c}
rm -f ${c}.old
# precise is now the worst - its dhclient *uses*
# /var/lib/dhcp3/dhclient.eth0.leases but ships without that
# directory, so you get no leases file at all.
LEASES=$LXC_DIR/rootfs/var/lib/dhcp3/dhclient.eth0.leases
LEASES2=$LXC_DIR/rootfs/var/lib/dhcp/dhclient.leases
sudo mkdir -p $LXC_DIR/rootfs/var/lib/dhcp3
sudo mkdir -p $LXC_DIR/rootfs/var/lib/dhcp
sudo truncate -c -s0 $LEASES
sudo truncate -c -s0 $LEASES2
}
start_container()
{
echo "Starting up the container..."
sudo lxc-start -n $LXC_NAME -d
LXC_RUNNING=1
if [ $COMMAND_LENGTH -gt 0 ]; then
# when lxc-attach support arrives in the kernel, we can switch to
# that
delay=30
while [ $delay -gt 0 -a ! -s $LEASES -a ! -s $LEASES2 ]
do
delay=$(( $delay - 1 ))
sleep 1
done
[ -s $LEASES ] || LEASES=$LEASES2
IP_ADDRESS=`sudo grep fixed-address $LEASES | tail -n 1 | sed -r 's/.* ([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}).*/\1/'`
if [ 0 -eq $? -a -n "$IP_ADDRESS" ]; then
# Repeatedly try to connect over SSH until we either succeed
# or time out.
for i in $(seq 1 30); do
ssh -n -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $LXC_KEY $LXC_USER@$IP_ADDRESS -- "$COMMAND"
if [ ! 255 -eq $? ]; then
# If ssh returns 255 then its connection failed.
# Anything else is either success (status 0) or a
# failure from whatever we ran over the SSH connection.
# In those cases we want to stop looping, so we break
# here.
break;
fi
sleep 1
done
else
echo "could not get IP address - aborting." >&2
echo "content of $LEASES:" >&2
cat $LEASES >&2
echo "content of $LEASES2:" >&2
cat $LEASES2 >&2
fi
else
sudo lxc-wait -n $LXC_NAME -s RUNNING
echo "$LXC_NAME is running"
echo "You connect with the command:"
echo " sudo lxc-console -n $LXC_NAME"
sudo lxc-wait -n $LXC_NAME -s STOPPED
fi
}
stop_container()
{
echo "Stopping lxc" >&2
sudo lxc-stop -n $LXC_NAME
sleep 2
# echo "umounting ephemeral_bind_dir $EPHEMERAL_BIND_DIR" >&2
sudo umount $EPHEMERAL_BIND_DIR
# echo "umounting lxc_dir $LXC_DIR" >&2
sudo umount $LXC_DIR
# echo "umounting overlay" >&2
sudo umount $OVERLAY_DIR
# echo "rming lxc_dir $LXC_DIR" >&2
sudo rmdir $LXC_DIR
# echo "rming overlay dir $OVERLAY_DIR" >&2
sudo rmdir $OVERLAY_DIR
}
handle_container()
{
setup_container
start_container
stop_container
exit 0
}
if [ $DAEMON -eq 1 ]; then
handle_container &
exit 0
fi
handle_container
|