]>
git.proxmox.com Git - mirror_lxc.git/blob - templates/lxc-altlinux.in
4 # template script for generating altlinux container for LXC
8 # lxc: linux Container library
11 # Alexey Shabalin <shaba@altlinux.org>
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.
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.
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
27 # Detect use under userns (unsupported)
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
37 # Make sure the usual locations are in PATH
38 export PATH
=$PATH:/usr
/sbin
:/usr
/bin
:/sbin
:/bin
42 cache_base
=@LOCALSTATEDIR@
/cache
/lxc
/altlinux
/$arch
43 default_path
=@LXCPATH@
44 default_profile
=default
45 profile_dir
=/etc
/lxc
/profiles
48 lxc_network_link
=virbr0
51 [ -f /etc
/altlinux-release
] && is_altlinux
=true
56 # disable selinux in altlinux
57 mkdir
-p $rootfs_path/selinux
58 echo 0 > $rootfs_path/selinux
/enforce
60 mkdir
-p ${rootfs_path}/etc
/net
/ifaces
/veth0
61 cat <<EOF > ${rootfs_path}/etc/net/ifaces/veth0/options
62 BOOTPROTO=${BOOTPROTO}
68 if [ ${BOOTPROTO} != "dhcp" ]; then
70 cat <<EOF > ${rootfs_path}/etc/net/ifaces/veth0/ipv4address
74 cat <<EOF > ${rootfs_path}/etc/net/ifaces/veth0/ipv4route
78 cat <<EOF > ${rootfs_path}/etc/net/ifaces/veth0/resolv.conf
82 cat <<EOF > ${rootfs_path}/etc/net/ifaces/veth0/ipv6address
86 cat <<EOF > ${rootfs_path}/etc/net/ifaces/veth0/ipv6route
93 cat <<EOF > ${rootfs_path}/etc/sysconfig/network
101 cat <<EOF > $rootfs_path/etc/hosts
102 127.0.0.1 localhost.localdomain localhost $name
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
107 # Allow root to login at virsh console
108 echo "pts/0" >> ${rootfs_path}/etc
/securetty
109 echo "console" >> ${rootfs_path}/etc
/securetty
111 chroot
${rootfs_path} chkconfig network on
112 chroot
${rootfs_path} chkconfig syslogd on
113 chroot
${rootfs_path} chkconfig random on
114 chroot
${rootfs_path} chkconfig rawdevices off
115 chroot
${rootfs_path} chkconfig fbsetfont off
116 # chroot ${rootfs_path} chkconfig keytable off
118 subst
's/^\([3-9]\+:[0-9]\+:respawn:\/sbin\/mingetty.*\)/#\1/' ${rootfs_path}/etc
/inittab
119 echo "c1:2345:respawn:/sbin/mingetty --noclear console" >> ${rootfs_path}/etc
/inittab
120 subst
's,\/dev\/tty12,/var/log/syslog/console,' ${rootfs_path}/etc
/syslog.conf
122 dev_path
="${rootfs_path}/dev"
125 mknod
-m 666 ${dev_path}/null c
1 3
126 mknod
-m 666 ${dev_path}/zero c
1 5
127 mknod
-m 644 ${dev_path}/random c
1 8
128 mknod
-m 644 ${dev_path}/urandom c
1 9
129 mkdir
-m 755 ${dev_path}/pts
130 mkdir
-m 1777 ${dev_path}/shm
131 mknod
-m 666 ${dev_path}/tty c
5 0
132 chown root
:tty
${dev_path}/tty
133 mknod
-m 600 ${dev_path}/tty0 c
4 0
134 mknod
-m 600 ${dev_path}/tty1 c
4 1
135 mknod
-m 600 ${dev_path}/tty2 c
4 2
136 mknod
-m 600 ${dev_path}/tty3 c
4 3
137 mknod
-m 600 ${dev_path}/tty4 c
4 4
138 mknod
-m 600 ${dev_path}/console c
5 1
139 mknod
-m 666 ${dev_path}/full c
1 7
140 mknod
-m 600 ${dev_path}/initctl p
141 mknod
-m 666 ${dev_path}/ptmx c
5 2
142 chown root
:tty
${dev_path}/ptmx
143 ln -s /proc
/self
/fd
${dev_path}/fd
144 ln -s /proc
/kcore
${dev_path}/core
145 mkdir
-m 755 ${dev_path}/mapper
146 mknod
-m 600 ${dev_path}/mapper
/control c
10 236
147 mkdir
-m 755 ${dev_path}/net
148 mknod
-m 666 ${dev_path}/net
/tun c
10 200
150 echo "setting root passwd to $root_password"
151 echo "root:$root_password" | chroot
$rootfs_path chpasswd
159 # check the mini altlinux was not already downloaded
160 INSTALL_ROOT
=$cache/partial
161 mkdir
-p $INSTALL_ROOT
162 if [ $?
-ne 0 ]; then
163 echo "Failed to create '$INSTALL_ROOT' directory"
167 # download a mini altlinux into a cache
168 echo "Downloading altlinux minimal ..."
169 APT_GET
="apt-get -o RPM::RootDir=$INSTALL_ROOT -y"
170 PKG_LIST
="$(grep -hs '^[^#]' "$profile_dir/$profile")"
171 # if no configuration file $profile -- fall back to default list of packages
172 [ -z "$PKG_LIST" ] && PKG_LIST
="interactivesystem apt apt-conf-sisyphus etcnet openssh-server systemd systemd-units systemd-sysvinit"
174 mkdir
-p $INSTALL_ROOT/var
/lib
/rpm
175 rpm
--root $INSTALL_ROOT --initdb
177 # some scripts want to have /dev/null at least
178 dev_path
="$INSTALL_ROOT/dev"
179 if [ ! -c "${dev_path}/null" ]; then
180 mkdir
-p "${dev_path}"
181 mknod
-m 666 "${dev_path}/null" c
1 3
184 $APT_GET install $PKG_LIST
186 if [ $?
-ne 0 ]; then
187 echo "Failed to download the rootfs, aborting."
191 mv "$INSTALL_ROOT" "$cache/rootfs"
192 echo "Download complete."
200 # make a local copy of the minialtlinux
201 echo -n "Copying rootfs to $rootfs_path ..."
202 #cp -a $cache/rootfs-$arch $rootfs_path || return 1
203 # i prefer rsync (no reason really)
204 mkdir
-p $rootfs_path
205 rsync
-Ha $cache/rootfs
/ $rootfs_path/
211 chroot
$cache/rootfs apt-get update
212 chroot
$cache/rootfs apt-get
-y dist-upgrade
217 mkdir
-p @LOCALSTATEDIR@
/lock
/subsys
/
220 if [ $?
-ne 0 ]; then
221 echo "Cache repository is busy."
225 echo "Checking cache download in $cache/rootfs ... "
226 if [ ! -e "$cache/rootfs" ]; then
228 if [ $?
-ne 0 ]; then
229 echo "Failed to download 'altlinux base'"
233 echo "Cache found. Updating..."
235 if [ $?
-ne 0 ]; then
236 echo "Failed to update 'altlinux base', continuing with last known good cache"
238 echo "Update finished"
242 echo "Copy $cache/rootfs to $rootfs_path ... "
244 if [ $?
-ne 0 ]; then
245 echo "Failed to copy rootfs"
249 ) 9>@LOCALSTATEDIR@
/lock
/subsys
/lxc-altlinux
257 mkdir
-p $config_path
258 grep -q "^lxc.rootfs" $config_path/config
2>/dev
/null ||
echo "lxc.rootfs = $rootfs_path" >> $config_path/config
259 cat <<EOF >> $config_path/config
263 lxc.mount = $config_path/fstab
264 lxc.cap.drop = sys_module mac_admin mac_override sys_time
266 # When using LXC with apparmor, uncomment the next line to run unconfined:
267 #lxc.aa_profile = unconfined
270 lxc.network.type = $lxc_network_type
271 lxc.network.flags = up
272 lxc.network.link = $lxc_network_link
273 lxc.network.name = veth0
274 lxc.network.mtu = 1500
276 if [ ! -z ${ipv4} ]; then
277 cat <<EOF >> $config_path/config
278 lxc.network.ipv4 = $ipv4
281 if [ ! -z ${gw} ]; then
282 cat <<EOF >> $config_path/config
283 lxc.network.ipv4.gateway = $gw
286 if [ ! -z ${ipv6} ]; then
287 cat <<EOF >> $config_path/config
288 lxc.network.ipv6 = $ipv6
291 if [ ! -z ${gw6} ]; then
292 cat <<EOF >> $config_path/config
293 lxc.network.ipv6.gateway = $gw6
296 cat <<EOF >> $config_path/config
298 lxc.cgroup.devices.deny = a
300 lxc.cgroup.devices.allow = c 1:3 rwm
301 lxc.cgroup.devices.allow = c 1:5 rwm
303 lxc.cgroup.devices.allow = c 5:1 rwm
304 lxc.cgroup.devices.allow = c 5:0 rwm
305 lxc.cgroup.devices.allow = c 4:0 rwm
306 lxc.cgroup.devices.allow = c 4:1 rwm
308 lxc.cgroup.devices.allow = c 1:9 rwm
309 lxc.cgroup.devices.allow = c 1:8 rwm
310 lxc.cgroup.devices.allow = c 136:* rwm
311 lxc.cgroup.devices.allow = c 5:2 rwm
313 lxc.cgroup.devices.allow = c 10:135 rwm
316 cat <<EOF > $config_path/fstab
317 proc proc proc nodev,noexec,nosuid 0 0
318 sysfs sys sysfs defaults 0 0
321 if [ $?
-ne 0 ]; then
322 echo "Failed to add configuration"
332 if [ ! -e $cache ]; then
336 # lock, so we won't purge while someone is creating a repository
340 echo "Cache repository is busy."
344 echo -n "Purging the download cache for ALTLinux-$release..."
345 rm --preserve-root --one-file-system -rf $cache && echo "Done." ||
exit 1
347 ) 9>@LOCALSTATEDIR@
/lock
/subsys
/lxc-altlinux
354 $1 -n|--name=<container_name>
355 [-p|--path=<path>] [-c|--clean] [-R|--release=<ALTLinux_release>]
356 [-4|--ipv4=<ipv4 address>] [-6|--ipv6=<ipv6 address>]
357 [-g|--gw=<gw address>] [-d|--dns=<dns address>]
358 [-P|--profile=<name of the profile>] [--rootfs=<path>]
359 [-A|--arch=<arch of the container>]
362 -n,--name container name, used to as an identifier for that container from now on
364 -p,--path path to where the container rootfs will be created, defaults to @LXCPATH@. The container config will go under @LXCPATH@ in that case
365 -c,--clean clean the cache
366 -R,--release ALTLinux release for the new container. if the host is ALTLinux, then it will defaultto the host's release.
367 -4,--ipv4 specify the ipv4 address to assign to the virtualized interface, eg. 192.168.1.123/24
368 -6,--ipv6 specify the ipv6 address to assign to the virtualized interface, eg. 2003:db8:1:0:214:1234:fe0b:3596/64
369 -g,--gw specify the default gw, eg. 192.168.1.1
370 -G,--gw6 specify the default gw, eg. 2003:db8:1:0:214:1234:fe0b:3596
371 -d,--dns specify the DNS server, eg. 192.168.1.2
372 -P,--profile Profile name is the file name in /etc/lxc/profiles contained packages name for install to cache.
373 -A,--arch NOT USED YET. Define what arch the container will be [i686,x86_64]
374 ---rootfs rootfs path
375 -h,--help print this help
380 options
=$
(getopt
-o hp
:n
:P
:cR
:4:6:g
:d
: -l help,rootfs
:,path
:,name
:,profile
:,clean
,release
:,ipv4
:,ipv6
:,gw
:,dns
: -- "$@")
381 if [ $?
-ne 0 ]; then
385 eval set -- "$options"
390 -h|
--help) usage
$0 && exit 0;;
391 -p|
--path) path
=$2; shift 2;;
392 --rootfs) rootfs_path
=$2; shift 2;;
393 -n|
--name) name
=$2; shift 2;;
394 -P|
--profile) profile
=$2; shift 2;;
395 -c|
--clean) clean
=$2; shift 2;;
396 -R|
--release) release
=$2; shift 2;;
397 -4|
--ipv4) ipv4
=$2; shift 2;;
398 -6|
--ipv6) ipv6
=$2; shift 2;;
399 -g|
--gw) gw
=$2; shift 2;;
400 -d|
--dns) dns
=$2; shift 2;;
401 --) shift 1; break ;;
406 if [ ! -z "$clean" -a -z "$path" ]; then
411 type apt-get
>/dev
/null
2>&1
412 if [ $?
-ne 0 ]; then
413 echo "'apt-get' command is missing"
417 if [ -z "$path" ]; then
421 if [ -z "$profile" ]; then
422 profile
=$default_profile
425 if [ -z "$release" ]; then
426 if [ "$is_altlinux" ]; then
427 release
=$
(cat /etc
/altlinux-release |
awk '/^ALT/ {print $3}')
429 echo "This is not a ALTLinux host and release missing, use -R|--release to specify release"
434 if [ -z "$ipv4" -a -z "$ipv6" ]; then
440 if [ "$(id -u)" != "0" ]; then
441 echo "This script should be run as 'root'"
445 # check for 'lxc.rootfs' passed in through default config by lxc-create
446 if [ -z "$rootfs_path" ]; then
447 if grep -q '^lxc.rootfs' $path/config
2>/dev
/null
; then
448 rootfs_path
=$
(awk -F= '/^lxc.rootfs =/{ print $2 }' $path/config
)
450 rootfs_path
=$path/rootfs
454 config_path
=$default_path/$name
455 cache
=$cache_base/$release/$profile
458 if [ $?
-ne 0 ]; then
459 echo "failed to install altlinux"
464 if [ $?
-ne 0 ]; then
465 echo "failed to configure altlinux for a container"
470 if [ $?
-ne 0 ]; then
471 echo "failed write configuration file"
475 if [ ! -z $clean ]; then
479 echo "container rootfs and config created"
480 echo "network configured as $lxc_network_type in the $lxc_network_link"