]> git.proxmox.com Git - mirror_lxc.git/blob - templates/lxc-altlinux.in
Merge pull request #872 from clopez/lxc-devsetup-sysvinit-upstart
[mirror_lxc.git] / templates / lxc-altlinux.in
1 #!/bin/bash
2
3 #
4 # template script for generating altlinux container for LXC
5 #
6
7 #
8 # lxc: linux Container library
9
10 # Authors:
11 # Alexey Shabalin <shaba@altlinux.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 # Make sure the usual locations are in PATH
38 export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin
39
40 #Configurations
41 arch=$(uname -m)
42 cache_base=@LOCALSTATEDIR@/cache/lxc/altlinux/$arch
43 default_path=@LXCPATH@
44 default_profile=default
45 profile_dir=/etc/lxc/profiles
46 root_password=rooter
47 lxc_network_type=veth
48 lxc_network_link=virbr0
49
50 # is this altlinux?
51 [ -f /etc/altlinux-release ] && is_altlinux=true
52
53 configure_altlinux()
54 {
55
56 # disable selinux in altlinux
57 mkdir -p $rootfs_path/selinux
58 echo 0 > $rootfs_path/selinux/enforce
59
60 mkdir -p ${rootfs_path}/etc/net/ifaces/eth0
61 cat <<EOF > ${rootfs_path}/etc/net/ifaces/eth0/options
62 BOOTPROTO=${BOOTPROTO}
63 ONBOOT=yes
64 NM_CONTROLLED=yes
65 TYPE=eth
66 EOF
67
68 if [ ${BOOTPROTO} != "dhcp" ]; then
69 # ip address
70 cat <<EOF > ${rootfs_path}/etc/net/ifaces/eth0/ipv4address
71 ${ipv4}
72 EOF
73
74 cat <<EOF > ${rootfs_path}/etc/net/ifaces/eth0/ipv4route
75 ${gw}
76 EOF
77
78 cat <<EOF > ${rootfs_path}/etc/net/ifaces/eth0/resolv.conf
79 nameserver ${dns}
80 EOF
81
82 cat <<EOF > ${rootfs_path}/etc/net/ifaces/eth0/ipv6address
83 ${ipv6}
84 EOF
85
86 cat <<EOF > ${rootfs_path}/etc/net/ifaces/eth0/ipv6route
87 ${gw6}
88 EOF
89
90 fi
91
92 # set the hostname
93 cat <<EOF > ${rootfs_path}/etc/sysconfig/network
94 NETWORKING=yes
95 CONFMETHOD=etcnet
96 HOSTNAME=${UTSNAME}
97 RESOLV_MODS=yes
98 EOF
99
100 # set minimal hosts
101 cat <<EOF > $rootfs_path/etc/hosts
102 127.0.0.1 localhost.localdomain localhost $name
103 EOF
104 # Allow to login at virsh console. loginuid.so doen't work in the absence of auditd.
105 # sed -i 's/^.*loginuid.so.*$/\#&/' ${rootfs_path}/etc/pam.d/common-login
106
107 # Allow root to login at virsh console
108 echo "pts/0" >> ${rootfs_path}/etc/securetty
109 echo "console" >> ${rootfs_path}/etc/securetty
110
111 # Enable services
112 for service in network syslogd random NetworkManager
113 do
114 chroot ${rootfs_path} chkconfig $service --list &>/dev/null && chroot ${rootfs_path} chkconfig $service on || true
115 # For systemd
116 chroot ${rootfs_path} systemctl -q enable $service &>/dev/null|| true
117 done
118 # Disable services
119 for service in rawdevices fbsetfont
120 do
121 chroot ${rootfs_path} chkconfig $service --list &>/dev/null && chroot ${rootfs_path} chkconfig $service off || true
122 chroot ${rootfs_path} systemctl -q disable $service &>/dev/null || true
123 done
124
125 subst 's/^\([3-9]\+:[0-9]\+:respawn:\/sbin\/mingetty.*\)/#\1/' ${rootfs_path}/etc/inittab
126 echo "c1:2345:respawn:/sbin/mingetty --noclear console" >> ${rootfs_path}/etc/inittab
127
128 [ -f "${rootfs_path}/etc/syslog.conf" ] && \
129 subst 's,\/dev\/tty12,/var/log/syslog/console,' ${rootfs_path}/etc/syslog.conf
130
131 dev_path="${rootfs_path}/dev"
132 rm -rf ${dev_path}
133 mkdir -p ${dev_path}
134 mknod -m 666 ${dev_path}/null c 1 3
135 mknod -m 666 ${dev_path}/zero c 1 5
136 mknod -m 644 ${dev_path}/random c 1 8
137 mknod -m 644 ${dev_path}/urandom c 1 9
138 mkdir -m 755 ${dev_path}/pts
139 mkdir -m 1777 ${dev_path}/shm
140 mknod -m 666 ${dev_path}/tty c 5 0
141 chown root:tty ${dev_path}/tty
142 mknod -m 600 ${dev_path}/tty0 c 4 0
143 mknod -m 600 ${dev_path}/tty1 c 4 1
144 mknod -m 600 ${dev_path}/tty2 c 4 2
145 mknod -m 600 ${dev_path}/tty3 c 4 3
146 mknod -m 600 ${dev_path}/tty4 c 4 4
147 mknod -m 600 ${dev_path}/console c 5 1
148 mknod -m 666 ${dev_path}/full c 1 7
149 mknod -m 600 ${dev_path}/initctl p
150 mknod -m 666 ${dev_path}/ptmx c 5 2
151 chown root:tty ${dev_path}/ptmx
152 ln -s /proc/self/fd ${dev_path}/fd
153 ln -s /proc/kcore ${dev_path}/core
154 mkdir -m 755 ${dev_path}/mapper
155 mknod -m 600 ${dev_path}/mapper/control c 10 236
156 mkdir -m 755 ${dev_path}/net
157 mknod -m 666 ${dev_path}/net/tun c 10 200
158
159 echo "setting root passwd to $root_password"
160 echo "root:$root_password" | chroot $rootfs_path chpasswd
161
162 return 0
163 }
164
165 download_altlinux()
166 {
167
168 # check the mini altlinux was not already downloaded
169 INSTALL_ROOT=$cache/partial
170 mkdir -p $INSTALL_ROOT
171 if [ $? -ne 0 ]; then
172 echo "Failed to create '$INSTALL_ROOT' directory"
173 return 1
174 fi
175
176 # download a mini altlinux into a cache
177 echo "Downloading altlinux minimal ..."
178 APT_GET="apt-get -o RPM::RootDir=$INSTALL_ROOT -y"
179 PKG_LIST="$(grep -hs '^[^#]' "$profile_dir/$profile")"
180 # if no configuration file $profile -- fall back to default list of packages
181 [ -z "$PKG_LIST" ] && PKG_LIST="interactivesystem apt apt-conf-sisyphus etcnet-full openssh-server systemd-sysvinit systemd-units systemd NetworkManager-daemon"
182
183 mkdir -p $INSTALL_ROOT/var/lib/rpm
184 rpm --root $INSTALL_ROOT --initdb
185
186 # some scripts want to have /dev/null at least
187 dev_path="$INSTALL_ROOT/dev"
188 if [ ! -c "${dev_path}/null" ]; then
189 mkdir -p "${dev_path}"
190 mknod -m 666 "${dev_path}/null" c 1 3
191 fi
192
193 $APT_GET install $PKG_LIST
194
195 if [ $? -ne 0 ]; then
196 echo "Failed to download the rootfs, aborting."
197 return 1
198 fi
199
200 mv "$INSTALL_ROOT" "$cache/rootfs"
201 echo "Download complete."
202
203 return 0
204 }
205
206 copy_altlinux()
207 {
208
209 # make a local copy of the minialtlinux
210 echo -n "Copying rootfs to $rootfs_path ..."
211 #cp -a $cache/rootfs-$arch $rootfs_path || return 1
212 # i prefer rsync (no reason really)
213 mkdir -p $rootfs_path
214 rsync -Ha $cache/rootfs/ $rootfs_path/
215 return 0
216 }
217
218 update_altlinux()
219 {
220 chroot $cache/rootfs apt-get update
221 chroot $cache/rootfs apt-get -y dist-upgrade
222 }
223
224 install_altlinux()
225 {
226 mkdir -p @LOCALSTATEDIR@/lock/subsys/
227 (
228 flock -x 9
229 if [ $? -ne 0 ]; then
230 echo "Cache repository is busy."
231 return 1
232 fi
233
234 echo "Checking cache download in $cache/rootfs ... "
235 if [ ! -e "$cache/rootfs" ]; then
236 download_altlinux
237 if [ $? -ne 0 ]; then
238 echo "Failed to download 'altlinux base'"
239 return 1
240 fi
241 else
242 echo "Cache found. Updating..."
243 update_altlinux
244 if [ $? -ne 0 ]; then
245 echo "Failed to update 'altlinux base', continuing with last known good cache"
246 else
247 echo "Update finished"
248 fi
249 fi
250
251 echo "Copy $cache/rootfs to $rootfs_path ... "
252 copy_altlinux
253 if [ $? -ne 0 ]; then
254 echo "Failed to copy rootfs"
255 return 1
256 fi
257 return 0
258 ) 9>@LOCALSTATEDIR@/lock/subsys/lxc-altlinux
259
260 return $?
261 }
262
263 copy_configuration()
264 {
265
266 mkdir -p $config_path
267 grep -q "^lxc.rootfs" $config_path/config 2>/dev/null || echo "lxc.rootfs = $rootfs_path" >> $config_path/config
268 cat <<EOF >> $config_path/config
269 lxc.utsname = $name
270 lxc.tty = 4
271 lxc.pts = 1024
272 lxc.cap.drop = sys_module mac_admin mac_override sys_time
273
274 # When using LXC with apparmor, uncomment the next line to run unconfined:
275 #lxc.aa_profile = unconfined
276
277 #networking
278 #lxc.network.type = $lxc_network_type
279 #lxc.network.flags = up
280 #lxc.network.link = $lxc_network_link
281 #lxc.network.name = veth0
282 #lxc.network.mtu = 1500
283 EOF
284 if [ ! -z ${ipv4} ]; then
285 cat <<EOF >> $config_path/config
286 lxc.network.ipv4 = $ipv4
287 EOF
288 fi
289 if [ ! -z ${gw} ]; then
290 cat <<EOF >> $config_path/config
291 lxc.network.ipv4.gateway = $gw
292 EOF
293 fi
294 if [ ! -z ${ipv6} ]; then
295 cat <<EOF >> $config_path/config
296 lxc.network.ipv6 = $ipv6
297 EOF
298 fi
299 if [ ! -z ${gw6} ]; then
300 cat <<EOF >> $config_path/config
301 lxc.network.ipv6.gateway = $gw6
302 EOF
303 fi
304 cat <<EOF >> $config_path/config
305 #cgroups
306 lxc.cgroup.devices.deny = a
307 # /dev/null and zero
308 lxc.cgroup.devices.allow = c 1:3 rwm
309 lxc.cgroup.devices.allow = c 1:5 rwm
310 # consoles
311 lxc.cgroup.devices.allow = c 5:1 rwm
312 lxc.cgroup.devices.allow = c 5:0 rwm
313 lxc.cgroup.devices.allow = c 4:0 rwm
314 lxc.cgroup.devices.allow = c 4:1 rwm
315 # /dev/{,u}random
316 lxc.cgroup.devices.allow = c 1:9 rwm
317 lxc.cgroup.devices.allow = c 1:8 rwm
318 lxc.cgroup.devices.allow = c 136:* rwm
319 lxc.cgroup.devices.allow = c 5:2 rwm
320 # rtc
321 lxc.cgroup.devices.allow = c 10:135 rwm
322
323 lxc.mount.auto = cgroup:mixed proc:mixed sys:mixed
324 EOF
325
326 if [ $? -ne 0 ]; then
327 echo "Failed to add configuration"
328 return 1
329 fi
330
331 return 0
332 }
333
334 clean()
335 {
336
337 if [ ! -e $cache ]; then
338 exit 0
339 fi
340
341 # lock, so we won't purge while someone is creating a repository
342 (
343 flock -x 9
344 if [ $? != 0 ]; then
345 echo "Cache repository is busy."
346 exit 1
347 fi
348
349 echo -n "Purging the download cache for ALTLinux-$release..."
350 rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
351 exit 0
352 ) 9>@LOCALSTATEDIR@/lock/subsys/lxc-altlinux
353 }
354
355 usage()
356 {
357 cat <<EOF
358 usage:
359 $1 -n|--name=<container_name>
360 [-p|--path=<path>] [-c|--clean] [-R|--release=<ALTLinux_release>]
361 [-4|--ipv4=<ipv4 address>] [-6|--ipv6=<ipv6 address>]
362 [-g|--gw=<gw address>] [-d|--dns=<dns address>]
363 [-P|--profile=<name of the profile>] [--rootfs=<path>]
364 [-A|--arch=<arch of the container>]
365 [-h|--help]
366 Mandatory args:
367 -n,--name container name, used to as an identifier for that container from now on
368 Optional args:
369 -p,--path path to where the container rootfs will be created, defaults to @LXCPATH@. The container config will go under @LXCPATH@ in that case
370 -c,--clean clean the cache
371 -R,--release ALTLinux release for the new container. if the host is ALTLinux, then it will defaultto the host's release.
372 -4,--ipv4 specify the ipv4 address to assign to the virtualized interface, eg. 192.168.1.123/24
373 -6,--ipv6 specify the ipv6 address to assign to the virtualized interface, eg. 2003:db8:1:0:214:1234:fe0b:3596/64
374 -g,--gw specify the default gw, eg. 192.168.1.1
375 -G,--gw6 specify the default gw, eg. 2003:db8:1:0:214:1234:fe0b:3596
376 -d,--dns specify the DNS server, eg. 192.168.1.2
377 -P,--profile Profile name is the file name in /etc/lxc/profiles contained packages name for install to cache.
378 -A,--arch NOT USED YET. Define what arch the container will be [i686,x86_64]
379 ---rootfs rootfs path
380 -h,--help print this help
381 EOF
382 return 0
383 }
384
385 options=$(getopt -o hp:n:P:cR:4:6:g:d: -l help,rootfs:,path:,name:,profile:,clean,release:,ipv4:,ipv6:,gw:,dns: -- "$@")
386 if [ $? -ne 0 ]; then
387 usage $(basename $0)
388 exit 1
389 fi
390 eval set -- "$options"
391
392 while true
393 do
394 case "$1" in
395 -h|--help) usage $0 && exit 0;;
396 -p|--path) path=$2; shift 2;;
397 --rootfs) rootfs_path=$2; shift 2;;
398 -n|--name) name=$2; shift 2;;
399 -P|--profile) profile=$2; shift 2;;
400 -c|--clean) clean=1; shift 1;;
401 -R|--release) release=$2; shift 2;;
402 -4|--ipv4) ipv4=$2; shift 2;;
403 -6|--ipv6) ipv6=$2; shift 2;;
404 -g|--gw) gw=$2; shift 2;;
405 -d|--dns) dns=$2; shift 2;;
406 --) shift 1; break ;;
407 *) break ;;
408 esac
409 done
410
411 if [ ! -z "$clean" -a -z "$path" ]; then
412 clean || exit 1
413 exit 0
414 fi
415
416 type apt-get >/dev/null 2>&1
417 if [ $? -ne 0 ]; then
418 echo "'apt-get' command is missing"
419 exit 1
420 fi
421
422 if [ -z "$path" ]; then
423 path=$default_path
424 fi
425
426 if [ -z "$profile" ]; then
427 profile=$default_profile
428 fi
429
430 if [ -z "$release" ]; then
431 if [ "$is_altlinux" ]; then
432 release=$(cat /etc/altlinux-release |awk '/^ALT/ {print $3}')
433 else
434 echo "This is not a ALTLinux host and release missing, use -R|--release to specify release"
435 exit 1
436 fi
437 fi
438
439 if [ -z "$ipv4" -a -z "$ipv6" ]; then
440 BOOTPROTO="dhcp"
441 else
442 BOOTPROTO="static"
443 fi
444
445 if [ "$(id -u)" != "0" ]; then
446 echo "This script should be run as 'root'"
447 exit 1
448 fi
449
450 # check for 'lxc.rootfs' passed in through default config by lxc-create
451 if [ -z "$rootfs_path" ]; then
452 if grep -q '^lxc.rootfs' $path/config 2>/dev/null ; then
453 rootfs_path=$(awk -F= '/^lxc.rootfs =/{ print $2 }' $path/config)
454 else
455 rootfs_path=$path/rootfs
456 fi
457 fi
458
459 config_path=$default_path/$name
460 cache=$cache_base/$release/$profile
461
462 install_altlinux
463 if [ $? -ne 0 ]; then
464 echo "failed to install altlinux"
465 exit 1
466 fi
467
468 configure_altlinux
469 if [ $? -ne 0 ]; then
470 echo "failed to configure altlinux for a container"
471 exit 1
472 fi
473
474 copy_configuration
475 if [ $? -ne 0 ]; then
476 echo "failed write configuration file"
477 exit 1
478 fi
479
480 if [ ! -z "$clean" ]; then
481 clean || exit 1
482 exit 0
483 fi
484 echo "container rootfs and config created"
485 echo "network configured as $lxc_network_type in the $lxc_network_link"