|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Copyright 2021 mobinmob <mobinmob@disroot.org> |
| 4 | +# Use of this source code is governed by the 2-Clause BSD License |
| 5 | +# that can be found in the LICENSE file at the root project directory. |
| 6 | +# SPDX short identifier: BSD-2-Clause |
| 7 | + |
| 8 | +# This script determines if the system has dmraid, luks, zfs, btrfs, |
| 9 | +# volumes, a swap partition or swapfile and configures the environment |
| 10 | +# file of the boot@system service accordingly. |
| 11 | +# Excluding the common posix userland commands, the script needs 66-which and 66-yeller |
| 12 | +# (from 66-tools), 66-env (from 66) and blkid (from util-linux). |
| 13 | + |
| 14 | +# Variables for 66-yeller |
| 15 | +export PROG="66boot-storage-autoconf" |
| 16 | +export COLOR_ENABLED="1" |
| 17 | +export CLOCK_ENABLED="0" |
| 18 | + |
| 19 | +# Script must run as root. |
| 20 | +user=$(id -u) |
| 21 | +[ "$user" != "0" ] && 66-yeller -F %r You need to run this script as root! %n && exit 1 |
| 22 | + |
| 23 | +# blkid(8) from util-linux should be available for the script to function. |
| 24 | +[ "$(blkid -v | grep util-linux -c)" != "1" ] && \ |
| 25 | +66-yeller -F %r You need blkid from util-linux to run this program! %n && exit 1 |
| 26 | + |
| 27 | +detect_fs() { |
| 28 | + # Uses blkid to detect partition TYPEs. The result is the number |
| 29 | + # of detected partitions. TYPEs are available with `blkid -k`. |
| 30 | + export "${1}"_detected="$(blkid -c /dev/null --match-token=TYPE="${1}" | wc -l )" |
| 31 | +} |
| 32 | + |
| 33 | +detect_swapfile() { |
| 34 | + # Finds swap references in /etc/fstab. |
| 35 | + swapfile_detected="$(awk '!/^(#)/' /etc/fstab | awk '$3=="swap" ' | wc -l )" |
| 36 | + export swapfile_detected |
| 37 | +} |
| 38 | + |
| 39 | +detect_util() { |
| 40 | + # Uses 66-which to determine if a utility exists. |
| 41 | + # Output is $fs_util_exitst=0 (or 1) |
| 42 | + # ${1} is the variable for the utility, in the form of "$fs_util" |
| 43 | + # ${2} is the name of the executable |
| 44 | + export "${1}"_exists="$(66-which "${2}" | wc -l)" |
| 45 | +} |
| 46 | + |
| 47 | +check_and_apply_conf() { |
| 48 | + # Checks for partition types and the existance of the relevant utilities. |
| 49 | + # Outputs user information |
| 50 | + # ${1} is in the form of $fs_detected |
| 51 | + # ${2} is in the form of $fs_name_exists |
| 52 | + # ${3} is the name of the configuration variable |
| 53 | + # ${4} is the user-friendly name for the configuration option |
| 54 | + # ${5} name of the utility needed for the block device type |
| 55 | + # ${6} is the name of the package that contains the program |
| 56 | + # ${6} is **distribution-dependent** |
| 57 | + if [ "${1}" != "0" ] && [ "${2}" = "1" ] |
| 58 | + then |
| 59 | + 66-env -t boot -r "${3}"=!yes boot@system |
| 60 | + 66-yeller %g "${4} detected and enabled!" %n |
| 61 | + elif [ "${1}" != "0" ] && [ "${2}" = "0" ] |
| 62 | + then |
| 63 | + 66-env -t boot -r "${3}"=!no boot@system |
| 64 | + 66-yeller -W %y "${4} detected but cannot be enabled,\n because the ${5} program is not installed. Please install the ${6} \n package and run this script again!" %n |
| 65 | + elif [ "${1}" = "0" ] |
| 66 | + then |
| 67 | + 66-env -t boot -r "${3}"=!no boot@system |
| 68 | + 66-yeller %b "${4} not detected!" %n |
| 69 | + fi |
| 70 | + |
| 71 | +} |
| 72 | + |
| 73 | +check_and_apply_conf_zpool() { |
| 74 | + # Checks for the zfs pool import method if ZFS is enabled. |
| 75 | + # Outputs user information |
| 76 | + # ${1} is in the form of $fs_detected |
| 77 | + # ${2} is in the form of $fs_name_exists |
| 78 | + if [ "${1}" != "0" ] && [ "${2}" = "1" ] |
| 79 | + then |
| 80 | + 66-env -t boot -r "ZFS"=!yes boot@system |
| 81 | + if [ -e /etc/zfs/zpool.cache ] |
| 82 | + then |
| 83 | + 66-env -t boot -r ZFS_IMPORT=!zpoolcache boot@system |
| 84 | + 66-yeller %g "ZFS import method configured as zpoolcache." %n |
| 85 | + else |
| 86 | + 66-env -t boot -r ZFS_IMPORT=!scan boot@system |
| 87 | + 66-yeller %g "ZFS import method configured as scan." %n |
| 88 | + fi |
| 89 | + fi |
| 90 | + |
| 91 | +} |
| 92 | + |
| 93 | +# devices_to_detect is a list of some device TYPEs we want to detect |
| 94 | +devices_to_detect="btrfs zfs_member crypto_LUKS linux_raid_member LVM2_member swap" |
| 95 | + |
| 96 | +# Use detect fs for all members of devices_to_detect we want |
| 97 | +for key in $devices_to_detect; do detect_fs "$key" ; done |
| 98 | + |
| 99 | +# dmraid supports multiple TYPEs of *-raid-member devices. |
| 100 | +# find them first and add the sum of the *_detected values to |
| 101 | +# dmraid_detected. |
| 102 | + |
| 103 | +# dmraid_TYPEs holds the device TYPEs dmraid handles |
| 104 | +dmraid_TYPEs="ddf_raid_member isw_raid_member lsi_mega_raid_member \ |
| 105 | +via_raid_member silicon_medley_raid_member nvidia_raid_member \ |
| 106 | +promise_fasttrack_raid_member hpt45x_raid_member hpt37x_raid_member \ |
| 107 | +adaptec_raid_member jmicron_raid_member" |
| 108 | + |
| 109 | +for TYPE in $dmraid_TYPEs; do detect_fs "$TYPE" ; done |
| 110 | + |
| 111 | +# shellcheck disable=SC2154 |
| 112 | +dmraid_detected=$((ddf_raid_member_detected+isw_raid_member_detected\ |
| 113 | ++lsi_mega_raid_member_detected+via_raid_member_detected\ |
| 114 | ++silicon_medley_raid_member_detected+nvidia_raid_member_detected\ |
| 115 | ++promise_fasttrack_raid_member_detected+hpt37x_raid_member_detecter\ |
| 116 | ++hpt45x_raid_member_detected+adaptec_raid_member_detected\ |
| 117 | ++jmicron_raid_member_detected)) |
| 118 | + |
| 119 | +# swap can be detected by blkid if it is a partition or it can be a swapfile |
| 120 | +# declared in fstab |
| 121 | +detect_swapfile |
| 122 | +swap_detected=$((swap_detected+swapfile_detected)) |
| 123 | + |
| 124 | +# Use detect_util for all utilities |
| 125 | +detect_util btrfs_util btrfs |
| 126 | +detect_util zfs_util zfs |
| 127 | +detect_util luks_util cryptsetup |
| 128 | +detect_util lvm_util lvchange |
| 129 | +detect_util dmraid_util dmraid |
| 130 | +detect_util swap_util swapon |
| 131 | +detect_util mdraid_util mdadm |
| 132 | + |
| 133 | +# Finally detect and apply the configuration |
| 134 | +# shellcheck disable=SC2154 |
| 135 | +check_and_apply_conf "$btrfs_detected" "$btrfs_util_exists" BTRFS BTRFS btrfs "[btrfs_progs]" |
| 136 | +# shellcheck disable=SC2154 |
| 137 | +check_and_apply_conf "$zfs_member_detected" "$zfs_util_exists" ZFS ZFS zfs "[zfs]" |
| 138 | +# shellcheck disable=SC2154 |
| 139 | +check_and_apply_conf_zpool "$zfs_member_detected" "$zfs_util_exists" ZFS |
| 140 | +# shellcheck disable=SC2154 |
| 141 | +check_and_apply_conf "$crypto_LUKS_detected" "$luks_util_exists" CRYPTTAB LUKS cryptsetup "[cryptsetup]" |
| 142 | +# shellcheck disable=SC2154 |
| 143 | +check_and_apply_conf "$LVM2_member_detected" "$lvm_util_exists" LVM LVM lvchange "[lvm2]" |
| 144 | +# shellcheck disable=SC2154 |
| 145 | +check_and_apply_conf "$dmraid_detected" "$dmraid_util_exists" DMRAID DMRAID dmraid "[dmraid]" |
| 146 | +# shellcheck disable=SC2154 |
| 147 | +check_and_apply_conf "$swap_detected" "$swap_util_exists" SWAP SWAP swapon "[util-linux]" |
| 148 | +# shellcheck disable=SC2154 |
| 149 | +check_and_apply_conf "$linux_raid_member_detected" "$mdraid_util_exists" MDRAID MDRAID mdadm "[mdadm]" |
| 150 | + |
| 151 | + |
| 152 | +66-yeller %g "Please re-enable the boot@system service |
| 153 | +for the changes to take effect by running (as root): |
| 154 | +'66-enable -F -t boot boot@system'" %n |
0 commit comments