aboutsummaryrefslogtreecommitdiff
blob: 095880a494e0b776db2ded6c3608ee1a046e1ddf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#!/bin/bash

#
# template script for generating Arch linux container for LXC
#

#
# lxc: linux Container library

# Authors:
# Alexander Vladimirov <idkfa@vlan1.ru>

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

# defaults
arch=$(arch)
cache=/var/cache/lxc/arch/${arch}
lxc_network_type="veth"
lxc_network_link="br0"
default_path=/var/lib/lxc
default_rc_locale="en-US.UTF-8"
default_rc_timezone="UTC"
host_mirror="http://mirrors.kernel.org/archlinux/\$repo/os/$arch"

# sort of minimal package set
base_packages=(
    "filesystem"
    "initscripts"
    "coreutils"
    "module-init-tools"
    "procps"
    "psmisc"
    "pacman"
    "bash"
    "syslog-ng"
    "cronie"
    "iproute2"
    "iputils"
    "inetutils"
    "dhcpcd"
    "dnsutils"
    "nano"
    "grep"
    "less"
    "gawk"
    "sed"
    "tar"
    "wget"
    "gzip"
    "which"
)
declare -a additional_packages

[ -f /etc/arch-release ] && is_arch=true

# find and extract parameter value from given config file
# ${1} - file to read parameter from
# ${2} - parameter name
# ${result} - result value on success
function read_parameter_value {
    [ -f ${1} ] && [ "${2}" ] || return 1
    local pattern="^[[:space:]]*${2}[[:space:]]*=[[:space:]]*"
    local str=$(grep "${pattern}" "${1}")
    local str=${str/#$(grep -o "${pattern}" "${1}")/}
    result=${str//\"/}
    return 0
}

# split comma-separated string into an array
# ${1} - string to split
# ${2} - separator (default is ",")
# ${result} - result value on success
function split_string {
    local ifs=${IFS}
    IFS="${2:-,}"
    read -a result < <(echo "${1}")
    IFS=${ifs}
    return 0
}

# Arch-specific preconfiguration for container
function configure_arch {
    # read locale and timezone defaults from system rc.conf if running on Arch
    if [ "${is_arch}" ]; then
        read_parameter_value "/etc/rc.conf" "LOCALE"
        rc_locale=${result:-${default_rc_locale}}
        read_parameter_value "/etc/rc.conf" "TIMEZONE"
        rc_timezone=${result:-${default_rc_timezone}}
    else
        rc_locale=${default_rc_locale}
        rc_timezone=${default_rc_timezone}
    fi

    echo "Setting up rc.conf"
    cat > "${rootfs_path}/etc/rc.conf" << EOF
# /etc/rc.conf - Main Configuration for Arch Linux
LOCALE="${rc_locale}"
DAEMON_LOCALE="no"
HARDWARECLOCK="local"
TIMEZONE="${rc_timezone}"
KEYMAP=us
CONSOLEFONT=
CONSOLEMAP=
USECOLOR="yes"
MODULES=()
HOSTNAME="${name}"
interface=eth0
address=
netmask=
broadcast=
gateway=
DAEMONS=(syslog-ng crond network)
EOF

    if [ -e "${rootfs_path}/etc/locale.gen" ]; then
        sed -i 's@^#\(en_US\.UTF-8\)@\1@' "${rootfs_path}/etc/locale.gen"
        if [ ! "${rc_locale}" = "en_US.UTF-8" ]; then
            echo "${rc_locale} ${rc_locale##*.}" >> "${rootfs_path}/etc/locale.gen"
        fi
        chroot "${rootfs_path}" locale-gen
    fi
    cp "${rootfs_path}/usr/share/zoneinfo/${rc_timezone}" \
       "${rootfs_path}/etc/localtime"

    echo "Setting up rc.sysinit"
    cat > "${rootfs_path}/etc/rc.sysinit.lxc" << EOF
#!/bin/bash
. /etc/rc.conf
. /etc/rc.d/functions

echo "starting Arch Linux"
rm -f \$(find /var/run -name '*pid')
rm -f /run/daemons/*
rm -f /var/lock/subsys/*
rm -f /etc/mtab
touch /etc/mtab
run_hook sysinit_end
EOF

    echo "Setting up rc.shutdown"
    cat > "${rootfs_path}/etc/rc.shutdown.lxc" << EOF
#!/bin/bash
. /etc/rc.conf
. /etc/rc.d/functions
stty onlcr
run_hook shutdown_start
[[ -x /etc/rc.local.shutdown ]] && /etc/rc.local.shutdown
stop_all_daemons
run_hook shutdown_prekillall
kill_all
run_hook shutdown_postkillall
[[ \${TIMEZONE} ]] && cp --remove-destination "/usr/share/zoneinfo/\${TIMEZONE}" /etc/localtime
halt -w
umount -a -r -t nodevtmpfs,notmpfs,nosysfs,noproc,nodevpts -O no_netdev
run_hook shutdown_postumount
run_hook shutdown_poweroff
if [[ \${RUNLEVEL} = 0 ]]; then
    poweroff -d -f -i
else
    reboot -d -f -i
fi
# vim: set ts=2 sw=2 noet:
EOF
    chmod 755 "${rootfs_path}/etc/rc.shutdown.lxc" "${rootfs_path}/etc/rc.sysinit.lxc"

    echo "Setting up inittab"
    cat > "${rootfs_path}/etc/inittab" << EOF
id:3:initdefault:
rc::sysinit:/etc/rc.sysinit.lxc
rs:S1:wait:/etc/rc.single
rm:2345:wait:/etc/rc.multi
rh:06:wait:/etc/rc.shutdown.lxc
su:S:wait:/sbin/sulogin -p
c1:2345:respawn:/sbin/agetty -8 38400 tty1 linux
EOF

    echo "Setting up hosts"
    cat > "${rootfs_path}/etc/hosts" << EOF
127.0.0.1   localhost.localdomain   localhost ${name}
::1     localhost.localdomain   localhost
EOF

    echo "Setting up nameserver"
    grep nameserver /etc/resolv.conf > "${rootfs_path}/etc/resolv.conf"

    echo "Setting up device nodes"
    mkdir -m 755 "${rootfs_path}/dev/pts"
    mkdir -m 1777 "${rootfs_path}/dev/shm"
    mknod -m 666 "${rootfs_path}/dev/null" c 1 3
    mknod -m 666 "${rootfs_path}/dev/full" c 1 7
    mknod -m 666 "${rootfs_path}/dev/random" c 1 8
    mknod -m 666 "${rootfs_path}/dev/urandom" c 1 9
    mknod -m 666 "${rootfs_path}/dev/tty0" c 4 0
    mknod -m 666 "${rootfs_path}/dev/tty1" c 4 1
    mknod -m 666 "${rootfs_path}/dev/tty2" c 4 2
    mknod -m 666 "${rootfs_path}/dev/tty3" c 4 3
    mknod -m 666 "${rootfs_path}/dev/tty4" c 4 4
    mknod -m 600 "${rootfs_path}/dev/initctl" p
    mknod -m 666 "${rootfs_path}/dev/tty" c 5 0
    mknod -m 666 "${rootfs_path}/dev/console" c 5 1
    mknod -m 666 "${rootfs_path}/dev/ptmx" c 5 2

    return 0
}

# write container configuration files
function copy_configuration {
    mkdir -p "${config_path}"
    cat > "${config_path}/config" << EOF
lxc.utsname=${name}
lxc.tty=4
lxc.pts=1024
lxc.rootfs=${rootfs_path}
lxc.mount=${config_path}/fstab
#networking
lxc.network.type=${lxc_network_type}
lxc.network.flags=up
lxc.network.link=${lxc_network_link}
lxc.network.name=eth0
lxc.network.mtu=1500
#cgroups
lxc.cgroup.devices.deny = a
# /dev/null and zero
lxc.cgroup.devices.allow = c 1:3 rwm
lxc.cgroup.devices.allow = c 1:5 rwm
# consoles
lxc.cgroup.devices.allow = c 5:1 rwm
lxc.cgroup.devices.allow = c 5:0 rwm
lxc.cgroup.devices.allow = c 4:0 rwm
lxc.cgroup.devices.allow = c 4:1 rwm
# /dev/{,u}random
lxc.cgroup.devices.allow = c 1:9 rwm
lxc.cgroup.devices.allow = c 1:8 rwm
# /dev/pts
lxc.cgroup.devices.allow = c 136:* rwm
lxc.cgroup.devices.allow = c 5:2 rwm
# rtc
lxc.cgroup.devices.allow = c 254:0 rwm
EOF

    cat > "${config_path}/fstab" << EOF
none ${rootfs_path}/dev/pts devpts defaults 0 0
none ${rootfs_path}/proc proc nodev,noexec,nosuid 0 0
none ${rootfs_path}/sys sysfs defaults 0 0
none ${rootfs_path}/dev/shm tmpfs defaults 0 0
EOF

    if [ ${?} -ne 0 ]; then
        echo "Failed to configure container"
        return 1
    fi

    return 0
}

# lock chroot and mount subdirectories before installing container
function mount_chroot {
    echo "mounting chroot"
    umask 0022
    [ -e "${rootfs_path}/sys" ] || mkdir "${rootfs_path}/sys"
    mount -t sysfs sysfs "${rootfs_path}/sys"
    [ -e "${rootfs_path}/proc" ] || mkdir "${rootfs_path}/proc"
    mount -t proc proc "${rootfs_path}/proc"
    [ -e "${rootfs_path}/dev" ] || mkdir "${rootfs_path}/dev"
    mount -t tmpfs dev "${rootfs_path}/dev" -o mode=0755,size=10M,nosuid
    mknod -m 666 "${rootfs_path}/dev/null" c 1 3
    mknod -m 666 "${rootfs_path}/dev/zero" c 1 5
    mknod -m 600 "${rootfs_path}/dev/console" c 5 1
    mknod -m 644 "${rootfs_path}/dev/random" c 1 8
    mknod -m 644 "${rootfs_path}/dev/urandom" c 1 9
    mknod -m 666 "${rootfs_path}/dev/tty" c 5 0
    mknod -m 666 "${rootfs_path}/dev/tty0" c 4 0
    mknod -m 666 "${rootfs_path}/dev/full" c 1 7
    ln -s /proc/kcore "${rootfs_path}/dev/core"
    ln -s /proc/self/fd "${rootfs_path}/dev/fd"
    ln -s /proc/self/fd/0 "${rootfs_path}/dev/stdin"
    ln -s /proc/self/fd/1 "${rootfs_path}/dev/stdout"
    ln -s /proc/self/fd/2 "${rootfs_path}/dev/stderr"
    [ -e "${rootfs_path}/dev/shm" ] || mkdir "${rootfs_path}/dev/shm"
    mount -t tmpfs shm "${rootfs_path}/dev/shm" -o nodev,nosuid,size=128M
    [ -e "${rootfs_path}/dev/pts" ] || mkdir "${rootfs_path}/dev/pts"
    mount -t devpts devpts "${rootfs_path}/dev/pts" -o newinstance,ptmxmode=666
    ln -s pts/ptmx "${rootfs_path}/dev/ptmx"
    [ -e "${cache_dir}" ] || mkdir -p "${cache_dir}"
    [ -e "${rootfs_path}/${cache_dir}" ] || mkdir -p "${rootfs_path}/${cache_dir}"
    mount -o bind "${cache_dir}" "${rootfs_path}/${cache_dir}"
    if [ -n "${host_mirror_path}" ]; then
        [ -e "${rootfs_path}/${host_mirror_path}" ] || mkdir -p "${rootfs_path}/${host_mirror_path}"
        mount -o bind "${host_mirror_path}" "${rootfs_path}/${host_mirror_path}"
        mount -o remount,ro,bind "${host_mirror_path}" "${rootfs_path}/${host_mirror_path}"
    fi
    trap 'umount_chroot' EXIT INT QUIT TERM HUP
}

function umount_chroot {
    if [ -z "${umount_done}" ]; then
        echo "unmounting chroot"
        umount "${rootfs_path}/proc"
        umount "${rootfs_path}/sys"
        umount "${rootfs_path}/dev/pts"
        umount "${rootfs_path}/dev/shm"
        umount "${rootfs_path}/dev"
        umount "${rootfs_path}/${cache_dir}"
        [ -n "${host_mirror_path}" ] && umount "${rootfs_path}/${host_mirror_path}"
        umount_done=1
    fi
}

# install packages within container chroot
function install_arch {
    pacman_config=$(mktemp)

    cat <<EOF > "${pacman_config}"
[options]
HoldPkg      = pacman glibc
SyncFirst    = pacman
Architecture = auto
#IgnorePkg    = udev
[core]
Include = /etc/pacman.d/mirrorlist
Server = ${host_mirror}
[extra]
Include = /etc/pacman.d/mirrorlist
Server = ${host_mirror}
[community]
Include = /etc/pacman.d/mirrorlist
Server = ${host_mirror}
EOF

    mkdir -p "${rootfs_path}/var/lib/pacman/sync"
    mkdir -p "${rootfs_path}/etc"

    if echo "${host_mirror}" | grep -q 'file://'; then
        host_mirror_path=$(echo "${host_mirror}" | sed -E 's#file://(/.*)/\$repo/os/\$arch#\1#g')
    fi
    cache_dir=$( (grep -m 1 '^CacheDir' "${pacman_config}" || echo 'CacheDir = /var/cache/pacman/pkg') | sed 's/CacheDir\s*=\s*//')
    mount_chroot
    params="--root ${rootfs_path} --config=${pacman_config} --noconfirm"
    if ! pacman -Sydd ${params} --dbonly udev; then
        echo "Failed to preinstall udev package record"
        return 1
    fi
    if ! pacman -S ${params} ${base_packages[@]}; then
        echo "Failed to install container packages"
        return 1
    fi
    [ -d "${rootfs_path}/lib/modules" ] && ldconfig -r "${rootfs_path}"
    mv "${pacman_config}" "${rootfs_path}/etc/pacman.conf"
    umount_chroot
    return 0
}

usage()
{
    cat <<EOF
usage:
    ${1} -n|--name=<container_name>
        [-P|--packages=<pkg1,pkg2,...>] [-p|--path=<path>] [-h|--help]
Mandatory args:
  -n,--name         container name, used to as an identifier for that container from now on
Optional args:
  -p,--path         path to where the container rootfs will be created, defaults to /var/lib/lxc. The container config will go under /var/lib/lxc in that case
  -P,--packages     preinstall additional packages, comma-separated list
  -h,--help         print this help
EOF
    return 0
}

options=$(getopt -o hp:P:n:cm: -l help,path:,packages:,name:,clean,mirror: -- "${@}")
if [ ${?} -ne 0 ]; then
    usage $(basename ${0})
    exit 1
fi
eval set -- "${options}"

while true
do
    case "${1}" in
    -h|--help)      usage ${0} && exit 0;;
    -p|--path)      path=${2}; shift 2;;
    -n|--name)      name=${2}; shift 2;;
    -P|--packages)  additional_packages=${2}; shift 2;;
    -m|--mirror)    host_mirror=${2}; shift 2;;
    --)             shift 1; break ;;
    *)              break ;;
    esac
done

if [ -z "${name}" ]; then
    echo "missing required 'name' parameter"
    exit 1
fi

type pacman >/dev/null 2>&1
if [ ${?} -ne 0 ]; then
    echo "'pacman' command is missing, refer to wiki.archlinux.org for information about installing pacman"
    exit 1
fi

if [ -z "${path}" ]; then
    path="${default_path}/${name}"
fi

if [ "${EUID}" != "0" ]; then
    echo "This script should be run as 'root'"
    exit 1
fi

rootfs_path="${path}/rootfs"
config_path="${default_path}/${name}"

revert()
{
    echo "Interrupted, so cleaning up"
    lxc-destroy -n "${name}"
    # maybe was interrupted before copy config
    rm -rf "${path}/${name}"
    rm -rf "${default_path}/${name}"
    exit 1
}

trap revert SIGHUP SIGINT SIGTERM

copy_configuration
if [ ${?} -ne 0 ]; then
    echo "failed write configuration file"
    rm -rf "${config_path}"
    exit 1
fi

if [ ${#additional_packages[@]} -gt 0 ]; then
    split_string ${additional_packages}
    base_packages+=(${result[@]})
fi

install_arch
if [ ${?} -ne 0 ]; then
    echo "failed to install Arch linux"
    rm -rf "${config_path}" "${path}"
    exit 1
fi

configure_arch
if [ ${?} -ne 0 ]; then
    echo "failed to configure Arch linux for a container"
    rm -rf "${config_path}" "${path}"
    exit 1
fi

echo "container rootfs and config created"