]> git.proxmox.com Git - mirror_lxc.git/blame - templates/lxc-plamo.in
config: fix the handling of lxc.hook and hwaddrs in unexpanded config
[mirror_lxc.git] / templates / lxc-plamo.in
CommitLineData
7a49a081 1#!/bin/bash -eu
cab79123
KY
2
3#
4# template script for generating Plamo Linux container for LXC
5#
6
7#
8# lxc: linux Container library
9
10# Authors:
11# KATOH Yasufumi <karma@jazz.email.ne.jp>
12# TAMUKI Shoichi <tamuki@linet.gr.jp>
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# ref. https://github.com/Ponce/lxc-slackware/blob/master/lxc-slackware
29# lxc-ubuntu script
30
8ec981fc 31# Detect use under userns (unsupported)
c63c04fc 32for arg in "$@"; do
96283b54
SG
33 [ "$arg" = "--" ] && break
34 if [ "$arg" = "--mapped-uid" -o "$arg" = "--mapped-gid" ]; then
8ec981fc
SG
35 echo "This template can't be used for unprivileged containers." 1>&2
36 echo "You may want to try the \"download\" template instead." 1>&2
37 exit 1
38 fi
39done
40
207bf0e4
SG
41# Make sure the usual locations are in PATH
42export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin
43
cab79123
KY
44[ -r /etc/default/lxc ] && . /etc/default/lxc
45
7a49a081 46DLSCHEME=${DLSCHEME:-"http"}
adf4b408 47MIRRORSRV=${MIRRORSRV:-"www.ring.gr.jp"}
7a49a081 48MIRRORPATH=${MIRRORPATH:-"/pub/linux/Plamo"}
8c3a756e
KY
49CATEGORIES=${CATEGORIES-"00_base 01_minimum"}
50EXTRACTGRS=${EXTRACTGRS-""}
51IGNOREPKGS=${IGNOREPKGS-"grub kernel lilo linux_firmware microcode_ctl
adf4b408 52 cpufreqd cpufrequtils gpm"}
8c3a756e 53ADDONPKGS=${ADDONPKGS-"`echo contrib/Hamradio/{morse,qrq}`"}
cab79123
KY
54
55download_plamo() {
56 # check the mini plamo was not already downloaded
57 if ! mkdir -p $ptcache ; then
58 echo "Failed to create '$ptcache' directory."
59 return 1
60 fi
61 # download a mini plamo into a cache
62 echo "Downloading Plamo-$release minimal..."
8adef761 63 cd $ptcache
7a49a081
KY
64 case $DLSCHEME in http) depth=2 ;; ftp) depth=3 ;; esac
65 rej=${IGNOREPKGS%% *} ; [ -n "$rej" ] && rej="$rej-*"
8c3a756e 66 if [ `echo $IGNOREPKGS | wc -w` -gt 1 ] ; then
adf4b408
KY
67 for p in ${IGNOREPKGS#* } ; do rej="$rej,$p-*" ; done
68 fi
7a49a081 69 for i in $CATEGORIES ; do
adf4b408 70 wget -nv -e robots=off -r -l $depth -nd -A .tgz,.txz -R "$rej" \
7a49a081
KY
71 -I $MIRRORPATH/Plamo-$release/$arch/plamo/$i \
72 -X $MIRRORPATH/Plamo-$release/$arch/plamo/$i/old \
73 $DLSCHEME://$MIRRORSRV$MIRRORPATH/Plamo-$release/$arch/plamo/$i
74 if [ $? -ne 0 ] ; then
75 echo "Failed to download the rootfs, aborting."
76 return 1
77 fi
78 done
79 for i in $EXTRACTGRS ; do
adf4b408 80 wget -nv -e robots=off -r -l $depth -nd -A .tgz,.txz -R "$rej" \
7a49a081
KY
81 -I $MIRRORPATH/Plamo-$release/$arch/contrib/$i \
82 -X $MIRRORPATH/Plamo-$release/$arch/contrib/$i/old \
83 $DLSCHEME://$MIRRORSRV$MIRRORPATH/Plamo-$release/$arch/contrib/$i
84 if [ $? -ne 0 ] ; then
85 echo "Failed to download the rootfs, aborting."
8adef761
KY
86 return 1
87 fi
cab79123 88 done
adf4b408
KY
89 for p in $ADDONPKGS ; do
90 wget -nv -e robots=off -r -l $depth -nd -A "`basename $p`-*" \
91 -I $MIRRORPATH/Plamo-$release/$arch/`dirname $p` \
92 -X $MIRRORPATH/Plamo-$release/$arch/`dirname $p`/old \
93 $DLSCHEME://$MIRRORSRV$MIRRORPATH/Plamo-$release/$arch/`dirname $p`
94 if [ $? -ne 0 ] ; then
95 echo "Failed to download the rootfs, aborting."
96 return 1
97 fi
98 done
cab79123
KY
99 mv $ptcache $dlcache
100 echo "Download complete."
101 return 0
102}
103
104copy_plamo() {
105 # make a local copy of the mini plamo
106 echo "Copying $rtcache to $rootfs..."
107 mkdir -p $rootfs
108 find $rtcache -mindepth 1 -maxdepth 1 -exec cp -a {} $rootfs \; || return 1
109 return 0
110}
111
112install_plamo() {
113 mkdir -p @LOCALSTATEDIR@/lock/subsys
114 (
ffeb76b4 115 if ! flock -n 9 ; then
cab79123
KY
116 echo "Cache repository is busy."
117 return 1
118 fi
119 echo "Checking cache download in $dlcache..."
120 if [ ! -d $dlcache ] ; then
121 if ! download_plamo ; then
122 echo "Failed to download plamo $release base packages."
123 return 1
124 fi
125 fi
92cbfdaf
KY
126 if [ ! -x /sbin/installpkg ] ; then
127 echo "'installpkg' command is missing."
128 echo "Installing 'installpkg' command into $dlcache/sbin..."
129 ( cd $dlcache ; tar xpJf hdsetup-*.txz ; rm -rf tmp usr var )
130 sed -i "/ldconfig/!s@/sbin@$dlcache&@g" $dlcache/sbin/installpkg*
ea00a202 131 sed -i "/^TAR=/s@tar@`which tar`@g" $dlcache/sbin/installpkg2
92cbfdaf
KY
132 PATH=$dlcache/sbin:$PATH
133 fi
cab79123
KY
134 echo "Installing packages to $rtcache..."
135 if [ ! -d $rtcache ] ; then
136 mkdir -p $rtcache
7a49a081 137 for p in `ls -cr $dlcache/*.t?z` ; do
8adef761 138 installpkg -root $rtcache -priority ADD $p
cab79123
KY
139 done
140 fi
141 echo "Copy $rtcache to $rootfs..."
142 if ! copy_plamo ; then
143 echo "Failed to copy rootfs."
144 return 1
145 fi
146 return 0
ffeb76b4 147 ) 9> @LOCALSTATEDIR@/lock/subsys/lxc-plamo
cab79123
KY
148}
149
150configure_plamo() {
151 # create /dev
ea00a202 152 chmod 666 $rootfs/dev/null
cab79123
KY
153 mknod -m 666 $rootfs/dev/zero c 1 5
154 chmod 666 $rootfs/dev/random
155 mknod -m 666 $rootfs/dev/urandom c 1 9
156 mkdir -m 755 $rootfs/dev/pts
157 chmod 666 $rootfs/dev/tty
158 chmod 600 $rootfs/dev/console
159 mknod -m 666 $rootfs/dev/tty0 c 4 0
160 mknod -m 666 $rootfs/dev/tty1 c 4 1
161 mknod -m 666 $rootfs/dev/tty2 c 4 2
162 mknod -m 666 $rootfs/dev/tty3 c 4 3
163 mknod -m 666 $rootfs/dev/tty4 c 4 4
164 mknod -m 666 $rootfs/dev/full c 1 7
165 mknod -m 600 $rootfs/dev/initctl p
166 mknod -m 666 $rootfs/dev/ptmx c 5 2
167 # suppress log level output for udev
168 sed -i 's/="err"/=0/' $rootfs/etc/udev/udev.conf
169 # /etc/fstab
170 cat <<- "EOF" > $rootfs/etc/fstab
171 none /proc proc defaults 0 0
172 none /sys sysfs defaults 0 0
173 none /dev tmpfs defaults 0 0
174 none /tmp tmpfs defaults 0 0
175 none /dev/pts devpts gid=5,mode=620 0 0
176 none /proc/bus/usb usbfs noauto 0 0
177 none /var/lib/nfs/rpc_pipefs rpc_pipefs defaults 0 0
178 EOF
179 # /etc/inittab
180 cat <<- "EOF" | patch $rootfs/etc/inittab
db821c3a
KY
181 32,33c32,33
182 < # What to do when power fails (shutdown to single user).
183 < pf::powerfail:/sbin/shutdown -f +5 "THE POWER IS FAILING"
184 ---
185 > # What to do when power fails (shutdown).
186 > pf::powerfail:/sbin/shutdown -h +0 "THE POWER IS FAILING"
cab79123
KY
187 47a48
188 > 1:1235:respawn:/sbin/agetty 38400 console
189 52,53d52
190 < c5:1235:respawn:/sbin/agetty 38400 tty5 linux
191 < c6:12345:respawn:/sbin/agetty 38400 tty6 linux
192 EOF
193 # set the hostname
194 echo "$name" > $rootfs/etc/HOSTNAME
195 # set minimal hosts
196 echo "127.0.0.1 localhost $name" > $rootfs/etc/hosts
197 # configure the network using the dhcp
198 echo "DHCP" > $rootfs/var/run/inet1-scheme
199 # localtime (JST)
200 ln -s ../usr/share/zoneinfo/Asia/Tokyo $rootfs/etc/localtime
201 # disable pam_loginuid.so in /etc/pam.d/login (for libvirt's lxc driver)
202 sed -i '/pam_loginuid/s/^/#/' $rootfs/etc/pam.d/login
a1e4c206 203 # glibc configure
0dc29614 204 mv $rootfs/etc/ld.so.conf{.new,}
a1e4c206 205 chroot $rootfs ldconfig
cab79123
KY
206 # root password
207 echo "Setting root password to 'root'..."
208 echo "root:root" | chroot $rootfs chpasswd
209 echo "Please change root password!"
210 # /etc/rc.d/rc.S
211 ed - $rootfs/etc/rc.d/rc.S <<- "EOF"
ffeb76b4
TS
212 /^mount -w -n -t proc/;/^# ln -s \/bin\/true/-1d
213 /^mknod \/dev\/unikey/;/^# Clean \/etc\/mtab/-2d
214 /^# copy the rules/;/^# Set the hostname/-1d
215 /^# Check the integrity/;/^# Clean up temporary/-1d
cab79123
KY
216 w
217 EOF
218 # /etc/rc.d/rc.M
219 ed - $rootfs/etc/rc.d/rc.M <<- "EOF"
ffeb76b4
TS
220 /^# Screen blanks/;/^# Initialize ip6tables/-1d
221 /^# Initialize sysctl/;/^echo "Starting services/-1d
222 /^sync/;/^# All done/-1d
cab79123
KY
223 w
224 EOF
225 # /etc/rc.d/rc.inet1.tradnet
226 head -n-93 $rootfs/sbin/netconfig.tradnet > /tmp/netconfig.rconly
227 cat <<- EOF >> /tmp/netconfig.rconly
228 PCMCIA=n
229 RC=$rootfs/etc/rc.d/rc.inet1.tradnet
230 IFCONFIG=sbin/ifconfig
231 ROUTE=sbin/route
232 INET1SCHEME=var/run/inet1-scheme
233 IPADDR=127.0.0.1
234 NETWORK=127.0.0.0
235 DHCPCD=usr/sbin/dhclient
236 LOOPBACK=y
237 make_config_file
238 EOF
239 rm -f $rootfs/etc/rc.d/rc.inet1.tradnet
240 sh /tmp/netconfig.rconly
241 rm -f /tmp/netconfig.rconly
aadd4582
TS
242 ed - $rootfs/etc/rc.d/rc.inet1.tradnet <<- "EOF"
243 g/cmdline/s/if/& false \&\&/
244 w
245 EOF
cab79123
KY
246 return 0
247}
248
249copy_configuration() {
7a49a081
KY
250 ret=0
251 cat <<- EOF >> $path/config || let ret++
cab79123 252 lxc.utsname = $name
bf3e09c0 253 lxc.mount = $path/fstab
7a49a081 254 lxc.arch = $arch
cab79123 255 EOF
7a49a081
KY
256 if [ -f "@LXCTEMPLATECONFIG@/plamo.common.conf" ] ; then
257 cat <<- "EOF" >> $path/config || let ret++
258
259 lxc.include = @LXCTEMPLATECONFIG@/plamo.common.conf
260 EOF
261 fi
262 # create the fstab (empty by default)
263 touch $path/fstab || let ret++
264 if [ $ret -ne 0 ] ; then
cab79123
KY
265 echo "Failed to add configuration."
266 return 1
267 fi
268 return 0
269}
270
271post_process() {
272 # nothing do in Plamo Linux
273 true
274}
275
276do_bindhome() {
277 # bind-mount the user's path into the container's /home
278 h=`getent passwd $bindhome | cut -d: -f6`
279 mkdir -p $rootfs/$h
280 echo "$h $rootfs/$h none bind 0 0" >> $path/fstab
281 # copy /etc/passwd, /etc/shadow, and /etc/group entries into container
282 if ! pwd=`getent passwd $bindhome` ; then
283 echo "Warning: failed to copy password entry for $bindhome."
284 else
285 echo $pwd >> $rootfs/etc/passwd
286 fi
287 echo `getent shadow $bindhome` >> $rootfs/etc/shadow
288}
289
290cleanup() {
291 [ -d $dlcache -a -d $rtcache ] || return 0
292 # lock, so we won't purge while someone is creating a repository
293 (
ffeb76b4 294 if ! flock -n 9 ; then
cab79123
KY
295 echo "Cache repository is busy."
296 return 1
297 fi
298 echo "Purging the download cache..."
299 rm -rf --one-file-system $dlcache $rtcache || return 1
300 echo "Done."
301 return 0
ffeb76b4 302 ) 9> @LOCALSTATEDIR@/lock/subsys/lxc-plamo
cab79123
KY
303}
304
305usage() {
306 cat <<- EOF
307 $prog [-h|--help] -p|--path=<path> -n|--name=<name> --rootfs=<rootfs>
ea00a202 308 [-c|--clean] [-r|--release=<release>] [-a|--arch=<arch>]
ffeb76b4 309 [-b|--bindhome=<user>]
cab79123 310 release: $release
cab79123 311 arch: x86 or x86_64: defaults to host arch
ffeb76b4 312 bindhome: bind <user>'s home into the container
cab79123
KY
313 EOF
314}
315
316prog=`basename $0`
317path="" ; name="" ; rootfs=""
318clean=0
319release=${release:-5.x}
cab79123 320arch=`uname -m | sed 's/i.86/x86/'` ; hostarch=$arch
ffeb76b4
TS
321bindhome=""
322sopts=hp:n:cr:a:b:
323lopts=help,path:,name:,rootfs:,clean,release:,arch:,bindhome:
cab79123
KY
324if ! options=`getopt -o $sopts -l $lopts -- "$@"` ; then
325 usage
326 exit 1
327fi
328eval set -- "$options"
329while true ; do
330 case "$1" in
331 -h|--help) usage && exit 0 ;;
332 -p|--path) path=$2 ; shift 2 ;;
333 -n|--name) name=$2 ; shift 2 ;;
334 --rootfs) rootfs=$2 ; shift 2 ;;
335 -c|--clean) clean=1 ; shift 1 ;;
336 -r|--release) release=$2 ; shift 2 ;;
cab79123 337 -a|--arch) arch=$2 ; shift 2 ;;
ffeb76b4 338 -b|--bindhome) bindhome=$2 ; shift 2 ;;
cab79123
KY
339 --) shift 1 ; break ;;
340 *) break ;;
341 esac
342done
343if [ $clean -eq 1 -a -z "$path" ] ; then
344 cleanup || exit 1
345 exit 0
346fi
347if [ $hostarch == "x86" -a $arch == "x86_64" ] ; then
348 echo "Can't create x86_64 container on x86."
349 exit 1
350fi
cab79123
KY
351if [ -z "$path" ] ; then
352 echo "'path' parameter is required."
353 exit 1
354fi
355if [ -z "$name" ] ; then
356 echo "'name' parameter is required."
357 exit 1
358fi
359if [ `id -u` -ne 0 ] ; then
360 echo "This script should be run as 'root'."
361 exit 1
362fi
363cache=@LOCALSTATEDIR@/cache/lxc
364ptcache=$cache/partial-${prog##*-}-$release-$arch
365dlcache=$cache/cache-${prog##*-}-$release-$arch
366rtcache=$cache/rootfs-${prog##*-}-$release-$arch
367if [ -z "$rootfs" ] ; then
368 if grep -q "^lxc.rootfs" $path/config ; then
8c3a756e 369 rootfs=`awk -F= '/^lxc.rootfs =/{ print $2 }' $path/config`
cab79123
KY
370 else
371 rootfs=$path/rootfs
372 fi
373fi
374if ! install_plamo ; then
375 echo "Failed to install plamo $release."
376 exit 1
377fi
378if ! configure_plamo ; then
379 echo "Failed to configure plamo $release for a container."
380 exit 1
381fi
382if ! copy_configuration ; then
383 echo "Failed to write configuration file."
384 exit 1
385fi
386post_process
387if [ -n "$bindhome" ] ; then
388 do_bindhome
389fi
390if [ $clean -eq 1 ] ; then
391 cleanup || exit 1
392 exit 0
393fi