4 # template script for generating openmandriva container for LXC
8 # lxc: linux Container library
11 # Alexander Khryukin <alexander@mezon.ru>
12 # Vokhmin Alexey V <avokhmin@gmail.com>
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.
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.
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
29 # Detect use under userns (unsupported)
31 [ "$arg" = "--" ] && break
32 if [ "$arg" = "--mapped-uid" -o "$arg" = "--mapped-gid" ]; then
33 echo "This template can't be used for unprivileged containers." 1>&2
34 echo "You may want to try the \"download\" template instead." 1>&2
39 # Make sure the usual locations are in PATH
40 export PATH
=$PATH:/usr
/sbin
:/usr
/bin
:/sbin
:/bin
45 cache_base
=@LOCALSTATEDIR@
/cache
/lxc
/openmandriva
/$arch
46 default_path
=@LXCPATH@
47 default_profile
=default
52 # is this openmandriva?
53 [ -f /etc
/mandriva-release
] && is_openmandriva
=true
55 configure_openmandriva
()
57 mkdir
-p ${rootfs_path}/etc
/sysconfig
/network-scripts
/
59 # configure the network using the dhcp
60 cat <<EOF > ${rootfs_path}/etc/sysconfig/network-scripts/ifcfg-eth0
69 cat <<EOF > ${rootfs_path}/etc/sysconfig/network
74 echo "${utsname}" > ${rootfs_path}/etc
/hostname
77 cat <<EOF > $rootfs_path/etc/hosts
78 127.0.0.1 localhost.localdomain localhost $utsname
79 ::1 localhost6.localdomain6 localhost6
85 echo -n "Create devices in /dev/"
86 dev_path
="${rootfs_path}/dev"
89 mknod
-m 666 ${dev_path}/null c
1 3
90 mknod
-m 666 ${dev_path}/zero c
1 5
91 mknod
-m 666 ${dev_path}/random c
1 8
92 mknod
-m 666 ${dev_path}/urandom c
1 9
93 mkdir
-m 755 ${dev_path}/pts
94 mkdir
-m 1777 ${dev_path}/shm
95 mknod
-m 666 ${dev_path}/tty c
5 0
96 mknod
-m 666 ${dev_path}/tty0 c
4 0
97 mknod
-m 666 ${dev_path}/tty1 c
4 1
98 mknod
-m 666 ${dev_path}/tty2 c
4 2
99 mknod
-m 666 ${dev_path}/tty3 c
4 3
100 mknod
-m 666 ${dev_path}/tty4 c
4 4
101 mknod
-m 600 ${dev_path}/console c
5 1
102 mknod
-m 666 ${dev_path}/full c
1 7
103 mknod
-m 600 ${dev_path}/initctl p
104 mknod
-m 666 ${dev_path}/ptmx c
5 2
105 mkdir
-m 755 ${dev_path}/net
106 mknod
-m 666 ${dev_path}/net
/tun c
10 200
110 set_guest_root_password
()
112 [ -z "$root_password" ] && return # pass is empty, abort
114 echo " - setting guest root password.."
115 echo "root passwd is: $root_password"
116 echo "root:$root_password" | chroot
"$rootfs_path" chpasswd
120 create_chroot_openmandriva
()
122 # check the mini openmandriva was not already downloaded
123 INSTALL_ROOT
=$cache/cache
124 mkdir
-p $INSTALL_ROOT
125 if [ $?
-ne 0 ]; then
126 echo "Failed to create '$INSTALL_ROOT' directory"
129 # package list to install
130 PKG_LIST
="basesystem-minimal locales locales-en initscripts urpmi cronie dhcp-client kbd"
131 # download a mini openmandriva into a cache
132 echo "Downloading openmandriva minimal ..."
133 URPMI
="/usr/sbin/urpmi.addmedia --urpmi-root $INSTALL_ROOT main http://abf.rosalinux.ru/downloads/$release/repository/$arch/main/release"
135 URPMI_BASE
="/usr/sbin/urpmi --no-suggests --no-verify-rpm --ignorearch --root $INSTALL_ROOT --urpmi-root $INSTALL_ROOT --auto $PKG_LIST"
138 # We're splitting the old loop into two loops plus a directory retrival.
139 # First loop... Try and retrive a mirror list with retries and a slight
140 # delay between attempts...
141 if [ $?
-ne 0 ]; then
142 echo "Failed to download the rootfs, aborting."
146 mv "$INSTALL_ROOT" "$cache/rootfs"
147 echo "Download complete."
156 echo -n "Copying rootfs to $rootfs_path ..."
157 mkdir
-p $rootfs_path
158 rsync
-Ha $cache/rootfs
/ $rootfs_path/
162 update_openmandriva
()
164 echo "automated update in progress..."
165 urpmi
--root $cache/rootfs
--urpmi-root $cache/rootfs
--auto --auto-update --ignorearch
168 configure_openmandriva_systemd
()
170 chroot
${rootfs_path} ln -s /dev
/null
/etc
/systemd
/system
/proc-sys-fs-binfmt_misc.automount
171 chroot
${rootfs_path} ln -s /dev
/null
/etc
/systemd
/system
/systemd-udevd.service
172 chroot
${rootfs_path} ln -s /dev
/null
/etc
/systemd
/system
/systemd-udevd-control.socket
173 chroot
${rootfs_path} ln -s /dev
/null
/etc
/systemd
/system
/systemd-udevd-kernel.socket
174 # remove numlock service
175 # KDGKBLED: Inappropriate ioctl for device
176 rm -f ${rootfs_path}/etc
/systemd
/system
/getty@.service.d
/enable-numlock.conf
178 unlink
${rootfs_path}/etc
/systemd
/system
/default.target
179 chroot
${rootfs_path} ln -s /lib
/systemd
/system
/multi-user.target
/etc
/systemd
/system
/default.target
180 sed -i 's!ConditionPathExists=/dev/tty0!ConditionPathExists=|/dev/tty0\nConditionVirtualization=|lxc!' \
181 ${rootfs_path}/lib
/systemd
/system
/getty\@.service
185 install_openmandriva
()
187 mkdir
-p @LOCALSTATEDIR@
/lock
/subsys
/
190 if [ $?
-ne 0 ]; then
191 echo "Cache repository is busy."
195 echo "Checking cache download in $cache/rootfs ... "
196 if [ ! -e "$cache/rootfs" ]; then
198 create_chroot_openmandriva
199 if [ $?
-ne 0 ]; then
200 echo "Failed to download 'openmandriva basesystem-minimal'"
204 echo "Cache found. Updating..."
206 if [ $?
-ne 0 ]; then
207 echo "Failed to update 'openmandriva base', continuing with last known good cache"
209 echo "Update finished"
213 echo "Copy $cache/rootfs to $rootfs_path ... "
215 if [ $?
-ne 0 ]; then
216 echo "Failed to copy rootfs"
220 ) 9>@LOCALSTATEDIR@
/lock
/subsys
/lxc-openmandriva
228 mkdir
-p $config_path
229 grep -q "^lxc.rootfs" $config_path/config
2>/dev
/null ||
echo "lxc.rootfs = $rootfs_path" >> $config_path/config
230 cat <<EOF >> $config_path/config
234 lxc.cap.drop = sys_module mac_admin mac_override sys_time
235 lxc.mount.auto = cgroup:mixed proc:mixed sys:mixed
237 # When using LXC with apparmor, uncomment the next line to run unconfined:
238 #lxc.aa_profile = unconfined
241 lxc.network.type = $lxc_network_type
242 lxc.network.flags = up
243 lxc.network.link = $lxc_network_link
244 lxc.network.name = eth0
245 lxc.network.mtu = 1500
247 if [ ! -z ${ipv4} ]; then
248 cat <<EOF >> $config_path/config
249 lxc.network.ipv4 = $ipv4
252 if [ ! -z ${gw} ]; then
253 cat <<EOF >> $config_path/config
254 lxc.network.ipv4.gateway = $gw
257 if [ ! -z ${ipv6} ]; then
258 cat <<EOF >> $config_path/config
259 lxc.network.ipv6 = $ipv6
262 if [ ! -z ${gw6} ]; then
263 cat <<EOF >> $config_path/config
264 lxc.network.ipv6.gateway = $gw6
267 cat <<EOF >> $config_path/config
269 lxc.cgroup.devices.deny = a
271 lxc.cgroup.devices.allow = c 1:3 rwm
272 lxc.cgroup.devices.allow = c 1:5 rwm
274 lxc.cgroup.devices.allow = c 5:1 rwm
275 lxc.cgroup.devices.allow = c 5:0 rwm
276 lxc.cgroup.devices.allow = c 4:0 rwm
277 lxc.cgroup.devices.allow = c 4:1 rwm
279 lxc.cgroup.devices.allow = c 1:9 rwm
280 lxc.cgroup.devices.allow = c 1:8 rwm
281 lxc.cgroup.devices.allow = c 136:* rwm
282 lxc.cgroup.devices.allow = c 5:2 rwm
284 lxc.cgroup.devices.allow = c 10:135 rwm
287 if [ $?
-ne 0 ]; then
288 echo "Failed to add configuration"
298 if [ ! -e $cache ]; then
302 # lock, so we won't purge while someone is creating a repository
306 echo "Cache repository is busy."
310 echo -n "Purging the download cache for OpenMandriva-$release..."
311 rm --preserve-root --one-file-system -rf $cache && echo "Done." ||
exit 1
313 ) 9>@LOCALSTATEDIR@
/lock
/subsys
/lxc-openmandriva
320 $1 -n|--name=<container_name>
321 [-p|--path=<path>] [-c|--clean] [-R|--release=<openmandriva2013.0/rosa2012.1/cooker/ release>]
322 [-4|--ipv4=<ipv4 address>] [-6|--ipv6=<ipv6 address>]
323 [-g|--gw=<gw address>] [-d|--dns=<dns address>]
324 [-P|--profile=<name of the profile>] [--rootfs=<path>]
325 [-A|--arch=<arch of the container>]
328 -n,--name container name, used to as an identifier for that container from now on
330 -p,--path path to where the container rootfs will be created, defaults to @LXCPATH@. The container config will go under @LXCPATH@ in that case
331 -c,--clean clean the cache
332 -R,--release openmandriva2013.0/cooker/rosa2012.1 release for the new container. if the host is OpenMandriva, then it will default to the host's release.
333 -4,--ipv4 specify the ipv4 address to assign to the virtualized interface, eg. 192.168.1.123/24
334 -6,--ipv6 specify the ipv6 address to assign to the virtualized interface, eg. 2003:db8:1:0:214:1234:fe0b:3596/64
335 -g,--gw specify the default gw, eg. 192.168.1.1
336 -G,--gw6 specify the default gw, eg. 2003:db8:1:0:214:1234:fe0b:3596
337 -d,--dns specify the DNS server, eg. 192.168.1.2
338 -P,--profile Profile name is the file name in /etc/lxc/profiles contained packages name for install to cache.
339 -A,--arch Define what arch the container will be [i586,x86_64,armv7l,armv7hl]
340 ---rootfs rootfs path
341 -h,--help print this help
346 options
=$
(getopt
-o hp
:n
:P
:cR
:4:6:g
:d
:A
-l help,rootfs
:,path
:,name
:,profile
:,clean
:,release
:,ipv4
:,ipv6
:,gw
:,dns
:,arch
: -- "$@")
347 if [ $?
-ne 0 ]; then
351 eval set -- "$options"
353 release
=${release:-"cooker"}
354 if [ -f /etc
/lsb-release
]; then
356 if [ "$DISTRIB_ID" = "OpenMandrivaLinux" ]; then
357 release
=openmandriva2013.0
358 elif [ "$DISTRIB_ID" = "RosaDesktop.Fresh" ]; then
361 echo "This is not an OpenMandriva or ROSA release"
369 -h|
--help) usage
$0 && exit 0;;
370 -p|
--path) path
=$2; shift 2;;
371 --rootfs) rootfs_path
=$2; shift 2;;
372 -n|
--name) name
=$2; shift 2;;
373 -P|
--profile) profile
=$2; shift 2;;
374 -c|
--clean) clean
=1; shift 1;;
375 -R|
--release) release
=$2; shift 2;;
376 -A|
--arch) arch
=$2; shift 2;;
377 -4|
--ipv4) ipv4
=$2; shift 2;;
378 -6|
--ipv6) ipv6
=$2; shift 2;;
379 -g|
--gw) gw
=$2; shift 2;;
380 -d|
--dns) dns
=$2; shift 2;;
381 --) shift 1; break ;;
386 arch
=${arch:-$hostarch}
388 if [ ! -z "$clean" -a -z "$path" ]; then
393 if [ -z "${utsname}" ]; then
397 type urpmi
>/dev
/null
2>&1
398 if [ $?
-ne 0 ]; then
399 echo "'urpmi' command is missing"
403 if [ -z "$path" ]; then
407 if [ -z "$profile" ]; then
408 profile
=$default_profile
411 if [ $hostarch = "i586" -a $arch = "x86_64" ]; then
412 echo "can't create x86_64 container on i586"
416 if [ -z "$ipv4" -a -z "$ipv6" ]; then
422 if [ "$(id -u)" != "0" ]; then
423 echo "This script should be run as 'root'"
427 # check for 'lxc.rootfs' passed in through default config by lxc-create
428 if [ -z "$rootfs_path" ]; then
429 if grep -q '^lxc.rootfs' $path/config
2>/dev
/null
; then
430 rootfs_path
=$
(awk -F= '/^lxc.rootfs =/{ print $2 }' $path/config
)
432 rootfs_path
=$path/$name/rootfs
436 config_path
=$default_path/$name
437 cache
=$cache_base/$release/$arch/$profile
439 if [ ! -f $config_path/config
]; then
440 echo "A container with that name exists, chose a different name"
445 if [ $?
-ne 0 ]; then
446 echo "failed to install openmandriva"
450 configure_openmandriva
451 if [ $?
-ne 0 ]; then
452 echo "failed to configure openmandriva for a container"
456 # If the systemd configuration directory exists - set it up for what we need.
457 if [ -d ${rootfs_path}/etc
/systemd
/system
]
459 configure_openmandriva_systemd
463 if [ $?
-ne 0 ]; then
464 echo "failed to populated /dev/ devices"
468 set_guest_root_password
469 if [ $?
-ne 0 ]; then
470 echo "failed to configure password for chroot"
475 if [ $?
-ne 0 ]; then
476 echo "failed write configuration file"
480 if [ ! -z "$clean" ]; then
484 echo "container rootfs and config created"