Commit fb7460fe by Daniel Lezcano Committed by Daniel Lezcano

cleanup lxc-debian script

The lxc-debian is epurated and consolidated with a better error handling. This script is no longer interactive but it installs in a specified place the debian rootfs. This script is not supposed to be called directly so it will fall in libexec path very soon. This script is called by lxc-create as a template with the right option and the right place. The debian network configuration is by dhcp. Signed-off-by: 's avatarDaniel Lezcano <dlezcano@fr.ibm.com>
parent 1d6b1976
#!/bin/bash #!/bin/bash
# set -ex
CACHE="@LOCALSTATEDIR@/cache/lxc/debian"
NAME="debian"
CONFFILE="lxc.conf"
MNTFILE=
TMPMNTFILE=
UTSNAME=
IPV4="172.20.0.21"
GATEWAY="172.20.0.1"
MTU="1500"
# These paths are within the container so do not need to obey configure prefixes
INTERFACES="/etc/network/interfaces"
INITTAB="/etc/inittab"
HOSTNAME="/etc/hostname"
FSTAB="/etc/fstab"
SSHD_CONFIG="/etc/ssh/sshd_config"
PROFILE="/etc/profile"
################################################################################
# debian custom configuration files
################################################################################
# custom selinux
write_debian_selinux() {
mkdir -p $ROOTFS/selinux
echo 0 > $ROOTFS/selinux/enforce
}
# custom fstab #
# lxc: linux Container library
write_debian_fstab() { # Authors:
cat <<EOF > $ROOTFS/$FSTAB # Daniel Lezcano <daniel.lezcano@free.fr>
tmpfs /dev/shm tmpfs defaults 0 0
EOF
}
# custom inittab # This library is free software; you can redistribute it and/or
# 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.
write_debian_inittab() { # This library is distributed in the hope that it will be useful,
cat <<EOF > $ROOTFS/$INITTAB # 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
configure_debian()
{
rootfs=$1
hostname=$2
# configure the inittab
cat <<EOF > $rootfs/etc/inittab
id:3:initdefault: id:3:initdefault:
si::sysinit:/etc/init.d/rcS si::sysinit:/etc/init.d/rcS
l0:0:wait:/etc/init.d/rc 0 l0:0:wait:/etc/init.d/rc 0
...@@ -60,35 +44,13 @@ c2:12345:respawn:/sbin/getty 38400 tty2 linux ...@@ -60,35 +44,13 @@ c2:12345:respawn:/sbin/getty 38400 tty2 linux
c3:12345:respawn:/sbin/getty 38400 tty3 linux c3:12345:respawn:/sbin/getty 38400 tty3 linux
c4:12345:respawn:/sbin/getty 38400 tty4 linux c4:12345:respawn:/sbin/getty 38400 tty4 linux
EOF EOF
}
# custom network configuration
write_debian_network() {
cat <<EOF > $ROOTFS/$INTERFACES
auto eth0 lo
iface eth0 inet static
address $IPV4
netmask 255.255.255.0
broadcast 0.0.0.0
mtu $MTU
up route add default gw $GATEWAY
iface lo inet loopback
EOF
}
# custom hostname
write_debian_hostname() {
cat <<EOF > $ROOTFS/$HOSTNAME
$UTSNAME
EOF
}
# custom sshd configuration file # disable selinux in debian
mkdir -p $rootfs/selinux
echo 0 > $rootfs/selinux/enforce
write_debian_sshd_config() { # by default setup root password with no password
cat <<EOF > $ROOTFS/$SSHD_CONFIG cat <<EOF > $rootfs/etc/ssh/sshd_config
Port 22 Port 22
Protocol 2 Protocol 2
HostKey /etc/ssh/ssh_host_rsa_key HostKey /etc/ssh/ssh_host_rsa_key
...@@ -109,317 +71,252 @@ HostbasedAuthentication no ...@@ -109,317 +71,252 @@ HostbasedAuthentication no
PermitEmptyPasswords yes PermitEmptyPasswords yes
ChallengeResponseAuthentication no ChallengeResponseAuthentication no
EOF EOF
}
reconfigure_debian_packages() {
chroot $ROOTFS /usr/sbin/dpkg-reconfigure locales
}
disable_debian_services() {
chroot $ROOTFS /usr/sbin/update-rc.d -f umountfs remove
chroot $ROOTFS /usr/sbin/update-rc.d -f hwclock.sh remove
chroot $ROOTFS /usr/sbin/update-rc.d -f hwclockfirst.sh remove
}
################################################################################ # configure the network using the dhcp
# lxc configuration files cat <<EOF > $rootfs/etc/network/interfaces
################################################################################ auto lo
iface lo inet loopback
write_lxc_configuration() { auto eth0
cat <<EOF > $CONFFILE iface eth0 inet dhcp
lxc.utsname = $UTSNAME
lxc.tty = 4
lxc.pts = 1024
lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = br0
lxc.network.name = eth0
lxc.network.mtu = $MTU
lxc.rootfs = $ROOTFS
lxc.cgroup.devices.deny = a
# /dev/null and zero
lxc.cgroup.devices.allow = c 1:3 rwm
lxc.cgroup.devices.allow = c 1:5 rwm
# consoles
lxc.cgroup.devices.allow = c 5:1 rwm
lxc.cgroup.devices.allow = c 5:0 rwm
lxc.cgroup.devices.allow = c 4:0 rwm
lxc.cgroup.devices.allow = c 4:1 rwm
# /dev/{,u}random
lxc.cgroup.devices.allow = c 1:9 rwm
lxc.cgroup.devices.allow = c 1:8 rwm
lxc.cgroup.devices.allow = c 136:* rwm
lxc.cgroup.devices.allow = c 5:2 rwm
# rtc
lxc.cgroup.devices.allow = c 254:0 rwm
EOF EOF
if [ ! -z "$TMPMNTFILE" ]; then # set the hostname
cat $TMPMNTFILE >> $CONFFILE cat <<EOF > $rootfs/etc/hostname
fi $hostname
} EOF
write_lxc_mounts() { # reconfigure some services
chroot $rootfs /usr/sbin/dpkg-reconfigure locales
if [ ! -z "$MNTFILE" ]; then # remove pointless services in a container
TMPMNTFILE=$(mktemp lxc.$NAME.XXXXXXXXXX) chroot $rootfs /usr/sbin/update-rc.d -f umountfs remove
sed -e 's/^\(.*\)/lxc.mount.entry=&/' $MNTFILE >$TMPMNTFILE chroot $rootfs /usr/sbin/update-rc.d -f hwclock.sh remove
fi chroot $rootfs /usr/sbin/update-rc.d -f hwclockfirst.sh remove
} }
collect_information() { download_debian()
# choose a container name, default is 'debian' {
echo -n "What is the name for the container ? [$NAME] " packages=\
read _NAME_ ifupdown,\
locales,\
if [ ! -z "$_NAME_" ]; then libui-dialog-perl,\
NAME=$_NAME_ dialog,\
fi dhcp-client,\
netbase,\
# choose a hostname, default is the container name net-tools,\
echo -n "What hostname do you wish for this container ? [$NAME] " iproute,\
read _UTSNAME_ openssh-server
if [ ! -z "$_UTSNAME_" ]; then cache=$1
UTSNAME=$_UTSNAME_ arch=$2
else
UTSNAME=$NAME
fi
# choose an ipv4 address, better to choose the same network than
# your host
echo -n "What IP address do you wish for this container ? [$IPV4] "
read _IPV4_
if [ ! -z "$_IPV4_" ]; then
IPV4=$_IPV4_
fi
# choose the gateway ip address
echo -n "What is the gateway IP address ? [$GATEWAY] "
read _GATEWAY_
if [ ! -z "$_GATEWAY_" ]; then # check the mini debian was not already downloaded
GATEWAY=$_GATEWAY_ mkdir -p "$cache/partial-$arch"
if [ $? -ne 0 ]; then
echo "Failed to create '$cache/partial-$arch' directory"
return 1
fi fi
# choose the MTU size # download a mini debian into a cache
echo -n "What is the MTU size ? [$MTU] " echo "Downloading debian minimal ..."
read _MTU_ debootstrap --verbose --variant=minbase --arch=$arch \
--include $packages \
if [ ! -z "$_MTU_" ]; then lenny $cache/partial-$arch http://ftp.debian.org/debian
MTU=$_MTU_ if [ $? -ne 0 ]; then
echo "Failed to download the rootfs, aborting."
return 1
fi fi
ROOTFS="./rootfs.$NAME" mv "$1/partial-$arch" "$1/rootfs-$arch"
# choose the rootfs echo "Download complete."
echo -n "Specify the location of the rootfs [$ROOTFS] "
read _ROOTFS_
if [ ! -z "$_ROOTFS_" ]; then return 0
ROOTFS=$_ROOTFS_ }
fi
echo -n "Specify the location for an extra fstab file [(none)] " copy_debian()
read _MNTFILE_ {
cache=$1
arch=$2
rootfs=$3
if [ ! -z "$_MNTFILE_" ]; then # make a local copy of the minidebian
MNTFILE=$_MNTFILE_ echo -n "Copying rootfs to $rootfs..."
fi cp -a $cache/rootfs-$arch $rootfs || return 1
return 0
} }
install_debian() install_debian()
{ {
# check if the rootfs does already exist cache="@LOCALSTATEDIR@/cache/lxc/debian"
if [ ! -e "$ROOTFS" ]; then rootfs=$1
mkdir -p @LOCALSTATEDIR@/lock/subsys/ mkdir -p @LOCALSTATEDIR@/lock/subsys/
( (
flock -n -x 200 flock -n -x 200
if [ $? -ne 0 ]; then
RES=$?
if [ "$RES" != "0" ]; then
echo "Cache repository is busy." echo "Cache repository is busy."
break return 1
fi fi
echo "Choose your architecture" arch=$(arch)
select ARCH in amd64 i386; do if [ "$arch" == "x86_64" ]; then
echo "Architecture $ARCH selected" arch=amd64
break;
done
# check the mini debian was not already downloaded
echo -n "Checking cache download ..."
if [ ! -e "$CACHE/rootfs-$ARCH" ]; then
echo "not cached"
mkdir -p "$CACHE/partial-$ARCH"
# download a mini debian into a cache
echo "Downloading debian minimal ..."
debootstrap --verbose --variant=minbase --arch=$ARCH \
--include ifupdown,locales,libui-dialog-perl,dialog,apache2,netbase,net-tools,iproute,openssh-server \
lenny $CACHE/partial-$ARCH http://ftp.debian.org/debian
RESULT=$?
if [ "$RESULT" != "0" ]; then
echo "Failed to download the rootfs, aborting."
exit 1
fi
mv "$CACHE/partial-$ARCH" "$CACHE/rootfs-$ARCH"
echo "Download complete."
else
echo "Found."
fi
# make a local copy of the minidebian
echo -n "Copying rootfs ..."
cp -a $CACHE/rootfs-$ARCH $ROOTFS && echo "Done." || exit
) 200> "@LOCALSTATEDIR@/lock/subsys/lxc"
fi fi
} if [ "$arch" == "i686" ]; then
arch=i386
create() {
collect_information
install_debian
write_lxc_mounts
write_lxc_configuration
write_debian_inittab
write_debian_hostname
write_debian_fstab
write_debian_network
write_debian_sshd_config
write_debian_selinux
reconfigure_debian_packages
disable_debian_services
@BINDIR@/lxc-create -n $NAME -f $CONFFILE
RES=$?
# remove the configuration files
rm -f $CONFFILE
rm -f $TMPMNTFILE
if [ "$RES" != "0" ]; then
echo "Failed to create '$NAME'"
exit 1
fi fi
echo "Done." echo "Checking cache download in $cache/rootfs-$arch ... "
echo -e "\nYou can run your container with the 'lxc-start -n $NAME'\n" if [ ! -e "$cache/rootfs-$arch" ]; then
} download_debian $cache $arch
if [ $? -ne 0 ]; then
destroy() { echo "Failed to download 'debian base'"
return 1
echo -n "What is the name for the container ? [$NAME] "
read _NAME_
if [ ! -z "$_NAME_" ]; then
NAME=$_NAME_
fi fi
@BINDIR@/lxc-destroy -n $NAME
RETVAL=$?
if [ ! $RETVAL -eq 0 ]; then
echo "Failed to destroyed '$NAME'"
return $RETVAL;
fi fi
ROOTFS="./rootfs.$NAME" copy_debian $cache $arch $rootfs
if [ $? -ne 0 ]; then
echo -n "Shall I remove the rootfs [y/n] ? " echo "Failed to copy rootfs"
read return 1
if [ "$REPLY" = "y" ]; then
rm -rf $ROOTFS
fi fi
return 0 return 0
}
help() {
cat <<EOF
This script is a helper to create debian system containers.
The script will create the container configuration file following ) 200>@LOCALSTATEDIR@/lock/subsys/lxc
the informations submitted interactively with 'lxc-debian create'
The first creation will download, with debootstrap, a debian return $?
minimal and store it into a cache. }
The script will copy from the cache the root filesystem to the
current directory.
If there is a problem with the container, (bad configuration for
example), you can destroy the container with 'lxc-debian destroy'
but without removing the rootfs and recreate it again with
'lxc-debian create'.
If you want to create another debian container, call the 'lxc-debian copy_configuration()
create' again, specifying another name and new parameters. {
path=$1
rootfs=$2
name=$3
At any time you can purge the debian cache download by calling cat <<EOF >> $path/config
'lxc-debian purge' lxc.tty = 4
lxc.pts = 1024
lxc.rootfs = $rootfs
lxc.cgroup.devices.deny = a
# /dev/null and zero
lxc.cgroup.devices.allow = c 1:3 rwm
lxc.cgroup.devices.allow = c 1:5 rwm
# consoles
lxc.cgroup.devices.allow = c 5:1 rwm
lxc.cgroup.devices.allow = c 5:0 rwm
lxc.cgroup.devices.allow = c 4:0 rwm
lxc.cgroup.devices.allow = c 4:1 rwm
# /dev/{,u}random
lxc.cgroup.devices.allow = c 1:9 rwm
lxc.cgroup.devices.allow = c 1:8 rwm
lxc.cgroup.devices.allow = c 136:* rwm
lxc.cgroup.devices.allow = c 5:2 rwm
# rtc
lxc.cgroup.devices.allow = c 254:0 rwm
EOF
Have fun :) if [ $? -ne 0 ]; then
echo "Failed to add configuration"
return 1
fi
EOF return 0
} }
purge() { clean()
{
cache="@LOCALSTATEDIR@/cache/lxc/debian"
if [ ! -e $CACHE ]; then if [ ! -e $cache ]; then
exit 0 exit 0
fi fi
# lock, so we won't purge while someone is creating a repository # lock, so we won't purge while someone is creating a repository
( (
flock -n -x 200 flock -n -x 200
if [ $? != 0 ]; then
RES=$?
if [ "$RES" != "0" ]; then
echo "Cache repository is busy." echo "Cache repository is busy."
exit 1 exit 1
fi fi
echo -n "Purging the download cache..." echo -n "Purging the download cache..."
rm --preserve-root --one-file-system -rf $CACHE && echo "Done." || exit 1 rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
exit 0 exit 0
) 200> "@LOCALSTATEDIR@/lock/subsys/lxc" ) 200>@LOCALSTATEDIR@/lock/subsys/lxc
}
usage()
{
cat <<EOF
$1 -h|--help -p|--path=<path> --clean
EOF
return 0
} }
options=$(getopt -o hp:n:c -l help,path:,name:,clean -- "$@")
if [ $? -ne 0 ]; then
usage $(basename $0)
exit 1
fi
eval set -- "$options"
while true
do
case "$1" in
-h|--help) usage $0 && exit 0;;
-p|--path) path=$2; shift 2;;
-n|--name) name=$2; shift 2;;
-c|--clean) clean=$2; shift 2;;
--) shift 1; break ;;
*) break ;;
esac
done
if [ ! -z "$clean" -a -z "$path" ]; then
clean || exit 1
exit 0
fi
type debootstrap
if [ $? -ne 0 ]; then
echo "'debootstrap' command is missing"
exit 1
fi
if [ -z "$path" ]; then
echo "'path' parameter is required"
exit 1
fi
if [ "$(id -u)" != "0" ]; then if [ "$(id -u)" != "0" ]; then
echo "This script should be run as 'root'" echo "This script should be run as 'root'"
exit 1 exit 1
fi fi
case "$1" in rootfs=$path/rootfs
create)
create;; install_debian $rootfs
destroy) if [ $? -ne 0 ]; then
destroy;; echo "failed to install debian"
help) exit 1
help;; fi
purge)
purge;; configure_debian $rootfs $name
*) if [ $? -ne 0 ]; then
echo "Usage: $0 {create|destroy|purge|help}" echo "failed to configure debian for a container"
exit 1;; exit 1
esac fi
copy_configuration $path $rootfs
if [ $? -ne 0 ]; then
echo "failed write configuration file"
exit 1
fi
if [ ! -z $clean ]; then
clean || exit 1
exit 0
fi
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment