#!/bin/bash exit 0 SRC_TAR="/mnt/ssd/meran-1.tar.zst" DST_NVME="nvme-MT006400KWSTD_SERIALxxxxxxxxxx" DST_NVME_PATH="/dev/disk/by-id/$DST_NVME" DST_EFI_PART="${DST_NVME_PATH}-part1" DST_ZFS_PART="${DST_NVME_PATH}-part2" # Cleanup umount -n -R /mnt/nvme || true zpool export zpool1 || true # Wipe all partitions zpool labelclear -f "$DST_NVME_PATH" wipefs -a "$DST_NVME_PATH" blkdiscard -v "$DST_NVME_PATH" # Partitioning sgdisk -Zog "$DST_NVME_PATH" sgdisk -n1::+512M -t1:EF00 -c1:"EFI System Partition" "$DST_NVME_PATH" sgdisk -n2 -t2:BF00 -c2:"ZFS" "$DST_NVME_PATH" sgdisk -p "$DST_NVME_PATH" # Wait for both partitions to show up while :; do if [ -r "$DST_EFI_PART" ] && [ -r "$DST_ZFS_PART" ]; then break fi sleep 0.1 done # Create EFI filesystem mkfs.vfat -v -n EFI "$DST_EFI_PART" # Create ZFS pool zpool create \ -f \ -m none \ -o autotrim=on \ -O normalization=formD \ -O compression=lz4 \ -O dnodesize=auto \ -O acltype=posix \ -O xattr=sa \ -O mountpoint=none \ -O canmount=off \ zpool1 "$DST_ZFS_PART" # Create ZFS filesystems zfs create zpool1/ROOT zfs create -o mountpoint=/ -o canmount=noauto zpool1/ROOT/arch # Set kernel boot parameters zpool set bootfs=zpool1/ROOT/arch zpool1 zfs set org.zfsbootmenu:commandline="rw delayacct audit=0 init_on_alloc=0 init_on_free=0 kvm-intel.nested=1 page_alloc.shuffle=1 intel_iommu=igfx_off iommu=pt msr.allow_writes=on" zpool1/ROOT zfs set org.zfsbootmenu:kernel="vmlinuz-linux-custom" zpool1/ROOT # Remount zpool export zpool1 zpool import -N -R /mnt/nvme zpool1 zfs mount zpool1/ROOT/arch || exit 1 # Uncompress backup pv -s $(du -sb "$SRC_TAR" | awk '{print $1}') < "$SRC_TAR" | \ zstdmt -dcf | \ tar --acls --xattrs --xattrs-include='*' -C /mnt/nvme/ -xpf- # Rewrite fstab EFI_PARTUUID=$(blkid | awk '/nvme.*EFI System Partition/ { a=$NF; gsub(/"/,"",a); print a }') cat </mnt/nvme/etc/fstab # Static information about the filesystems. # See fstab(5) for details. # /dev/sr0 /mnt/cdrom auto ro,user,noauto,unhide 0 0 $EFI_PARTUUID /boot/efi vfat rw,noauto,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro 0 2 EOF # Create hostid if it does not exist if ! [ -r /mnt/nvme/etc/hostid ]; then zgenhostid -o /mnt/nvme/etc/hostid fi # Get ZFS hostid HOSTID=$(od -An -tx4 /mnt/nvme/etc/hostid | tr -d " ") # ESP setup mkdir -p /mnt/nvme/boot/efi mount "$DST_EFI_PART" /mnt/nvme/boot/efi mkdir -p /mnt/nvme/boot/efi/EFI/BOOT ./zbm-kcl -r zbm.timeout -a zbm.timeout=5 -r spl.spl_hostid -a spl.spl_hostid=0x"$HOSTID" -o /mnt/nvme/boot/efi/EFI/BOOT/BOOTX64.EFI zfsbootmenu-recovery-x86_64-v3.0.1-linux6.12.EFI ./zbm-kcl /mnt/nvme/boot/efi/EFI/BOOT/BOOTX64.EFI # Refresh zpool.cache rm -f /etc/zfs/zpool.cache zpool set cachefile=/etc/zfs/zpool.cache zpool1 cp -fav /etc/zfs/zpool.cache /mnt/nvme/etc/zfs/ arch-chroot -r /mnt/nvme mkinitcpio -P