Skip to content

Instantly share code, notes, and snippets.

@sfan5 sfan5/alpine-container.sh
Last active Apr 12, 2019

Embed
What would you like to do?
systemd-nspawn containers with various Linux distributions: Alpine, Arch Linux, Ubuntu
#!/bin/bash -e
# Creates a systemd-nspawn container with Alpine
MIRROR=http://nl.alpinelinux.org/alpine
ARCH=x86_64
VERSION=v3.9
APKTOOLS_VERSION=2.10.3-r1
if [ $UID -ne 0 ]; then
echo "run this script as root" >&2
exit 1
fi
if [ -z "$1" ]; then
echo "Usage: $0 <destination>" >&2
exit 0
fi
dest="$1"
apkdir=$(mktemp -d)
guestarch=x86
[ "$(uname -m)" == x86_64 ] && guestarch=x86_64
wget -qO- $MIRROR/latest-stable/main/x86/apk-tools-static-$APKTOOLS_VERSION.apk \
| tar -xz -C $apkdir || \
{ echo "Couldn't download apk-tools, the version might have changed..."; exit 1; }
$apkdir/sbin/apk.static \
-X $MIRROR/$VERSION/main -U --arch $guestarch \
--allow-untrusted --root "$dest" \
--initdb add alpine-base
mkdir -p "$dest"/{etc/apk,root}
printf '%s/%s/main\n' $MIRROR $VERSION >"$dest"/etc/apk/repositories
sed '/tty[0-9]:/ s/^/#/' -i "$dest"/etc/inittab
printf 'console::respawn:/sbin/getty 38400 console\n' >>"$dest"/etc/inittab
for s in hostname bootmisc syslog; do
ln -s /etc/init.d/$s "$dest"/etc/runlevels/boot/$s
done
for s in killprocs savecache; do
ln -s /etc/init.d/$s "$dest"/etc/runlevels/shutdown/$s
done
rm -r $apkdir
echo ""
echo "Alpine $VERSION container was created successfully."
#!/bin/bash -e
# Creates a systemd-nspawn container with Arch Linux
MIRROR=http://mirror.rackspace.com/archlinux
ISO_DATE=2019.02.01
PACKAGES="base"
if [ $UID -ne 0 ]; then
echo "run this script as root" >&2
exit 1
fi
if [ -z "$1" ]; then
echo "Usage: $0 <destination>" >&2
exit 0
fi
dest="$1"
tarfile=$(mktemp)
wget $MIRROR/iso/$ISO_DATE/archlinux-bootstrap-$ISO_DATE-x86_64.tar.gz -O $tarfile
mkdir "$dest"
tar -xzf $tarfile -C "$dest" --strip-components=1
rm "$dest"/etc/resolv.conf "$dest"/etc/securetty
printf 'Server = %s/$repo/os/$arch\n' $MIRROR >"$dest"/etc/pacman.d/mirrorlist
systemd-nspawn -q -D "$dest" sh <<SCRIPT
pacman-key --init
pacman-key --populate archlinux
pacman -Sy
# avoid installing the kernel
pkgs=\$(pacman -Sg $PACKAGES | while read _ p;do [[ "\$p" == "linux"* ]]||echo -n "\$p ";done)
pacman -S --noconfirm --needed \$pkgs
SCRIPT
rm $tarfile
echo ""
echo "Arch $ISO_DATE container was created successfully."
#!/bin/bash -e
# Creates a systemd-nspawn container with Ubuntu
CODENAME=bionic
if [ $UID -ne 0 ]; then
echo "run this script as root" >&2
exit 1
fi
if [ -z "$1" ]; then
echo "Usage: $0 <destination>" >&2
exit 0
fi
dest="$1"
rootfs=$(mktemp)
wget "http://cloud-images.ubuntu.com/${CODENAME}/current/${CODENAME}-server-cloudimg-amd64-root.tar.xz" -O $rootfs
mkdir -p "$dest"
tar -xaf $rootfs -C "$dest"
sed '/^root:/ s|\*||' -i "$dest/etc/shadow"
rm "$dest/etc/resolv.conf" "$dest/etc/securetty"
disable="ebtables rsync systemd-timesyncd snapd snapd.seeded"
disable="$disable networkd-dispatcher systemd-networkd systemd-networkd-wait-online systemd-resolved"
for s in $disable; do
rm -f "$dest/etc/systemd/system/"*.target.wants"/$s.service" "$dest"/etc/rc[S5].d/S??"$s"
done
# ssh and iscsi cause startup to hang
systemd-nspawn -q -D "$dest" apt-get -qq purge -y openssh-server open-iscsi
rm $rootfs
echo ""
echo "Ubuntu $CODENAME container was created successfully."
@smekkley

This comment has been minimized.

Copy link

smekkley commented Oct 21, 2017

Awesome, thank you for sharing the script.
It's a little bit ugly but I change to use edge instead.

VERSION=edge
APKTOOLS_VERSION=$(curl -s $MIRROR/edge/main/$ARCH/ | awk -F'=' 'match($0, /href="apk-tools-static-(.*).apk">/, a) {print a[1]}')
@mikhailnov

This comment has been minimized.

Copy link

mikhailnov commented Apr 3, 2019

Thank you, very useful scripts, saved a lot of time.

@mikhailnov

This comment has been minimized.

Copy link

mikhailnov commented Apr 3, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.