Skip to content

Commit

Permalink
In BTRFS filesystem create the swap file in its own subvolume
Browse files Browse the repository at this point in the history
  • Loading branch information
picodotdev committed Dec 17, 2021
1 parent 0450ed0 commit fe1dc1e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
6 changes: 3 additions & 3 deletions alis.conf
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ LOG="false"
## LVM sets if the DEVICE is partitioned with LVM partition scheme.
## LUKS_PASSWORD and LUKS_PASSWORD_RETYPE if set is the password for encrypt the device. With LVM, LVM on LUKS is used. If "ask" password alis asks for a secure password.
## FILE_SYSTEM_TYPE root device is formatted with this file system type.
## BTRFS_SUBVOLUMES_MOUNTPOINTS allows to customize btrfs file system subvolumes mountpoints.
## BTRFS_SUBVOLUMES_MOUNTPOINTS allows to customize btrfs file system subvolumes mountpoints. swap sobvolume is created only if swap file is created (only if there is SWAP_SIZE value).
### Format ("name,subvolume,mount_point"))
## SWAP_SIZE swap file size with a size of the value in megabytes. If no value no swap file is created.
## PARTITION_MODE how partition is performed on DEVICE.
Expand All @@ -34,8 +34,8 @@ LVM="false"
LUKS_PASSWORD=""
LUKS_PASSWORD_RETYPE=""
FILE_SYSTEM_TYPE="ext4 !btrfs !xfs !f2fs !reiserfs" # (single)
BTRFS_SUBVOLUMES_MOUNTPOINTS=("root,root,/" "home,home,/home" "var,var,/var" "snapshots,snapshots,/snapshots")
#BTRFS_SUBVOLUMES_MOUNTPOINTS=("root,@,/" "home,@home,/home" "var,@var,/var" "snapshots,@snapshots,/.snapshots")
#BTRFS_SUBVOLUMES_MOUNTPOINTS=("root,root,/" "home,home,/home" "var,var,/var" "snapshots,snapshots,/snapshots" "swap,swap,/swap")
BTRFS_SUBVOLUMES_MOUNTPOINTS=("root,@,/" "home,@home,/home" "var,@var,/var" "snapshots,@snapshots,/.snapshots" "swap,@swap,/swap")
SWAP_SIZE="!2048 !4096 !8192" # (single)
PARTITION_MODE="auto !custom !manual"
PARTITION_CUSTOM_PARTED_UEFI="mklabel gpt mkpart ESP fat32 1MiB 512MiB mkpart root $FILE_SYSTEM_TYPE 512MiB 100% set 1 esp on"
Expand Down
42 changes: 32 additions & 10 deletions alis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ GPU_VENDOR=""
VIRTUALBOX=""
CMDLINE_LINUX_ROOT=""
CMDLINE_LINUX=""
BTRFS_SUBVOLUME_ROOT=""
BTRFS_SUBVOLUME_ROOT=()
BTRFS_SUBVOLUME_SWAP=()

CONF_FILE="alis.conf"
GLOBALS_FILE="alis-globals.conf"
Expand Down Expand Up @@ -106,10 +107,11 @@ function sanitize_variables() {

for I in "${BTRFS_SUBVOLUMES_MOUNTPOINTS[@]}"; do
IFS=',' SUBVOLUME=($I)
if [ ${SUBVOLUME[0]} != "root" ]; then
continue
if [ ${SUBVOLUME[0]} == "root" ]; then
BTRFS_SUBVOLUME_ROOT=("${SUBVOLUME[@]}")
elif [ ${SUBVOLUME[0]} == "swap" ]; then
BTRFS_SUBVOLUME_SWAP=("${SUBVOLUME[@]}")
fi
BTRFS_SUBVOLUME_ROOT=("${SUBVOLUME[@]}")
done
}

Expand Down Expand Up @@ -137,7 +139,15 @@ function check_variables() {
check_variables_boolean "LVM" "$LVM"
check_variables_equals "LUKS_PASSWORD" "LUKS_PASSWORD_RETYPE" "$LUKS_PASSWORD" "$LUKS_PASSWORD_RETYPE"
check_variables_list "FILE_SYSTEM_TYPE" "$FILE_SYSTEM_TYPE" "ext4 btrfs xfs f2fs reiserfs"
check_variables_value "BTRFS_SUBVOLUME_ROOT" "$BTRFS_SUBVOLUME_ROOT"
check_variables_size "BTRFS_SUBVOLUME_ROOT" ${#BTRFS_SUBVOLUME_ROOT[@]} 3
check_variables_list "BTRFS_SUBVOLUME_ROOT" "${BTRFS_SUBVOLUME_ROOT[2]}" "/"
if [ -n "$SWAP_SIZE" ]; then
check_variables_size "BTRFS_SUBVOLUME_SWAP" ${#BTRFS_SUBVOLUME_SWAP[@]} 3
fi
for I in "${BTRFS_SUBVOLUMES_MOUNTPOINTS[@]}"; do
IFS=',' SUBVOLUME=($I)
check_variables_size "SUBVOLUME" ${#SUBVOLUME[@]} 3
done
check_variables_list "PARTITION_MODE" "$PARTITION_MODE" "auto custom manual" "true"
if [ "$PARTITION_MODE" == "custom" ]; then
check_variables_value "PARTITION_CUSTOM_PARTED_UEFI" "$PARTITION_CUSTOM_PARTED_UEFI"
Expand Down Expand Up @@ -669,23 +679,34 @@ function partition() {

# mount
if [ "$FILE_SYSTEM_TYPE" == "btrfs" ]; then
# create subvolumes
mount -o "$PARTITION_OPTIONS" "$DEVICE_ROOT" /mnt
for I in "${BTRFS_SUBVOLUMES_MOUNTPOINTS[@]}"; do
IFS=',' SUBVOLUME=($I)
if [ ${SUBVOLUME[0]} == "swap" -a -z "$SWAP_SIZE" ]; then
continue
fi
btrfs subvolume create "/mnt/${SUBVOLUME[1]}"
done
umount /mnt

mount -o "subvol=${BTRFS_SUBVOLUME_ROOT[1]},$PARTITION_OPTIONS,compress=zstd" "$DEVICE_ROOT" "/mnt${BTRFS_SUBVOLUME_ROOT[2]}"

mkdir /mnt/boot
mount -o "$PARTITION_OPTIONS_BOOT" "$PARTITION_BOOT" /mnt/boot
# mount subvolumes
mount -o "subvol=${BTRFS_SUBVOLUME_ROOT[1]},$PARTITION_OPTIONS,compress=zstd" "$DEVICE_ROOT" "/mnt"
mkdir "/mnt/boot"
mount -o "$PARTITION_OPTIONS_BOOT" "$PARTITION_BOOT" "/mnt/boot"
for I in "${BTRFS_SUBVOLUMES_MOUNTPOINTS[@]}"; do
IFS=',' SUBVOLUME=($I)
if [ ${SUBVOLUME[0]} == "root" ]; then
continue
fi
mkdir "/mnt${SUBVOLUME[2]}"
if [ ${SUBVOLUME[0]} == "swap" -a -z "$SWAP_SIZE" ]; then
continue
fi
if [ ${SUBVOLUME[0]} == "swap" ]; then
mkdir -m 0700 "/mnt${SUBVOLUME[2]}"
else
mkdir "/mnt${SUBVOLUME[2]}"
fi
mount -o "subvol=${SUBVOLUME[1]},$PARTITION_OPTIONS_ROOT,compress=zstd" "$DEVICE_ROOT" "/mnt${SUBVOLUME[2]}"
done
else
Expand All @@ -698,6 +719,7 @@ function partition() {
# swap
if [ -n "$SWAP_SIZE" ]; then
if [ "$FILE_SYSTEM_TYPE" == "btrfs" ]; then
SWAPFILE="${BTRFS_SUBVOLUME_SWAP[2]}$SWAPFILE"
truncate -s 0 /mnt$SWAPFILE
chattr +C /mnt$SWAPFILE
btrfs property set /mnt$SWAPFILE compression none
Expand Down

0 comments on commit fe1dc1e

Please sign in to comment.