Created
October 8, 2017 20:34
-
-
Save jirutka/3d9c04bd417a057cf7e96a01959e8030 to your computer and use it in GitHub Desktop.
This is a demo script used at Alpine Linux booth at LinuxDays 2017 to demonstrate how is apk-tools and Alpine Linux awesome. Recorded output: https://asciinema.org/a/141360
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# vim: set ts=4: | |
# | |
# Any copyright is dedicated to the Public Domain. | |
# http://creativecommons.org/publicdomain/zero/1.0/ | |
# | |
set -eu | |
APK_KEY_URI='https://alpinelinux.org/keys/[email protected]' | |
APK_KEY_SHA1='3af08548ef78cfdedcf349880c2c6a1a48763a0e' | |
APK_TOOLS_URI='https://github.com/alpinelinux/apk-tools/releases/download/v2.7.2/apk-tools-2.7.2-x86_64-linux.tar.gz' | |
APK_TOOLS_SHA1='f77512787d94e223414c46749b0aaefc15e2914f' | |
ALPINE_REPOS='https://cz.alpinelinux.org/alpine/v3.6/main | |
https://cz.alpinelinux.org/alpine/v3.6/community' | |
ESC_RESET='\033[0m' | |
ESC_BOLD='\033[1m' | |
ESC_DIM='\033[2m' | |
ESC_RED='\033[31m' | |
ESC_GREEN='\033[32m' | |
ESC_YELLOW='\033[33m' | |
ESC_BLUE='\033[34m' | |
ESC_MANGENTA='\033[35m' | |
ESC_CYAN='\033[36m' | |
ESC_WHITE='\033[37m' | |
TYPING_SPEED=30 | |
PROMPT="$ESC_RED$(hostname) #$ESC_RESET" | |
# Unmounts all filesystem under the specified directory tree. | |
umount_recursively() { | |
local mount_point="$1" | |
test -n "$mount_point" || return 1 | |
cat /proc/mounts \ | |
| cut -d ' ' -f 2 \ | |
| grep "^$mount_point" \ | |
| sort -r \ | |
| xargs -r umount -rn | |
} | |
typing() { | |
if [ $# -eq 0 ]; then | |
cat | pv -qL $TYPING_SPEED | |
else | |
printf '%s\n' "$@" | pv -qL $TYPING_SPEED | |
fi | |
} | |
hr() { | |
printf '~%.0s' $(seq 1 80) | |
printf '\n' | |
} | |
blank() { | |
local count=${1:-1} | |
printf '\n%.0s' $(seq 1 $count) | |
} | |
say() { | |
printf "$ESC_BOLD$ESC_YELLOW" | |
hr | |
typing "$@" | |
hr | |
printf "$ESC_RESET\n" | |
} | |
run() { | |
printf "$PROMPT ${ESC_BOLD}" | |
typing "$*" | |
printf "$ESC_RESET" | |
if [ $# -eq 1 ]; then | |
eval "$@" | |
else | |
"$@" | |
fi | |
} | |
#------------------------------ M a i n ------------------------------ | |
temp_dir=$(mktemp -d) | |
cleanup() { | |
set +e | |
echo 'Cleaning up...' >&2 | |
umount_recursively "$temp_dir" | |
rm -Rf "$temp_dir" | |
} | |
trap cleanup EXIT HUP INT TERM | |
cd "$temp_dir" | |
say <<-EOF | |
It's extremely easy to create a rootfs with Alpine Linux, i.e. install | |
Alpine inside chroot, container or create a custom VM image. | |
All you need is just apk-tools (Alpine's package manager) which you can | |
download also as a statically linked binary. | |
I will show you how... | |
>> Please note that this is NOT a video, it's a script running in a loop. << | |
EOF | |
blank | |
sleep 2 | |
say <<-EOF | |
Okay, first we download statically linked apk-tools... | |
EOF | |
run wget -T 3 -q "$APK_TOOLS_URI" | |
run "echo '$APK_TOOLS_SHA1 ${APK_TOOLS_URI##*/}' | sha1sum -c || exit 1" | |
run tar --strip-components 1 -xzf "${APK_TOOLS_URI##*/}" | |
blank | |
say 'And GPG key to verify packages...' | |
run wget -q "$APK_KEY_URI" | |
run 'echo "$APK_KEY_SHA1 ${APK_KEY_URI##*/}" | sha1sum -c || exit 1' | |
blank | |
run mkdir -p rootfs/etc/apk/keys | |
run cp "${APK_KEY_URI##*/}" rootfs/etc/apk/keys/ | |
run 'echo "$ALPINE_REPOS" > rootfs/etc/apk/repositories' | |
blank | |
sleep 1 | |
say 'Now we install base system into the specified directory' | |
run time ./apk --root rootfs --update-cache --initdb add alpine-base | |
blank | |
say <<-EOF | |
And that's it! We have installed a fully working base Alpine system from | |
packages (no prepared image) just with apk-tools. This was very fast, | |
wasn't? ;) Even when it fetches packages from Internet through WiFi... | |
And it's really very small... | |
EOF | |
sleep 2 | |
run cd rootfs | |
run du -d 1 -hx . | |
run 'find . -xdev -type f | wc -l' | |
blank | |
say <<-EOF | |
Let's try to actually chroot into it... | |
EOF | |
run cp /etc/resolv.conf etc/ | |
run mount -v -t proc none proc | |
run mount -v --rbind /sys sys | |
run mount -v --rbind /dev dev | |
blank | |
run chroot . ps aux | |
blank | |
say <<-EOF | |
And install some packages... | |
EOF | |
run chroot . apk add nginx neovim | |
blank | |
say <<-EOF | |
You may ask how is this useful. Well, for example, you can trivially | |
install Alpine on Travis CI, even with emulation of different architectures | |
and build or test software. | |
There's a script made specially for that: | |
https://github.com/alpinelinux/alpine-chroot-install/ | |
EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment