Skip to content

Instantly share code, notes, and snippets.

@andres-erbsen
Created September 29, 2021 20:46
Show Gist options
  • Save andres-erbsen/8272587218ed1daa3f8bcce3dd6867e8 to your computer and use it in GitHub Desktop.
Save andres-erbsen/8272587218ed1daa3f8bcce3dd6867e8 to your computer and use it in GitHub Desktop.
#!/bin/sh -eux
img="arch-$(date '+%Y-%m-%d').img.qcow2"
mnt="${img}-root"
cleanup() {
umount -lRf "$mnt/var/cache/pacman/pkg" || true
umount -lRf "$mnt/run" || true
umount -lRf "$mnt" || true
umount -Rf "$mnt" || true
qemu-nbd --disconnect /dev/nbd0 || true
rmdir "$mnt" || true
}
qemu-img create -f qcow2 -o extended_l2=on,cluster_size=128k,preallocation=metadata,lazy_refcounts=on "$img" 5G
modprobe nbd
qemu-nbd --aio=io_uring --cache=unsafe --connect=/dev/nbd0 "$img"
trap cleanup EXIT
## disk & boot
sfdisk --no-reread /dev/nbd0 << 'EOF'
,,L,*
EOF
mkfs.ext4 /dev/nbd0p1
rmdir "$mnt" || true
mkdir "$mnt"
mount /dev/nbd0p1 "$mnt"
mkdir "$mnt/etc"
cat > "$mnt/etc/mkinitcpio.conf" <<'EOF'
MODULES="virtio virtio_blk virtio_pci virtio_net"
HOOKS="base udev autodetect modconf block filesystems keyboard fsck"
EOF
pacstrap -c "$mnt" base linux-lts mkinitcpio openssh syslinux
mount --bind /proc "$mnt/proc"
mount --bind /sys "$mnt/sys"
mount --bind /dev "$mnt/dev"
mount --bind /dev/pts "$mnt/dev/pts"
mount --bind /dev/shm "$mnt/dev/shm"
mount --bind /run "$mnt/run"
mount --bind /var/cache/pacman/pkg "$mnt/var/cache/pacman/pkg"
dd count=440 count=1 conv=notrunc if=/usr/lib/syslinux/bios/mbr.bin of=/dev/nbd0
chroot "$mnt" extlinux --install /boot
root_partuuid="$(chroot "$mnt" findmnt -f / -n -o PARTUUID)"
cat > "$mnt"/boot/extlinux.conf << EOF
default archlinux
label archlinux
linux vmlinuz-linux-lts
initrd initramfs-linux-lts.img
append root=PARTUUID=${root_partuuid} rw console=ttyS0 quiet
EOF
genfstab -t PARTUUID "$mnt" >> "$mnt/etc/fstab"
## network
cat >"$mnt/etc/systemd/network/25-dhcp.network" <<'EOF'
[Match]
Name=*
[Network]
DHCP=yes
EOF
chroot "$mnt" systemctl enable systemd-networkd
cat > "$mnt/etc/resolv.conf" << 'EOF'
nameserver 1.1.1.1
nameserver 1.0.0.1
EOF
cat > "$mnt/etc/hosts" << 'EOF'
127.0.0.1 localhost.localdomain localhost
::1 localhost.localdomain localhost
EOF
cat > "$mnt/etc/pacman.d/mirrorlist" <<'EOF'
Server = https://mirror.rackspace.com/archlinux/$repo/os/$arch
EOF
sed -e 's/#\(IgnorePkg\s*=\)/\1 linux linux-api-headers linux-firmware util-linux libutil-linux mkinitcpio/' -i "$mnt/etc/ssh/sshd_config"
chroot "$mnt" passwd -d root
sed -e 's/#PermitEmptyPasswords no/PermitEmptyPasswords yes/' -i "$mnt/etc/ssh/sshd_config"
sed -e 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' -i "$mnt/etc/ssh/sshd_config"
chroot "$mnt" systemctl enable sshd
## environment
sed -e 's/#en_US.UTF-8/en_US.UTF-8/' -i "$mnt/etc/locale.gen"
chroot "$mnt" locale-gen
printf "LANG=en_US.UTF-8\n" > "$mnt/etc/locale.conf"
## customization
chroot "$mnt" /bin/sh -eux << 'EOF'
pacman --noconfirm -S sudo base-devel coq python vim emacs-nox git
groupadd sudo
useradd -mG sudo artifact
passwd -d artifact
printf "%s\n" '%sudo ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers
EOF
chroot "$mnt" /bin/su -s /bin/sh artifact -- -eux << 'EOF'
cd
git clone --depth=1 https://github.com/let-def/vimbufsync.git ~/.vim/pack/coq/start/vimbufsync
git clone --depth=1 https://github.com/whonore/coqtail.git ~/.vim/pack/coq/start/coqtail
git clone --depth=1 https://github.com/ProofGeneral/PG ~/.emacs.d/lisp/PG
cat > ~/.emacs << 'eof'
;; Open .v files with Proof General's Coq mode
(load "~/.emacs.d/lisp/PG/generic/proof-site")
(setq proof-splash-enable nil)
eof
make -C ~/.emacs.d/lisp/PG
EOF
sync && cleanup && trap - EXIT
printf "\nPotential usage:\nqemu-system-x86_64 -enable-kvm -cpu host -smp cpus=2 -m 4096 -display none -device virtio-balloon -device virtio-rng-pci -drive file=%s,media=disk,if=virtio -nic user,model=virtio-net-pci,hostfwd=tcp:127.0.0.1:10022-:22 -serial stdio\n" "$img"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment