#!/bin/bash
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
#
# This script should format the given disk for ZFS partition
#
# Taken from https://github.com/proxmox/pve-installer/blob/master/proxinstall
#
# Legacy Layout
# 1 - GRUB boot partition: 1M
# 2 - OS/Data partition
# 9 - ZFS reserved partition
#
# EFI Layout
# 1 - GRUB boot partition: 1M
# 2 - EFI ESP: 128M
# 3 - OS/Data partition
TARGET_DEV="$1"
if [ ! -b "$TARGET_DEV" -o -z "$TARGET_DEV" ]; then
echo "Usage:"
echo " zfs-format-disk "
echo " zfs-format-disk /dev/sda"
exit 1
fi
# Since Proxmox v6, EFI layout might be in used even if efi is not used for booting.
# For this reason, we need to check if EFI layout is in used for other disk.
EFI=0
if fdisk -l | grep 'EFI System' > /dev/null; then
EFI=1
fi
CMD="/sbin/sgdisk -a1"
if [ $EFI -eq 1 ]; then
# GRUB boot partition
CMD="$CMD -n1:34:2047 -t1:EF02"
# EFI Boot
CMD="$CMD -n2:2048:1050623 -t2:ef00"
# OS/DATA partition
CMD="$CMD -n3:1050624:0 -t3:BF01 -c 3:zfs"
else
# GRUB boot partition: 1M
CMD="$CMD -n1:34:2047 -t1:EF02"
# ZFS reserved partition
CMD="$CMD -n3:-8M:0 -t3:BF07"
# OS/Data partition
CMD="$CMD -n2:2048:0 -t2:BF01 -c 2:zfs"
fi
CMD="$CMD $TARGET_DEV"
# Zap-all partition tale
/sbin/sgdisk -Z "$TARGET_DEV"
# Execute the command line.
$CMD
# Install grub on it
/usr/sbin/grub-install "$TARGET_DEV"
# Install EFI if required
if [ $EFI -eq 1 ]; then
partprobe "${TARGET_DEV}"
while [ ! -e "${TARGET_DEV}2" ]; do
sleep 1
echo "."
done
pve-efiboot-tool format ${TARGET_DEV}2 --force
pve-efiboot-tool init ${TARGET_DEV}2
fi