Skip to content

Instantly share code, notes, and snippets.

@MakiseKurisu
Last active December 3, 2024 19:31
Show Gist options
  • Save MakiseKurisu/1b46df3374c1dcbab1048b23312a4e0e to your computer and use it in GitHub Desktop.
Save MakiseKurisu/1b46df3374c1dcbab1048b23312a4e0e to your computer and use it in GitHub Desktop.
Convert Proxmox VE over Btrfs to RAID-1 configuration
# Please follow https://gist.github.com/MakiseKurisu/3a44918bccf3383c0b5ccf362f429c8b first to set up rootfs
# Assuming rootfs on /dev/sdb2, and adding /dev/sda to the RAID-1 configuration
# Remove existing Proxmox installation and LVM Thin storage
pvesm remove local-lvm
vgremove pve -y
# Set up partition table
(echo g; echo n; echo ''; echo ''; echo '+512M'; echo t; echo 1; echo n; echo ''; echo ''; echo ''; echo w) | fdisk /dev/sda
# Add /dev/sda2 to current root
btrfs device add -f /dev/sda2 /
# Rebalance to RAID-1
# The first step could fail with message "ERROR: error during balancing '/': No space left on device".
# However, the profile will be changed regardless. Thus a proper rebalance is done after the first step.
# You can use "btrfs fi usage /" to check the rebalance result.
btrfs balance start -dconvert=raid1 -mconvert=raid1 /
btrfs balance start -dusage=100 /
btrfs balance start -musage=100 /
btrfs balance start -dusage=100 -musage=100 /
# Create scrub services
cat > /etc/systemd/system/[email protected] << EOF
[Unit]
Description=Btrfs scrub on %f
[Service]
Nice=19
IOSchedulingClass=idle
KillSignal=SIGINT
ExecStart=/usr/bin/btrfs scrub start -B %f
EOF
cat > /etc/systemd/system/[email protected] << EOF
[Unit]
Description=Monthly Btrfs scrub on %f
[Timer]
OnCalendar=monthly
AccuracySec=1d
RandomizedDelaySec=1w
Persistent=true
[Install]
WantedBy=multi-user.target
EOF
systemctl enable [email protected]
# Update boot config
sed -i "s/btrfs defaults /btrfs defaults,degraded,noatime,nodiratime /g" /etc/fstab
sed -i 's/GRUB_CMDLINE_LINUX=""/GRUB_CMDLINE_LINUX="rootflags=degraded"/g' /etc/default/grub
# Install MDADM for ESP RAID-1
apt install mdadm
# Create ESP RAID-1
umount /boot/efi
mdadm --create --level=1 --metadata=1.0 --raid-devices=2 /dev/md0 /dev/sd[ab]1 --run
mkfs.fat -F32 /dev/md0
mount /dev/md0 /boot/efi
# Update configs
sed -i "s/^UUID=.* \/boot\/efi vfat defaults 0 1/`blkid /dev/md0 | perl -wlne 'print "UUID=$1 \\\/boot\\\/efi vfat defaults,noauto 0 1" if /UUID="([-\w]*)"\s\w/'`/g" /etc/fstab
blkid /dev/sda1 | perl -wlne 'print "ARRAY <ignore> metadata=1.0 UUID=$1" if /UUID="([-\w]*)"\s\w/' >> /etc/mdadm/mdadm.conf
update-initramfs -u
# Create resync service
cat > /etc/systemd/system/mdadm-efi.service << EOF
[Unit]
Description=Resync /boot/efi RAID
DefaultDependencies=no
After=local-fs.target
[Service]
Type=oneshot
ExecStart=/sbin/mdadm -A /dev/md0 --uuid=`blkid /dev/sda1 | perl -wlne 'print "$1" if /UUID="([-\w]*)"\s\w/'` --update=resync
ExecStart=/bin/mount /boot/efi
RemainAfterExit=yes
[Install]
WantedBy=sysinit.target
EOF
systemctl enable mdadm-efi
# Fix boot
# When boot partition is MD RAID, grub will fail to run efibootmgr for adding boot entry.
# To allow normal booting, copy bootloader to the default location so BIOS can pick it up automatically.
grub-install /dev/sda
grub-install /dev/sdb
mkdir /boot/efi/EFI/BOOT
cp /boot/efi/EFI/proxmox/grubx64.efi /boot/efi/EFI/BOOT/BOOTX64.EFI
update-grub
# Finish
reboot
# If you need to recover a failed system, please check
# https://gist.github.com/MakiseKurisu/ce76ec78c5672bb493b81c30598c7cc9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment