lxc-start-ephemeral.in revision 95a717e9b907de94bf29c91abca13012ec47d088
#
# lxc-start-ephemeral: Start a copy of a container using an overlay
#
# This python implementation is based on the work done in the original
# shell implementation done by Serge Hallyn in Ubuntu (and other contributors)
#
# (C) Copyright Canonical Ltd. 2012
#
# Authors:
# Stéphane Graber <stgraber@ubuntu.com>
#
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# NOTE: To remove once the API is stabilized
_ = gettext.gettext
# Other functions
random.randint(0x00, 0x7f),
random.randint(0x00, 0xff),
random.randint(0x00, 0xff)]
# Begin parsing the command line
"""If a COMMAND is given, then the container will run only as long
as the command runs.
If no COMMAND is given, this command will attach to tty1 and stop the
container when exiting (with ctrl-a-q).
If no COMMAND is given and -d is used, the name and IP addresses of the
container will be printed to the console."""))
# Basic requirements check
## Check that -d and CMD aren't used at the same time
print(_("You can't use -d and a command at the same time."))
sys.exit(1)
## The user needs to be uid 0
print(_("You must be root to run this script. Try running: sudo %s" %
sys.exit(1)
# Load the orig container
if not orig.defined:
sys.exit(1)
# Create the new container paths
# Setup the new container's configuration
# Generate a new fstab
# Start by replacing any reference to the container rootfs
# Skip any line that's not a bind mount
continue
continue
# Process any remaining line
continue
print(_(
overlay_dirs += [(fields[0], dest_mount)]
# Generate pre-mount script
LXC_DIR="%s"
LXC_BASE="%s"
LXC_NAME="%s"
count = 0
" -oupperdir=%s,lowerdir=%s none %s\n" % (
entry[0],
entry[1]))
"-o br=${upper}=rw:${lower}=ro,noplink none %s\n" % (
entry[0],
entry[1]))
count += 1
if args.bdir:
print(_("Path '%s' doesn't exist, won't be bind-mounted.") %
else:
[ -e $LXC_DIR/configured ] && exit 0
done
touch $LXC_DIR/configured
""")
# Generate post-stop script
[ -d "%s" ] && rm -Rf "%s"
# Start the container
if dest.defined:
sys.exit(1)
# Try to get the IP addresses
# Deal with the case where we don't start a command in the container
if not args.command:
if args.daemon:
print(_("""The ephemeral container is now started.
You can enter it from the command line with: lxc-console -n %s
The following IP addresses have be found in the container:
%s""") % (
))
sys.exit(0)
else:
dest.console(tty=1)
sys.exit(0)
# Now deal with the case where we want to run a command in the container
if dest.defined:
sys.exit(1)
# NOTE: To replace by .attach() once the kernel supports it
"-o", "StrictHostKeyChecking=no",
if args.user:
if args.key:
ssh_cmd = cmd + [ip] + args.command
if retval == 255:
print(_("SSH failed to connect, trying next IP address."))
continue
if retval != 0:
break
# Shutdown the container