Skip to content

Instantly share code, notes, and snippets.

@digitalsignalperson
Created September 12, 2023 00:32
Show Gist options
  • Save digitalsignalperson/62b6f70094738dcdaaeedfc995599045 to your computer and use it in GitHub Desktop.
Save digitalsignalperson/62b6f70094738dcdaaeedfc995599045 to your computer and use it in GitHub Desktop.
arch linux LXD stuff
_source() {
source ${BASE_DIR}/lxd/lxd-common
}
#======================
# General VM functions
vm_bash() {
lxc exec "$VMNAME" -- bash "$@"
}
vm_exec() {
lxc exec "$VMNAME" -- "$@"
}
vm_start() {
lxc start "$VMNAME"
}
vm_start_console() {
lxc start "$VMNAME" --console=vga
}
vm_console() {
lxc console "$VMNAME" --type vga
}
vm_wait() {
sleep 2
until vm_exec true
do
sleep 1
done
sleep 2
}
vm_status() {
lxc info "$VMNAME" | grep "^Status: " | awk '{ print $2 }'
}
vm_stop() {
timeout 5 lxc stop "$VMNAME" || \
sleep 1 && lxc stop -f "$VMNAME" || true
vm_status | grep -q "STOPPED"
}
vm_restart() {
#timeout 5 lxc restart "$VMNAME" || \
# sleep 1 && lxc restart -f "$VMNAME"
vm_stop
vm_start
}
vm_delete() {
lxc delete -f "$VMNAME"
}
vm_create() {
lxc init images:archlinux "$VMNAME" --vm \
-c security.secureboot=false \
-c limits.memory=16GiB \
-c limits.cpu=$(nproc)
}
#==============================
# Project specific: Attaching storage, installing dependencies and zfs
vm_create_storage() {
# lxc storage volume create default disk-arch-gen-work size=10GiB --type=block
# lxc storage volume create default disk-arch-gen-usb size=4GiB --type=block
# Names can't be this long https://github.com/canonical/lxd/issues/12152
lxc storage volume create default agwork size=20GiB --type=block
lxc storage volume create default agusb size=4GiB --type=block
}
vm_attach_storage() {
lxc config device add "$VMNAME" disk-repo disk "source=${BASE_DIR}" "path=${VM_BASE_DIR}" readonly=true
lxc config device add "$VMNAME" disk-repo-work disk "source=${BASE_DIR}/work" "path=${VM_BASE_DIR}/work" readonly=false
lxc storage volume attach default agwork "$VMNAME"
lxc storage volume attach default agusb "$VMNAME"
}
vm_detach_storage() {
lxc config device remove "$VMNAME" disk-repo
lxc config device remove "$VMNAME" disk-repo-work
lxc storage volume detach default agwork "$VMNAME"
lxc storage volume detach default agusb "$VMNAME"
}
vm_destroy_storage() {
lxc storage volume delete default agwork
lxc storage volume delete default agusb
}
vm_detect_storage() {
# lxc exec "$VMNAME" -- ls /dev/disk/by-id
disk_work="$(vm_exec find /dev/disk/by-id | grep "_agwork" | grep -vF -- "-part")"
disk_usb="$(vm_exec find /dev/disk/by-id | grep "_agusb" | grep -vF -- "-part")"
echo "$disk_work"
echo "$disk_usb"
}
vm_update() {
lxc file push /etc/pacman.d/mirrorlist "${VMNAME}/etc/pacman.d/mirrorlist"
vm_exec sed -i '/^#*ParallelDownloads.*/c\ParallelDownloads = 5' /etc/pacman.conf
vm_bash << 'EOF'
pacman -R --noconfirm linux
pacman -Syu --noconfirm --needed linux-lts linux-lts-headers # For ZFS
pacman -Syu --noconfirm --needed gptfdisk parted dosfstools # For disk management
pacman -S --noconfirm --needed arch-install-scripts # for arch-chroot
pacman -S --noconfirm --needed rsync
pacman -S --noconfirm --needed git
grub-mkconfig -o /boot/grub/grub.cfg
EOF
vm_restart
vm_wait
}
vm_install_zfs() {
vm_bash << 'EOF'
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
rm -rf /root/zfs.tar
dkms mktarball zfs/$(ls /var/lib/dkms/zfs | grep -v kernel) --archive /root/zfs.tar
modprobe zfs
EOF
}
@digitalsignalperson
Copy link
Author

Setup VM

vm_stop
vm_delete
vm_create
vm_attach_storage
vm_start
vm_wait
vm_detect_storage
vm_update
vm_install_zfs

@digitalsignalperson
Copy link
Author

vm_exec cat "$disk_work" | pv > ~/disk.img

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