lxc-start-ephemeral.in revision 250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77
#
# 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
#
# 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
"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."""))
"defaults to overlayfs."))
"(command as argument)"))
# Basic requirements check
## Check that -d and CMD aren't used at the same time
## Check that -k isn't used with -s tmpfs
else:
## The user needs to be uid 0
# 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 any line that's not a bind mount
continue
continue
# Process any remaining line
continue
print(_("Skipping mount entry '%s' as it's outside "
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)
# 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)
# NOTE: To replace by .attach() once the kernel supports it
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