lxc-alpine.in revision 5be56973e5e874a142263dfb164b0b03e18a65f3
569bee5cc3d647032573db8f72734faa9307d577Natanael Copakey_sha256sums="9c102bcc376af1498d549b77bdbfa815ae86faa1d2d82f040e616b18ef2df2d4 alpine-devel@lists.alpinelinux.org-4a6a0840.rsa.pub
569bee5cc3d647032573db8f72734faa9307d577Natanael Copa2adcf7ce224f476330b5360ca5edb92fd0bf91c92d83292ed028d7c4e26333ab alpine-devel@lists.alpinelinux.org-4d07755e.rsa.pub"
85b41c7d7f72213199b5cff9525d17f44b49a842Kaarle Ritvanen url=http://wiki.alpinelinux.org/cgi-bin/dl.cgi
982e7b6ea40ea57923f4f094858424debc1a5f7fKaarle Ritvanen echo -n "Determining the latest release... "
982e7b6ea40ea57923f4f094858424debc1a5f7fKaarle Ritvanen release=$($wget $url/.latest.$apk_arch.txt | \
569bee5cc3d647032573db8f72734faa9307d577Natanael Copa echo "Using static apk from $repository/$apk_arch"
569bee5cc3d647032573db8f72734faa9307d577Natanael Copa BEGIN { split(pkglist,pkg) }
569bee5cc3d647032573db8f72734faa9307d577Natanael Copa $0 != "" { f[$1] = $2 }
569bee5cc3d647032573db8f72734faa9307d577Natanael Copa $0 == "" { for (i in pkg)
569bee5cc3d647032573db8f72734faa9307d577Natanael Copa if (pkg[i] == f["P"])
569bee5cc3d647032573db8f72734faa9307d577Natanael Copa keyname=$(echo $rootfs/sbin/apk.static.*.pub | sed 's/.*\.SIGN\.RSA\.//')
569bee5cc3d647032573db8f72734faa9307d577Natanael Copa checksum=$(echo "$key_sha256sums" | grep -w "$keyname")
569bee5cc3d647032573db8f72734faa9307d577Natanael Copa echo "ERROR: checksum is missing for $keyname"
569bee5cc3d647032573db8f72734faa9307d577Natanael Copa (cd $rootfs/etc/apk/keys && echo "$checksum" | sha256sum -c -) || return 1
569bee5cc3d647032573db8f72734faa9307d577Natanael Copa openssl dgst -verify $rootfs/etc/apk/keys/$keyname \
569bee5cc3d647032573db8f72734faa9307d577Natanael Copa -signature "$APK.SIGN.RSA.$keyname" "$APK" || return 1
85b41c7d7f72213199b5cff9525d17f44b49a842Kaarle Ritvanen mirror_list=$rootfs/usr/share/alpine-mirrors/MIRRORS.txt
85b41c7d7f72213199b5cff9525d17f44b49a842Kaarle Ritvanen mirror_count=$(wc -l $mirror_list | cut -d " " -f 1)
85b41c7d7f72213199b5cff9525d17f44b49a842Kaarle Ritvanen repository=$(sed $(expr $RANDOM % $mirror_count + 1)\!d \
569bee5cc3d647032573db8f72734faa9307d577Natanael Copa if ! [ -d "$rootfs"/etc/apk/keys ] && [ -d "$keys_dir" ]; then
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copa echo "$repository" > "$rootfs"/etc/apk/repositories
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copa cp /etc/apk/repositories "$rootfs"/etc/apk/repositories || return 1
2e599a6a25b533fe63840edc34ee265811b7b814Natanael Copa sed -i -e "s:/[^/]\+/\([^/]\+\)$:/$release/\1:" \
569bee5cc3d647032573db8f72734faa9307d577Natanael Copa $APK add -U --initdb --root $rootfs $opt_arch "$@" alpine-base
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copa::sysinit:/sbin/rc sysinit
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copa::wait:/sbin/rc default
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copatty1:12345:respawn:/sbin/getty 38400 tty1
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copa::ctrlaltdel:/sbin/reboot
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copa::shutdown:/sbin/rc shutdown
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copa grep nameserver /etc/resolv.conf > "$rootfs/etc/resolv.conf"
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copaiface lo inet loopback
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copaiface eth0 inet dhcp
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copa# mknod -m 600 "$rootfs/dev/initctl" p
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copa ln -s /etc/init.d/syslog "$rootfs"/etc/runlevels/default/syslog
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copa grep -q "^lxc.rootfs" $path/config 2>/dev/null \
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copa || echo "lxc.rootfs = $rootfs" >> $path/config
d42277f769d1bed8a4a198a49dbe96582a4fa2ecNatanael Copa lxc_network_link_line="# lxc.network.link = br0"
d42277f769d1bed8a4a198a49dbe96582a4fa2ecNatanael Copa if [ -d /sys/class/net/$br/bridge ]; then
d42277f769d1bed8a4a198a49dbe96582a4fa2ecNatanael Copa lxc_network_link_line="lxc.network.link = $br"
d42277f769d1bed8a4a198a49dbe96582a4fa2ecNatanael Copa if ! grep -q "^lxc.network.type" $path/config 2>/dev/null; then
d42277f769d1bed8a4a198a49dbe96582a4fa2ecNatanael Copalxc.network.type = veth
d42277f769d1bed8a4a198a49dbe96582a4fa2ecNatanael Copa$lxc_network_link_line
d42277f769d1bed8a4a198a49dbe96582a4fa2ecNatanael Copalxc.network.flags = up
d42277f769d1bed8a4a198a49dbe96582a4fa2ecNatanael Copa # if there is exactly one veth network entry, make sure it has an
d42277f769d1bed8a4a198a49dbe96582a4fa2ecNatanael Copa nics=$(grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l)
d42277f769d1bed8a4a198a49dbe96582a4fa2ecNatanael Copa if [ "$nics" -eq 1 ] && ! grep -q "^lxc.network.hwaddr" $path/config; then
d42277f769d1bed8a4a198a49dbe96582a4fa2ecNatanael Copa # see http://sourceforge.net/tracker/?func=detail&aid=3411497&group_id=163076&atid=826303
d42277f769d1bed8a4a198a49dbe96582a4fa2ecNatanael Copa hwaddr="fe:$(dd if=/dev/urandom bs=8 count=1 2>/dev/null |od -t x8 | \
d42277f769d1bed8a4a198a49dbe96582a4fa2ecNatanael Copa echo "lxc.network.hwaddr = $hwaddr" >> $path/config
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copalxc.pts = 1024
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copalxc.utsname = $hostname
eee3ba81c88e64b8a732694fc4843a39d5bde491Serge Hallynlxc.cap.drop = sys_module mac_admin mac_override sys_time
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copa# When using LXC with apparmor, uncomment the next line to run unconfined:
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copa#lxc.aa_profile = unconfined
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copalxc.cgroup.devices.deny = a
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copa# /dev/null and zero
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copalxc.cgroup.devices.allow = c 1:3 rwm
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copalxc.cgroup.devices.allow = c 1:5 rwm
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copalxc.cgroup.devices.allow = c 5:1 rwm
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copalxc.cgroup.devices.allow = c 5:0 rwm
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copalxc.cgroup.devices.allow = c 4:0 rwm
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copalxc.cgroup.devices.allow = c 4:1 rwm
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copa# /dev/{,u}random
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copalxc.cgroup.devices.allow = c 1:9 rwm
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copalxc.cgroup.devices.allow = c 1:8 rwm
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copalxc.cgroup.devices.allow = c 136:* rwm
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copalxc.cgroup.devices.allow = c 5:2 rwm
eee3ba81c88e64b8a732694fc4843a39d5bde491Serge Hallynlxc.cgroup.devices.allow = c 254:0 rm
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copa# mounts point
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copalxc.mount.entry=proc proc proc nodev,noexec,nosuid 0 0
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copalxc.mount.entry=run run tmpfs nodev,noexec,nosuid,relatime,size=1m,mode=0755 0 0
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copalxc.mount.entry=none dev/pts devpts gid=5,mode=620 0 0
982e7b6ea40ea57923f4f094858424debc1a5f7fKaarle RitvanenUsage: $(basename $0) [-h|--help] [-r|--repository <url>]
982e7b6ea40ea57923f4f094858424debc1a5f7fKaarle Ritvanen [-R|--release <release>] [-a|--arch <arch>]
1897e3bcd36af9f3fe6d3649910a9adb93e5e988Serge Hallyn [--rootfs <rootfs>] -p|--path <path> -n|--name <name>
5be56973e5e874a142263dfb164b0b03e18a65f3Serge Hallyn# template mknods, requires root
b1aa0624bae5a81d6f6bbc2653a388d148cffef8Natanael Copa # split --myopt=foo=bar into --myopt foo=bar
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copa # split opts -abc into -a -b -c
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copa set -- $(echo "${opt#-}" | sed 's/\(.\)/ -\1/g') "$@"
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copaif [ -z "${path}" ]; then
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copaif [ -z "$rootfs" ]; then
1897e3bcd36af9f3fe6d3649910a9adb93e5e988Serge Hallyn rootfs=`awk -F= '$1 ~ /^lxc.rootfs/ { print $2 }' "$path/config" 2>/dev/null`
1897e3bcd36af9f3fe6d3649910a9adb93e5e988Serge Hallyn if [ -z "$rootfs" ]; then
569bee5cc3d647032573db8f72734faa9307d577Natanael Copa get_static_apk "$rootfs" || die "Failed to download a valid static apk"
670c6ab8caac48ce5cf043796dd8908114b7f607Natanael Copainstall_alpine "$rootfs" "$@" || die "Failed to install rootfs for $name"
2a9a0a08077d88ee1d70ca46ca122216f3d1c89aNatanael Copaconfigure_alpine "$rootfs" "$name" || die "Failed to configure $name"