Skip to content

Instantly share code, notes, and snippets.

@tobert
Created August 29, 2011 04:54

Revisions

  1. tobert created this gist Aug 29, 2011.
    75 changes: 75 additions & 0 deletions start-lxc-cluster.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    #!/bin/bash

    COUNT=10
    BRIDGE="br1"
    FORMAT="lxc%d"
    PARENT=$(hostname --fqdn)
    LXC_ROOT=/srv/virt/lxc
    OS_ROOT=/srv/virt/lxc/base/root

    for num in $(seq 1 $COUNT)
    do
    name=$(printf "$FORMAT" $num)
    utsname="${name}.${PARENT}"

    top="$LXC_ROOT/$utsname"
    mkdir -p $top

    root="${top}/root"
    rootrw="${top}/rw"
    conf="${top}/lxc.conf"
    logfile="${top}/${utsname}.log"

    # configuraable
    mac=$(printf "4a:49:43:49:79:%02x" $num)
    ip="192.168.4.$(($num + 100))/24"

    mkdir -p $rootrw $root

    # update /etc/ethers for dnsmasq
    if (grep -q $mac /etc/ethers) ; then
    sed -i "s/$mac.*$/$mac $ip/" /etc/ethers
    else
    echo "$mac $ip" >> /etc/ethers
    fi
    pkill -HUP dnsmasq

    # lxc config file
    cat > $conf <<EOF
    lxc.utsname = $utsname
    lxc.rootfs = $root
    #lxc.tty = 1
    lxc.console = $logfile
    lxc.cgroup.cpuset.cpus = $(cat /cgroup/cpuset.cpus)
    lxc.cgroup.cpu.shares = 100
    lxc.mount.entry=none proc proc nodev,noexec,nosuid 0 0
    lxc.mount.entry=none dev/shm tmpfs defaults 0 0
    lxc.mount.entry=none dev/pts devpts defaults 0 0
    lxc.network.type = veth
    lxc.network.flags = up
    lxc.network.link = $BRIDGE
    lxc.network.hwaddr = $mac
    lxc.network.ipv4 = $ip
    lxc.cgroup.devices.deny = a
    lxc.cgroup.devices.allow = c 1:3 rwm # /dev/null
    lxc.cgroup.devices.allow = c 1:5 rwm # /dev/zero
    lxc.cgroup.devices.allow = c 5:1 rwm # /dev/console
    lxc.cgroup.devices.allow = c 5:0 rwm # /dev/tty
    lxc.cgroup.devices.allow = c 4:0 rwm # /dev/tty0
    lxc.cgroup.devices.allow = c 1:9 rwm # /dev/random
    lxc.cgroup.devices.allow = c 1:8 rwm # /dev/urandom
    lxc.cgroup.devices.allow = c 136:* rwm # /dev/pts/*
    lxc.cgroup.devices.allow = c 5:2 rwm # /dev/pts/ptmx
    #lxc.cgroup.devices.allow = c 254:0 rwm
    lxc.cap.drop = sys_boot sys_module sys_time
    EOF

    unionfs -o cow,max_files=32768 \
    -o allow_other,use_ino,suid,dev,nonempty \
    "${rootrw}=RW:${OS_ROOT}=RO" $root

    lxc-start -d -n $name -f $conf -o $logfile
    done
    18 changes: 18 additions & 0 deletions stop-lxc-cluster.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    #!/bin/bash

    COUNT=10
    FORMAT="lxc%d"
    PARENT=$(hostname --fqdn)
    LXC_ROOT=/srv/virt/lxc

    for num in $(seq 1 $COUNT)
    do
    name=$(printf "$FORMAT" $num)
    utsname="${name}.${PARENT}"
    top="$LXC_ROOT/$utsname"
    root="${top}/root"

    lxc-stop -n $name

    umount -t fuse.unionfs $root
    done