Skip to content

Instantly share code, notes, and snippets.

@artizirk
Last active April 23, 2020 11:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artizirk/fab2ce13277a190ee6063b03b8e0a6e9 to your computer and use it in GitHub Desktop.
Save artizirk/fab2ce13277a190ee6063b03b8e0a6e9 to your computer and use it in GitHub Desktop.
#!/usr/bin/bash -e
BASE="/var/lib/machines"
LANG="C.UTF-8"
PKGS=(
filesystem
bash
bash-completion
glibc
iproute2
bzip2
gzip
coreutils
diffutils
file
findutils
gawk
gcc-libs
gettext
inetutils
iputils
less
logrotate
nano
pacman
procps-ng
psmisc
s-nail
sed
shadow
sysfsutils
systemd-sysvcompat
tar
util-linux
vi
which
openssh
grep
)
PKGS=${PKGS[*]}
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
function deleteContainer {
btrfs subvolume delete $BASE/$name/var/lib/machines
btrfs subvolume delete $BASE/$name
exit;
}
while getopts 'hn:d' flag; do
case "${flag}" in
h) echo "-n container name; -h help";;
n) name="${OPTARG}" ;;
d) deleteContainer ;;
*) echo "Unexpected option ${flag}" ;;
esac
done
if [ -z ${name+x} ]; then
echo "container name is unset"
exit;
else
echo "Container name is $name"
fi
function mustRun {
"$@"
local status=$?
if [ $status -ne 0 ]; then
echo "error with $1" >&2
exit;
fi
return $status
}
mustRun btrfs subvolume create "$BASE/$name"
mustRun pacstrap -i -c -d "$BASE/$name" --noconfirm $PKGS
if [ ! -d "$BASE/$name/root/.ssh" ]; then
mkdir "$BASE/$name/root/.ssh"
chmod 700 "$BASE/$name/root/.ssh"
if [ ! -f "$BASE/$name/root/.ssh/authorized_keys" ]; then
curl https://github.com/artizirk.keys >> "$BASE/$name/root/.ssh/authorized_keys"
chmod 600 "$BASE/$name/root/.ssh/authorized_keys"
echo "added ssh keys to root"
fi
else
echo "ssh keys probably already added"
fi
if [[ ! -L "$BASE/$name/etc/resolv.conf" ]]; then
rm "$BASE/$name/etc/resolv.conf"
ln -s "/run/systemd/resolve/resolv.conf" "$BASE/$name/etc/resolv.conf"
fi
sed --in-place '/auth required pam_securetty.so/d' "$BASE/$name/etc/pam.d/login"
mustRun arch-chroot "$BASE/$name" --pipe /bin/bash <<'EOF'
echo "Now running inside arch-chroot $(pwd)"
sed -i '/en_US.UTF-8 UTF-8/s/^#//g' /etc/locale.gen
echo LANG=en_US.UTF-8 > /etc/locale.conf
locale-gen
ln -s /usr/share/zoneinfo/Europe/Tallinn /etc/localtime
systemctl enable systemd-networkd
systemctl enable systemd-resolved
systemctl enable sshd
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment