lxc-start-ephemeral.in revision 2ae6732f6b351ddbd299678fec2c43d02faef5e0
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
_ = gettext.gettext
# Other functions
print("lxc-start-ephemeral is deprecated in favor of lxc-copy\n",
*args, file=sys.stderr)
random.randint(0x00, 0x7f),
random.randint(0x00, 0xff),
random.randint(0x00, 0xff)]
return "@RUNTIME_PATH@"
return os.environ["XDG_RUNTIME_DIR"]
# Inform that lxc-start-ephemeral is deprecated
# Begin parsing the command line
"LXC: Start an ephemeral container"),
"""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."""))
"either --bdir=/src-path or --bdir=/src-path:/dst-path"))
"(when using ssh)"))
"defaults to overlayfs."))
"(command as argument)"))
# Check that -d and CMD aren't used at the same time
# Check that -k isn't used with -s tmpfs
else:
# Load the orig container
if not orig.defined:
# Create the new container paths
if not args.lxcpath:
else:
if args.name:
else:
# Setup the new container's configuration
# Generate a new fstab
# Start by replacing any reference to the container rootfs
# Skip invalid entries
continue
# Non-bind mounts are kept as-is
continue
# Bind mounts of virtual filesystems are also kept as-is
continue
# Skip invalid mount points
print(_("Skipping mount entry '%s' as it's outside "
# Setup an overlay for anything remaining
overlay_dirs += [(fields[0], dest_mount)]
print(_("Path '%s' doesn't exist, won't be cow-mounted.") %
else:
overlay_dirs += [(src_path, dst_path)]
# do we have the new overlay fs which requires workdir, or the older
# overlayfs which does not?
if line == "nodev\toverlay\n":
# Generate pre-mount script
LXC_DIR="%s"
LXC_BASE="%s"
LXC_NAME="%s"
count = 0
if have_new_overlay:
if have_new_overlay:
" -oupperdir=%s,lowerdir=%s,workdir=%s none %s\n" % (
entry[0],
entry[1]))
else:
" -oupperdir=%s,lowerdir=%s none %s\n" % (
entry[0],
entry[1]))
"-o br=%s=rw:%s=ro,noplink,xino=%s none %s\n" % (
entry[0],
entry[1]))
count += 1
else:
print(_("Path '%s' doesn't exist, won't be bind-mounted.") %
else:
[ -e $LXC_DIR/configured ] && exit 0
done
touch $LXC_DIR/configured
""")
# Start the container
if dest.defined:
sys.exit(1)
# Deal with the case where we just attach to the container's console
sys.exit(0)
# Try to get the IP addresses
# Deal with the case where we just print info about the container
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:
sys.exit(0)
# Now deal with the case where we want to run a command in the container
if dest.defined:
sys.exit(1)
try:
username = "root"
if args.user:
sys.exit(1)
else:
cmd = ["ssh",
"-o", "StrictHostKeyChecking=no",
if args.user:
if args.key:
if retval == 255:
print(_("SSH failed to connect, trying next IP address."))
continue
if retval != 0:
break
# Shutdown the container