| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# |
| 4 |
# lxc: linux Container library |
| 5 |
|
| 6 |
# Authors: |
| 7 |
# Daniel Lezcano <[email protected]> |
| 8 |
# Greg Olsen <[email protected]> |
| 9 |
# Tanya <[email protected]> |
| 10 |
|
| 11 |
# This library is free software; you can redistribute it and/or |
| 12 |
# modify it under the terms of the GNU Lesser General Public |
| 13 |
# License as published by the Free Software Foundation; either |
| 14 |
# version 2.1 of the License, or (at your option) any later version. |
| 15 |
|
| 16 |
# This library is distributed in the hope that it will be useful, |
| 17 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 |
# Lesser General Public License for more details. |
| 20 |
|
| 21 |
# You should have received a copy of the GNU Lesser General Public |
| 22 |
# License along with this library; if not, write to the Free Software |
| 23 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 24 |
|
| 25 |
# Detect use under userns (unsupported) |
| 26 |
for arg in "$@"; do |
| 27 |
[ "$arg" = "--" ] && break |
| 28 |
if [ "$arg" = "--mapped-uid" -o "$arg" = "--mapped-gid" ]; then |
| 29 |
echo "This template can't be used for unprivileged containers." 1>&2 |
| 30 |
echo "You may want to try the \"download\" template instead." 1>&2 |
| 31 |
exit 1 |
| 32 |
fi |
| 33 |
done |
| 34 |
|
| 35 |
# Make sure the usual locations are in PATH |
| 36 |
export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin |
| 37 |
export GREP_OPTIONS="" |
| 38 |
|
| 39 |
MIRROR=${MIRROR:-http://deb.devuan.org/merged} |
| 40 |
SECURITY_MIRROR=${SECURITY_MIRROR:-http://deb.devuan.org/merged} |
| 41 |
LOCALSTATEDIR="@LOCALSTATEDIR@" |
| 42 |
LXC_TEMPLATE_CONFIG="@LXCTEMPLATECONFIG@" |
| 43 |
# Allows the lxc-cache directory to be set by environment variable |
| 44 |
LXC_CACHE_PATH=${LXC_CACHE_PATH:-"$LOCALSTATEDIR/cache/lxc"} |
| 45 |
DOWNLOAD_KEYRING=${DOWNLOAD_KEYRING:-1} |
| 46 |
|
| 47 |
find_interpreter() |
| 48 |
{ |
| 49 |
given_interpreter=$(basename "$1") |
| 50 |
|
| 51 |
if [ ! -d /proc/sys/fs/binfmt_misc/ ] ; then |
| 52 |
return 1 |
| 53 |
fi |
| 54 |
for file in /proc/sys/fs/binfmt_misc/* ; do |
| 55 |
if [ "$file" = "/proc/sys/fs/binfmt_misc/register" -o \ |
| 56 |
"$file" = "/proc/sys/fs/binfmt_misc/status" ] ; then |
| 57 |
continue |
| 58 |
fi |
| 59 |
interpreter_path=$(sed -n "/^interpreter/s/interpreter \([^[:space:]]*\)/\1/p" "$file") |
| 60 |
interpreter=$(basename "$interpreter_path") |
| 61 |
|
| 62 |
if [ "$given_interpreter" = "$interpreter" ] ; then |
| 63 |
echo "$interpreter_path" |
| 64 |
return 0 |
| 65 |
fi |
| 66 |
done |
| 67 |
return 1 |
| 68 |
} |
| 69 |
|
| 70 |
release_info() { |
| 71 |
if wget "${1}/dists/${release}/Release" -O /dev/null --quiet 2> /dev/null; then |
| 72 |
local release_file=$(wget "${1}/dists/${release}/Release" -O - --quiet 2> /dev/null) |
| 73 |
distro_suite=${release_file#*Suite: } |
| 74 |
distro_suite=${distro_suite%%$'\n'*} |
| 75 |
return 0 |
| 76 |
fi |
| 77 |
return 1 |
| 78 |
} |
| 79 |
|
| 80 |
configure_devuan() |
| 81 |
{ |
| 82 |
rootfs=$1 |
| 83 |
hostname=$2 |
| 84 |
num_tty=$3 |
| 85 |
|
| 86 |
# squeeze only has /dev/tty and /dev/tty0 by default, |
| 87 |
# therefore creating missing device nodes for tty1-4. |
| 88 |
for tty in $(seq 1 "$num_tty"); do |
| 89 |
if [ ! -e "$rootfs/dev/tty$tty" ]; then |
| 90 |
mknod "$rootfs/dev/tty$tty" c 4 "$tty" |
| 91 |
fi |
| 92 |
done |
| 93 |
|
| 94 |
# configure the inittab |
| 95 |
cat <<EOF > $rootfs/etc/inittab |
| 96 |
id:3:initdefault: |
| 97 |
si::sysinit:/etc/init.d/rcS |
| 98 |
l0:0:wait:/etc/init.d/rc 0 |
| 99 |
l1:1:wait:/etc/init.d/rc 1 |
| 100 |
l2:2:wait:/etc/init.d/rc 2 |
| 101 |
l3:3:wait:/etc/init.d/rc 3 |
| 102 |
l4:4:wait:/etc/init.d/rc 4 |
| 103 |
l5:5:wait:/etc/init.d/rc 5 |
| 104 |
l6:6:wait:/etc/init.d/rc 6 |
| 105 |
# Normally not reached, but fallthrough in case of emergency. |
| 106 |
z6:6:respawn:/sbin/sulogin |
| 107 |
1:2345:respawn:/sbin/getty 38400 console |
| 108 |
$(for tty in $(seq 1 "$num_tty"); do echo "c${tty}:12345:respawn:/sbin/getty 38400 tty${tty} linux" ; done;) |
| 109 |
p6::ctrlaltdel:/sbin/init 6 |
| 110 |
p0::powerfail:/sbin/init 0 |
| 111 |
EOF |
| 112 |
|
| 113 |
# symlink mtab |
| 114 |
[ -e "$rootfs/etc/mtab" ] && rm "$rootfs/etc/mtab" |
| 115 |
ln -s /proc/self/mounts "$rootfs/etc/mtab" |
| 116 |
|
| 117 |
# disable selinux in devuan |
| 118 |
mkdir -p "$rootfs/selinux" |
| 119 |
echo 0 > "$rootfs/selinux/enforce" |
| 120 |
|
| 121 |
# remove links protection settings |
| 122 |
rm -f "$rootfs/etc/sysctl.d/protect-links.conf" |
| 123 |
|
| 124 |
# configure the network using the dhcp |
| 125 |
cat <<EOF > $rootfs/etc/network/interfaces |
| 126 |
auto lo |
| 127 |
iface lo inet loopback |
| 128 |
|
| 129 |
auto eth0 |
| 130 |
iface eth0 inet dhcp |
| 131 |
EOF |
| 132 |
|
| 133 |
# set the hostname |
| 134 |
cat <<EOF > $rootfs/etc/hostname |
| 135 |
$hostname |
| 136 |
EOF |
| 137 |
|
| 138 |
# set minimal hosts |
| 139 |
cat <<EOF > $rootfs/etc/hosts |
| 140 |
127.0.0.1 localhost |
| 141 |
127.0.1.1 $hostname |
| 142 |
|
| 143 |
# The following lines are desirable for IPv6 capable hosts |
| 144 |
::1 localhost ip6-localhost ip6-loopback |
| 145 |
ff02::1 ip6-allnodes |
| 146 |
ff02::2 ip6-allrouters |
| 147 |
EOF |
| 148 |
|
| 149 |
# reconfigure some services |
| 150 |
|
| 151 |
# but first reconfigure locales - so we get no noisy perl-warnings |
| 152 |
if [ -z "$LANG" ] || echo $LANG | grep -E -q "^C(\..+)*$"; then |
| 153 |
cat >> "$rootfs/etc/locale.gen" << EOF |
| 154 |
en_US.UTF-8 UTF-8 |
| 155 |
EOF |
| 156 |
chroot "$rootfs" locale-gen en_US.UTF-8 UTF-8 |
| 157 |
chroot "$rootfs" update-locale LANG=en_US.UTF-8 |
| 158 |
else |
| 159 |
encoding=$(locale charmap) |
| 160 |
[ -z "${encoding}" ] && encoding="UTF-8" |
| 161 |
chroot "$rootfs" sed -e "s/^# \(${LANG} ${encoding}\)/\1/" \ |
| 162 |
-i /etc/locale.gen 2> /dev/null |
| 163 |
cat >> "$rootfs/etc/locale.gen" << EOF |
| 164 |
$LANG $encoding |
| 165 |
EOF |
| 166 |
chroot "$rootfs" locale-gen "$LANG" "$encoding" |
| 167 |
chroot "$rootfs" update-locale LANG="$LANG" |
| 168 |
fi |
| 169 |
|
| 170 |
# remove pointless services in a container |
| 171 |
chroot "$rootfs" /usr/sbin/update-rc.d -f checkroot.sh disable |
| 172 |
chroot "$rootfs" /usr/sbin/update-rc.d -f umountfs disable |
| 173 |
chroot "$rootfs" /usr/sbin/update-rc.d -f hwclock.sh disable |
| 174 |
chroot "$rootfs" /usr/sbin/update-rc.d -f hwclockfirst.sh disable |
| 175 |
|
| 176 |
# generate new SSH keys |
| 177 |
if [ -x "$rootfs/var/lib/dpkg/info/openssh-server.postinst" ]; then |
| 178 |
cat > "$rootfs/usr/sbin/policy-rc.d" << EOF |
| 179 |
#!/bin/sh |
| 180 |
exit 101 |
| 181 |
EOF |
| 182 |
chmod +x "$rootfs/usr/sbin/policy-rc.d" |
| 183 |
|
| 184 |
if [ -f "$rootfs/etc/init/ssh.conf" ]; then |
| 185 |
mv "$rootfs/etc/init/ssh.conf" "$rootfs/etc/init/ssh.conf.disabled" |
| 186 |
fi |
| 187 |
|
| 188 |
rm -f "$rootfs/etc/ssh/"ssh_host_*key* |
| 189 |
|
| 190 |
DPKG_MAINTSCRIPT_PACKAGE=openssh DPKG_MAINTSCRIPT_NAME=postinst chroot "$rootfs" /var/lib/dpkg/info/openssh-server.postinst configure |
| 191 |
sed -i "s/root@$(hostname)/root@$hostname/g" "$rootfs/etc/ssh/"ssh_host_*.pub |
| 192 |
|
| 193 |
if [ -f "$rootfs/etc/init/ssh.conf.disabled" ]; then |
| 194 |
mv "$rootfs/etc/init/ssh.conf.disabled" "$rootfs/etc/init/ssh.conf" |
| 195 |
fi |
| 196 |
|
| 197 |
rm -f "$rootfs/usr/sbin/policy-rc.d" |
| 198 |
fi |
| 199 |
|
| 200 |
# set initial timezone as on host |
| 201 |
if [ -f /etc/timezone ]; then |
| 202 |
cat /etc/timezone > "$rootfs/etc/timezone" |
| 203 |
rm -f "$rootfs/etc/localtime" |
| 204 |
chroot "$rootfs" dpkg-reconfigure -f noninteractive tzdata |
| 205 |
elif [ -f /etc/sysconfig/clock ]; then |
| 206 |
. /etc/sysconfig/clock |
| 207 |
echo "$ZONE" > "$rootfs/etc/timezone" |
| 208 |
rm -f "$rootfs/etc/localtime" |
| 209 |
chroot "$rootfs" dpkg-reconfigure -f noninteractive tzdata |
| 210 |
else |
| 211 |
echo "Timezone in container is not configured. Adjust it manually." |
| 212 |
fi |
| 213 |
|
| 214 |
if [ -n "$authkey" ]; then |
| 215 |
local ssh_dir_path="${rootfs}/root/.ssh" |
| 216 |
mkdir -p "$ssh_dir_path" |
| 217 |
cp "$authkey" "${ssh_dir_path}/authorized_keys" |
| 218 |
chmod 700 "$ssh_dir_path" |
| 219 |
echo "Inserted SSH public key from '$authkey' into /root/.ssh/authorized_keys" |
| 220 |
fi |
| 221 |
|
| 222 |
return 0 |
| 223 |
} |
| 224 |
|
| 225 |
write_sourceslist() |
| 226 |
{ |
| 227 |
local rootfs="$1"; shift |
| 228 |
local release="$1"; shift |
| 229 |
local arch="$1"; shift |
| 230 |
|
| 231 |
local prefix="deb" |
| 232 |
if [ -n "${arch}" ]; then |
| 233 |
prefix="deb [arch=${arch}]" |
| 234 |
fi |
| 235 |
|
| 236 |
if [ "$mainonly" = 1 ]; then |
| 237 |
non_main='' |
| 238 |
else |
| 239 |
non_main=' contrib non-free' |
| 240 |
fi |
| 241 |
|
| 242 |
cat >> "${rootfs}/etc/apt/sources.list" << EOF |
| 243 |
${prefix} $MIRROR ${release} main${non_main} |
| 244 |
EOF |
| 245 |
|
| 246 |
if [ "$release" != "unstable" -a "$release" != "ceres" ]; then |
| 247 |
cat >> "${rootfs}/etc/apt/sources.list" << EOF |
| 248 |
${prefix} $SECURITY_MIRROR ${release}-updates main${non_main} |
| 249 |
EOF |
| 250 |
fi |
| 251 |
} |
| 252 |
|
| 253 |
apt_update_packages(){ |
| 254 |
local apt_update_args="update" |
| 255 |
local lpath="${rootfs}/var/lib/apt/lists/$(echo "$MIRROR" | sed 's#.*://##;s#/#_#g')_dists_${release}_Release" |
| 256 |
if [ -f "$lpath" ] && [ ! -z "$distro_suite" ]; then |
| 257 |
local lsuite=$(cat "$lpath" | grep 'Suite:') |
| 258 |
[ "${lsuite#Suite: }" != "$distro_suite" ] && apt_update_args="--allow-releaseinfo-change $apt_update_args" |
| 259 |
fi |
| 260 |
chroot "${rootfs}" apt-get $apt_update_args |
| 261 |
return $? |
| 262 |
} |
| 263 |
|
| 264 |
install_packages() |
| 265 |
{ |
| 266 |
local rootfs="$1"; shift |
| 267 |
local packages="$*" |
| 268 |
|
| 269 |
if [ -n "${packages}" ] && apt_update_packages; then |
| 270 |
chroot "${rootfs}" apt-get install --force-yes -y --no-install-recommends ${packages} |
| 271 |
elif [ -n "${packages}" ]; then |
| 272 |
echo "Apt update failed, not downloading packages" |
| 273 |
fi |
| 274 |
} |
| 275 |
|
| 276 |
# Check if given path is in a btrfs partition |
| 277 |
is_btrfs() |
| 278 |
{ |
| 279 |
[ -e "$1" -a "$(stat -f -c '%T' "$1")" = "btrfs" ] |
| 280 |
} |
| 281 |
|
| 282 |
# Check if given path is the root of a btrfs subvolume |
| 283 |
is_btrfs_subvolume() |
| 284 |
{ |
| 285 |
[ -d "$1" -a "$(stat -f -c '%T' "$1")" = "btrfs" -a "$(stat -c '%i' "$1")" -eq 256 ] |
| 286 |
} |
| 287 |
|
| 288 |
try_mksubvolume() |
| 289 |
{ |
| 290 |
path=$1 |
| 291 |
[ -d "$path" ] && return 0 |
| 292 |
mkdir -p "$(dirname "$path")" |
| 293 |
if which btrfs >/dev/null 2>&1 && is_btrfs "$(dirname "$path")"; then |
| 294 |
btrfs subvolume create "$path" |
| 295 |
else |
| 296 |
mkdir -p "$path" |
| 297 |
fi |
| 298 |
} |
| 299 |
|
| 300 |
try_rmsubvolume() |
| 301 |
{ |
| 302 |
path=$1 |
| 303 |
[ -d "$path" ] || return 0 |
| 304 |
if which btrfs >/dev/null 2>&1 && is_btrfs_subvolume "$path"; then |
| 305 |
btrfs subvolume delete "$path" |
| 306 |
else |
| 307 |
rm -rf "$path" |
| 308 |
fi |
| 309 |
} |
| 310 |
|
| 311 |
cleanup() |
| 312 |
{ |
| 313 |
try_rmsubvolume "$cache/partial-$release-$arch" |
| 314 |
try_rmsubvolume "$cache/rootfs-$release-$arch" |
| 315 |
} |
| 316 |
|
| 317 |
download_devuan() |
| 318 |
{ |
| 319 |
packages=\ |
| 320 |
init,\ |
| 321 |
ifupdown,\ |
| 322 |
locales,\ |
| 323 |
ca-certificates,\ |
| 324 |
dialog,\ |
| 325 |
isc-dhcp-client,\ |
| 326 |
netbase,\ |
| 327 |
net-tools,\ |
| 328 |
iproute2,\ |
| 329 |
openssh-server,\ |
| 330 |
apt-transport-https |
| 331 |
|
| 332 |
cache=$1 |
| 333 |
arch=$2 |
| 334 |
release=$3 |
| 335 |
interpreter="$4" |
| 336 |
interpreter_path="$5" |
| 337 |
|
| 338 |
trap cleanup EXIT SIGHUP SIGINT SIGTERM |
| 339 |
|
| 340 |
# Create the cache |
| 341 |
mkdir -p "$cache" |
| 342 |
|
| 343 |
# If devuan-archive-keyring isn't installed, fetch GPG keys directly |
| 344 |
releasekeyring=/usr/share/keyrings/devuan-archive-keyring.gpg |
| 345 |
lreleasekeyring=/etc/apt/trusted.gpg.d/devuan-archive-keyring.gpg |
| 346 |
if [ -f "$releasekeyring" ]; then |
| 347 |
apt_gpg_opt="--keyring=${releasekeyring}" |
| 348 |
elif [ -f "$lreleasekeyring" ]; then |
| 349 |
apt_gpg_opt="--keyring=${lreleasekeyring}" |
| 350 |
elif [ "$DOWNLOAD_KEYRING" = 1 ]; then |
| 351 |
[ ! -d "/etc/apt/trusted.gpg.d" ] && lreleasekeyring="$cache/archive-key.gpg" |
| 352 |
if [[ "$(id -u)" == "0" ]]; then |
| 353 |
wget https://files.devuan.org/devuan-archive-keyring.gpg -O - --quiet \ |
| 354 |
| gpg --import --no-default-keyring --keyring="${lreleasekeyring}" |
| 355 |
apt_gpg_opt="--keyring=${lreleasekeyring}" |
| 356 |
else |
| 357 |
echo "Must be root (sudo) to save $lreleasekeyring" |
| 358 |
fi |
| 359 |
fi |
| 360 |
if [ -z "$apt_gpg_opt" ]; then |
| 361 |
echo "WARNING: No GPG check" |
| 362 |
apt_gpg_opt='--no-check-gpg' |
| 363 |
fi |
| 364 |
# check the mini devuan was not already downloaded |
| 365 |
try_mksubvolume "$cache/partial-$release-$arch" |
| 366 |
if [ $? -ne 0 ]; then |
| 367 |
echo "Failed to create '$cache/partial-$release-$arch' directory" |
| 368 |
return 1 |
| 369 |
fi |
| 370 |
|
| 371 |
# download a mini devuan into a cache |
| 372 |
echo "Downloading devuan minimal ..." |
| 373 |
if [ "$interpreter" = "" ] ; then |
| 374 |
debootstrap --verbose --variant=minbase --arch="$arch" \ |
| 375 |
--include=$packages "${apt_gpg_opt}" \ |
| 376 |
"$release" "$cache/partial-$release-$arch" "$MIRROR" |
| 377 |
if [ $? -ne 0 ]; then |
| 378 |
echo "Failed to download the rootfs, aborting." |
| 379 |
return 1 |
| 380 |
fi |
| 381 |
else |
| 382 |
debootstrap --foreign --verbose --variant=minbase --arch="$arch" \ |
| 383 |
--include=$packages "${apt_gpg_opt}" \ |
| 384 |
"$release" "$cache/partial-$release-$arch" "$MIRROR" |
| 385 |
if [ $? -ne 0 ]; then |
| 386 |
echo "Failed to download the rootfs, aborting." |
| 387 |
return 1 |
| 388 |
fi |
| 389 |
mkdir -p "$(dirname "$cache/partial-$release-$arch/$interpreter_path")" |
| 390 |
cp "$interpreter" "$cache/partial-$release-$arch/$interpreter_path" |
| 391 |
if [ $? -ne 0 ]; then |
| 392 |
echo "failed to copy $interpreter to $cache/partial-$release-$arch/$interpreter_path" |
| 393 |
return 1 |
| 394 |
fi |
| 395 |
chroot "$cache/partial-$release-$arch" debootstrap/debootstrap --second-stage |
| 396 |
if [ $? -ne 0 ]; then |
| 397 |
echo "failed to update the rootfs, aborting" |
| 398 |
return 1 |
| 399 |
fi |
| 400 |
fi |
| 401 |
|
| 402 |
mv "$1/partial-$release-$arch" "$1/rootfs-$release-$arch" |
| 403 |
echo "Download complete." |
| 404 |
trap EXIT |
| 405 |
trap SIGINT |
| 406 |
trap SIGTERM |
| 407 |
trap SIGHUP |
| 408 |
|
| 409 |
return 0 |
| 410 |
} |
| 411 |
|
| 412 |
copy_devuan() |
| 413 |
{ |
| 414 |
cache=$1 |
| 415 |
arch=$2 |
| 416 |
rootfs=$3 |
| 417 |
release=$4 |
| 418 |
|
| 419 |
# make a local copy of the minidevuan |
| 420 |
echo -n "Copying rootfs to $rootfs..." |
| 421 |
try_mksubvolume "$rootfs" |
| 422 |
if which btrfs >/dev/null 2>&1 && \ |
| 423 |
is_btrfs_subvolume "$cache/rootfs-$release-$arch" && \ |
| 424 |
is_btrfs_subvolume "$rootfs"; then |
| 425 |
realrootfs="$(dirname "$config")"/rootfs |
| 426 |
[ "$rootfs" = "$realrootfs" ] || umount "$rootfs" || return 1 |
| 427 |
btrfs subvolume delete "$realrootfs" || return 1 |
| 428 |
btrfs subvolume snapshot "$cache/rootfs-$release-$arch" "$realrootfs" || return 1 |
| 429 |
[ "$rootfs" = "$realrootfs" ] || mount --bind "$realrootfs" "$rootfs" || return 1 |
| 430 |
else |
| 431 |
rsync -SHaAX "$cache/rootfs-$release-$arch"/ $rootfs/ || return 1 |
| 432 |
fi |
| 433 |
return 0 |
| 434 |
} |
| 435 |
|
| 436 |
install_devuan() |
| 437 |
{ |
| 438 |
rootfs=$1 |
| 439 |
release=$2 |
| 440 |
arch=$3 |
| 441 |
cache="$4/devuan" |
| 442 |
interpreter="$5" |
| 443 |
interpreter_path="$6" |
| 444 |
flushcache=$7 |
| 445 |
mkdir -p $LOCALSTATEDIR/lock/subsys/ |
| 446 |
( |
| 447 |
flock -x 9 |
| 448 |
if [ $? -ne 0 ]; then |
| 449 |
echo "Cache repository is busy." |
| 450 |
return 1 |
| 451 |
fi |
| 452 |
|
| 453 |
if [ "$flushcache" -eq 1 ]; then |
| 454 |
echo "Flushing cache..." |
| 455 |
cleanup |
| 456 |
fi |
| 457 |
|
| 458 |
echo "Checking cache download in $cache/rootfs-$release-$arch ... " |
| 459 |
if [ ! -e "$cache/rootfs-$release-$arch" ]; then |
| 460 |
download_devuan "$cache" "$arch" "$release" "$interpreter" "$interpreter_path" |
| 461 |
if [ $? -ne 0 ]; then |
| 462 |
echo "Failed to download 'devuan base'" |
| 463 |
return 1 |
| 464 |
fi |
| 465 |
fi |
| 466 |
|
| 467 |
copy_devuan "$cache" "$arch" "$rootfs" "$release" |
| 468 |
if [ $? -ne 0 ]; then |
| 469 |
echo "Failed to copy rootfs" |
| 470 |
return 1 |
| 471 |
fi |
| 472 |
|
| 473 |
return 0 |
| 474 |
|
| 475 |
) 9>$LOCALSTATEDIR/lock/subsys/lxc-devuan |
| 476 |
|
| 477 |
return $? |
| 478 |
} |
| 479 |
|
| 480 |
copy_configuration() |
| 481 |
{ |
| 482 |
path=$1 |
| 483 |
rootfs=$2 |
| 484 |
hostname=$3 |
| 485 |
arch=$4 |
| 486 |
num_tty=$5 |
| 487 |
|
| 488 |
# Generate the configuration file |
| 489 |
# if there is exactly one veth network entry, make sure it has an |
| 490 |
# associated hwaddr. |
| 491 |
nics=$(grep -ce '^lxc\.net\.0\.type[ \t]*=[ \t]*veth' "$path/config") |
| 492 |
if [ "$nics" -eq 1 ]; then |
| 493 |
grep -q "^lxc.net.0.hwaddr" "$path/config" || sed -i -e "/^lxc\.net\.0\.type[ \t]*=[ \t]*veth/a lxc.net.0.hwaddr = 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')" "$path/config" |
| 494 |
fi |
| 495 |
|
| 496 |
## Add all the includes |
| 497 |
echo "" >> "$path/config" |
| 498 |
echo "# Common configuration" >> "$path/config" |
| 499 |
if [ -e "${LXC_TEMPLATE_CONFIG}/devuan.common.conf" ]; then |
| 500 |
echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/devuan.common.conf" >> "$path/config" |
| 501 |
fi |
| 502 |
if [ -e "${LXC_TEMPLATE_CONFIG}/devuan.${release}.conf" ]; then |
| 503 |
echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/devuan.${release}.conf" >> "$path/config" |
| 504 |
fi |
| 505 |
|
| 506 |
## Add the container-specific config |
| 507 |
echo "" >> "$path/config" |
| 508 |
echo "# Container specific configuration" >> "$path/config" |
| 509 |
grep -q "^lxc.rootfs.path" "$path/config" 2> /dev/null || echo "lxc.rootfs.path = $rootfs" >> "$path/config" |
| 510 |
|
| 511 |
cat <<EOF >> $path/config |
| 512 |
lxc.tty.max = $num_tty |
| 513 |
lxc.uts.name = $hostname |
| 514 |
lxc.arch = $arch |
| 515 |
lxc.pty.max = 1024 |
| 516 |
EOF |
| 517 |
|
| 518 |
if [ $? -ne 0 ]; then |
| 519 |
echo "Failed to add configuration" |
| 520 |
return 1 |
| 521 |
fi |
| 522 |
|
| 523 |
return 0 |
| 524 |
} |
| 525 |
|
| 526 |
post_process() |
| 527 |
{ |
| 528 |
local rootfs="$1"; shift |
| 529 |
local release="$1"; shift |
| 530 |
local arch="$1"; shift |
| 531 |
local hostarch="$1"; shift |
| 532 |
local interpreter="$1"; shift |
| 533 |
local packages="$*" |
| 534 |
|
| 535 |
# Disable service startup |
| 536 |
cat > "${rootfs}/usr/sbin/policy-rc.d" << EOF |
| 537 |
#!/bin/sh |
| 538 |
exit 101 |
| 539 |
EOF |
| 540 |
chmod +x "${rootfs}/usr/sbin/policy-rc.d" |
| 541 |
|
| 542 |
# If the container isn't running a native architecture, setup multiarch |
| 543 |
if [ "$interpreter" = "" -a "${arch}" != "${hostarch}" ]; then |
| 544 |
# Test if dpkg supports multiarch |
| 545 |
if ! chroot "$rootfs" dpkg --print-foreign-architectures 2>&1; then |
| 546 |
chroot "$rootfs" dpkg --add-architecture "${hostarch}" |
| 547 |
fi |
| 548 |
fi |
| 549 |
|
| 550 |
# Write a new sources.list containing both native and multiarch entries |
| 551 |
> "${rootfs}/etc/apt/sources.list" |
| 552 |
if [ "$interpreter" != "" -a "${arch}" = "${hostarch}" ]; then |
| 553 |
write_sourceslist "${rootfs}" "${release}" "${arch}" |
| 554 |
else |
| 555 |
write_sourceslist "${rootfs}" "${release}" |
| 556 |
fi |
| 557 |
|
| 558 |
# Install Packages in container |
| 559 |
if [ -n "${packages}" ]; then |
| 560 |
local pack_list |
| 561 |
pack_list="${packages//,/ }" |
| 562 |
echo "Installing packages: ${pack_list}" |
| 563 |
install_packages "${rootfs}" "${pack_list}" |
| 564 |
fi |
| 565 |
|
| 566 |
# Re-enable service startup |
| 567 |
rm "${rootfs}/usr/sbin/policy-rc.d" |
| 568 |
|
| 569 |
# Fix by Greg Olsen, avoid warning 'stdin not a tty' in some cases |
| 570 |
if [[ -f "${rootfs}/root/.profile" ]]; then |
| 571 |
sed -i -e 's/^mesg n/test -t 0 \&\& mesg n/g' ${rootfs}/root/.profile |
| 572 |
fi |
| 573 |
|
| 574 |
# end |
| 575 |
} |
| 576 |
|
| 577 |
clean() |
| 578 |
{ |
| 579 |
cache=${LXC_CACHE_PATH:-"$LOCALSTATEDIR/cache/lxc/devuan"} |
| 580 |
|
| 581 |
if [ ! -e "$cache" ]; then |
| 582 |
exit 0 |
| 583 |
fi |
| 584 |
|
| 585 |
# lock, so we won't purge while someone is creating a repository |
| 586 |
( |
| 587 |
flock -x 9 |
| 588 |
if [ $? != 0 ]; then |
| 589 |
echo "Cache repository is busy." |
| 590 |
exit 1 |
| 591 |
fi |
| 592 |
|
| 593 |
echo -n "Purging the download cache..." |
| 594 |
rm --preserve-root --one-file-system -rf "$cache" && echo "Done." || exit 1 |
| 595 |
exit 0 |
| 596 |
|
| 597 |
) 9>$LOCALSTATEDIR/lock/subsys/lxc-devuan |
| 598 |
} |
| 599 |
|
| 600 |
usage() |
| 601 |
{ |
| 602 |
cat <<EOF |
| 603 |
Template specific options can be passed to lxc-create after a '--' like this: |
| 604 |
|
| 605 |
lxc-create --name=NAME [-lxc-create-options] -- [-template-options] |
| 606 |
|
| 607 |
Usage: $1 -h|--help -p|--path=<path> [-c|--clean] [-a|--arch=<arch>] [-r|--release=<release>] |
| 608 |
[--mirror=<mirror>] [--security-mirror=<security mirror>] |
| 609 |
[--package=<package_name1,package_name2,...>] |
| 610 |
[-I|--interpreter-path=<interpreter path>] |
| 611 |
[-F | --flush-cache] [-S|--auth-key=<keyfile>] |
| 612 |
|
| 613 |
Options : |
| 614 |
|
| 615 |
-h, --help print this help text |
| 616 |
-p, --path=PATH directory where config and rootfs of this VM will be kept |
| 617 |
-S, --auth-key=KEYFILE SSH public key to inject into the container as the root user. |
| 618 |
-a, --arch=ARCH The container architecture. Can be one of: i686, x86_64, |
| 619 |
amd64, armhf, armel, powerpc. Defaults to host arch. |
| 620 |
-r, --release=RELEASE Devuan release. Can be one of: ascii, beowulf, chimaera, ceres. |
| 621 |
Defaults to current stable. |
| 622 |
--mirror=MIRROR Devuan mirror to use during installation. Overrides the MIRROR |
| 623 |
environment variable (see below). |
| 624 |
--security-mirror=SECURITY_MIRROR |
| 625 |
Devuan mirror to use for security updates. Overrides the |
| 626 |
SECURITY_MIRROR environment variable (see below). |
| 627 |
--packages=PACKAGE_NAME1,PACKAGE_NAME2,... |
| 628 |
List of additional packages to install. Comma separated, without space. |
| 629 |
-c, --clean only clean up the cache and terminate |
| 630 |
--enable-non-free include also Devuan's contrib and non-free repositories. |
| 631 |
-I|--interpreter-path=INTERPRETER-PATH |
| 632 |
Path of the binfmt interpreter to copy to the rootfs |
| 633 |
-F | --flush-cache Flush the devuan release cache |
| 634 |
|
| 635 |
Environment variables: |
| 636 |
|
| 637 |
MIRROR The Devuan package mirror to use. See also the --mirror switch above. |
| 638 |
Defaults to '$MIRROR' |
| 639 |
SECURITY_MIRROR The Devuan package security mirror to use. See also the --security-mirror switch above. |
| 640 |
Defaults to '$SECURITY_MIRROR' |
| 641 |
DOWNLOAD_KEYRING Sets whether to download keyring when missing or ignore keyring checks |
| 642 |
Defaults to 1 |
| 643 |
|
| 644 |
EOF |
| 645 |
return 0 |
| 646 |
} |
| 647 |
|
| 648 |
options=$(getopt -o hp:n:a:r:cI:FS: -l arch:,auth-key:,clean,help,enable-non-free,mirror:,name:,packages:,path:,release:,rootfs:,security-mirror:,interpreter-path:,flush-cache -- "$@") |
| 649 |
if [ $? -ne 0 ]; then |
| 650 |
usage "$(basename "$0")" |
| 651 |
exit 1 |
| 652 |
fi |
| 653 |
eval set -- "$options" |
| 654 |
|
| 655 |
littleendian=$(lscpu | grep '^Byte Order' | grep -q Little && echo yes) |
| 656 |
|
| 657 |
arch=$(uname -m) |
| 658 |
if [ "$arch" = "i686" ]; then |
| 659 |
arch="i386" |
| 660 |
elif [ "$arch" = "x86_64" ]; then |
| 661 |
arch="amd64" |
| 662 |
elif [ "$arch" = "armv7l" ]; then |
| 663 |
arch="armhf" |
| 664 |
elif [ "$arch" = "aarch64" ]; then |
| 665 |
arch="arm64" |
| 666 |
elif [ "$arch" = "ppc" ]; then |
| 667 |
arch="powerpc" |
| 668 |
elif [ "$arch" = "ppc64le" ]; then |
| 669 |
arch="ppc64el" |
| 670 |
elif [ "$arch" = "mips" -a "$littleendian" = "yes" ]; then |
| 671 |
arch="mipsel" |
| 672 |
elif [ "$arch" = "mips64" -a "$littleendian" = "yes" ]; then |
| 673 |
arch="mips64el" |
| 674 |
fi |
| 675 |
hostarch=$arch |
| 676 |
mainonly=1 |
| 677 |
flushcache=0 |
| 678 |
|
| 679 |
while true |
| 680 |
do |
| 681 |
case "$1" in |
| 682 |
-h|--help) usage "$0" && exit 1;; |
| 683 |
--) shift 1; break ;; |
| 684 |
|
| 685 |
-a|--arch) arch=$2; shift 2;; |
| 686 |
-S|--auth-key) authkey=$2; shift 2;; |
| 687 |
-I|--interpreter-path) |
| 688 |
interpreter="$2"; shift 2;; |
| 689 |
-c|--clean) clean=1; shift 1;; |
| 690 |
--enable-non-free) mainonly=0; shift 1;; |
| 691 |
--mirror) MIRROR=$2; shift 2;; |
| 692 |
-n|--name) name=$2; shift 2;; |
| 693 |
--packages) packages=$2; shift 2;; |
| 694 |
-p|--path) path=$2; shift 2;; |
| 695 |
-r|--release) release=$2; shift 2;; |
| 696 |
--rootfs) rootfs=$2; shift 2;; |
| 697 |
--security-mirror) SECURITY_MIRROR=$2; shift 2;; |
| 698 |
-F|--flush-cache) flushcache=1; shift 1;; |
| 699 |
*) break ;; |
| 700 |
esac |
| 701 |
done |
| 702 |
|
| 703 |
if [ ! -z "$clean" -a -z "$path" ]; then |
| 704 |
clean || exit 1 |
| 705 |
exit 0 |
| 706 |
fi |
| 707 |
|
| 708 |
if [ "$arch" = "i686" ]; then |
| 709 |
arch=i386 |
| 710 |
fi |
| 711 |
|
| 712 |
if [ "$arch" = "x86_64" ]; then |
| 713 |
arch=amd64 |
| 714 |
fi |
| 715 |
|
| 716 |
if [ "$interpreter" = "" ] ; then |
| 717 |
if [ $hostarch = "i386" -a $arch = "amd64" ]; then |
| 718 |
echo "can't create $arch container on $hostarch" |
| 719 |
exit 1 |
| 720 |
fi |
| 721 |
|
| 722 |
if [ $hostarch = "armhf" -o $hostarch = "armel" ] && \ |
| 723 |
[ $arch != "armhf" -a $arch != "armel" ]; then |
| 724 |
echo "can't create $arch container on $hostarch" |
| 725 |
exit 1 |
| 726 |
fi |
| 727 |
|
| 728 |
if [ $hostarch = "powerpc" -a $arch != "powerpc" ]; then |
| 729 |
echo "can't create $arch container on $hostarch" |
| 730 |
exit 1 |
| 731 |
fi |
| 732 |
|
| 733 |
if [ $hostarch = "mips" -a $arch != "mips" ] || \ |
| 734 |
[ $hostarch = "mipsel" -a $arch != "mipsel" ] || \ |
| 735 |
[ $hostarch = "mips64" -a $arch != "mips" -a $arch != "mips64" ] || \ |
| 736 |
[ $hostarch = "mips64el" -a $arch != "mipsel" -a $arch != "mips64el" ]; then |
| 737 |
echo "can't create $arch container on $hostarch" |
| 738 |
exit 1 |
| 739 |
fi |
| 740 |
else |
| 741 |
if ! file -b "${interpreter}" |grep -q "statically linked" ; then |
| 742 |
echo "'${interpreter}' must be statically linked" 1>&2 |
| 743 |
exit 1 |
| 744 |
fi |
| 745 |
interpreter_path=$(find_interpreter "$interpreter") |
| 746 |
if [ $? -ne 0 ] ; then |
| 747 |
echo "no binfmt interpreter using $(basename "$interpreter")" 1>&2 |
| 748 |
exit 1 |
| 749 |
fi |
| 750 |
fi |
| 751 |
|
| 752 |
type debootstrap |
| 753 |
if [ $? -ne 0 ]; then |
| 754 |
echo "'debootstrap' command is missing" |
| 755 |
exit 1 |
| 756 |
fi |
| 757 |
|
| 758 |
if [ -z "$path" ]; then |
| 759 |
echo "'path' parameter is required" |
| 760 |
exit 1 |
| 761 |
fi |
| 762 |
|
| 763 |
if [ "$(id -u)" != "0" ]; then |
| 764 |
echo "This script should be run as 'root'" |
| 765 |
exit 1 |
| 766 |
fi |
| 767 |
|
| 768 |
if [ -n "$authkey" ]; then |
| 769 |
if [ ! -f "$authkey" ]; then |
| 770 |
echo "SSH keyfile '$authkey' not found" |
| 771 |
exit 1 |
| 772 |
fi |
| 773 |
# This is mostly to prevent accidental uage of the private key instead |
| 774 |
# of the public key. |
| 775 |
if [ "${authkey: -4}" != ".pub" ]; then |
| 776 |
echo "SSH keyfile '$authkey' does not end with '.pub'" |
| 777 |
exit 1 |
| 778 |
fi |
| 779 |
fi |
| 780 |
|
| 781 |
release=${release:-stable} |
| 782 |
permanent_releases=('stable' 'testing' 'sid' 'unstable') |
| 783 |
if [[ ! "${permanent_releases[*]}" =~ (^|[^[:alpha:]])$release([^[:alpha:]]|$) ]]; then |
| 784 |
if ! release_info "${MIRROR}"; then |
| 785 |
echo "Invalid release ${release} (not found in mirror)" |
| 786 |
exit 1 |
| 787 |
fi |
| 788 |
else |
| 789 |
release_info "${MIRROR}" |
| 790 |
fi |
| 791 |
|
| 792 |
# detect rootfs |
| 793 |
config="$path/config" |
| 794 |
if [ -z "$rootfs" ]; then |
| 795 |
if grep -q '^lxc.rootfs.path' "$config" 2> /dev/null ; then |
| 796 |
rootfs=$(awk -F= '/^lxc.rootfs.path[ \t]+=/{ print $2 }' "$config") |
| 797 |
else |
| 798 |
rootfs=$path/rootfs |
| 799 |
fi |
| 800 |
fi |
| 801 |
|
| 802 |
# determine the number of ttys - default is 4 |
| 803 |
if grep -q '^lxc.tty.max' "$config" 2> /dev/null ; then |
| 804 |
num_tty=$(awk -F= '/^lxc.tty.max[ \t]+=/{ print $2 }' "$config") |
| 805 |
else |
| 806 |
num_tty=4 |
| 807 |
fi |
| 808 |
|
| 809 |
install_devuan "$rootfs" "$release" "$arch" "$LXC_CACHE_PATH" "$interpreter" "$interpreter_path" "$flushcache" |
| 810 |
if [ $? -ne 0 ]; then |
| 811 |
echo "failed to install devuan" |
| 812 |
exit 1 |
| 813 |
fi |
| 814 |
|
| 815 |
configure_devuan "$rootfs" "$name" $num_tty |
| 816 |
if [ $? -ne 0 ]; then |
| 817 |
echo "failed to configure devuan for a container" |
| 818 |
exit 1 |
| 819 |
fi |
| 820 |
|
| 821 |
copy_configuration "$path" "$rootfs" "$name" $arch $num_tty |
| 822 |
if [ $? -ne 0 ]; then |
| 823 |
echo "failed write configuration file" |
| 824 |
exit 1 |
| 825 |
fi |
| 826 |
|
| 827 |
post_process "${rootfs}" "${release}" ${arch} ${hostarch} "${interpreter}" "${packages}" |
| 828 |
|
| 829 |
if [ ! -z "$clean" ]; then |
| 830 |
clean || exit 1 |
| 831 |
exit 0 |
| 832 |
fi |