Skip to content

Instantly share code, notes, and snippets.

@digitalsignalperson
Created February 5, 2024 23:22
Show Gist options
  • Save digitalsignalperson/df10ea15752bdf63e319c9125a8a5b0a to your computer and use it in GitHub Desktop.
Save digitalsignalperson/df10ea15752bdf63e319c9125a8a5b0a to your computer and use it in GitHub Desktop.
incus ZFS test VM for arch linux
#!/bin/bash
VMNAME=arch-zfs-test
vm_bash() { incus exec $VMNAME -- bash "$@"; }
vm_exec() { incus exec $VMNAME -- "$@"; }
vm_wait() { until vm_exec true; do sleep 1; done; }
vm_start() { incus start $VMNAME; }
vm_console() { incus console $VMNAME --type vga; }
vm_status() { incus info $VMNAME | grep "^Status: " | awk '{ print $2 }'; }
vm_stop() { timeout 5 incus stop $VMNAME || echo "Using -f" && incus stop -f $VMNAME || true; }
vm_restart() { vm_stop; vm_start; }
vm_delete() { incus delete -f $VMNAME; }
incus init images:archlinux $VMNAME --vm \
-c security.secureboot=false \
-c limits.memory=16GiB \
-c limits.cpu=$(nproc) \
-c boot.autostart=false
incus start $VMNAME
vm_wait
incus file push /etc/pacman.d/mirrorlist "$VMNAME/etc/pacman.d/mirrorlist"
sleep 1
_exec() ( set -e
echo "MAKEFLAGS=\"-j$(nproc)\"" >> /etc/makepkg.conf
sed -i '/^#*ParallelDownloads.*/c\ParallelDownloads = 5' /etc/pacman.conf
pacman -Syu --noconfirm
pacman -R --noconfirm linux
pacman -S --noconfirm --needed base-devel git linux-lts linux-lts-headers
grub-mkconfig -o /boot/grub/grub.cfg
)
vm_bash -c "$(declare -f _exec); _exec"
vm_restart
vm_wait
## install zfs
_exec() ( set -e
if ! grep -qF "[archzfs]" /etc/pacman.conf; then
cat >> /etc/pacman.conf << 'FOE'
[archzfs]
Server = http://archzfs.com/$repo/x86_64
Server = http://mirror.sum7.eu/archlinux/archzfs/$repo/x86_64
Server = https://mirror.biocrafting.net/archlinux/archzfs/$repo/x86_64
FOE
fi
pacman-key -r F75D9D76
pacman-key --lsign-key F75D9D76
pacman -Syyu --noconfirm
pacman -S --noconfirm zfs-dkms # zfs-utils installed above
)
vm_bash -c "$(declare -f _exec); _exec"
vm_exec modprobe zfs
vm_bash # interactive shell
@digitalsignalperson
Copy link
Author

I am using

_exec() (
# code here
)
vm_bash -c "$(declare -f _exec); _exec"

which is nicer to have proper code highlighting etc in the editor than with heredocs like

vm_bash << EOF
# code here
EOF

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment