]> git.proxmox.com Git - mirror_lxc.git/blob - templates/lxc-plamo.in
Merge pull request #1665 from brauner/2017-07-01/deprecate_lxc_rootfs_backend
[mirror_lxc.git] / templates / lxc-plamo.in
1 #!/bin/bash -eu
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
31 # Detect use under userns (unsupported)
32 for arg in "$@"; do
33 [ "$arg" = "--" ] && break
34 if [ "$arg" = "--mapped-uid" -o "$arg" = "--mapped-gid" ]; then
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
39 done
40
41 # Make sure the usual locations are in PATH
42 export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin
43
44 [ -r /etc/default/lxc ] && . /etc/default/lxc
45
46 DLSCHEME=${DLSCHEME:-"http"}
47 MIRRORSRV=${MIRRORSRV:-"repository.plamolinux.org"}
48 MIRRORPATH=${MIRRORPATH:-"/pub/linux/Plamo"}
49 CATEGORIES=${CATEGORIES-"00_base 01_minimum"}
50 EXTRACTGRS=${EXTRACTGRS-""}
51 IGNOREPKGS=${IGNOREPKGS-"grub kernel lilo linux_firmware microcode_ctl
52 cpufreqd cpufrequtils gpm ntp kmod"}
53 ADDONPKGS=${ADDONPKGS-"`echo contrib/Hamradio/{morse,qrq}`"}
54
55 download_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..."
63 cd $ptcache
64 case $DLSCHEME in http) depth=2 ;; ftp) depth=3 ;; esac
65 rej=${IGNOREPKGS%% *} ; [ -n "$rej" ] && rej="$rej-*"
66 if [ `echo $IGNOREPKGS | wc -w` -gt 1 ] ; then
67 for p in ${IGNOREPKGS#* } ; do rej="$rej,$p-*" ; done
68 fi
69 for i in $CATEGORIES ; do
70 wget -nv -e robots=off -r -l $depth -nd -A .tgz,.txz -R "$rej" \
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
80 wget -nv -e robots=off -r -l $depth -nd -A .tgz,.txz -R "$rej" \
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."
86 return 1
87 fi
88 done
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
99 mv $ptcache $dlcache
100 echo "Download complete."
101 return 0
102 }
103
104 copy_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
112 install_plamo() {
113 mkdir -p @LOCALSTATEDIR@/lock/subsys
114 (
115 if ! flock -n 9 ; then
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
126 # install "installpkg" command temporarily with static linked tar
127 # command into the lxc cache directory to keep the original uid/
128 # gid of files/directories.
129 echo "Installing 'installpkg' command into $dlcache/sbin..."
130 ( cd $dlcache ; tar xpJf hdsetup-*.txz ; rm -rf tmp usr var )
131 sed -i "/ldconfig/!s@/sbin@$dlcache&@g" $dlcache/sbin/installpkg*
132 PATH=$dlcache/sbin:$PATH
133 echo "Installing packages to $rtcache..."
134 if [ ! -d $rtcache ] ; then
135 mkdir -p $rtcache
136 for p in `ls -cr $dlcache/*.t?z` ; do
137 installpkg -root $rtcache -priority ADD $p
138 done
139 fi
140 echo "Copy $rtcache to $rootfs..."
141 if ! copy_plamo ; then
142 echo "Failed to copy rootfs."
143 return 1
144 fi
145 return 0
146 ) 9> @LOCALSTATEDIR@/lock/subsys/lxc-plamo
147 }
148
149 configure_plamo() {
150 # suppress log level output for udev
151 sed -i 's/="err"/=0/' $rootfs/etc/udev/udev.conf
152 # /etc/fstab
153 cat <<- "EOF" > $rootfs/etc/fstab
154 none /proc proc defaults 0 0
155 none /sys sysfs defaults 0 0
156 none /dev tmpfs defaults 0 0
157 none /tmp tmpfs defaults 0 0
158 none /dev/pts devpts gid=5,mode=620 0 0
159 none /proc/bus/usb usbfs noauto 0 0
160 none /var/lib/nfs/rpc_pipefs rpc_pipefs defaults 0 0
161 EOF
162 # /etc/inittab
163 cat <<- "EOF" | patch $rootfs/etc/inittab
164 32,33c32,33
165 < # What to do when power fails (shutdown to single user).
166 < pf::powerfail:/sbin/shutdown -f +5 "THE POWER IS FAILING"
167 ---
168 > # What to do when power fails (shutdown).
169 > pf::powerfail:/sbin/shutdown -h +0 "THE POWER IS FAILING"
170 47a48
171 > 1:1235:respawn:/sbin/agetty 38400 console
172 52,53d52
173 < c5:1235:respawn:/sbin/agetty 38400 tty5 linux
174 < c6:12345:respawn:/sbin/agetty 38400 tty6 linux
175 EOF
176 # set the hostname
177 echo "$name" > $rootfs/etc/HOSTNAME
178 # set minimal hosts
179 echo "127.0.0.1 localhost $name" > $rootfs/etc/hosts
180 # configure the network using the dhcp
181 echo "DHCP" > $rootfs/var/run/inet1-scheme
182 # localtime (JST)
183 ln -s ../usr/share/zoneinfo/Asia/Tokyo $rootfs/etc/localtime
184 # disable pam_loginuid.so in /etc/pam.d/login (for libvirt's lxc driver)
185 sed -i '/pam_loginuid/s/^/#/' $rootfs/etc/pam.d/login
186 # glibc configure
187 mv $rootfs/etc/ld.so.conf{.new,}
188 chroot $rootfs ldconfig
189 echo "Please change root password!"
190 ed - $rootfs/etc/rc.d/rc.S <<- "EOF"
191 /^mount -w -n -t proc/;/^mkdir \/dev\/shm/-1d
192 /^mknod \/dev\/null/;/^# Clean \/etc\/mtab/-2d
193 /^# copy the rules/;/^# Set the hostname/-1d
194 /^# Check the integrity/;/^# Clean up temporary/-1d
195 w
196 EOF
197 # /etc/rc.d/rc.M
198 ed - $rootfs/etc/rc.d/rc.M <<- "EOF"
199 /^# Screen blanks/;/^# Initialize ip6tables/-1d
200 /^# Initialize sysctl/;/^echo "Starting services/-1d
201 /^sync/;/^# All done/-1d
202 w
203 EOF
204 # /etc/rc.d/rc.inet1.tradnet
205 head -n-93 $rootfs/sbin/netconfig.tradnet > /tmp/netconfig.rconly
206 cat <<- EOF >> /tmp/netconfig.rconly
207 PCMCIA=n
208 RC=$rootfs/etc/rc.d/rc.inet1.tradnet
209 IFCONFIG=sbin/ifconfig
210 ROUTE=sbin/route
211 INET1SCHEME=var/run/inet1-scheme
212 IPADDR=127.0.0.1
213 NETWORK=127.0.0.0
214 DHCPCD=usr/sbin/dhclient
215 LOOPBACK=y
216 make_config_file
217 EOF
218 rm -f $rootfs/etc/rc.d/rc.inet1.tradnet
219 sh /tmp/netconfig.rconly
220 rm -f /tmp/netconfig.rconly
221 sed -i '/cmdline/s/if/& false \&\&/' $rootfs/etc/rc.d/rc.inet1.tradnet
222 # /etc/rc.d/rc.inet2
223 sed -i '/rpc.mountd/s/^/#/' $rootfs/etc/rc.d/rc.inet2
224 sed -i '/modprobe/s/^/#/' $rootfs/etc/rc.d/rc.inet2
225 # configure to start only the minimum of service
226 chmod 644 $rootfs/etc/rc.d/init.d/saslauthd
227 chmod 644 $rootfs/etc/rc.d/init.d/open-iscsi
228 rm -f $rootfs/etc/rc.d/init.d/postfix
229 rm -f $rootfs/var/log/initpkg/shadow
230 return 0
231 }
232
233 copy_configuration() {
234 ret=0
235 cat <<- EOF >> $path/config || let ret++
236 lxc.uts.name = $name
237 lxc.arch = $arch
238 EOF
239 if [ -f "@LXCTEMPLATECONFIG@/plamo.common.conf" ] ; then
240 cat <<- "EOF" >> $path/config || let ret++
241
242 lxc.include = @LXCTEMPLATECONFIG@/plamo.common.conf
243 EOF
244 fi
245 if [ $ret -ne 0 ] ; then
246 echo "Failed to add configuration."
247 return 1
248 fi
249 return 0
250 }
251
252 post_process() {
253 # nothing do in Plamo Linux
254 true
255 }
256
257 do_bindhome() {
258 # bind-mount the user's path into the container's /home
259 h=`getent passwd $bindhome | cut -d: -f6`
260 mkdir -p $rootfs/$h
261 echo "lxc.mount.entry = $h $rootfs/$h none bind 0 0" >> $path/config
262 # copy /etc/passwd, /etc/shadow, and /etc/group entries into container
263 if ! pwd=`getent passwd $bindhome` ; then
264 echo "Warning: failed to copy password entry for $bindhome."
265 else
266 echo $pwd >> $rootfs/etc/passwd
267 fi
268 echo `getent shadow $bindhome` >> $rootfs/etc/shadow
269 }
270
271 cleanup() {
272 [ -d $dlcache -a -d $rtcache ] || return 0
273 # lock, so we won't purge while someone is creating a repository
274 (
275 if ! flock -n 9 ; then
276 echo "Cache repository is busy."
277 return 1
278 fi
279 echo "Purging the download cache..."
280 rm -rf --one-file-system $dlcache $rtcache || return 1
281 echo "Done."
282 return 0
283 ) 9> @LOCALSTATEDIR@/lock/subsys/lxc-plamo
284 }
285
286 usage() {
287 cat <<- EOF
288 $prog [-h|--help] -p|--path=<path> -n|--name=<name> --rootfs=<rootfs>
289 [-c|--clean] [-r|--release=<release>] [-a|--arch=<arch>]
290 [-b|--bindhome=<user>]
291 release: $release
292 arch: x86 or x86_64: defaults to host arch
293 bindhome: bind <user>'s home into the container
294 EOF
295 }
296
297 prog=`basename $0`
298 path="" ; name="" ; rootfs=""
299 clean=0
300 release=${release:-6.x}
301 arch=`uname -m | sed 's/i.86/x86/'` ; hostarch=$arch
302 bindhome=""
303 sopts=hp:n:cr:a:b:
304 lopts=help,path:,name:,rootfs:,clean,release:,arch:,bindhome:
305 if ! options=`getopt -o $sopts -l $lopts -- "$@"` ; then
306 usage
307 exit 1
308 fi
309 eval set -- "$options"
310 while true ; do
311 case "$1" in
312 -h|--help) usage && exit 0 ;;
313 -p|--path) path=$2 ; shift 2 ;;
314 -n|--name) name=$2 ; shift 2 ;;
315 --rootfs) rootfs=$2 ; shift 2 ;;
316 -c|--clean) clean=1 ; shift 1 ;;
317 -r|--release) release=$2 ; shift 2 ;;
318 -a|--arch) arch=$2 ; shift 2 ;;
319 -b|--bindhome) bindhome=$2 ; shift 2 ;;
320 --) shift 1 ; break ;;
321 *) break ;;
322 esac
323 done
324 if [ $clean -eq 1 -a -z "$path" ] ; then
325 cleanup || exit 1
326 exit 0
327 fi
328 if [ $hostarch == "x86" -a $arch == "x86_64" ] ; then
329 echo "Can't create x86_64 container on x86."
330 exit 1
331 fi
332 if [ -z "$path" ] ; then
333 echo "'path' parameter is required."
334 exit 1
335 fi
336 if [ -z "$name" ] ; then
337 echo "'name' parameter is required."
338 exit 1
339 fi
340 if [ `id -u` -ne 0 ] ; then
341 echo "This script should be run as 'root'."
342 exit 1
343 fi
344 cache="${LXC_CACHE_PATH:-@LOCALSTATEDIR@/cache/lxc}"
345 ptcache=$cache/partial-${prog##*-}-$release-$arch
346 dlcache=$cache/cache-${prog##*-}-$release-$arch
347 rtcache=$cache/rootfs-${prog##*-}-$release-$arch
348 if [ -z "$rootfs" ] ; then
349 if grep -q "^lxc.rootfs.path" $path/config ; then
350 rootfs=`awk -F= '/^lxc.rootfs.path =/{ print $2 }' $path/config`
351 else
352 rootfs=$path/rootfs
353 fi
354 fi
355 if ! install_plamo ; then
356 echo "Failed to install plamo $release."
357 exit 1
358 fi
359 if ! configure_plamo ; then
360 echo "Failed to configure plamo $release for a container."
361 exit 1
362 fi
363 if ! copy_configuration ; then
364 echo "Failed to write configuration file."
365 exit 1
366 fi
367 post_process
368 if [ -n "$bindhome" ] ; then
369 do_bindhome
370 fi
371 if [ $clean -eq 1 ] ; then
372 cleanup || exit 1
373 exit 0
374 fi