2021-04-28 14:06:52 +00:00
# shellcheck source=./scripts/protection.sh
2020-01-08 16:21:01 -01:00
source " $GENTOO_INSTALL_REPO_DIR /scripts/protection.sh " || exit 1
2019-12-31 14:28:42 -01:00
################################################
# Script internal configuration
# The temporary directory for this script,
# must reside in /tmp to allow the chrooted system to access the files
2020-01-08 16:21:01 -01:00
TMP_DIR = "/tmp/gentoo-install"
2019-12-31 14:28:42 -01:00
# Mountpoint for the new system
ROOT_MOUNTPOINT = " $TMP_DIR /root "
# Mountpoint for the script files for access from chroot
2020-01-08 16:21:01 -01:00
GENTOO_INSTALL_REPO_BIND = " $TMP_DIR /bind "
2019-12-31 14:28:42 -01:00
# Mountpoint for the script files for access from chroot
UUID_STORAGE_DIR = " $TMP_DIR /uuids "
2020-04-22 21:08:11 +00:00
# Backup dir for luks headers
LUKS_HEADER_BACKUP_DIR = " $TMP_DIR /luks-headers "
2019-12-31 14:28:42 -01:00
2020-04-20 22:40:54 +00:00
# Flag to track usage of raid (needed to check for mdadm existence)
USED_RAID = false
# Flag to track usage of luks (needed to check for cryptsetup existence)
USED_LUKS = false
2021-04-20 14:55:38 +00:00
# Flag to track usage of zfs
USED_ZFS = false
2020-10-11 19:45:26 +00:00
# Flag to track usage of btrfs
USED_BTRFS = false
2021-05-02 13:29:21 +00:00
# Flag to track usage of encryption
USED_ENCRYPTION = false
2020-04-20 22:40:54 +00:00
2020-04-19 19:34:36 +00:00
# An array of disk related actions to perform
DISK_ACTIONS = ( )
2020-04-25 13:12:35 +00:00
# An array of dracut parameters needed to boot the selected configuration
2021-04-28 14:06:52 +00:00
DISK_DRACUT_CMDLINE = ( )
2020-04-23 14:53:31 +00:00
# An associative array from disk id to a resolvable string
2021-04-28 14:06:52 +00:00
declare -gA DISK_ID_TO_RESOLVABLE
2020-04-22 21:48:36 +00:00
# An associative array from disk id to parent gpt disk id (only for partitions)
2021-04-28 14:06:52 +00:00
declare -gA DISK_ID_PART_TO_GPT_ID
2020-04-21 16:38:46 +00:00
# An associative array to check for existing ids (maps to uuids)
2021-04-28 14:06:52 +00:00
declare -gA DISK_ID_TO_UUID
2020-04-21 12:33:42 +00:00
# An associative set to check for correct usage of size=remaining in gpt tables
2021-04-28 14:06:52 +00:00
declare -gA DISK_GPT_HAD_SIZE_REMAINING
2020-04-19 19:34:36 +00:00
2021-04-28 14:06:52 +00:00
function only_one_of( ) {
2020-04-19 19:34:36 +00:00
local previous = ""
local a
for a in " $@ " ; do
2020-04-20 22:40:54 +00:00
if [ [ -v arguments[ $a ] ] ] ; then
if [ [ -z $previous ] ] ; then
2020-04-19 19:34:36 +00:00
previous = " $a "
else
die_trace 2 " Only one of the arguments ( $* ) can be given "
fi
fi
done
}
2021-04-28 14:06:52 +00:00
function create_new_id( ) {
2020-04-19 19:34:36 +00:00
local id = " ${ arguments [ $1 ] } "
2020-04-20 22:40:54 +00:00
[ [ $id = = *';' * ] ] \
&& die_trace 2 "Identifier contains invalid character ';'"
2020-04-21 16:38:46 +00:00
[ [ ! -v DISK_ID_TO_UUID[ $id ] ] ] \
2020-04-19 19:34:36 +00:00
|| die_trace 2 " Identifier ' $id ' already exists "
2020-04-21 16:38:46 +00:00
DISK_ID_TO_UUID[ $id ] = " $( load_or_generate_uuid " $( base64 -w 0 <<< " $id " ) " ) "
2020-04-19 19:34:36 +00:00
}
2021-04-28 14:06:52 +00:00
function verify_existing_id( ) {
2020-04-19 19:34:36 +00:00
local id = " ${ arguments [ $1 ] } "
2020-04-21 16:38:46 +00:00
[ [ -v DISK_ID_TO_UUID[ $id ] ] ] \
2020-04-19 19:34:36 +00:00
|| die_trace 2 " Identifier $1 =' $id ' not found "
}
2021-04-28 14:06:52 +00:00
function verify_existing_unique_ids( ) {
2020-04-20 22:40:54 +00:00
local arg = " $1 "
local ids = " ${ arguments [ $arg ] } "
2020-04-19 19:34:36 +00:00
2020-04-20 22:40:54 +00:00
count_orig = " $( tr ';' '\n' <<< " $ids " | grep -c '\S' ) "
count_uniq = " $( tr ';' '\n' <<< " $ids " | grep '\S' | sort -u | wc -l) "
[ [ $count_orig -gt 0 ] ] \
|| die_trace 2 " $arg =... must contain at least one entry "
[ [ $count_orig -eq $count_uniq ] ] \
|| die_trace 2 " $arg =... contains duplicate identifiers "
2020-04-19 19:34:36 +00:00
2020-04-20 22:40:54 +00:00
local id
2020-04-19 19:34:36 +00:00
# Splitting is intentional here
2020-04-20 22:40:54 +00:00
# shellcheck disable=SC2086
for id in ${ ids // ';' / } ; do
2020-04-21 16:38:46 +00:00
[ [ -v DISK_ID_TO_UUID[ $id ] ] ] \
2020-04-20 22:40:54 +00:00
|| die_trace 2 " $arg =... contains unknown identifier ' $id ' "
2020-04-19 19:34:36 +00:00
done
}
2021-04-28 14:06:52 +00:00
function verify_option( ) {
2020-04-19 19:34:36 +00:00
local opt = " $1 "
shift
local arg = " ${ arguments [ $opt ] } "
local i
for i in " $@ " ; do
2020-04-20 22:40:54 +00:00
[ [ $i = = " $arg " ] ] \
2020-04-19 19:34:36 +00:00
&& return 0
done
die_trace 2 " Invalid option $opt =' $arg ', must be one of ( $* ) "
}
# Named arguments:
2020-10-03 14:24:48 +00:00
# new_id: Id for the new gpt table
# device|id: The operand block device or previously allocated id
2021-04-28 14:06:52 +00:00
function create_gpt( ) {
2020-04-20 22:40:54 +00:00
local known_arguments = ( '+new_id' '+device|id' )
2020-04-21 19:52:46 +00:00
local extra_arguments = ( )
declare -A arguments; parse_arguments " $@ "
2020-04-19 19:34:36 +00:00
only_one_of device id
create_new_id new_id
2020-04-20 22:40:54 +00:00
[ [ -v arguments[ id] ] ] \
2020-04-19 19:34:36 +00:00
&& verify_existing_id id
2020-04-24 21:02:00 +00:00
local new_id = " ${ arguments [new_id] } "
create_resolve_entry " $new_id " ptuuid " ${ DISK_ID_TO_UUID [ $new_id ] } "
2020-04-20 22:40:54 +00:00
DISK_ACTIONS += ( "action=create_gpt" " $@ " ";" )
}
# Named arguments:
# new_id: Id for the new partition
2020-04-21 12:33:42 +00:00
# size: Size for the new partition, or 'remaining' to allocate the rest
2020-04-21 19:52:46 +00:00
# type: The parition type, either (bios, efi, swap, raid, luks, linux) (or a 4 digit hex-code for gdisk).
2020-04-20 22:40:54 +00:00
# id: The operand device id
2021-04-28 14:06:52 +00:00
function create_partition( ) {
2020-04-20 22:40:54 +00:00
local known_arguments = ( '+new_id' '+id' '+size' '+type' )
2020-04-21 19:52:46 +00:00
local extra_arguments = ( )
declare -A arguments; parse_arguments " $@ "
2020-04-20 22:40:54 +00:00
create_new_id new_id
verify_existing_id id
2020-04-21 19:52:46 +00:00
verify_option type bios efi swap raid luks linux
2020-04-20 22:40:54 +00:00
2020-04-21 12:33:42 +00:00
[ [ -v " DISK_GPT_HAD_SIZE_REMAINING[ ${ arguments [id] } ] " ] ] \
&& die_trace 1 " Cannot add another partition to table ( ${ arguments [id] } ) after size=remaining was used "
2021-04-28 14:06:52 +00:00
# shellcheck disable=SC2034
2020-04-21 12:33:42 +00:00
[ [ ${ arguments [size] } = = "remaining" ] ] \
&& DISK_GPT_HAD_SIZE_REMAINING[ ${ arguments [id] } ] = true
2020-04-24 21:02:00 +00:00
local new_id = " ${ arguments [new_id] } "
DISK_ID_PART_TO_GPT_ID[ $new_id ] = " ${ arguments [id] } "
create_resolve_entry " $new_id " partuuid " ${ DISK_ID_TO_UUID [ $new_id ] } "
2020-04-20 22:40:54 +00:00
DISK_ACTIONS += ( "action=create_partition" " $@ " ";" )
2020-04-19 19:34:36 +00:00
}
# Named arguments:
# new_id: Id for the new raid
# level: Raid level
2020-04-21 21:29:06 +00:00
# name: Raid name (/dev/md/<name>)
2020-04-20 22:40:54 +00:00
# ids: Comma separated list of all member ids
2021-04-28 14:06:52 +00:00
function create_raid( ) {
2020-04-20 22:40:54 +00:00
USED_RAID = true
2020-04-21 21:29:06 +00:00
local known_arguments = ( '+new_id' '+level' '+name' '+ids' )
2020-04-21 19:52:46 +00:00
local extra_arguments = ( )
declare -A arguments; parse_arguments " $@ "
2020-04-19 19:34:36 +00:00
create_new_id new_id
verify_option level 0 1 5 6
verify_existing_unique_ids ids
2020-04-24 21:02:00 +00:00
local new_id = " ${ arguments [new_id] } "
2020-04-25 18:17:24 +00:00
local uuid = " ${ DISK_ID_TO_UUID [ $new_id ] } "
create_resolve_entry " $new_id " mdadm " $uuid "
DISK_DRACUT_CMDLINE += ( " rd.md.uuid= $( uuid_to_mduuid " $uuid " ) " )
2020-04-20 22:40:54 +00:00
DISK_ACTIONS += ( "action=create_raid" " $@ " ";" )
2020-04-19 19:34:36 +00:00
}
# Named arguments:
# new_id: Id for the new luks
2020-04-20 22:40:54 +00:00
# id: The operand device id
2021-04-28 14:06:52 +00:00
function create_luks( ) {
2020-04-20 22:40:54 +00:00
USED_LUKS = true
2021-05-02 13:29:21 +00:00
USED_ENCRYPTION = true
2020-04-20 22:40:54 +00:00
2020-10-03 14:24:48 +00:00
local known_arguments = ( '+new_id' '+name' '+device|id' )
2020-04-21 19:52:46 +00:00
local extra_arguments = ( )
declare -A arguments; parse_arguments " $@ "
2020-04-19 19:34:36 +00:00
2020-10-03 14:24:48 +00:00
only_one_of device id
2020-04-19 19:34:36 +00:00
create_new_id new_id
2020-10-03 14:24:48 +00:00
[ [ -v arguments[ id] ] ] \
&& verify_existing_id id
2020-04-19 19:34:36 +00:00
2020-04-24 21:02:00 +00:00
local new_id = " ${ arguments [new_id] } "
local name = " ${ arguments [name] } "
2020-04-25 18:17:24 +00:00
local uuid = " ${ DISK_ID_TO_UUID [ $new_id ] } "
2020-04-24 21:02:00 +00:00
create_resolve_entry " $new_id " luks " $name "
2020-04-25 18:17:24 +00:00
DISK_DRACUT_CMDLINE += ( " rd.luks.uuid= $uuid " )
2020-04-20 22:40:54 +00:00
DISK_ACTIONS += ( "action=create_luks" " $@ " ";" )
2020-04-19 19:34:36 +00:00
}
2020-10-03 15:59:45 +00:00
# Named arguments:
# new_id: Id for the new luks
# device: The device
2021-04-28 14:06:52 +00:00
function create_dummy( ) {
2020-10-03 15:59:45 +00:00
local known_arguments = ( '+new_id' '+device' )
local extra_arguments = ( )
declare -A arguments; parse_arguments " $@ "
create_new_id new_id
local new_id = " ${ arguments [new_id] } "
local device = " ${ arguments [device] } "
local uuid = " ${ DISK_ID_TO_UUID [ $new_id ] } "
create_resolve_entry_device " $new_id " " $device "
DISK_ACTIONS += ( "action=create_dummy" " $@ " ";" )
}
2020-04-19 19:34:36 +00:00
# Named arguments:
# id: Id of the device / partition created earlier
2020-10-11 19:45:26 +00:00
# type: One of (bios, efi, swap, ext4)
2020-04-19 19:34:36 +00:00
# label: The label for the formatted disk
2021-04-28 14:06:52 +00:00
function format( ) {
2020-04-20 22:40:54 +00:00
local known_arguments = ( '+id' '+type' '?label' )
2020-04-21 19:52:46 +00:00
local extra_arguments = ( )
declare -A arguments; parse_arguments " $@ "
2020-04-19 19:34:36 +00:00
verify_existing_id id
2020-10-28 23:19:45 -01:00
verify_option type bios efi swap ext4 btrfs
local type = " ${ arguments [type] } "
if [ [ " $type " = = "btrfs" ] ] ; then
USED_BTRFS = true
fi
2020-04-19 19:34:36 +00:00
2020-04-20 22:40:54 +00:00
DISK_ACTIONS += ( "action=format" " $@ " ";" )
}
2021-04-28 12:56:38 +00:00
# Named arguments:
# ids: List of ids for devices / partitions created earlier. Must contain at least 1 element.
# pool_type: The zfs pool type
# encrypt: Whether or not to encrypt the pool
2021-04-28 14:06:52 +00:00
function format_zfs( ) {
2021-04-28 12:56:38 +00:00
USED_ZFS = true
2021-06-02 14:18:31 +00:00
local known_arguments = ( '+ids' '?pool_type' '?encrypt' '?compress' )
2021-04-28 12:56:38 +00:00
local extra_arguments = ( )
declare -A arguments; parse_arguments " $@ "
verify_existing_unique_ids ids
2021-05-02 13:29:21 +00:00
USED_ENCRYPTION = ${ arguments [encrypt] :- false }
2021-04-28 12:56:38 +00:00
DISK_ACTIONS += ( "action=format_zfs" " $@ " ";" )
}
2020-10-03 14:24:48 +00:00
# Named arguments:
# ids: List of ids for devices / partitions created earlier. Must contain at least 1 element.
# label: The label for the formatted disk
2021-04-28 14:06:52 +00:00
function format_btrfs( ) {
2020-10-11 19:45:26 +00:00
USED_BTRFS = true
local known_arguments = ( '+ids' '?raid_type' '?label' )
2020-10-03 14:24:48 +00:00
local extra_arguments = ( )
declare -A arguments; parse_arguments " $@ "
verify_existing_unique_ids ids
DISK_ACTIONS += ( "action=format_btrfs" " $@ " ";" )
}
2020-04-20 22:40:54 +00:00
# Returns a comma separated list of all registered ids matching the given regex.
2021-04-28 14:06:52 +00:00
function expand_ids( ) {
2020-04-20 22:40:54 +00:00
local regex = " $1 "
2020-04-21 16:38:46 +00:00
for id in " ${ !DISK_ID_TO_UUID[@] } " ; do
2020-04-20 22:40:54 +00:00
[ [ $id = ~ $regex ] ] \
&& echo -n " $id ; "
done
2020-04-19 19:34:36 +00:00
}
2020-04-21 16:43:17 +00:00
2021-04-22 23:33:46 +00:00
# Single disk, 3 partitions (efi, swap, root)
2020-04-21 19:52:46 +00:00
# Parameters:
2020-11-25 13:56:46 -01:00
# swap=<size> Create a swap partition with given size, or no swap at all if set to false
# type=[efi|bios] Selects the boot type. Defaults to efi if not given.
# luks=[true|false] Encrypt root partition. Defaults to false if not given.
# root_fs=[ext4|btrfs] Root filesystem
2021-04-28 14:06:52 +00:00
function create_classic_single_disk_layout( ) {
2020-10-03 14:40:47 +00:00
local known_arguments = ( '+swap' '?type' '?luks' '?root_fs' )
2020-04-21 19:52:46 +00:00
local extra_arguments = ( )
declare -A arguments; parse_arguments " $@ "
[ [ ${# extra_arguments [@] } -eq 1 ] ] \
|| die_trace 1 "Expected exactly one positional argument (the device)"
local device = " ${ extra_arguments [0] } "
local size_swap = " ${ arguments [swap] } "
2021-04-28 14:06:52 +00:00
local type = " ${ arguments [type] :- efi } "
2020-10-03 14:24:48 +00:00
local use_luks = " ${ arguments [luks] :- false } "
local root_fs = " ${ arguments [root_fs] :- ext4 } "
2020-04-21 16:43:17 +00:00
create_gpt new_id = gpt device = " $device "
2020-10-03 14:24:48 +00:00
create_partition new_id = " part_ $type " id = gpt size = 256MiB type = " $type "
2021-04-22 19:09:11 +00:00
[ [ $size_swap != "false" ] ] \
&& create_partition new_id = part_swap id = gpt size = " $size_swap " type = swap
2020-04-21 19:52:46 +00:00
create_partition new_id = part_root id = gpt size = remaining type = linux
2020-04-21 16:43:17 +00:00
2020-10-01 13:46:51 +00:00
local root_id = "part_root"
if [ [ " $use_luks " = = "true" ] ] ; then
create_luks new_id = part_luks_root name = "root" id = part_root
root_id = "part_luks_root"
fi
2020-04-21 19:52:46 +00:00
format id = " part_ $type " type = " $type " label = " $type "
2021-04-22 19:09:11 +00:00
[ [ $size_swap != "false" ] ] \
&& format id = part_swap type = swap label = swap
2020-10-01 13:46:51 +00:00
format id = " $root_id " type = " $root_fs " label = root
2020-04-21 16:43:17 +00:00
2020-04-21 19:52:46 +00:00
if [ [ $type = = "efi" ] ] ; then
DISK_ID_EFI = " part_ $type "
else
DISK_ID_BIOS = " part_ $type "
fi
2021-04-22 19:09:11 +00:00
[ [ $size_swap != "false" ] ] \
&& DISK_ID_SWAP = part_swap
2020-10-01 13:46:51 +00:00
DISK_ID_ROOT = " $root_id "
2020-10-05 20:28:32 +00:00
if [ [ $root_fs = = "btrfs" ] ] ; then
DISK_ID_ROOT_TYPE = "btrfs"
2020-11-06 16:58:15 -01:00
DISK_ID_ROOT_MOUNT_OPTS = "defaults,noatime,compress-force=zstd,subvol=/root"
2020-11-18 12:23:35 -01:00
elif [ [ $root_fs = = "ext4" ] ] ; then
2020-10-05 20:28:32 +00:00
DISK_ID_ROOT_TYPE = "ext4"
DISK_ID_ROOT_MOUNT_OPTS = "defaults,noatime,errors=remount-ro,discard"
else
die "Unsupported root filesystem type"
fi
2020-04-21 16:43:17 +00:00
}
2021-04-28 14:06:52 +00:00
function create_single_disk_layout( ) {
2021-04-22 23:33:46 +00:00
die "'create_single_disk_layout' is deprecated, please use 'create_classic_single_disk_layout' instead. It is fully option-compatible to the old version."
}
2021-04-28 12:56:38 +00:00
# Multiple disks, up to 3 partitions on first disk (efi, optional swap, root with zfs).
# Additional devices will be added to the zfs pool.
# Parameters:
# swap=<size> Create a swap partition with given size, or no swap at all if set to false
# type=[efi|bios] Selects the boot type. Defaults to efi if not given.
# encrypt=[true|false] Encrypt zfs pool. Defaults to false if not given.
# pool_type=[stripe|mirror] Select raid type. Defaults to stripe.
2021-04-28 14:06:52 +00:00
function create_zfs_centric_layout( ) {
2021-04-28 12:56:38 +00:00
local known_arguments = ( '+swap' '?type' '?pool_type' '?encrypt' )
local extra_arguments = ( )
declare -A arguments; parse_arguments " $@ "
[ [ ${# extra_arguments [@] } -gt 0 ] ] \
|| die_trace 1 "Expected at least one positional argument (the devices)"
local device = " ${ extra_arguments [0] } "
local size_swap = " ${ arguments [swap] } "
local pool_type = " ${ arguments [pool_type] :- stripe } "
2021-04-28 14:06:52 +00:00
local type = " ${ arguments [type] :- efi } "
2021-04-28 12:56:38 +00:00
local encrypt = " ${ arguments [encrypt] :- false } "
# Create layout on first disk
create_gpt new_id = "gpt_dev0" device = " ${ extra_arguments [0] } "
create_partition new_id = " part_ ${ type } _dev0 " id = "gpt_dev0" size = 256MiB type = " $type "
[ [ $size_swap != "false" ] ] \
&& create_partition new_id = "part_swap_dev0" id = "gpt_dev0" size = " $size_swap " type = swap
create_partition new_id = "part_root_dev0" id = "gpt_dev0" size = remaining type = linux
local root_id = "part_root_dev0"
local root_ids = "part_root_dev0;"
local dev_id
for i in " ${ !extra_arguments[@] } " ; do
[ [ $i != 0 ] ] || continue
dev_id = " root_dev $i "
create_dummy new_id = " $dev_id " device = " ${ extra_arguments [ $i ] } "
root_ids = " ${ root_ids } $dev_id ; "
done
format id = " part_ ${ type } _dev0 " type = " $type " label = " $type "
[ [ $size_swap != "false" ] ] \
&& format id = "part_swap_dev0" type = swap label = swap
format_zfs ids = " $root_ids " encrypt = " $encrypt " pool_type = " $pool_type "
if [ [ $type = = "efi" ] ] ; then
DISK_ID_EFI = " part_ ${ type } _dev0 "
else
DISK_ID_BIOS = " part_ ${ type } _dev0 "
fi
[ [ $size_swap != "false" ] ] \
&& DISK_ID_SWAP = part_swap_dev0
DISK_ID_ROOT = " $root_id "
DISK_ID_ROOT_TYPE = "zfs"
}
2021-04-22 23:33:46 +00:00
# Multiple disks, with raid 0 and luks
2020-04-21 16:43:17 +00:00
# - efi: partition on all disks, but only first disk used
# - swap: raid 0 → fs
# - root: raid 0 → luks → fs
2020-04-21 19:52:46 +00:00
# Parameters:
2020-11-25 13:56:46 -01:00
# swap=<size> Create a swap partition with given size for each disk, or no swap at all if set to false
# type=[efi|bios] Selects the boot type. Defaults to efi if not given.
# root_fs=[ext4|btrfs] Root filesystem
2021-04-28 14:06:52 +00:00
function create_raid0_luks_layout( ) {
2020-10-03 14:24:48 +00:00
local known_arguments = ( '+swap' '?type' '?root_fs' )
2020-04-21 19:52:46 +00:00
local extra_arguments = ( )
declare -A arguments; parse_arguments " $@ "
[ [ ${# extra_arguments [@] } -gt 0 ] ] \
|| die_trace 1 "Expected at least one positional argument (the devices)"
local size_swap = " ${ arguments [swap] } "
2021-04-28 14:06:52 +00:00
local type = " ${ arguments [type] :- efi } "
2020-10-03 14:24:48 +00:00
local root_fs = " ${ arguments [root_fs] :- ext4 } "
2020-04-21 19:52:46 +00:00
for i in " ${ !extra_arguments[@] } " ; do
create_gpt new_id = " gpt_dev ${ i } " device = " ${ extra_arguments [ $i ] } "
2020-10-03 14:24:48 +00:00
create_partition new_id = " part_ ${ type } _dev ${ i } " id = " gpt_dev ${ i } " size = 256MiB type = " $type "
2021-04-22 19:09:11 +00:00
[ [ $size_swap != "false" ] ] \
&& create_partition new_id = " part_swap_dev ${ i } " id = " gpt_dev ${ i } " size = " $size_swap " type = raid
2020-04-21 19:52:46 +00:00
create_partition new_id = " part_root_dev ${ i } " id = " gpt_dev ${ i } " size = remaining type = raid
2020-04-21 16:43:17 +00:00
done
2021-04-22 19:09:11 +00:00
[ [ $size_swap != "false" ] ] \
&& create_raid new_id = part_raid_swap name = "swap" level = 0 ids = " $( expand_ids '^part_swap_dev[[:digit:]]$' ) "
2020-04-21 21:29:06 +00:00
create_raid new_id = part_raid_root name = "root" level = 0 ids = " $( expand_ids '^part_root_dev[[:digit:]]$' ) "
2020-04-24 21:02:00 +00:00
create_luks new_id = part_luks_root name = "root" id = part_raid_root
2020-04-21 16:43:17 +00:00
2020-04-21 19:52:46 +00:00
format id = " part_ ${ type } _dev0 " type = " $type " label = " $type "
2021-04-22 19:09:11 +00:00
[ [ $size_swap != "false" ] ] \
&& format id = part_raid_swap type = swap label = swap
2020-10-01 13:46:51 +00:00
format id = part_luks_root type = " $root_fs " label = root
2020-04-21 16:43:17 +00:00
2020-04-21 19:52:46 +00:00
if [ [ $type = = "efi" ] ] ; then
DISK_ID_EFI = " part_ ${ type } _dev0 "
else
DISK_ID_BIOS = " part_ ${ type } _dev0 "
fi
2021-04-22 19:09:11 +00:00
[ [ $size_swap != "false" ] ] \
&& DISK_ID_SWAP = part_raid_swap
2020-04-21 16:43:17 +00:00
DISK_ID_ROOT = part_luks_root
2020-10-05 20:28:32 +00:00
if [ [ $root_fs = = "btrfs" ] ] ; then
DISK_ID_ROOT_TYPE = "btrfs"
DISK_ID_ROOT_MOUNT_OPTS = "defaults,noatime,compress=zstd,subvol=/root"
elif [ [ $root_fs = = "btrfs" ] ] ; then
DISK_ID_ROOT_TYPE = "ext4"
DISK_ID_ROOT_MOUNT_OPTS = "defaults,noatime,errors=remount-ro,discard"
else
die "Unsupported root filesystem type"
fi
2020-04-21 16:43:17 +00:00
}
2020-10-03 14:24:48 +00:00
2021-04-22 23:33:46 +00:00
# Multiple disks, up to 3 partitions on first disk (efi, optional swap, root with btrfs).
2020-10-03 14:24:48 +00:00
# Additional devices will be first encrypted and then put directly into btrfs array.
# Parameters:
2020-11-25 13:56:46 -01:00
# swap=<size> Create a swap partition with given size, or no swap at all if set to false
# type=[efi|bios] Selects the boot type. Defaults to efi if not given.
# luks=[true|false] Encrypt root partition and btrfs devices. Defaults to false if not given.
2020-10-11 19:45:26 +00:00
# raid_type=[raid0|raid1] Select raid type. Defaults to raid0.
2021-04-28 14:06:52 +00:00
function create_btrfs_centric_layout( ) {
2020-10-03 14:40:47 +00:00
local known_arguments = ( '+swap' '?type' '?raid_type' '?luks' )
2020-10-03 14:24:48 +00:00
local extra_arguments = ( )
declare -A arguments; parse_arguments " $@ "
[ [ ${# extra_arguments [@] } -gt 0 ] ] \
|| die_trace 1 "Expected at least one positional argument (the devices)"
local device = " ${ extra_arguments [0] } "
local size_swap = " ${ arguments [swap] } "
2020-10-11 19:45:26 +00:00
local raid_type = " ${ arguments [raid_type] :- raid0 } "
2021-04-28 14:06:52 +00:00
local type = " ${ arguments [type] :- efi } "
2020-10-03 16:28:55 +00:00
local use_luks = " ${ arguments [luks] :- false } "
2020-10-03 14:24:48 +00:00
# Create layout on first disk
create_gpt new_id = "gpt_dev0" device = " ${ extra_arguments [0] } "
create_partition new_id = " part_ ${ type } _dev0 " id = "gpt_dev0" size = 256MiB type = " $type "
2021-04-22 19:09:11 +00:00
[ [ $size_swap != "false" ] ] \
&& create_partition new_id = "part_swap_dev0" id = "gpt_dev0" size = " $size_swap " type = swap
2020-10-28 23:19:45 -01:00
create_partition new_id = "part_root_dev0" id = "gpt_dev0" size = remaining type = linux
2020-10-03 14:24:48 +00:00
2020-10-03 14:40:47 +00:00
local root_id
2020-10-03 14:24:48 +00:00
local root_ids = ""
if [ [ " $use_luks " = = "true" ] ] ; then
create_luks new_id = luks_dev0 name = "luks_root_0" id = part_root_dev0
2020-10-03 14:40:47 +00:00
root_id = "luks_dev0"
2020-10-03 14:24:48 +00:00
root_ids = " ${ root_ids } luks_dev0; "
for i in " ${ !extra_arguments[@] } " ; do
[ [ $i != 0 ] ] || continue
create_luks new_id = " luks_dev $i " name = " luks_root_ $i " device = " ${ extra_arguments [ $i ] } "
root_ids = " ${ root_ids } luks_dev $i ; "
done
else
local dev_id = ""
2020-10-03 14:40:47 +00:00
root_id = "part_root_dev0"
2020-10-03 14:24:48 +00:00
root_ids = " ${ root_ids } part_root_dev0; "
for i in " ${ !extra_arguments[@] } " ; do
[ [ $i != 0 ] ] || continue
dev_id = " root_dev $i "
2020-10-03 15:59:45 +00:00
create_dummy new_id = " $dev_id " device = " ${ extra_arguments [ $i ] } "
2020-10-03 14:24:48 +00:00
root_ids = " ${ root_ids } $dev_id ; "
done
fi
format id = " part_ ${ type } _dev0 " type = " $type " label = " $type "
2021-04-22 19:09:11 +00:00
[ [ $size_swap != "false" ] ] \
&& format id = "part_swap_dev0" type = swap label = swap
2020-10-11 19:45:26 +00:00
format_btrfs ids = " $root_ids " label = root raid_type = " $raid_type "
2020-10-03 14:24:48 +00:00
if [ [ $type = = "efi" ] ] ; then
DISK_ID_EFI = " part_ ${ type } _dev0 "
else
DISK_ID_BIOS = " part_ ${ type } _dev0 "
fi
2021-04-22 19:09:11 +00:00
[ [ $size_swap != "false" ] ] \
&& DISK_ID_SWAP = part_swap_dev0
2020-10-03 14:42:03 +00:00
DISK_ID_ROOT = " $root_id "
2020-10-05 20:28:32 +00:00
DISK_ID_ROOT_TYPE = "btrfs"
DISK_ID_ROOT_MOUNT_OPTS = "defaults,noatime,compress=zstd,subvol=/root"
2020-10-03 14:24:48 +00:00
}
2021-04-22 23:33:46 +00:00
2021-04-28 14:06:52 +00:00
function create_btrfs_raid_layout( ) {
2021-04-22 23:33:46 +00:00
die "'create_btrfs_raid_layout' is deprecated, please use 'create_btrfs_centric_layout' instead. It is fully option-compatible to the old version."
}