system

the Chamesle handbook, init, live ISO and install helpers
git clone https://github.com/tortr-rs/system
Log | Files | Refs | README | LICENSE

chamesle-disk (6119B)


      1 #!/bin/sh
      2 # chamesle-disk [-t ext4|btrfs|zfs] [-y] DISK: erases a disk and sets it up for Chamesle. the boot partition becomes vfat,
      3 # the root partition ext4, btrfs (subvolumes @ and @home) or a ZFS pool (the default), all mounted under /mnt with a
      4 # fstab written. you lay out the partitions in cfdisk; -y skips the questions and takes the whole disk: a gpt label on
      5 # UEFI (BIOS boot, 512M EFI /boot, the rest root), a dos label on BIOS (bootable 512M /boot, the rest root, no BIOS boot part).
      6 #   chamesle-disk /dev/nvme0n1
      7 #   chamesle-disk -t btrfs -y /dev/sda
      8 die() { echo "chamesle-disk: $*" >&2; exit 1; }
      9 usage() { echo "usage: chamesle-disk [-t ext4|btrfs|zfs] [-y] DISK" >&2; exit 2; }
     10 fs=zfs yes=
     11 while getopts t:y o; do
     12 	case $o in
     13 	t) fs=$OPTARG;;
     14 	y) yes=1;;
     15 	*) usage;;
     16 	esac
     17 done
     18 shift $((OPTIND - 1))
     19 [ $# = 1 ] || usage
     20 case $fs in ext4|btrfs|zfs) ;; *) die "-t is ext4, btrfs or zfs";; esac
     21 [ "$(id -u)" = 0 ] || die "needs root"
     22 disk=$1
     23 [ -b "$disk" ] || die "$disk is not a block device"
     24 need="mkfs.fat blkid lsblk wipefs"
     25 [ -n "$yes" ] && need="$need sfdisk" || need="$need cfdisk"
     26 case $fs in
     27 ext4) need="$need mkfs.ext4";;
     28 btrfs) need="$need mkfs.btrfs btrfs";;
     29 zfs) need="$need zpool zfs";;
     30 esac
     31 for t in $need; do command -v $t >/dev/null || die "$t is not installed"; done
     32 mountpoint -q /mnt && die "/mnt is in use, unmount it first"
     33 [ $fs = zfs ] && zpool list rpool >/dev/null 2>&1 && die "a pool called rpool is already imported (zpool export -f rpool to let go of it)"
     34 
     35 # part N: the Nth partition of the disk, nvme0n1p2 as well as sda2
     36 part() { case $disk in *[0-9]) echo "${disk}p$1";; *) echo "$disk$1";; esac; }
     37 
     38 if [ -n "$yes" ]; then
     39 	wipefs -aq "$disk"
     40 	if [ -d /sys/firmware/efi ]; then
     41 		# UEFI: gpt with a BIOS boot partition (so it boots by BIOS too), the EFI system partition and the root
     42 		sfdisk -q "$disk" <<P || die "sfdisk failed"
     43 label: gpt
     44 size=1M, type=21686148-6449-6E6F-744E-656564454649
     45 size=512M, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B
     46 type=0FC63DAF-8483-4772-8E79-3D69D8477DE4
     47 P
     48 		boot=$(part 2) root=$(part 3)
     49 	else
     50 		# BIOS: a dos label wants no BIOS boot partition and limine goes straight on the MBR
     51 		sfdisk -q "$disk" <<P || die "sfdisk failed"
     52 label: dos
     53 size=512M, type=c, bootable
     54 type=83
     55 P
     56 		boot=$(part 1) root=$(part 2)
     57 	fi
     58 	command -v udevadm >/dev/null && udevadm settle
     59 	sleep 1
     60 	[ -b "$boot" ] && [ -b "$root" ] || die "the new partitions $boot and $root are not there"
     61 else
     62 	lsblk "$disk"
     63 	echo "Everything on $disk will be erased."
     64 	printf 'Type the name of the disk (%s) to go on: ' "${disk##*/}"
     65 	read -r answer
     66 	[ "$answer" = "${disk##*/}" ] || [ "$answer" = "$disk" ] || die "not confirmed, nothing was changed"
     67 	[ -d /sys/firmware/efi ] || echo "This machine boots by BIOS: on a gpt label, make a 1M partition of type BIOS boot, or pick dos."
     68 	cfdisk "$disk" || die "cfdisk failed"
     69 	sleep 1
     70 	echo
     71 	lsblk "$disk"
     72 	ask() {
     73 		printf '%s: ' "$1" >&2
     74 		read -r p
     75 		case $p in /dev/*) ;; *) p=/dev/$p;; esac
     76 		[ -b "$p" ] || die "$p is not a block device"
     77 		case $p in "$disk"?*) ;; *) die "$p is not a partition of $disk";; esac
     78 		echo "$p"
     79 	}
     80 	boot=$(ask "Which partition is the boot partition (vfat, the EFI system partition)") || exit 1
     81 	root=$(ask "Which partition is the root ($fs)") || exit 1
     82 	[ "$boot" != "$root" ] || die "boot and root cannot be the same partition"
     83 	if [ ! -d /sys/firmware/efi ] && [ "$(lsblk -dno PTTYPE "$disk")" = gpt ] && ! lsblk -no PARTTYPE "$disk" | grep -qi 21686148-6449-6e6f-744e-656564454649; then
     84 		die "this machine boots by BIOS and $disk is GPT with no BIOS boot partition: add a 1M one (type BIOS boot) in cfdisk, or use dos"
     85 	fi
     86 fi
     87 
     88 wipefs -aq "$boot" "$root"
     89 modprobe vfat 2>/dev/null; modprobe nls_utf8 2>/dev/null
     90 mkfs.fat -F32 "$boot" >/dev/null || die "mkfs.fat failed"
     91 case $fs in
     92 zfs)
     93 	[ -e /dev/zfs ] || modprobe zfs || die "the zfs module is not there"
     94 	undo() { umount /mnt/boot 2>/dev/null; zpool export -f rpool 2>/dev/null; die "$1, rpool was exported again, run chamesle-disk again"; }
     95 	zpool create -f -o ashift=12 -O compression=zstd -O acltype=posixacl -O xattr=sa -O mountpoint=none -R /mnt rpool "$root" || die "zpool create failed"
     96 	zfs create -o mountpoint=/ -o canmount=noauto rpool/root || undo "zfs create failed"
     97 	zfs mount rpool/root || undo "cannot mount rpool/root"
     98 	zfs create -o mountpoint=/home rpool/home
     99 	zfs create -o mountpoint=/var rpool/var
    100 	zpool set bootfs=rpool/root rpool
    101 	;;
    102 ext4)
    103 	undo() { umount -R /mnt 2>/dev/null; die "$1"; }
    104 	mkfs.ext4 -F -q "$root" || die "mkfs.ext4 failed"
    105 	mount -t ext4 "$root" /mnt || die "cannot mount $root"
    106 	;;
    107 btrfs)
    108 	undo() { umount -R /mnt 2>/dev/null; die "$1"; }
    109 	modprobe btrfs 2>/dev/null
    110 	mkfs.btrfs -f -q "$root" >/dev/null || die "mkfs.btrfs failed"
    111 	mount -t btrfs "$root" /mnt || die "cannot mount $root"
    112 	btrfs -q subvolume create /mnt/@ && btrfs -q subvolume create /mnt/@home || undo "cannot make the subvolumes"
    113 	umount /mnt
    114 	mount -t btrfs -o subvol=@,compress=zstd,noatime "$root" /mnt || die "cannot mount $root"
    115 	mkdir -p /mnt/home
    116 	mount -t btrfs -o subvol=@home,compress=zstd,noatime "$root" /mnt/home || undo "cannot mount @home"
    117 	;;
    118 esac
    119 mkdir -p /mnt/boot /mnt/etc
    120 mount -t vfat "$boot" /mnt/boot || undo "cannot mount $boot"
    121 
    122 if [ -e /mnt/etc/fstab ]; then
    123 	echo "/mnt/etc/fstab is there already, left alone"
    124 else
    125 	r=$(blkid -s UUID -o value "$root")
    126 	{
    127 		echo "# device  mount  type  options  dump pass"
    128 		case $fs in
    129 		zfs) echo "# the zfs datasets mount themselves, so they are not listed here";;
    130 		ext4) echo "UUID=$r / ext4 rw,noatime 0 1";;
    131 		btrfs)
    132 			echo "UUID=$r / btrfs rw,noatime,compress=zstd,subvol=@ 0 0"
    133 			echo "UUID=$r /home btrfs rw,noatime,compress=zstd,subvol=@home 0 0";;
    134 		esac
    135 		echo "UUID=$(blkid -s UUID -o value "$boot") /boot vfat rw,noatime 0 2"
    136 		echo "proc /proc proc defaults 0 0"
    137 		echo "sysfs /sys sysfs defaults 0 0"
    138 		echo "tmpfs /tmp tmpfs mode=1777,nosuid,nodev 0 0"
    139 	} > /mnt/etc/fstab
    140 fi
    141 
    142 echo
    143 echo "$boot is vfat on /mnt/boot, $root is $fs on /mnt."
    144 [ -n "$yes" ] || echo "Now install the system into /mnt: chapter 3 of the handbook, section 3.3 onwards."