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

Revision 9918, 17.0 KB checked in by daisuke, 8 years ago (diff)

lxc/lxc-vinelinux

  • 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
47lxc_network_type=veth
48lxc_network_link=lxcbr0
49
50# is this vinelinux?
51[ -f /etc/vine-release ] && is_vinelinux=true
52
53configure_vinelinux()
54{
55    # Set default localtime to the host localtime if not set...
56    if [ -e /etc/localtime -a ! -e ${rootfs_path}/etc/localtime ]
57    then
58        # if /etc/localtime is a symlink, this should preserve it.
59        cp -a /etc/localtime ${rootfs_path}/etc/localtime
60    fi
61
62    # create /lxcroot
63    touch ${rootfs_path}/lxcroot
64
65    # fix bxxxn damaged halt script.
66    if [ -f ${rootfs_path}/etc/init.d/halt ]
67    then
68        sed -e '/hwclock/,$d' \
69            < ${rootfs_path}/etc/init.d/halt \
70            > ${rootfs_path}/etc/init.d/lxc-halt
71
72        echo '$command -f' >> ${rootfs_path}/etc/init.d/lxc-halt
73        chmod 755 ${rootfs_path}/etc/init.d/lxc-halt
74
75        # Link them into the rc directories...
76        (
77             cd ${rootfs_path}/etc/rc.d/rc0.d
78             ln -s ../init.d/lxc-halt S00lxc-halt
79             cd ${rootfs_path}/etc/rc.d/rc6.d
80             ln -s ../init.d/lxc-halt S00lxc-reboot
81        )
82    fi
83
84    # configure the network using the dhcp
85    cat <<EOF > ${rootfs_path}/etc/sysconfig/network-scripts/ifcfg-eth0
86DEVICE=eth0
87BOOTPROTO=dhcp
88ONBOOT=yes
89HOSTNAME=${UTSNAME}
90NM_CONTROLLED=no
91TYPE=Ethernet
92MTU=${MTU}
93DHCP_HOSTNAME=\`hostname\`
94EOF
95
96    # set the hostname
97    cat <<EOF > ${rootfs_path}/etc/sysconfig/network
98NETWORKING=yes
99HOSTNAME=${UTSNAME}
100EOF
101
102    # set minimal hosts
103    cat <<EOF > $rootfs_path/etc/hosts
104127.0.0.1 localhost.localdomain localhost $name
105EOF
106
107    # set minimal fstab
108    cat <<EOF > $rootfs_path/etc/fstab
109/dev/root               /                       rootfs   defaults        0 0
110EOF
111
112    # create lxc compatibility init script
113    cat <<EOF > $rootfs_path/etc/init/lxc-sysinit.conf
114start on startup
115env container
116
117pre-start script
118        if [ "x\$container" != "xlxc" -a "x\$container" != "xlibvirt" ]; then
119                stop;
120        fi
121
122        rm -f /var/lock/subsys/*
123        rm -f /var/run/*.pid
124        [ -e /etc/mtab ] || ln -s /proc/mounts /etc/mtab
125        mkdir -p /dev/shm
126        mount -t tmpfs -o nosuid,nodev tmpfs /dev/shm
127
128        initctl start tty TTY=console
129        telinit 3
130        exit 0
131end script
132EOF
133
134    # Enable services
135    for service in network random
136    do
137       chroot ${rootfs_path} chkconfig $service --list &>/dev/null && chroot ${rootfs_path} chkconfig $service on || true
138    done
139
140    dev_path="${rootfs_path}/dev"
141    rm -rf ${dev_path}
142    mkdir -p ${dev_path}
143    mknod -m 666 ${dev_path}/null c 1 3
144    mknod -m 666 ${dev_path}/zero c 1 5
145    mknod -m 644 ${dev_path}/random c 1 8
146    mknod -m 644 ${dev_path}/urandom c 1 9
147    mkdir -m 755 ${dev_path}/pts
148    mkdir -m 1777 ${dev_path}/shm
149    mknod -m 666 ${dev_path}/tty c 5 0
150    chown root:tty ${dev_path}/tty
151    mknod -m 600 ${dev_path}/tty0 c 4 0
152    mknod -m 600 ${dev_path}/tty1 c 4 1
153    mknod -m 600 ${dev_path}/tty2 c 4 2
154    mknod -m 600 ${dev_path}/tty3 c 4 3
155    mknod -m 600 ${dev_path}/tty4 c 4 4
156    mknod -m 600 ${dev_path}/console c 5 1
157    mknod -m 666 ${dev_path}/full c 1 7
158    mknod -m 600 ${dev_path}/initctl p
159    mknod -m 666 ${dev_path}/ptmx c 5 2
160    chown root:tty ${dev_path}/ptmx
161    ln -s /proc/self/fd ${dev_path}/fd
162    ln -s /proc/kcore ${dev_path}/core
163    mkdir -m 755 ${dev_path}/mapper
164    mknod -m 600 ${dev_path}/mapper/control c 10 236
165    mkdir -m 755 ${dev_path}/net
166    mknod -m 666 ${dev_path}/net/tun c 10 200
167
168    # setup console and tty[1-4] for login. note that /dev/console and
169    # /dev/tty[1-4] will be symlinks to the ptys /dev/lxc/console and
170    # /dev/lxc/tty[1-4] so that package updates can overwrite the symlinks.
171    # lxc will maintain these links and bind mount ptys over /dev/lxc/*
172    # since lxc.devttydir is specified in the config.
173
174    # allow root login on console, tty[1-4], and pts/0 for libvirt
175    echo "# LXC (Linux Containers)" >>${rootfs_path}/etc/securetty
176    echo "lxc/console"  >>${rootfs_path}/etc/securetty
177    echo "lxc/tty1"     >>${rootfs_path}/etc/securetty
178    echo "lxc/tty2"     >>${rootfs_path}/etc/securetty
179    echo "lxc/tty3"     >>${rootfs_path}/etc/securetty
180    echo "lxc/tty4"     >>${rootfs_path}/etc/securetty
181    echo "# For libvirt/Virtual Machine Monitor" >>${rootfs_path}/etc/securetty
182    echo "pts/0"        >>${rootfs_path}/etc/securetty
183
184    # prevent mingetty from calling vhangup(2) since it fails with userns.
185    # Same issue as oracle template: prevent mingetty from calling vhangup(2)
186    # commit 2e83f7201c5d402478b9849f0a85c62d5b9f1589.
187    sed -i 's|mingetty|mingetty --nohangup|' $rootfs_path/etc/init/tty.conf
188
189    # set root password
190    echo "Setting root password to $root_password"
191    echo "root:$root_password" | chroot $rootfs_path chpasswd
192    # store root password
193    touch ${config_path}/tmp_root_pass
194    chmod 600 ${config_path}/tmp_root_pass
195    echo ${root_password} > ${config_path}/tmp_root_pass
196    echo "Storing root password in '${config_path}/tmp_root_pass'"
197
198    # create default user.
199    echo "Create default user '${default_user}'"
200    chroot ${rootfs_path} /usr/sbin/useradd -G wheel ${default_user}
201    echo "Setting default user \'${default_user}\' password to $default_user_password"
202    echo "${default_user}:${default_user_password}" | chroot $rootfs_path chpasswd
203    # store default user password
204    touch ${config_path}/tmp_user_pass
205    chmod 600 ${config_path}/tmp_user_pass
206    echo "username: ${default_user}" > ${config_path}/tmp_user_pass
207    echo "password: ${default_user_password}" >> ${config_path}/tmp_user_pass
208    echo "Storing default user infomation in '${config_path}/tmp_user_pass'"
209
210    return 0
211}
212
213download_vinelinux()
214{
215    # Default configuration
216    FETCH_URL="http://updates.vinelinux.org/apt"
217
218    # create cache dir
219    mkdir -p $cache
220   
221    # check target availability
222    if ! (vbootstrap | grep -q "${release}_${arch}"); then
223        echo "Specified release and/or arch is not supported, aborting."
224        return 1
225    fi
226    if [ "$(uname -i)" == "i386" ] && [ "${arch}" == "x86_64" ]; then
227        echo "x86_64 containers does not run on $(uname -i) host, aborting."
228        return 1
229    fi
230
231    # download a mini vinelinux into a cache
232    echo "Downloading vinelinux minimal ..."
233    VBOOTSTRAP="vbootstrap ${release}_${arch} ${FETCH_URL} $cache/partial"
234
235    $VBOOTSTRAP
236
237    if [ $? -ne 0 ]; then
238        echo "Failed to download the rootfs, aborting."
239        return 1
240    fi
241   
242    # install additional packages
243    PKG_LIST0="openssh-server openssh-clients etcskel sudo net-tools"
244    PKG_LIST="$(grep -hs '^[^#]' "$profile_dir/$profile")"
245    # if no configuration file $profile -- fall back to default list of packages
246    PKG_LIST="$PKG_LIST0 $PKG_LIST"
247    chroot $cache/partial apt-get -y install $PKG_LIST
248
249    if [ $? -ne 0 ]; then
250        echo "Failed to install additional packages to the rootfs, aborting."
251        return 1
252    fi
253   
254    mv "$cache/partial" "$cache/rootfs"
255    echo "Download complete."
256
257    return 0
258}
259
260copy_vinelinux()
261{
262
263    # make a local copy of the minivinelinux
264    echo -n "Copying rootfs to $rootfs_path ..."
265    # prefer rsync
266    mkdir -p $rootfs_path
267    if [ -x /usr/bin/rsync ]; then
268      rsync -Ha $cache/rootfs/ $rootfs_path/
269    else
270      echo "rsync is not found, using cp instead."
271      cp -a $cache/rootfs-$arch $rootfs_path
272    fi
273    return 0
274}
275
276update_vinelinux()
277{
278    chroot $cache/rootfs apt-get update
279    chroot $cache/rootfs apt-get -y dist-upgrade
280}
281
282install_vinelinux()
283{
284    mkdir -p /var/lock/subsys/
285    (
286        flock -x 9
287        if [ $? -ne 0 ]; then
288            echo "Cache repository is busy."
289            return 1
290        fi
291
292        echo "Checking cache download in $cache/rootfs ... "
293        if [ ! -e "$cache/rootfs" ]; then
294            download_vinelinux
295            if [ $? -ne 0 ]; then
296                echo "Failed to download 'vinelinux base'"
297                return 1
298            fi
299        else
300            echo "Cache found. Updating..."
301            update_vinelinux
302            if [ $? -ne 0 ]; then
303                echo "Failed to update 'vinelinux base', continuing with last known good cache"
304            else
305                echo "Update finished"
306            fi
307        fi
308
309        echo "Copy $cache/rootfs to $rootfs_path ... "
310        copy_vinelinux
311        if [ $? -ne 0 ]; then
312            echo "Failed to copy rootfs"
313            return 1
314        fi
315        return 0
316    ) 9>/var/lock/subsys/lxc-vinelinux
317
318    return $?
319}
320
321create_hwaddr()
322{
323    openssl rand -hex 5 | sed -e 's/\(..\)/:\1/g; s/^/fe/'
324}
325
326copy_configuration()
327{
328    mkdir -p $config_path
329
330    grep -q "^lxc.rootfs" $config_path/config 2>/dev/null || echo "
331lxc.rootfs = $rootfs_path
332" >> $config_path/config
333
334    # The following code is to create static MAC addresses for each
335    # interface in the container.  This code will work for multiple
336    # interfaces in the default config.
337    mv $config_path/config $config_path/config.def
338    while read LINE
339    do
340        # This should catch variable expansions from the default config...
341        if expr "${LINE}" : '.*\$' > /dev/null 2>&1
342        then
343                LINE=$(eval "echo \"${LINE}\"")
344        fi
345
346        # There is a tab and a space in the regex bracket below!
347        # Seems that \s doesn't work in brackets.
348        KEY=$(expr "${LINE}" : '\s*\([^  ]*\)\s*=')
349
350        if [[ "${KEY}" != "lxc.network.hwaddr" ]]
351        then
352            echo ${LINE} >> $config_path/config
353
354            if [[ "${KEY}" == "lxc.network.link" ]]
355            then
356                echo "lxc.network.hwaddr = $(create_hwaddr)" >> $config_path/config
357            fi
358        fi
359    done < $config_path/config.def
360   
361    rm -f $config_path/config.def
362
363    # static network settings
364    if [ ! -z ${ipv4} ]; then
365        cat <<EOF >> $config_path/config
366lxc.network.ipv4 = $ipv4
367EOF
368    fi
369    if [ ! -z ${gw} ]; then
370        cat <<EOF >> $config_path/config
371lxc.network.ipv4.gateway = $gw
372EOF
373    fi
374#if [ ! -z ${ipv6} ]; then
375#    cat <<EOF >> $config_path/config
376#lxc.network.ipv6 = $ipv6
377#EOF
378#fi
379#if [ ! -z ${gw6} ]; then
380#    cat <<EOF >> $config_path/config
381#lxc.network.ipv6.gateway = $gw6
382#EOF
383#fi
384
385    # include common configuration
386    if [ -e "/usr/share/lxc/config/vinelinux.common.conf" ]; then
387        echo "
388# Include common configuration
389lxc.include = /usr/share/lxc/config/vinelinux.common.conf
390" >> $config_path/config
391    fi
392
393    # append lxc.utsname
394    echo "lxc.utsname = $utsname" >> $config_path/config
395    if [ "$arch" == "i386" ] && [ "$(uname -i)" == "x86_64" ]; then
396        cat <<EOF >> $config_path/config
397# lxc container architecture
398lxc.arch = x86
399EOF
400
401    fi
402
403    if [ $? -ne 0 ]; then
404        echo "Failed to add configuration"
405        return 1
406    fi
407
408    return 0
409}
410
411clean()
412{
413
414    if [ ! -e $cache ]; then
415        exit 0
416    fi
417
418    # lock, so we won't purge while someone is creating a repository
419    (
420        flock -x 9
421        if [ $? != 0 ]; then
422            echo "Cache repository is busy."
423            exit 1
424        fi
425
426        echo -n "Purging the download cache for Vine Linux $release..."
427        rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
428        exit 0
429    ) 9>/var/lock/subsys/lxc-vinelinux
430}
431
432usage()
433{
434    cat <<EOF
435usage:
436    $1 -n|--name=<container_name>
437        [-p|--path=<path>] [-c|--clean]
438        [-R|--release=<Vine Linux release>]
439        [-A|--arch=<arch of the container>]
440        [-4|--ipv4=<ipv4 address>]
441        [-g|--gw=<gw address>] [-d|--dns=<dns address>]
442        [-u|--user=<user>] [--password=<password>]
443        [-P|--profile=<name of the profile>] [--rootfs=<path>]
444        [-h|--help]
445Mandatory args:
446  -n,--name         container name, used to as an identifier for that container from now on
447Optional args:
448  -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
449  -c,--clean        clean the cache
450  -R,--release      Vine Linux release [VineSeed, 6] for the new container, defaults to VineSeed
451  -A,--arch         Define what arch the container will be [i386,x86_64]
452  -4,--ipv4         specify the ipv4 address to assign to the virtualized interface, eg. 192.168.1.123/24
453  -g,--gw           specify the default gw, eg. 192.168.1.1
454  -d,--dns          specify the DNS server, eg. 192.168.1.2
455  -u,--user         specify default user name, who can sudo.
456  --password        initial password for default user.
457  --fqdn            fully qualified domain name (FQDN) for DNS and system naming.
458  --rootpassword    initial password for root user.
459  -P,--profile      Profile name is the file name in /etc/lxc/profiles contained packages name for install to cache.
460  ---rootfs         rootfs path
461  -h,--help         print this help
462EOF
463    return 0
464}
465
466options=$(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:,fqdn:,rootpassword: -- "$@")
467if [ $? -ne 0 ]; then
468    usage $(basename $0)
469    exit 1
470fi
471eval set -- "$options"
472
473while true
474do
475    case "$1" in
476        -h|--help)      usage $0 && exit 0;;
477        -p|--path)      path=$2; shift 2;;
478        --rootfs)       rootfs_path=$2; shift 2;;
479        -n|--name)      name=$2; shift 2;;
480        -P|--profile)   profile=$2; shift 2;;
481        -c|--clean)     clean=1; shift 1;;
482        -R|--release)   release=$2; shift 2;;
483        -A|--arch)      arch=$2; shift 2;;
484        -4|--ipv4)      ipv4=$2; shift 2;;
485        -g|--gw)        gw=$2; shift 2;;
486        -d|--dns)       dns=$2; shift 2;;
487        -u|--user)      default_user=$2; shift 2;;
488        --password)     default_user_password=$2; shift 2;;
489        --rootpassword) root_password=$2; shift 2;;
490        --fqdn)         utsname=$2; shift 2;;
491        --)             shift 1; break ;;
492        *)              break ;;
493    esac
494done
495
496if [ ! -z "$clean" -a -z "$path" ]; then
497    clean || exit 1
498    exit 0
499fi
500
501type apt-get >/dev/null 2>&1
502if [ $? -ne 0 ]; then
503    echo "'apt-get' command is missing"
504    exit 1
505fi
506
507type vbootstrap >/dev/null 2>&1
508if [ $? -ne 0 ]; then
509    echo "'vbootstrap' command is missing"
510    exit 1
511fi
512
513if [ -z "$path" ]; then
514    path=$default_path
515fi
516
517if [ -z "$profile" ]; then
518    profile=$default_profile
519fi
520
521if [ -z "$release" ]; then
522    release="VineSeed"
523fi
524
525if [ -z "$ipv4" ]; then
526    BOOTPROTO="dhcp"
527else
528    BOOTPROTO="static"
529fi
530
531if [ -z "$default_user" ]; then
532    default_user="vine"
533fi
534
535if [ -z "$default_user_password" ]; then
536    default_user_password="$(mktemp -u XXXXXXXX)"
537fi
538
539if [ -z "$arch" ]; then
540    arch="$(uname -i)"
541fi
542
543if [ -z "${utsname}" ]; then
544    utsname=${name}
545fi
546
547if [ $(expr "$utsname" : '.*\..*\.') = 0 ]; then
548    if [[ "$(dnsdomainname)" != "" && "$(dnsdomainname)" != "localdomain" ]]; then
549        utsname=${utsname}.$(dnsdomainname)
550    fi
551fi
552
553if [ -z "${root_password}" ]; then
554    root_password="$(mktemp -u XXXXXXXX)"
555fi
556
557if [ "$(id -u)" != "0" ]; then
558    echo "This script should be run as 'root'"
559    exit 1
560fi
561
562# check for 'lxc.rootfs' passed in through default config by lxc-create
563if [ -z "$rootfs_path" ]; then
564    if grep -q '^lxc.rootfs' $path/config 2>/dev/null ; then
565        rootfs_path=$(awk -F= '/^lxc.rootfs =/{ print $2 }' $path/config)
566    else
567        rootfs_path=$path/rootfs
568    fi
569fi
570
571config_path=$default_path/$name
572cache=$cache_base/$arch/$release/$profile
573
574install_vinelinux
575if [ $? -ne 0 ]; then
576    echo "failed to install vinelinux"
577    exit 1
578fi
579
580configure_vinelinux
581if [ $? -ne 0 ]; then
582    echo "failed to configure vinelinux for a container"
583    exit 1
584fi
585
586copy_configuration
587if [ $? -ne 0 ]; then
588    echo "failed write configuration file"
589    exit 1
590fi
591
592if [ ! -z "$clean" ]; then
593    clean || exit 1
594    exit 0
595fi
596echo "container rootfs and config created"
597echo "network configured as $lxc_network_type in the $lxc_network_link"
Note: See TracBrowser for help on using the repository browser.