Le script ci-dessous est utilisé pour créer plusieurs conteneurs pour des étudiants. Chaque étudiant ce connecte ensuite au conteneur en SSH. La machine initiale est créée avec le script lxc-debian
fourni par debian unstable en ajoutant les paquets :
à la liste des paquets installés.
#!/bin/bash # Copyright (C) 2011 by Sylvain Collilieux <Sylvain@Collilieux.net> # # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. uid=$(id -u) if [ "$uid" != 0 ] ; then echo 'ERROR, you have to be root !!!' exit 1 fi lxc_root='/var/lib/lxc' fruits="abricot airelle alkekenge ananas arbouse banane bergamote bigarade canneberge cassis cerise citron clementine coing datte figue fraise framboise grenade groseille jujube kiwi lime mandarine melon mirabelle mure myrtille orange pamplemousse papaye pasteque peche poire pomme prune raisin" # finish install cat <<EOF ################################################### Installation des paquets nécessaires ################################################### EOF apt-get install apache2 nagios3 lxc iproute ssh bridge-utils apt-cacher-ng debootstrap vim less w3m echo "deb http://ftp.univ-nantes.fr/debian squeeze main" > /etc/apt/sources.list echo 'Acquire::http{ Proxy "http://localhost:3142"; };' > /etc/apt/apt.conf.d/01proxy apt-get update # configure network cat <<EOF >> /etc/network/interfaces # bridge for lxc iface br0 inet static pre-up ip link add type veth bridge_ports veth0 bridge_fd 0 bridge_maxwait 0 address 192.168.30.254 netmask 255.255.255.0 EOF echo 1 > /proc/sys/net/ipv4/ip_forward echo 'net.ipv4.ip_forward=1' > /etc/sysctl.d/local.conf # Start lxc at boot time perl -i -pe "s/#RUN=yes/RUN=yes/" /etc/default/lxc cat <<EOF ################################################### Création du containeur maître ################################################### EOF # prepare container mkdir -p $lxc_root/master mkdir -p /cgroup echo "cgroup /cgroup cgroup defaults 0 0">> /etc/fstab mount -a # create main container export MIRROR='http://ftp.univ-nantes.fr/debian' ./lxc-debian -p $lxc_root/master cat <<EOF ################################################### Création des containeurs élève ################################################### EOF # create a container per fruit based on master i=1 for hostname in $fruits do # create account for fruits on host useradd --gid users --create-home --shell /bin/bash --password $(openssl passwd -1 $hostname) $hostname # create container from master cp -r $lxc_root/master $lxc_root/$hostname # replace all reference to master by hostname perl -i -pe "s/master/$hostname/" $lxc_root/$hostname/config # generate a private MAC address mac=$(printf "AC:DE:48:E1:E1:%x" $i) # configure hostname and network cat <<EOF >> $lxc_root/$hostname/config lxc.utsname = $hostname lxc.network.hwaddr = $mac lxc.network.ipv4 = 192.168.30.$i lxc.network.veth.pair = veth10$i EOF # Configure DEBIAN interfaces with static address perl -i -pe "s/dhcp/static/" $lxc_root/$hostname/rootfs/etc/network/interfaces cat <<EOF >> $lxc_root/$hostname/rootfs/etc/network/interfaces address 192.168.30.$i netmask 255.255.255.0 gateway 192.168.30.254 broadcast 192.168.30.255 EOF # host machine as proxy echo 'Acquire::http{ Proxy "http://192.168.30.254:3142"; };' >> $lxc_root/$hostname/rootfs/etc/apt/apt.conf.d/01proxy i=$((i+1)) done cat <<EOF ################################################### Fini ! ################################################### EOF
Création de l'interface sur la machine.
sudo ip link add type veth sudo ip addr add 192.168.30.254/24 dev veth1