262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# template script for generating altlinux container for LXC
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# lxc: linux Container library
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# Alexey Shabalin <shaba@altlinux.org>
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# This library is free software; you can redistribute it and/or
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# modify it under the terms of the GNU Lesser General Public
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# License as published by the Free Software Foundation; either
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# version 2.1 of the License, or (at your option) any later version.
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# This library is distributed in the hope that it will be useful,
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# but WITHOUT ANY WARRANTY; without even the implied warranty of
14d9c0f09d1a55d124ef210a4b4e205c9fe7596cStéphane Graber# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# Lesser General Public License for more details.
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# You should have received a copy of the GNU Lesser General Public
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# License along with this library; if not, write to the Free Software
250b1eec71b074acdff1c5f6b5a1f0d7d2c20b77Stéphane Graber# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
8ec981fc8b0105da5f071e40811e0c2472a6c3c9Stéphane Graber# Detect use under userns (unsupported)
96283b546081e7ff709968378fca25cb44f1ab6cStéphane Graber if [ "$arg" = "--mapped-uid" -o "$arg" = "--mapped-gid" ]; then
8ec981fc8b0105da5f071e40811e0c2472a6c3c9Stéphane Graber echo "This template can't be used for unprivileged containers." 1>&2
8ec981fc8b0105da5f071e40811e0c2472a6c3c9Stéphane Graber echo "You may want to try the \"download\" template instead." 1>&2
207bf0e475f1dc6e9a2dac2cee3a209b56427855Stéphane Graber# Make sure the usual locations are in PATH
207bf0e475f1dc6e9a2dac2cee3a209b56427855Stéphane Graberexport PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin#Configurations
e29bf450cafa2ce2564aeb0b64d2014c17228407Dwight Engencache_base=@LOCALSTATEDIR@/cache/lxc/altlinux/$arch
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# is this altlinux?
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin[ -f /etc/altlinux-release ] && is_altlinux=true
5c60f9903768b49d329e20e3fdb333753f719502Denis Pynkin cat <<EOF > ${rootfs_path}/etc/net/ifaces/eth0/options
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey ShabalinBOOTPROTO=${BOOTPROTO}
5c60f9903768b49d329e20e3fdb333753f719502Denis PynkinNM_CONTROLLED=yes
5c60f9903768b49d329e20e3fdb333753f719502Denis Pynkin cat <<EOF > ${rootfs_path}/etc/net/ifaces/eth0/ipv4address
5c60f9903768b49d329e20e3fdb333753f719502Denis Pynkin cat <<EOF > ${rootfs_path}/etc/net/ifaces/eth0/ipv4route
5c60f9903768b49d329e20e3fdb333753f719502Denis Pynkin cat <<EOF > ${rootfs_path}/etc/net/ifaces/eth0/resolv.conf
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalinnameserver ${dns}
5c60f9903768b49d329e20e3fdb333753f719502Denis Pynkin cat <<EOF > ${rootfs_path}/etc/net/ifaces/eth0/ipv6address
5c60f9903768b49d329e20e3fdb333753f719502Denis Pynkin cat <<EOF > ${rootfs_path}/etc/net/ifaces/eth0/ipv6route
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin cat <<EOF > ${rootfs_path}/etc/sysconfig/network
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey ShabalinNETWORKING=yes
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey ShabalinCONFMETHOD=etcnet
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey ShabalinHOSTNAME=${UTSNAME}
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey ShabalinRESOLV_MODS=yes
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin127.0.0.1 localhost.localdomain localhost $name
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin # Allow to login at virsh console. loginuid.so doen't work in the absence of auditd.
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin# sed -i 's/^.*loginuid.so.*$/\#&/' ${rootfs_path}/etc/pam.d/common-login
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin echo "pts/0" >> ${rootfs_path}/etc/securetty
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin echo "console" >> ${rootfs_path}/etc/securetty
5c60f9903768b49d329e20e3fdb333753f719502Denis Pynkin for service in network syslogd random NetworkManager
05e61b82cff6c44621e7d23bf3cbb756f3ae681bDenis Pynkin chroot ${rootfs_path} chkconfig $service --list &>/dev/null && chroot ${rootfs_path} chkconfig $service on || true
5c60f9903768b49d329e20e3fdb333753f719502Denis Pynkin chroot ${rootfs_path} systemctl -q enable $service &>/dev/null|| true
05e61b82cff6c44621e7d23bf3cbb756f3ae681bDenis Pynkin chroot ${rootfs_path} chkconfig $service --list &>/dev/null && chroot ${rootfs_path} chkconfig $service off || true
5c60f9903768b49d329e20e3fdb333753f719502Denis Pynkin chroot ${rootfs_path} systemctl -q disable $service &>/dev/null || true
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin subst 's/^\([3-9]\+:[0-9]\+:respawn:\/sbin\/mingetty.*\)/#\1/' ${rootfs_path}/etc/inittab
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin echo "c1:2345:respawn:/sbin/mingetty --noclear console" >> ${rootfs_path}/etc/inittab
05e61b82cff6c44621e7d23bf3cbb756f3ae681bDenis Pynkin subst 's,\/dev\/tty12,/var/log/syslog/console,' ${rootfs_path}/etc/syslog.conf
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin mknod -m 666 ${dev_path}/null c 1 3
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin mknod -m 666 ${dev_path}/zero c 1 5
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin mknod -m 644 ${dev_path}/random c 1 8
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin mknod -m 644 ${dev_path}/urandom c 1 9
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin mknod -m 666 ${dev_path}/tty c 5 0
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin mknod -m 600 ${dev_path}/tty0 c 4 0
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin mknod -m 600 ${dev_path}/tty1 c 4 1
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin mknod -m 600 ${dev_path}/tty2 c 4 2
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin mknod -m 600 ${dev_path}/tty3 c 4 3
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin mknod -m 600 ${dev_path}/tty4 c 4 4
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin mknod -m 600 ${dev_path}/console c 5 1
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin mknod -m 666 ${dev_path}/full c 1 7
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin mknod -m 666 ${dev_path}/ptmx c 5 2
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin mknod -m 600 ${dev_path}/mapper/control c 10 236
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin mknod -m 666 ${dev_path}/net/tun c 10 200
8c7dddc25c47f9f060b91e0a39f7218aa433755cEvgeni Golov echo "root:$root_password" | chroot $rootfs_path chpasswd
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin # check the mini altlinux was not already downloaded
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin if [ $? -ne 0 ]; then
14d9c0f09d1a55d124ef210a4b4e205c9fe7596cStéphane Graber echo "Failed to create '$INSTALL_ROOT' directory"
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin APT_GET="apt-get -o RPM::RootDir=$INSTALL_ROOT -y"
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin PKG_LIST="$(grep -hs '^[^#]' "$profile_dir/$profile")"
a9ae28a05a65f2bd0dc6695ab5cdfb723ee8b226Denis Pynkin # if no configuration file $profile -- fall back to default list of packages
3d5658d167db8866d5cd3b4b89f0e4becc735719Denis Pynkin [ -z "$PKG_LIST" ] && PKG_LIST="interactivesystem apt apt-conf etcnet-full openssh-server systemd-sysvinit systemd-units systemd NetworkManager-daemon"
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin if [ $? -ne 0 ]; then
14d9c0f09d1a55d124ef210a4b4e205c9fe7596cStéphane Graber echo "Failed to download the rootfs, aborting."
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin echo -n "Copying rootfs to $rootfs_path ..."
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin #cp -a $cache/rootfs-$arch $rootfs_path || return 1
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin chroot $cache/rootfs apt-get -y dist-upgrade
14d9c0f09d1a55d124ef210a4b4e205c9fe7596cStéphane Graber if [ $? -ne 0 ]; then
14d9c0f09d1a55d124ef210a4b4e205c9fe7596cStéphane Graber echo "Checking cache download in $cache/rootfs ... "
14d9c0f09d1a55d124ef210a4b4e205c9fe7596cStéphane Graber if [ $? -ne 0 ]; then
14d9c0f09d1a55d124ef210a4b4e205c9fe7596cStéphane Graber if [ $? -ne 0 ]; then
14d9c0f09d1a55d124ef210a4b4e205c9fe7596cStéphane Graber echo "Failed to update 'altlinux base', continuing with last known good cache"
14d9c0f09d1a55d124ef210a4b4e205c9fe7596cStéphane Graber echo "Copy $cache/rootfs to $rootfs_path ... "
14d9c0f09d1a55d124ef210a4b4e205c9fe7596cStéphane Graber if [ $? -ne 0 ]; then
1881820ae4ff9004beef1bf7f04553580840441dSerge Hallyn grep -q "^lxc.rootfs" $config_path/config 2>/dev/null || echo "lxc.rootfs = $rootfs_path" >> $config_path/config
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalinlxc.utsname = $name
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalinlxc.pts = 1024
eee3ba81c88e64b8a732694fc4843a39d5bde491Serge Hallynlxc.cap.drop = sys_module mac_admin mac_override sys_time
f02ce27d4b1a9d01b88d0ffaf626e5bafa671bf0Stéphane Graber# When using LXC with apparmor, uncomment the next line to run unconfined:
f02ce27d4b1a9d01b88d0ffaf626e5bafa671bf0Stéphane Graber#lxc.aa_profile = unconfined
5c60f9903768b49d329e20e3fdb333753f719502Denis Pynkin#lxc.network.type = $lxc_network_type
5c60f9903768b49d329e20e3fdb333753f719502Denis Pynkin#lxc.network.flags = up
5c60f9903768b49d329e20e3fdb333753f719502Denis Pynkin#lxc.network.link = $lxc_network_link
5c60f9903768b49d329e20e3fdb333753f719502Denis Pynkin#lxc.network.name = veth0
5c60f9903768b49d329e20e3fdb333753f719502Denis Pynkin#lxc.network.mtu = 1500
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalinif [ ! -z ${ipv4} ]; then
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalinlxc.network.ipv4 = $ipv4
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalinif [ ! -z ${gw} ]; then
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalinlxc.network.ipv4.gateway = $gw
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalinif [ ! -z ${ipv6} ]; then
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalinlxc.network.ipv6 = $ipv6
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalinif [ ! -z ${gw6} ]; then
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalinlxc.network.ipv6.gateway = $gw6
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalinlxc.cgroup.devices.deny = a
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# /dev/null and zero
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalinlxc.cgroup.devices.allow = c 1:3 rwm
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalinlxc.cgroup.devices.allow = c 1:5 rwm
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalinlxc.cgroup.devices.allow = c 5:1 rwm
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalinlxc.cgroup.devices.allow = c 5:0 rwm
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalinlxc.cgroup.devices.allow = c 4:0 rwm
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalinlxc.cgroup.devices.allow = c 4:1 rwm
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin# /dev/{,u}random
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalinlxc.cgroup.devices.allow = c 1:9 rwm
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalinlxc.cgroup.devices.allow = c 1:8 rwm
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalinlxc.cgroup.devices.allow = c 136:* rwm
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalinlxc.cgroup.devices.allow = c 5:2 rwm
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalinlxc.cgroup.devices.allow = c 10:135 rwm
f24a52d5f588ff4e4575046903fb9498c376d833Stéphane Graberlxc.mount.auto = cgroup:mixed proc:mixed sys:mixed
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin if [ $? -ne 0 ]; then
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin if [ ! -e $cache ]; then
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin # lock, so we won't purge while someone is creating a repository
14d9c0f09d1a55d124ef210a4b4e205c9fe7596cStéphane Graber if [ $? != 0 ]; then
14d9c0f09d1a55d124ef210a4b4e205c9fe7596cStéphane Graber echo -n "Purging the download cache for ALTLinux-$release..."
14d9c0f09d1a55d124ef210a4b4e205c9fe7596cStéphane Graber rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin $1 -n|--name=<container_name>
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin [-p|--path=<path>] [-c|--clean] [-R|--release=<ALTLinux_release>]
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin [-4|--ipv4=<ipv4 address>] [-6|--ipv6=<ipv6 address>]
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin [-g|--gw=<gw address>] [-d|--dns=<dns address>]
1897e3bcd36af9f3fe6d3649910a9adb93e5e988Serge Hallyn [-P|--profile=<name of the profile>] [--rootfs=<path>]
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin [-A|--arch=<arch of the container>]
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey ShabalinMandatory args:
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin -n,--name container name, used to as an identifier for that container from now on
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey ShabalinOptional args:
e29bf450cafa2ce2564aeb0b64d2014c17228407Dwight Engen -p,--path path to where the container rootfs will be created, defaults to @LXCPATH@. The container config will go under @LXCPATH@ in that case
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin -c,--clean clean the cache
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin -R,--release ALTLinux release for the new container. if the host is ALTLinux, then it will defaultto the host's release.
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin -4,--ipv4 specify the ipv4 address to assign to the virtualized interface, eg. 192.168.1.123/24
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin -6,--ipv6 specify the ipv6 address to assign to the virtualized interface, eg. 2003:db8:1:0:214:1234:fe0b:3596/64
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin -g,--gw specify the default gw, eg. 192.168.1.1
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin -G,--gw6 specify the default gw, eg. 2003:db8:1:0:214:1234:fe0b:3596
b031f0d2ca1b40eab86286b82d3c5e8b379122e6Alexey Shabalin -d,--dns specify the DNS server, eg. 192.168.1.2
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin -P,--profile Profile name is the file name in /etc/lxc/profiles contained packages name for install to cache.
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin -A,--arch NOT USED YET. Define what arch the container will be [i686,x86_64]
1897e3bcd36af9f3fe6d3649910a9adb93e5e988Serge Hallyn ---rootfs rootfs path
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin -h,--help print this help
a9ae28a05a65f2bd0dc6695ab5cdfb723ee8b226Denis Pynkinoptions=$(getopt -o hp:n:P:cR:4:6:g:d: -l help,rootfs:,path:,name:,profile:,clean,release:,ipv4:,ipv6:,gw:,dns: -- "$@")
14d9c0f09d1a55d124ef210a4b4e205c9fe7596cStéphane Graber --) shift 1; break ;;
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin echo "'apt-get' command is missing"
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalinif [ -z "$path" ]; then
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalinif [ -z "$profile" ]; then
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalinif [ -z "$release" ]; then
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin release=$(cat /etc/altlinux-release |awk '/^ALT/ {print $3}')
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin echo "This is not a ALTLinux host and release missing, use -R|--release to specify release"
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin echo "This script should be run as 'root'"
1897e3bcd36af9f3fe6d3649910a9adb93e5e988Serge Hallyn# check for 'lxc.rootfs' passed in through default config by lxc-create
1897e3bcd36af9f3fe6d3649910a9adb93e5e988Serge Hallynif [ -z "$rootfs_path" ]; then
1897e3bcd36af9f3fe6d3649910a9adb93e5e988Serge Hallyn if grep -q '^lxc.rootfs' $path/config 2>/dev/null ; then
853d58fdf5af0960b7b6edc9dea0fadddb8535f1Elan Ruusamäe rootfs_path=$(awk -F= '/^lxc.rootfs =/{ print $2 }' $path/config)
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin echo "failed to install altlinux"
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin echo "failed to configure altlinux for a container"
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalin echo "failed write configuration file"
4986f1c435636a69193ac90452b10075122d9005Michael Adamif [ ! -z "$clean" ]; then
262f4e48a51a55ad9cee06abbcfe4a6ad6166f49Alexey Shabalinecho "container rootfs and config created"
eba7df9ee0a1963984ef212e7ddfc0e0835af288Stéphane Graberecho "network configured as $lxc_network_type in the $lxc_network_link"