]> git.proxmox.com Git - mirror_lxc.git/blob - templates/lxc-pld.in
templates: add PLD Linux support
[mirror_lxc.git] / templates / lxc-pld.in
1 #!/bin/sh
2
3 #
4 # template script for generating PLD Linux container for LXC
5 #
6
7 #
8 # lxc: Linux Container library
9
10 # Authors:
11 # Elan Ruusamäe <glen@pld-linux.org>
12
13 # This library is free software; you can redistribute it and/or
14 # modify it under the terms of the GNU Lesser General Public
15 # License as published by the Free Software Foundation; either
16 # version 2.1 of the License, or (at your option) any later version.
17
18 # This library is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 # Lesser General Public License for more details.
22
23 # You should have received a copy of the GNU Lesser General Public
24 # License along with this library; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
27 # Detect use under userns (unsupported)
28 for arg in "$@"; do
29 [ "$arg" = "--" ] && break
30 if [ "$arg" = "--mapped-uid" -o "$arg" = "--mapped-gid" ]; then
31 echo "This template can't be used for unprivileged containers." 1>&2
32 echo "You may want to try the \"download\" template instead." 1>&2
33 exit 1
34 fi
35 done
36
37 # Configuration
38 arch=$(uname -m)
39 cache_base=@LOCALSTATEDIR@/cache/lxc/pld/$arch
40 default_path=@LXCPATH@
41 root_password=root
42
43 if [ -e /etc/os-release ]; then
44 # This is a shell friendly configuration file. We can just source it.
45 # What we're looking for in here is the ID, VERSION_ID and the CPE_NAME
46 . /etc/os-release
47 echo "Host CPE ID from /etc/os-release: ${CPE_NAME}"
48 fi
49
50 if [ "${CPE_NAME}" != "" -a "${ID}" = "pld" -a "${VERSION_ID}" != "" ]; then
51 pld_host_ver=${VERSION_ID}
52 is_pld=true
53 elif [ -e /etc/pld-release ]; then
54 # Only if all other methods fail, try to parse the pld-release file.
55 pld_host_ver=$(sed -e '/PLD /!d' -e 's/^\([0-9.]*\)\sPLD.*/\1/' < /etc/pld-release)
56 if [ "$pld_host_ver" != "" ]; then
57 is_pld=true
58 fi
59 fi
60
61 # Map a few architectures to their generic PLD Linux repository archs.
62 case "$pld_host_ver:$arch" in
63 3.0:i586) arch=i486 ;;
64 esac
65
66 configure_pld()
67 {
68
69 # disable selinux
70 mkdir -p $rootfs_path/selinux
71 echo 0 > $rootfs_path/selinux/enforce
72
73 # configure the network using the dhcp
74 sed -i -e "s/^HOSTNAME=.*/HOSTNAME=${utsname}/" ${rootfs_path}/etc/sysconfig/network
75
76 # set hostname on systemd
77 if [ $release = "3.0" ]; then
78 echo "${utsname}" > ${rootfs_path}/etc/hostname
79 fi
80
81 # set minimal hosts
82 test -e $rootfs_path/etc/hosts || \
83 cat <<EOF > $rootfs_path/etc/hosts
84 127.0.0.1 localhost.localdomain localhost $utsname
85 ::1 localhost6.localdomain6 localhost6
86 EOF
87
88 dev_path="${rootfs_path}/dev"
89 rm -rf $dev_path
90 mkdir -p $dev_path
91 mknod -m 666 ${dev_path}/null c 1 3
92 mknod -m 666 ${dev_path}/zero c 1 5
93 mknod -m 666 ${dev_path}/random c 1 8
94 mknod -m 666 ${dev_path}/urandom c 1 9
95 mkdir -m 755 ${dev_path}/pts
96 mkdir -m 1777 ${dev_path}/shm
97 mknod -m 666 ${dev_path}/tty c 5 0
98 mknod -m 666 ${dev_path}/tty0 c 4 0
99 mknod -m 666 ${dev_path}/tty1 c 4 1
100 mknod -m 666 ${dev_path}/tty2 c 4 2
101 mknod -m 666 ${dev_path}/tty3 c 4 3
102 mknod -m 666 ${dev_path}/tty4 c 4 4
103 mknod -m 600 ${dev_path}/console c 5 1
104 mknod -m 666 ${dev_path}/full c 1 7
105 mknod -m 600 ${dev_path}/initctl p
106 mknod -m 666 ${dev_path}/ptmx c 5 2
107
108 echo "setting root passwd to $root_password"
109 echo "root:$root_password" | chroot $rootfs_path chpasswd
110
111 return 0
112 }
113
114 configure_pld_init()
115 {
116 # default powerfail action waits 2 minutes. for lxc we want it immediately
117 sed -i -e '/^pf::powerfail:/ s,/sbin/shutdown.*,/sbin/halt,' ${rootfs_path}/etc/inittab
118 }
119
120 configure_pld_systemd()
121 {
122 unlink ${rootfs_path}/etc/systemd/system/default.target
123 chroot ${rootfs_path} ln -s /dev/null /etc/systemd/system/udev.service
124 chroot ${rootfs_path} ln -s /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
125
126 # Actually, the After=dev-%i.device line does not appear in the
127 # Fedora 17 or Fedora 18 systemd getty@.service file. It may be left
128 # over from an earlier version and it's not doing any harm. We do need
129 # to disable the "ConditionalPathExists=/dev/tty0" line or no gettys are
130 # started on the ttys in the container. Lets do it in an override copy of
131 # the service so it can still pass rpm verifies and not be automatically
132 # updated by a new systemd version. -- mhw /\/\|=mhw=|\/\/
133
134 sed -e 's/^ConditionPathExists=/# ConditionPathExists=/' \
135 -e 's/After=dev-%i.device/After=/' \
136 < ${rootfs_path}/lib/systemd/system/getty@.service \
137 > ${rootfs_path}/etc/systemd/system/getty@.service
138
139 # Setup getty service on the 4 ttys we are going to allow in the
140 # default config. Number should match lxc.tty
141 for i in 1 2 3 4; do
142 ln -sf ../getty@.service ${rootfs_path}/etc/systemd/system/getty.target.wants/getty@tty${i}.service
143 done
144 }
145
146 download_pld()
147 {
148
149 # check the mini pld was not already downloaded
150 INSTALL_ROOT=$cache/partial
151 mkdir -p $INSTALL_ROOT
152 if [ $? -ne 0 ]; then
153 echo "Failed to create '$INSTALL_ROOT' directory"
154 return 1
155 fi
156
157 # download a mini pld into a cache
158 echo "Downloading PLD Linux minimal ..."
159 POLDEK="poldek --root $INSTALL_ROOT --noask --nohold --noignore"
160 PKG_LIST="basesystem filesystem pld-release rpm poldek vserver-packages rc-scripts pwdutils mingetty"
161
162 mkdir -p $INSTALL_ROOT@LOCALSTATEDIR@/lib/rpm
163 rpm --root $INSTALL_ROOT --initdb
164 $POLDEK -u $PKG_LIST
165
166 if [ $? -ne 0 ]; then
167 echo "Failed to download the rootfs, aborting."
168 return 1
169 fi
170
171 mv "$INSTALL_ROOT" "$cache/rootfs"
172 echo "Download complete."
173
174 return 0
175 }
176
177 copy_pld()
178 {
179
180 # make a local copy of the minipld
181 echo -n "Copying rootfs to $rootfs_path ..."
182 cp -a $cache/rootfs/* $rootfs_path || return 1
183 return 0
184 }
185
186 update_pld()
187 {
188 POLDEK="poldek --root $cache/rootfs --noask"
189 $POLDEK --upgrade-dist
190 }
191
192 install_pld()
193 {
194 mkdir -p @LOCALSTATEDIR@/lock/subsys/
195 (
196 flock -x 9
197 if [ $? -ne 0 ]; then
198 echo "Cache repository is busy."
199 return 1
200 fi
201
202 echo "Checking cache download in $cache/rootfs ... "
203 if [ ! -e "$cache/rootfs" ]; then
204 download_pld
205 if [ $? -ne 0 ]; then
206 echo "Failed to download 'pld base'"
207 return 1
208 fi
209 else
210 echo "Cache found. Updating..."
211 update_pld
212 if [ $? -ne 0 ]; then
213 echo "Failed to update 'pld base', continuing with last known good cache"
214 else
215 echo "Update finished"
216 fi
217 fi
218
219 echo "Copy $cache/rootfs to $rootfs_path ... "
220 copy_pld
221 if [ $? -ne 0 ]; then
222 echo "Failed to copy rootfs"
223 return 1
224 fi
225
226 return 0
227 ) 9>@LOCALSTATEDIR@/lock/subsys/lxc-pld
228
229 return $?
230 }
231
232 copy_configuration()
233 {
234
235 mkdir -p $config_path
236 grep -q "^lxc.rootfs" $config_path/config 2>/dev/null || echo "lxc.rootfs = $rootfs_path" >> $config_path/config
237 cat <<EOF >> $config_path/config
238 # Most of below settings should be taken as defaults from
239 # lxc.include = /usr/share/lxc/config/common.conf
240 lxc.utsname = $utsname
241 lxc.tty = 4
242 lxc.pts = 1024
243 # Consider if below line is right for systemd container
244 lxc.mount = $config_path/fstab
245 lxc.cap.drop = sys_module mac_admin mac_override sys_time
246
247 lxc.autodev = $auto_dev
248
249 # When using LXC with apparmor, uncomment the next line to run unconfined:
250 #lxc.aa_profile = unconfined
251
252 ## Devices
253 # Allow all devices
254 #lxc.cgroup.devices.allow = a
255 # Deny all devices
256 lxc.cgroup.devices.deny = a
257 # Allow to mknod all devices (but not using them)
258 lxc.cgroup.devices.allow = c *:* m
259 lxc.cgroup.devices.allow = b *:* m
260
261 # /dev/null and zero
262 lxc.cgroup.devices.allow = c 1:3 rwm
263 lxc.cgroup.devices.allow = c 1:5 rwm
264 # consoles
265 lxc.cgroup.devices.allow = c 5:1 rwm
266 lxc.cgroup.devices.allow = c 5:0 rwm
267 lxc.cgroup.devices.allow = c 4:0 rwm
268 lxc.cgroup.devices.allow = c 4:1 rwm
269 # /dev/{,u}random
270 lxc.cgroup.devices.allow = c 1:9 rwm
271 lxc.cgroup.devices.allow = c 1:8 rwm
272 lxc.cgroup.devices.allow = c 136:* rwm
273 lxc.cgroup.devices.allow = c 5:2 rwm
274 # rtc
275 lxc.cgroup.devices.allow = c 254:0 rm
276 EOF
277
278 cat <<EOF > $config_path/fstab
279 proc proc proc nodev,noexec,nosuid 0 0
280 sysfs sys sysfs defaults 0 0
281 EOF
282 if [ $? -ne 0 ]; then
283 echo "Failed to add configuration"
284 return 1
285 fi
286
287 return 0
288 }
289
290 clean()
291 {
292
293 if [ ! -e $cache ]; then
294 exit 0
295 fi
296
297 # lock, so we won't purge while someone is creating a repository
298 (
299 flock -x 9
300 if [ $? != 0 ]; then
301 echo "Cache repository is busy."
302 exit 1
303 fi
304
305 echo -n "Purging the download cache for PLD Linux $release..."
306 rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
307 exit 0
308 ) 9>@LOCALSTATEDIR@/lock/subsys/lxc-pld
309 }
310
311 usage()
312 {
313 cat <<EOF
314 usage:
315 $1 -n|--name=<container_name>
316 [-p|--path=<path>] [-c|--clean] [-R|--release=<PLD Release>] [--fqdn=<network name of container>] [-A|--arch=<arch of the container>]
317 [-h|--help]
318 Mandatory args:
319 -n,--name container name, used to as an identifier for that container from now on
320 Optional args:
321 -p,--path path to where the container will be created, defaults to @LXCPATH@. The container config will go under @LXCPATH@ in that case
322 --rootfs path for actual rootfs.
323 -c,--clean clean the cache
324 -R,--release PLD Linux release for the new container. if the host is PLD Linux, then it will default to the host's release.
325 --fqdn fully qualified domain name (FQDN) for DNS and system naming
326 -A,--arch NOT USED YET. Define what arch the container will be [i686,x86_64]
327 -h,--help print this help
328 EOF
329 return 0
330 }
331
332 options=$(getopt -o hp:n:cR: -l help,path:,rootfs:,name:,clean,release:,fqdn: -- "$@")
333 if [ $? -ne 0 ]; then
334 usage $(basename $0)
335 exit 1
336 fi
337 eval set -- "$options"
338
339 while :; do
340 case "$1" in
341 -h|--help) usage $0 && exit 0;;
342 -p|--path) path=$2; shift 2;;
343 --rootfs) rootfs=$2; shift 2;;
344 -n|--name) name=$2; shift 2;;
345 -c|--clean) clean=$2; shift 2;;
346 -R|--release) release=$2; shift 2;;
347 --fqdn) utsname=$2; shift 2;;
348 --) shift 1; break ;;
349 *) break ;;
350 esac
351 done
352
353 if [ ! -z "$clean" -a -z "$path" ]; then
354 clean || exit 1
355 exit 0
356 fi
357
358 if [ -z "${utsname}" ]; then
359 utsname=${name}
360 fi
361
362 # This follows a standard "resolver" convention that an FQDN must have
363 # at least two dots or it is considered a local relative host name.
364 # If it doesn't, append the dns domain name of the host system.
365 #
366 # This changes one significant behavior when running
367 # "lxc_create -n Container_Name" without using the
368 # --fqdn option.
369 #
370 # Old behavior:
371 # utsname and hostname = Container_Name
372 # New behavior:
373 # utsname and hostname = Container_Name.Domain_Name
374
375 if [ $(expr "$utsname" : '.*\..*\.') = 0 ]; then
376 if [ -n "$(dnsdomainname)" ]; then
377 utsname=${utsname}.$(dnsdomainname)
378 fi
379 fi
380
381 needed_pkgs=""
382 type poldek >/dev/null 2>&1
383 if [ $? -ne 0 ]; then
384 needed_pkgs="poldek $needed_pkgs"
385 fi
386
387 #type curl >/dev/null 2>&1
388 #if [ $? -ne 0 ]; then
389 # needed_pkgs="curl $needed_pkgs"
390 #fi
391
392 if [ -n "$needed_pkgs" ]; then
393 echo "Missing commands: $needed_pkgs"
394 echo "Please install these using \"sudo poldek -u $needed_pkgs\""
395 exit 1
396 fi
397
398 if [ -z "$path" ]; then
399 path=$default_path/$name
400 fi
401
402 if [ -z "$release" ]; then
403 if [ "$is_pld" -a "$pld_host_ver" ]; then
404 release=$pld_host_ver
405 else
406 echo "This is not a PLD Linux host and release missing, defaulting to 3.0. use -R|--release to specify release"
407 release=3.0
408 fi
409 fi
410
411 # pld th have systemd. We need autodev enabled to keep systemd from causing problems.
412 if [ $release = 3.0 ]; then
413 auto_dev="0"
414 else
415 auto_dev="0"
416 fi
417
418 if [ "$(id -u)" != "0" ]; then
419 echo "This script should be run as 'root'"
420 exit 1
421 fi
422
423 if [ -z "$rootfs" ]; then
424 rootfs_path=$path/rootfs
425 # check for 'lxc.rootfs' passed in through default config by lxc-create
426 # TODO: should be lxc.rootfs.mount used instead?
427 if grep -q '^lxc.rootfs' $path/config 2>/dev/null ; then
428 rootfs_path=$(awk -F= '/^lxc.rootfs =/{ print $2 }' $path/config)
429 fi
430 else
431 rootfs_path=$rootfs
432 fi
433 config_path=$default_path/$name
434 cache=$cache_base/$release
435
436 revert()
437 {
438 echo "Interrupted, so cleaning up"
439 lxc-destroy -n $name
440 # maybe was interrupted before copy config
441 rm -rf $path
442 rm -rf $default_path/$name
443 echo "exiting..."
444 exit 1
445 }
446
447 trap revert SIGHUP SIGINT SIGTERM
448
449 copy_configuration
450 if [ $? -ne 0 ]; then
451 echo "Failed write configuration file"
452 exit 1
453 fi
454
455 install_pld
456 if [ $? -ne 0 ]; then
457 echo "Failed to install PLD Linux"
458 exit 1
459 fi
460
461 configure_pld
462 if [ $? -ne 0 ]; then
463 echo "Failed to configure PLD Linux for a container"
464 exit 1
465 fi
466
467 # If the systemd configuration directory exists - set it up for what we need.
468 if [ -d ${rootfs_path}/etc/systemd/system ]; then
469 configure_pld_systemd
470 fi
471
472 # This configuration (rc.sysinit) is not inconsistent with the systemd stuff
473 # above and may actually coexist on some upgraded systems. Let's just make
474 # sure that, if it exists, we update this file, even if it's not used...
475 if [ -f ${rootfs_path}/etc/rc.sysinit ]; then
476 configure_pld_init
477 fi
478
479 if [ ! -z $clean ]; then
480 clean || exit 1
481 exit 0
482 fi
483 echo "container rootfs and config created"