lxc-plamo.in revision f24a52d5f588ff4e4575046903fb9498c376d833
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher# template script for generating Plamo Linux container for LXC
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher# lxc: linux Container library
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher# KATOH Yasufumi <karma@jazz.email.ne.jp>
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher# TAMUKI Shoichi <tamuki@linet.gr.jp>
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher# This library is free software; you can redistribute it and/or
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher# modify it under the terms of the GNU Lesser General Public
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher# License as published by the Free Software Foundation; either
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher# version 2.1 of the License, or (at your option) any later version.
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher# This library is distributed in the hope that it will be useful,
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher# but WITHOUT ANY WARRANTY; without even the implied warranty of
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher# Lesser General Public License for more details.
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher# You should have received a copy of the GNU Lesser General Public
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher# License along with this library; if not, write to the Free Software
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher# ref. https://github.com/Ponce/lxc-slackware/blob/master/lxc-slackware
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher# lxc-ubuntu script
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher# Detect use under userns (unsupported)
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher if [ "$arg" = "--mapped-uid" -o "$arg" = "--mapped-gid" ]; then
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher echo "This template can't be used for unprivileged containers." 1>&2
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher echo "You may want to try the \"download\" template instead." 1>&2
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose# Make sure the usual locations are in PATH
949fbc93defad394648b2651b43a7bbfa5bff42bSumit BoseIGNOREPKGS=${IGNOREPKGS-"grub kernel lilo linux_firmware microcode_ctl
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose cpufreqd cpufrequtils gpm"}
949fbc93defad394648b2651b43a7bbfa5bff42bSumit BoseADDONPKGS=${ADDONPKGS-"`echo contrib/Hamradio/{morse,qrq}`"}
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose # check the mini plamo was not already downloaded
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose if ! mkdir -p $ptcache ; then
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose case $DLSCHEME in http) depth=2 ;; ftp) depth=3 ;; esac
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose rej=${IGNOREPKGS%% *} ; [ -n "$rej" ] && rej="$rej-*"
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose for p in ${IGNOREPKGS#* } ; do rej="$rej,$p-*" ; done
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose wget -nv -e robots=off -r -l $depth -nd -A .tgz,.txz -R "$rej" \
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose -X $MIRRORPATH/Plamo-$release/$arch/plamo/$i/old \
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose $DLSCHEME://$MIRRORSRV$MIRRORPATH/Plamo-$release/$arch/plamo/$i
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose if [ $? -ne 0 ] ; then
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose wget -nv -e robots=off -r -l $depth -nd -A .tgz,.txz -R "$rej" \
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose -X $MIRRORPATH/Plamo-$release/$arch/contrib/$i/old \
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose $DLSCHEME://$MIRRORSRV$MIRRORPATH/Plamo-$release/$arch/contrib/$i
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose if [ $? -ne 0 ] ; then
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose wget -nv -e robots=off -r -l $depth -nd -A "`basename $p`-*" \
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose -I $MIRRORPATH/Plamo-$release/$arch/`dirname $p` \
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose -X $MIRRORPATH/Plamo-$release/$arch/`dirname $p`/old \
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose $DLSCHEME://$MIRRORSRV$MIRRORPATH/Plamo-$release/$arch/`dirname $p`
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose if [ $? -ne 0 ] ; then
bfb40893be20b45279a40188cf16ef0eec1f9423Sumit Bose find $rtcache -mindepth 1 -maxdepth 1 -exec cp -a {} $rootfs \; || return 1
bfb40893be20b45279a40188cf16ef0eec1f9423Sumit Bose if ! flock -n 9 ; then
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher echo "Checking cache download in $dlcache..."
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher if [ ! -d $dlcache ] ; then
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher echo "Failed to download plamo $release base packages."
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher # install "installpkg" command temporarily with static linked tar
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher # command into the lxc cache directory to keep the original uid/
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek echo "Installing 'installpkg' command into $dlcache/sbin..."
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek ( cd $dlcache ; tar xpJf hdsetup-*.txz ; rm -rf tmp usr var )
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek sed -i "/ldconfig/!s@/sbin@$dlcache&@g" $dlcache/sbin/installpkg*
df0596ec12bc5091608371e2977f3111241e8cafSimo Sorce if [ ! -d $rtcache ] ; then
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher installpkg -root $rtcache -priority ADD $p
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher ) 9> @LOCALSTATEDIR@/lock/subsys/lxc-plamo
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek sed -i 's/="err"/=0/' $rootfs/etc/udev/udev.conf
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher none /var/lib/nfs/rpc_pipefs rpc_pipefs defaults 0 0
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher < # What to do when power fails (shutdown to single user).
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher < pf::powerfail:/sbin/shutdown -f +5 "THE POWER IS FAILING"
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher > # What to do when power fails (shutdown).
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher > pf::powerfail:/sbin/shutdown -h +0 "THE POWER IS FAILING"
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher > 1:1235:respawn:/sbin/agetty 38400 console
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek < c5:1235:respawn:/sbin/agetty 38400 tty5 linux
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek < c6:12345:respawn:/sbin/agetty 38400 tty6 linux
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek echo "127.0.0.1 localhost $name" > $rootfs/etc/hosts
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose ln -s ../usr/share/zoneinfo/Asia/Tokyo $rootfs/etc/localtime
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose # disable pam_loginuid.so in /etc/pam.d/login (for libvirt's lxc driver)
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose sed -i '/pam_loginuid/s/^/#/' $rootfs/etc/pam.d/login
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher /^mount -w -n -t proc/;/^# ln -s \/bin\/true/-1d
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher /^# copy the rules/;/^# Set the hostname/-1d
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher /^# Check the integrity/;/^# Clean up temporary/-1d
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher /^# Screen blanks/;/^# Initialize ip6tables/-1d
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher /^# Initialize sysctl/;/^echo "Starting services/-1d
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher head -n-93 $rootfs/sbin/netconfig.tradnet > /tmp/netconfig.rconly
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher RC=$rootfs/etc/rc.d/rc.inet1.tradnet
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher IFCONFIG=sbin/ifconfig
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher ROUTE=sbin/route
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher INET1SCHEME=var/run/inet1-scheme
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher IPADDR=127.0.0.1
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher NETWORK=127.0.0.0
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher DHCPCD=usr/sbin/dhclient
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher make_config_file
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher rm -f $rootfs/etc/rc.d/rc.inet1.tradnet
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher sh /tmp/netconfig.rconly
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher rm -f /tmp/netconfig.rconly
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher sed -i '/cmdline/s/if/& false \&\&/' $rootfs/etc/rc.d/rc.inet1.tradnet
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallaghercopy_configuration() {
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher cat <<- EOF >> $path/config || let ret++
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher lxc.utsname = $name
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher lxc.arch = $arch
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher if [ -f "@LXCTEMPLATECONFIG@/plamo.common.conf" ] ; then
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher cat <<- "EOF" >> $path/config || let ret++
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher lxc.include = @LXCTEMPLATECONFIG@/plamo.common.conf
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher if [ $ret -ne 0 ] ; then
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher echo "Failed to add configuration."
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagherpost_process() {
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagher # nothing do in Plamo Linux
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagherdo_bindhome() {
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagher # bind-mount the user's path into the container's /home
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagher h=`getent passwd $bindhome | cut -d: -f6`
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagher mkdir -p $rootfs/$h
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagher echo "lxc.mount.entry = $h $rootfs/$h none bind 0 0" >> $path/config
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagher # copy /etc/passwd, /etc/shadow, and /etc/group entries into container
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagher if ! pwd=`getent passwd $bindhome` ; then
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagher echo "Warning: failed to copy password entry for $bindhome."
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagher echo $pwd >> $rootfs/etc/passwd
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagher echo `getent shadow $bindhome` >> $rootfs/etc/shadow
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagher [ -d $dlcache -a -d $rtcache ] || return 0
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagher # lock, so we won't purge while someone is creating a repository
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagher if ! flock -n 9 ; then
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagher echo "Cache repository is busy."
5dedd73d90f0c1f23299f0c613f384ef902c3653Stephen Gallagher echo "Purging the download cache..."
5dedd73d90f0c1f23299f0c613f384ef902c3653Stephen Gallagher rm -rf --one-file-system $dlcache $rtcache || return 1
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagher ) 9> @LOCALSTATEDIR@/lock/subsys/lxc-plamo
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher $prog [-h|--help] -p|--path=<path> -n|--name=<name> --rootfs=<rootfs>
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher [-c|--clean] [-r|--release=<release>] [-a|--arch=<arch>]
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher [-b|--bindhome=<user>]
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher release: $release
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher arch: x86 or x86_64: defaults to host arch
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher bindhome: bind <user>'s home into the container
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagherprog=`basename $0`
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagherpath="" ; name="" ; rootfs=""
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagherrelease=${release:-5.x}
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagherarch=`uname -m | sed 's/i.86/x86/'` ; hostarch=$arch
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallaghersopts=hp:n:cr:a:b:
46222e5191473f9a46aec581273eb2eef22e23beMichal Zideklopts=help,path:,name:,rootfs:,clean,release:,arch:,bindhome:
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagherif ! options=`getopt -o $sopts -l $lopts -- "$@"` ; then
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidekeval set -- "$options"
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidekwhile true ; do
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek case "$1" in
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher -h|--help) usage && exit 0 ;;
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher -p|--path) path=$2 ; shift 2 ;;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek -n|--name) name=$2 ; shift 2 ;;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek --rootfs) rootfs=$2 ; shift 2 ;;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek -c|--clean) clean=1 ; shift 1 ;;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek -r|--release) release=$2 ; shift 2 ;;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek -a|--arch) arch=$2 ; shift 2 ;;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek -b|--bindhome) bindhome=$2 ; shift 2 ;;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek --) shift 1 ; break ;;
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagherif [ $clean -eq 1 -a -z "$path" ] ; then
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher cleanup || exit 1
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagherif [ $hostarch == "x86" -a $arch == "x86_64" ] ; then
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher echo "Can't create x86_64 container on x86."
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagherif [ -z "$path" ] ; then
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher echo "'path' parameter is required."
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagherif [ -z "$name" ] ; then
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher echo "'name' parameter is required."
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagherif [ `id -u` -ne 0 ] ; then
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher echo "This script should be run as 'root'."
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallaghercache=@LOCALSTATEDIR@/cache/lxc
df0596ec12bc5091608371e2977f3111241e8cafSimo Sorceptcache=$cache/partial-${prog##*-}-$release-$arch
9a7b6d3248c5aac460e164f2246b26131cfbc055Simo Sorcedlcache=$cache/cache-${prog##*-}-$release-$arch
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagherrtcache=$cache/rootfs-${prog##*-}-$release-$arch
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidekif [ -z "$rootfs" ] ; then
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher if grep -q "^lxc.rootfs" $path/config ; then
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher rootfs=`awk -F= '/^lxc.rootfs =/{ print $2 }' $path/config`
45f75fc8e98092fa48faa3d180fd42f7efd51486Stephen Gallagher rootfs=$path/rootfs
45f75fc8e98092fa48faa3d180fd42f7efd51486Stephen Gallagherif ! install_plamo ; then
45f75fc8e98092fa48faa3d180fd42f7efd51486Stephen Gallagher echo "Failed to install plamo $release."
45f75fc8e98092fa48faa3d180fd42f7efd51486Stephen Gallagherif ! configure_plamo ; then
45f75fc8e98092fa48faa3d180fd42f7efd51486Stephen Gallagher echo "Failed to configure plamo $release for a container."
45f75fc8e98092fa48faa3d180fd42f7efd51486Stephen Gallagherif ! copy_configuration ; then
45f75fc8e98092fa48faa3d180fd42f7efd51486Stephen Gallagher echo "Failed to write configuration file."
45f75fc8e98092fa48faa3d180fd42f7efd51486Stephen Gallagherif [ -n "$bindhome" ] ; then
45f75fc8e98092fa48faa3d180fd42f7efd51486Stephen Gallagherif [ $clean -eq 1 ] ; then
45f75fc8e98092fa48faa3d180fd42f7efd51486Stephen Gallagher cleanup || exit 1