lxc-ubuntu: Add extra options

This adds support for "packages", "user" and "password"

Signed-off-by: Guilhem Lettron <guilhem.lettron@optiflows.com>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
This commit is contained in:
Guilhem Lettron 2013-10-31 16:54:55 +01:00 committed by Stéphane Graber
parent 853d58fdf5
commit 2004e7da82

View File

@ -35,6 +35,8 @@ configure_ubuntu()
rootfs=$1 rootfs=$1
hostname=$2 hostname=$2
release=$3 release=$3
user=$4
password=$5
# configure the network using the dhcp # configure the network using the dhcp
cat <<EOF > $rootfs/etc/network/interfaces cat <<EOF > $rootfs/etc/network/interfaces
@ -76,8 +78,8 @@ EOF
fi fi
if [ -z "$bindhome" ]; then if [ -z "$bindhome" ]; then
chroot $rootfs useradd --create-home -s /bin/bash ubuntu chroot $rootfs useradd --create-home -s /bin/bash $user
echo "ubuntu:ubuntu" | chroot $rootfs chpasswd echo "$user:$password" | chroot $rootfs chpasswd
fi fi
# make sure we have the current locale defined in the container # make sure we have the current locale defined in the container
@ -224,8 +226,7 @@ download_ubuntu()
cache=$1 cache=$1
arch=$2 arch=$2
release=$3 release=$3
packages=$4
packages=vim,ssh
# Try to guess a list of langpacks to install # Try to guess a list of langpacks to install
langpacks="language-pack-en" langpacks="language-pack-en"
@ -330,7 +331,7 @@ install_ubuntu()
echo "Checking cache download in $cache/rootfs-$arch ... " echo "Checking cache download in $cache/rootfs-$arch ... "
if [ ! -e "$cache/rootfs-$arch" ]; then if [ ! -e "$cache/rootfs-$arch" ]; then
download_ubuntu $cache $arch $release download_ubuntu $cache $arch $release $packages
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "Failed to download 'ubuntu $release base'" echo "Failed to download 'ubuntu $release base'"
return 1 return 1
@ -554,18 +555,19 @@ usage()
cat <<EOF cat <<EOF
$1 -h|--help [-a|--arch] [-b|--bindhome <user>] [-d|--debug] $1 -h|--help [-a|--arch] [-b|--bindhome <user>] [-d|--debug]
[-F | --flush-cache] [-r|--release <release>] [ -S | --auth-key <keyfile>] [-F | --flush-cache] [-r|--release <release>] [ -S | --auth-key <keyfile>]
[--rootfs <rootfs>] [--rootfs <rootfs>] [--packages <packages>] [-u|--user <user>] [--password <password>]
release: the ubuntu release (e.g. precise): defaults to host release on ubuntu, otherwise uses latest LTS release: the ubuntu release (e.g. precise): defaults to host release on ubuntu, otherwise uses latest LTS
bindhome: bind <user>'s home into the container bindhome: bind <user>'s home into the container
The ubuntu user will not be created, and <user> will have The ubuntu user will not be created, and <user> will have
sudo access. sudo access.
arch: the container architecture (e.g. amd64): defaults to host arch arch: the container architecture (e.g. amd64): defaults to host arch
auth-key: SSH Public key file to inject into container auth-key: SSH Public key file to inject into container
packages: list of packages to add comma separated
EOF EOF
return 0 return 0
} }
options=$(getopt -o a:b:hp:r:n:FS:d -l arch:,bindhome:,help,path:,release:,name:,flush-cache,auth-key:,debug,rootfs: -- "$@") options=$(getopt -o a:b:hp:r:n:FS:du: -l arch:,bindhome:,help,path:,release:,name:,flush-cache,auth-key:,debug,rootfs:,packages:,user:,password: -- "$@")
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
usage $(basename $0) usage $(basename $0)
exit 1 exit 1
@ -601,6 +603,9 @@ fi
debug=0 debug=0
hostarch=$arch hostarch=$arch
flushcache=0 flushcache=0
packages="vim,ssh"
user="ubuntu"
password="ubuntu"
while true while true
do do
case "$1" in case "$1" in
@ -608,8 +613,11 @@ do
--rootfs) rootfs=$2; shift 2;; --rootfs) rootfs=$2; shift 2;;
-p|--path) path=$2; shift 2;; -p|--path) path=$2; shift 2;;
-n|--name) name=$2; shift 2;; -n|--name) name=$2; shift 2;;
-u|--user) user=$2; shift 2;;
--password) password=$2; shift 2;;
-F|--flush-cache) flushcache=1; shift 1;; -F|--flush-cache) flushcache=1; shift 1;;
-r|--release) release=$2; shift 2;; -r|--release) release=$2; shift 2;;
--packages) packages=$2; shift 2;;
-b|--bindhome) bindhome=$2; shift 2;; -b|--bindhome) bindhome=$2; shift 2;;
-a|--arch) arch=$2; shift 2;; -a|--arch) arch=$2; shift 2;;
-S|--auth-key) auth_key=$2; shift 2;; -S|--auth-key) auth_key=$2; shift 2;;
@ -675,13 +683,13 @@ if [ -z "$rootfs" ]; then
fi fi
fi fi
install_ubuntu $rootfs $release $flushcache install_ubuntu $rootfs $release $flushcache $packages
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "failed to install ubuntu $release" echo "failed to install ubuntu $release"
exit 1 exit 1
fi fi
configure_ubuntu $rootfs $name $release configure_ubuntu $rootfs $name $release $user $password
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "failed to configure ubuntu $release for a container" echo "failed to configure ubuntu $release for a container"
exit 1 exit 1
@ -699,7 +707,7 @@ if [ -n "$bindhome" ]; then
do_bindhome $rootfs $bindhome do_bindhome $rootfs $bindhome
finalize_user $bindhome finalize_user $bindhome
else else
finalize_user ubuntu finalize_user $user
fi fi
echo "" echo ""
@ -707,7 +715,7 @@ echo "##"
if [ -n "$bindhome" ]; then if [ -n "$bindhome" ]; then
echo "# Log in as user $bindhome" echo "# Log in as user $bindhome"
else else
echo "# The default user is 'ubuntu' with password 'ubuntu'!" echo "# The default user is '$user' with password '$password'!"
echo "# Use the 'sudo' command to run tasks as root in the container." echo "# Use the 'sudo' command to run tasks as root in the container."
fi fi
echo "##" echo "##"