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 # 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.
# 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.
# custom inittab # 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
write_debian_inittab() { configure_debian()
cat <<EOF > $ROOTFS/$INITTAB {
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 # disable selinux in debian
mkdir -p $rootfs/selinux
write_debian_network() { echo 0 > $rootfs/selinux/enforce
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 # by default setup root password with no password
cat <<EOF > $rootfs/etc/ssh/sshd_config
write_debian_sshd_config() {
cat <<EOF > $ROOTFS/$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() { # configure the network using the dhcp
chroot $ROOTFS /usr/sbin/dpkg-reconfigure locales cat <<EOF > $rootfs/etc/network/interfaces
} auto lo
iface lo inet loopback
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
}
################################################################################
# lxc configuration files
################################################################################
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 # check the mini debian was not already downloaded
fi mkdir -p "$cache/partial-$arch"
if [ $? -ne 0 ]; then
# choose an ipv4 address, better to choose the same network than echo "Failed to create '$cache/partial-$arch' directory"
# your host return 1
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
GATEWAY=$_GATEWAY_
fi
# choose the MTU size
echo -n "What is the MTU size ? [$MTU] "
read _MTU_
if [ ! -z "$_MTU_" ]; then
MTU=$_MTU_
fi fi
ROOTFS="./rootfs.$NAME" # download a mini debian into a cache
# choose the rootfs echo "Downloading debian minimal ..."
echo -n "Specify the location of the rootfs [$ROOTFS] " debootstrap --verbose --variant=minbase --arch=$arch \
read _ROOTFS_ --include $packages \
lenny $cache/partial-$arch http://ftp.debian.org/debian
if [ ! -z "$_ROOTFS_" ]; then if [ $? -ne 0 ]; then
ROOTFS=$_ROOTFS_ echo "Failed to download the rootfs, aborting."
return 1
fi fi
echo -n "Specify the location for an extra fstab file [(none)] " mv "$1/partial-$arch" "$1/rootfs-$arch"
read _MNTFILE_ echo "Download complete."
if [ ! -z "$_MNTFILE_" ]; then return 0
MNTFILE=$_MNTFILE_
fi
} }
install_debian() copy_debian()
{ {
# check if the rootfs does already exist cache=$1
if [ ! -e "$ROOTFS" ]; then arch=$2
mkdir -p @LOCALSTATEDIR@/lock/subsys/ rootfs=$3
(
flock -n -x 200
RES=$?
if [ "$RES" != "0" ]; then
echo "Cache repository is busy."
break
fi
echo "Choose your architecture"
select ARCH in amd64 i386; do
echo "Architecture $ARCH selected"
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
# make a local copy of the minidebian
echo -n "Copying rootfs to $rootfs..."
cp -a $cache/rootfs-$arch $rootfs || return 1
return 0
} }
create() { install_debian()
{
collect_information cache="@LOCALSTATEDIR@/cache/lxc/debian"
rootfs=$1
install_debian mkdir -p @LOCALSTATEDIR@/lock/subsys/
(
write_lxc_mounts flock -n -x 200
if [ $? -ne 0 ]; then
write_lxc_configuration echo "Cache repository is busy."
return 1
write_debian_inittab fi
write_debian_hostname
write_debian_fstab
write_debian_network
write_debian_sshd_config
write_debian_selinux arch=$(arch)
if [ "$arch" == "x86_64" ]; then
arch=amd64
fi
reconfigure_debian_packages if [ "$arch" == "i686" ]; then
arch=i386
fi
disable_debian_services echo "Checking cache download in $cache/rootfs-$arch ... "
if [ ! -e "$cache/rootfs-$arch" ]; then
download_debian $cache $arch
if [ $? -ne 0 ]; then
echo "Failed to download 'debian base'"
return 1
fi
fi
@BINDIR@/lxc-create -n $NAME -f $CONFFILE copy_debian $cache $arch $rootfs
RES=$? if [ $? -ne 0 ]; then
echo "Failed to copy rootfs"
return 1
fi
# remove the configuration files return 0
rm -f $CONFFILE
rm -f $TMPMNTFILE
if [ "$RES" != "0" ]; then ) 200>@LOCALSTATEDIR@/lock/subsys/lxc
echo "Failed to create '$NAME'"
exit 1
fi
echo "Done." return $?
echo -e "\nYou can run your container with the 'lxc-start -n $NAME'\n"
} }
destroy() { copy_configuration()
{
echo -n "What is the name for the container ? [$NAME] " path=$1
read _NAME_ rootfs=$2
name=$3
if [ ! -z "$_NAME_" ]; then
NAME=$_NAME_
fi
@BINDIR@/lxc-destroy -n $NAME
RETVAL=$?
if [ ! $RETVAL -eq 0 ]; then
echo "Failed to destroyed '$NAME'"
return $RETVAL;
fi
ROOTFS="./rootfs.$NAME" cat <<EOF >> $path/config
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
echo -n "Shall I remove the rootfs [y/n] ? " if [ $? -ne 0 ]; then
read echo "Failed to add configuration"
if [ "$REPLY" = "y" ]; then return 1
rm -rf $ROOTFS
fi fi
return 0 return 0
} }
help() { clean()
cat <<EOF {
cache="@LOCALSTATEDIR@/cache/lxc/debian"
This script is a helper to create debian system containers.
The script will create the container configuration file following
the informations submitted interactively with 'lxc-debian create'
The first creation will download, with debootstrap, a debian
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
create' again, specifying another name and new parameters.
At any time you can purge the debian cache download by calling
'lxc-debian purge'
Have fun :)
EOF
}
purge() {
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
} }
if [ "$(id -u)" != "0" ]; then usage()
echo "This script should be run as 'root'" {
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 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
echo "This script should be run as 'root'"
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