source: projects/specs/trunk/l/lxc/lxc-vinelinux @ 9811

Revision 9811, 16.0 KB checked in by daisuke, 9 years ago (diff)

lxc: update to 1.1.4, add vinelinux template

  • Property svn:executable set to *
Line 
1#!/bin/bash
2
3#
4# template script for generating Vine Linux container for LXC
5#   (based on altlinux/centos template script)
6#
7
8#
9# lxc: linux Container library
10
11# Authors:
12# Daisuke SUZUKI <daisuke@vinelinux.org>
13
14# This library is free software; you can redistribute it and/or
15# modify it under the terms of the GNU Lesser General Public
16# License as published by the Free Software Foundation; either
17# version 2.1 of the License, or (at your option) any later version.
18
19# This library is distributed in the hope that it will be useful,
20# but WITHOUT ANY WARRANTY; without even the implied warranty of
21# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22# Lesser General Public License for more details.
23
24# You should have received a copy of the GNU Lesser General Public
25# License along with this library; if not, write to the Free Software
26# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27
28# Detect use under userns (unsupported)
29for arg in "$@"; do
30    [ "$arg" = "--" ] && break
31    if [ "$arg" = "--mapped-uid" -o "$arg" = "--mapped-gid" ]; then
32        echo "This template can't be used for unprivileged containers." 1>&2
33        echo "You may want to try the \"download\" template instead." 1>&2
34        exit 1
35    fi
36done
37
38# Make sure the usual locations are in PATH
39export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin
40
41# Configurations
42arch=$(uname -i)
43cache_base=/var/cache/lxc/vinelinux
44default_path=/var/lib/lxc
45default_profile=default
46profile_dir=/etc/lxc/profiles
47root_password=vineroot
48default_user=vine
49default_user_password=vine123
50lxc_network_type=veth
51lxc_network_link=lxcbr0
52
53# is this vinelinux?
54[ -f /etc/vine-release ] && is_vinelinux=true
55
56configure_vinelinux()
57{
58    # Set default localtime to the host localtime if not set...
59    if [ -e /etc/localtime -a ! -e ${rootfs_path}/etc/localtime ]
60    then
61        # if /etc/localtime is a symlink, this should preserve it.
62        cp -a /etc/localtime ${rootfs_path}/etc/localtime
63    fi
64
65    # create /lxcroot
66    touch ${rootfs_path}/lxcroot
67
68    # fix bxxxn damaged halt script.
69    if [ -f ${rootfs_path}/etc/init.d/halt ]
70    then
71        sed -e '/hwclock/,$d' \
72            < ${rootfs_path}/etc/init.d/halt \
73            > ${rootfs_path}/etc/init.d/lxc-halt
74
75        echo '$command -f' >> ${rootfs_path}/etc/init.d/lxc-halt
76        chmod 755 ${rootfs_path}/etc/init.d/lxc-halt
77
78        # Link them into the rc directories...
79        (
80             cd ${rootfs_path}/etc/rc.d/rc0.d
81             ln -s ../init.d/lxc-halt S00lxc-halt
82             cd ${rootfs_path}/etc/rc.d/rc6.d
83             ln -s ../init.d/lxc-halt S00lxc-reboot
84        )
85    fi
86
87    # configure the network using the dhcp
88    cat <<EOF > ${rootfs_path}/etc/sysconfig/network-scripts/ifcfg-eth0
89DEVICE=eth0
90BOOTPROTO=dhcp
91ONBOOT=yes
92HOSTNAME=${UTSNAME}
93NM_CONTROLLED=no
94TYPE=Ethernet
95MTU=${MTU}
96DHCP_HOSTNAME=\`hostname\`
97EOF
98
99    # set the hostname
100    cat <<EOF > ${rootfs_path}/etc/sysconfig/network
101NETWORKING=yes
102HOSTNAME=${UTSNAME}
103EOF
104
105    # set minimal hosts
106    cat <<EOF > $rootfs_path/etc/hosts
107127.0.0.1 localhost.localdomain localhost $name
108EOF
109
110    # set minimal fstab
111    cat <<EOF > $rootfs_path/etc/fstab
112/dev/root               /                       rootfs   defaults        0 0
113EOF
114
115    # create lxc compatibility init script
116    cat <<EOF > $rootfs_path/etc/init/lxc-sysinit.conf
117start on startup
118env container
119
120pre-start script
121        if [ "x\$container" != "xlxc" -a "x\$container" != "xlibvirt" ]; then
122                stop;
123        fi
124
125        rm -f /var/lock/subsys/*
126        rm -f /var/run/*.pid
127        [ -e /etc/mtab ] || ln -s /proc/mounts /etc/mtab
128        mkdir -p /dev/shm
129        mount -t tmpfs -o nosuid,nodev tmpfs /dev/shm
130
131        initctl start tty TTY=console
132        telinit 3
133        exit 0
134end script
135EOF
136
137    # Enable services
138    for service in network random
139    do
140       chroot ${rootfs_path} chkconfig $service --list &>/dev/null && chroot ${rootfs_path} chkconfig $service on || true
141    done
142
143    dev_path="${rootfs_path}/dev"
144    rm -rf ${dev_path}
145    mkdir -p ${dev_path}
146    mknod -m 666 ${dev_path}/null c 1 3
147    mknod -m 666 ${dev_path}/zero c 1 5
148    mknod -m 644 ${dev_path}/random c 1 8
149    mknod -m 644 ${dev_path}/urandom c 1 9
150    mkdir -m 755 ${dev_path}/pts
151    mkdir -m 1777 ${dev_path}/shm
152    mknod -m 666 ${dev_path}/tty c 5 0
153    chown root:tty ${dev_path}/tty
154    mknod -m 600 ${dev_path}/tty0 c 4 0
155    mknod -m 600 ${dev_path}/tty1 c 4 1
156    mknod -m 600 ${dev_path}/tty2 c 4 2
157    mknod -m 600 ${dev_path}/tty3 c 4 3
158    mknod -m 600 ${dev_path}/tty4 c 4 4
159    mknod -m 600 ${dev_path}/console c 5 1
160    mknod -m 666 ${dev_path}/full c 1 7
161    mknod -m 600 ${dev_path}/initctl p
162    mknod -m 666 ${dev_path}/ptmx c 5 2
163    chown root:tty ${dev_path}/ptmx
164    ln -s /proc/self/fd ${dev_path}/fd
165    ln -s /proc/kcore ${dev_path}/core
166    mkdir -m 755 ${dev_path}/mapper
167    mknod -m 600 ${dev_path}/mapper/control c 10 236
168    mkdir -m 755 ${dev_path}/net
169    mknod -m 666 ${dev_path}/net/tun c 10 200
170
171    # setup console and tty[1-4] for login. note that /dev/console and
172    # /dev/tty[1-4] will be symlinks to the ptys /dev/lxc/console and
173    # /dev/lxc/tty[1-4] so that package updates can overwrite the symlinks.
174    # lxc will maintain these links and bind mount ptys over /dev/lxc/*
175    # since lxc.devttydir is specified in the config.
176
177    # allow root login on console, tty[1-4], and pts/0 for libvirt
178    echo "# LXC (Linux Containers)" >>${rootfs_path}/etc/securetty
179    echo "lxc/console"  >>${rootfs_path}/etc/securetty
180    echo "lxc/tty1"     >>${rootfs_path}/etc/securetty
181    echo "lxc/tty2"     >>${rootfs_path}/etc/securetty
182    echo "lxc/tty3"     >>${rootfs_path}/etc/securetty
183    echo "lxc/tty4"     >>${rootfs_path}/etc/securetty
184    echo "# For libvirt/Virtual Machine Monitor" >>${rootfs_path}/etc/securetty
185    echo "pts/0"        >>${rootfs_path}/etc/securetty
186
187    # prevent mingetty from calling vhangup(2) since it fails with userns.
188    # Same issue as oracle template: prevent mingetty from calling vhangup(2)
189    # commit 2e83f7201c5d402478b9849f0a85c62d5b9f1589.
190    sed -i 's|mingetty|mingetty --nohangup|' $rootfs_path/etc/init/tty.conf
191
192    # set root password
193    echo "Setting root password to $root_password"
194    echo "root:$root_password" | chroot $rootfs_path chpasswd
195    # store root password
196    touch ${config_path}/tmp_root_pass
197    chmod 600 ${config_path}/tmp_root_pass
198    echo ${root_password} > ${config_path}/tmp_root_pass
199    echo "Storing root password in '${config_path}/tmp_root_pass'"
200
201    # create default user 'vine'
202    echo "Create default user 'vine'"
203    chroot ${rootfs_path} /usr/sbin/useradd -G wheel ${default_user}
204    echo "Setting default user \'${default_user}\' password to $default_user_password"
205    echo "${default_user}:${default_user_password}" | chroot $rootfs_path chpasswd
206    # store default user password
207    touch ${config_path}/tmp_user_pass
208    chmod 600 ${config_path}/tmp_user_pass
209    echo "username: ${default_user}" > ${config_path}/tmp_user_pass
210    echo "password: ${default_user_password}" >> ${config_path}/tmp_user_pass
211    echo "Storing default user infomation in '${config_path}/tmp_user_pass'"
212
213    return 0
214}
215
216download_vinelinux()
217{
218    # Default configuration
219    FETCH_URL="http://updates.vinelinux.org/apt"
220
221    # create cache dir
222    mkdir -p $cache
223   
224    # check target availability
225    if ! (vbootstrap | grep -q "${release}_${arch}"); then
226        echo "Specified release and/or arch is not supported, aborting."
227        return 1
228    fi
229    if [ "$(uname -i)" == "i386" ] && [ "${arch}" == "x86_64" ]; then
230        echo "x86_64 containers does not run on $(uname -i) host, aborting."
231        return 1
232    fi
233
234    # download a mini vinelinux into a cache
235    echo "Downloading vinelinux minimal ..."
236    VBOOTSTRAP="vbootstrap ${release}_${arch} ${FETCH_URL} $cache/partial"
237
238    $VBOOTSTRAP
239
240    if [ $? -ne 0 ]; then
241        echo "Failed to download the rootfs, aborting."
242        return 1
243    fi
244   
245    # install additional packages
246    PKG_LIST0="openssh-server openssh-clients etcskel sudo net-tools"
247    PKG_LIST="$(grep -hs '^[^#]' "$profile_dir/$profile")"
248    # if no configuration file $profile -- fall back to default list of packages
249    PKG_LIST="$PKG_LIST0 $PKG_LIST"
250    chroot $cache/partial apt-get -y install $PKG_LIST
251
252    if [ $? -ne 0 ]; then
253        echo "Failed to install additional packages to the rootfs, aborting."
254        return 1
255    fi
256   
257    mv "$cache/partial" "$cache/rootfs"
258    echo "Download complete."
259
260    return 0
261}
262
263copy_vinelinux()
264{
265
266    # make a local copy of the minivinelinux
267    echo -n "Copying rootfs to $rootfs_path ..."
268    #cp -a $cache/rootfs-$arch $rootfs_path || return 1
269    # i prefer rsync (no reason really)
270    mkdir -p $rootfs_path
271    rsync -Ha $cache/rootfs/ $rootfs_path/
272    return 0
273}
274
275update_vinelinux()
276{
277    chroot $cache/rootfs apt-get update
278    chroot $cache/rootfs apt-get -y dist-upgrade
279}
280
281install_vinelinux()
282{
283    mkdir -p /var/lock/subsys/
284    (
285        flock -x 9
286        if [ $? -ne 0 ]; then
287            echo "Cache repository is busy."
288            return 1
289        fi
290
291        echo "Checking cache download in $cache/rootfs ... "
292        if [ ! -e "$cache/rootfs" ]; then
293            download_vinelinux
294            if [ $? -ne 0 ]; then
295                echo "Failed to download 'vinelinux base'"
296                return 1
297            fi
298        else
299            echo "Cache found. Updating..."
300            update_vinelinux
301            if [ $? -ne 0 ]; then
302                echo "Failed to update 'vinelinux base', continuing with last known good cache"
303            else
304                echo "Update finished"
305            fi
306        fi
307
308        echo "Copy $cache/rootfs to $rootfs_path ... "
309        copy_vinelinux
310        if [ $? -ne 0 ]; then
311            echo "Failed to copy rootfs"
312            return 1
313        fi
314        return 0
315    ) 9>/var/lock/subsys/lxc-vinelinux
316
317    return $?
318}
319
320copy_configuration()
321{
322
323    mkdir -p $config_path
324    grep -q "^lxc.rootfs" $config_path/config 2>/dev/null || echo "lxc.rootfs = $rootfs_path" >> $config_path/config
325    cat <<EOF >> $config_path/config
326lxc.utsname = $name
327lxc.tty = 4
328lxc.pts = 1024
329lxc.cap.drop = sys_module mac_admin mac_override sys_time
330EOF
331
332    if [ "$arch" == "i386" ] && [ "$(uname -i)" == "x86_64" ]; then
333        cat <<EOF >> $config_path/config
334
335# lxc container architecture
336lxc.arch = x86
337EOF
338
339    fi
340
341    cat <<EOF >> $config_path/config
342# When using LXC with apparmor, uncomment the next line to run unconfined:
343#lxc.aa_profile = unconfined
344
345#networking
346lxc.network.type = $lxc_network_type
347lxc.network.flags = up
348lxc.network.link = $lxc_network_link
349lxc.network.name = veth0
350lxc.network.mtu = 1500
351EOF
352    if [ ! -z ${ipv4} ]; then
353        cat <<EOF >> $config_path/config
354lxc.network.ipv4 = $ipv4
355EOF
356    fi
357    if [ ! -z ${gw} ]; then
358        cat <<EOF >> $config_path/config
359lxc.network.ipv4.gateway = $gw
360EOF
361    fi
362#if [ ! -z ${ipv6} ]; then
363#    cat <<EOF >> $config_path/config
364#lxc.network.ipv6 = $ipv6
365#EOF
366#fi
367#if [ ! -z ${gw6} ]; then
368#    cat <<EOF >> $config_path/config
369#lxc.network.ipv6.gateway = $gw6
370#EOF
371#fi
372    cat <<EOF >> $config_path/config
373#cgroups
374lxc.cgroup.devices.deny = a
375# /dev/null and zero
376lxc.cgroup.devices.allow = c 1:3 rwm
377lxc.cgroup.devices.allow = c 1:5 rwm
378# consoles
379lxc.cgroup.devices.allow = c 5:1 rwm
380lxc.cgroup.devices.allow = c 5:0 rwm
381lxc.cgroup.devices.allow = c 4:0 rwm
382lxc.cgroup.devices.allow = c 4:1 rwm
383# /dev/{,u}random
384lxc.cgroup.devices.allow = c 1:9 rwm
385lxc.cgroup.devices.allow = c 1:8 rwm
386lxc.cgroup.devices.allow = c 136:* rwm
387lxc.cgroup.devices.allow = c 5:2 rwm
388# rtc
389lxc.cgroup.devices.allow = c 10:135 rwm
390
391lxc.mount.auto = cgroup:mixed proc:mixed sys:mixed
392EOF
393
394    if [ $? -ne 0 ]; then
395        echo "Failed to add configuration"
396        return 1
397    fi
398
399    return 0
400}
401
402clean()
403{
404
405    if [ ! -e $cache ]; then
406        exit 0
407    fi
408
409    # lock, so we won't purge while someone is creating a repository
410    (
411        flock -x 9
412        if [ $? != 0 ]; then
413            echo "Cache repository is busy."
414            exit 1
415        fi
416
417        echo -n "Purging the download cache for Vine Linux $release..."
418        rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
419        exit 0
420    ) 9>/var/lock/subsys/lxc-vinelinux
421}
422
423usage()
424{
425    cat <<EOF
426usage:
427    $1 -n|--name=<container_name>
428        [-p|--path=<path>] [-c|--clean] [-R|--release=<Vine Linux release>]
429        [-4|--ipv4=<ipv4 address>]
430        [-g|--gw=<gw address>] [-d|--dns=<dns address>]
431        [-u|--user=<user>] [--password=<password>]
432        [-P|--profile=<name of the profile>] [--rootfs=<path>]
433        [-A|--arch=<arch of the container>]
434        [-h|--help]
435Mandatory args:
436  -n,--name         container name, used to as an identifier for that container from now on
437Optional args:
438  -p,--path         path to where the container rootfs will be created, defaults to /var/lib/lxc. The container config will go under /var/lib/lxc in that case
439  -c,--clean        clean the cache
440  -R,--release      Vine Linux release [VineSeed, 6] for the new container, defaults to VineSeed
441  -4,--ipv4         specify the ipv4 address to assign to the virtualized interface, eg. 192.168.1.123/24
442  -g,--gw           specify the default gw, eg. 192.168.1.1
443  -d,--dns          specify the DNS server, eg. 192.168.1.2
444  -P,--profile      Profile name is the file name in /etc/lxc/profiles contained packages name for install to cache.
445  -A,--arch         Define what arch the container will be [i386,x86_64]
446  ---rootfs         rootfs path
447  -h,--help         print this help
448EOF
449    return 0
450}
451
452options=$(getopt -o hp:n:P:cR:4:g:d:u:A: -l help,rootfs:,path:,name:,profile:,clean,release:,ipv4:,gw:,dns:,user:,password:,arch: -- "$@")
453if [ $? -ne 0 ]; then
454    usage $(basename $0)
455    exit 1
456fi
457eval set -- "$options"
458
459while true
460do
461    case "$1" in
462        -h|--help)      usage $0 && exit 0;;
463        -p|--path)      path=$2; shift 2;;
464        --rootfs)       rootfs_path=$2; shift 2;;
465        -n|--name)      name=$2; shift 2;;
466        -P|--profile)   profile=$2; shift 2;;
467        -c|--clean)     clean=1; shift 1;;
468        -R|--release)   release=$2; shift 2;;
469        -A|--arch)      arch=$2; shift 2;;
470        -4|--ipv4)      ipv4=$2; shift 2;;
471        -g|--gw)        gw=$2; shift 2;;
472        -d|--dns)       dns=$2; shift 2;;
473        -u|--user)      default_user=$2; shift 2;;
474        --password)     default_user_password=$2; shift 2;;
475        --)             shift 1; break ;;
476        *)              break ;;
477    esac
478done
479
480if [ ! -z "$clean" -a -z "$path" ]; then
481    clean || exit 1
482    exit 0
483fi
484
485type apt-get >/dev/null 2>&1
486if [ $? -ne 0 ]; then
487    echo "'apt-get' command is missing"
488    exit 1
489fi
490
491type vbootstrap >/dev/null 2>&1
492if [ $? -ne 0 ]; then
493    echo "'vbootstrap' command is missing"
494    exit 1
495fi
496
497if [ -z "$path" ]; then
498    path=$default_path
499fi
500
501if [ -z "$profile" ]; then
502    profile=$default_profile
503fi
504
505if [ -z "$release" ]; then
506    release="VineSeed"
507fi
508
509if [ -z "$ipv4" ]; then
510    BOOTPROTO="dhcp"
511else
512    BOOTPROTO="static"
513fi
514
515if [ -z "$default_user" ]; then
516    default_user="vine"
517fi
518
519if [ -z "$default_user_password" ]; then
520    default_user_password="vine123"
521fi
522
523if [ -z "$arch" ]; then
524    arch="$(uname -i)"
525fi
526
527
528if [ "$(id -u)" != "0" ]; then
529    echo "This script should be run as 'root'"
530    exit 1
531fi
532
533# check for 'lxc.rootfs' passed in through default config by lxc-create
534if [ -z "$rootfs_path" ]; then
535    if grep -q '^lxc.rootfs' $path/config 2>/dev/null ; then
536        rootfs_path=$(awk -F= '/^lxc.rootfs =/{ print $2 }' $path/config)
537    else
538        rootfs_path=$path/rootfs
539    fi
540fi
541
542config_path=$default_path/$name
543cache=$cache_base/$arch/$release/$profile
544
545install_vinelinux
546if [ $? -ne 0 ]; then
547    echo "failed to install vinelinux"
548    exit 1
549fi
550
551configure_vinelinux
552if [ $? -ne 0 ]; then
553    echo "failed to configure vinelinux for a container"
554    exit 1
555fi
556
557copy_configuration
558if [ $? -ne 0 ]; then
559    echo "failed write configuration file"
560    exit 1
561fi
562
563if [ ! -z "$clean" ]; then
564    clean || exit 1
565    exit 0
566fi
567echo "container rootfs and config created"
568echo "network configured as $lxc_network_type in the $lxc_network_link"
Note: See TracBrowser for help on using the repository browser.