PortValhalla/scripts/Common/OS/partition.sh

110 lines
2.3 KiB
Bash
Raw Normal View History

2023-07-12 20:37:31 +00:00
#!/bin/bash
pushd "${BASH_SOURCE%/*}" > /dev/null;
2024-03-23 16:29:25 +00:00
source "../Scripts/hooks.sh";
source "../Scripts/is-truthy.sh";
source "../Scripts/choose-disk.sh";
2023-11-28 00:22:48 +00:00
OS_MOUNT_ROOT="${OS_ROOT:-"/mnt"}";
OS_DISK="${OS_DISK}";
BOOT_LABEL="${BOOT_LABEL:-Boot}";
OS_LABEL="${OS_LABEL:-Linux}";
SWAP_LABEL="${SWAP_LABEL:-Swap}";
BOOT_MOUNTPOINT="${BOOT_MOUNTPOINT:-"/boot"}";
2023-07-12 21:30:00 +00:00
SKIP_FORMAT_EFI="${SKIP_FORMAT_EFI}";
memory="$(cat /proc/meminfo | awk -F " " '/^MemTotal/ { print $2 }')";
2023-08-07 18:33:51 +00:00
swapSize="$(echo "$memory" | awk '{ print int((($1 / 1024 / 1024) * 0.75) + 0.5)}')";
if [ ! -z "$OS_DISK" ] && [ ! -e "$OS_DISK" ]
then
OS_DISK="/dev/${OS_DISK}"
fi;
if [ -z "$OS_DISK" ] || [ ! -e "$OS_DISK" ]
2023-04-01 11:40:52 +00:00
then
chooseDisk OS_DISK "Which disk do you wish to format for installing the OS?";
2023-04-01 11:40:52 +00:00
fi;
2024-03-23 16:29:25 +00:00
function partitionDisk() {
{
echo "g";
echo "n";
echo "";
echo "";
echo "+1G";
echo "y";
echo "t";
echo "1";
echo "n";
echo "";
echo "";
echo "+${SWAP_SIZE}G";
echo "y";
echo "t";
echo "";
echo "19";
echo "n";
echo "";
echo "";
echo "";
echo "y";
echo "t";
echo "";
echo "23";
echo "w";
} | fdisk "$OS_DISK";
}
OS_DISK="${OS_DISK}" \
SWAP_SIZE="$swapSize" \
2024-03-23 16:29:25 +00:00
runHook initializePartitions \
partitionDisk;
2023-06-09 21:31:02 +00:00
while true
do
disks=($(bash -c "echo $OS_DISK*"));
2023-06-09 21:31:02 +00:00
[ "${#disks[@]}" -ge 4 ] && break;
done;
bootDisk="${BOOT_DISK:-"${disks[1]}"}";
swapDisk="${SWAP_DISK:-"${disks[2]}"}";
linuxDisk="${LINUX_DISK:-"${disks[3]}"}";
labels=("$SWAP_LABEL" "$OS_LABEL");
2023-04-01 01:30:58 +00:00
2023-07-12 21:30:00 +00:00
if ! isTruthy "$SKIP_FORMAT_EFI"
then
mkfs.fat -F 32 -n "$BOOT_LABEL" "$bootDisk";
labels+=("$BOOT_LABEL");
2023-07-12 21:30:00 +00:00
fi
mkswap -L "$SWAP_LABEL" "$swapDisk";
mkfs.ext4 -L "$OS_LABEL" -F "$linuxDisk";
2023-04-01 01:30:58 +00:00
for label in ${labels[*]};
do
while true;
do
[ -b "/dev/disk/by-label/$label" ] && break;
done;
done
2023-11-28 00:22:48 +00:00
mount "/dev/disk/by-label/$OS_LABEL" "$OS_MOUNT_ROOT";
if ! isTruthy "$SKIP_FORMAT_EFI"
then
bootDisk="/dev/disk/by-label/$BOOT_LABEL";
fi
mount --mkdir "$bootDisk" "$OS_MOUNT_ROOT$BOOT_MOUNTPOINT";
2023-11-28 00:22:48 +00:00
swapon "/dev/disk/by-label/$SWAP_LABEL";
2023-04-01 01:15:32 +00:00
2023-08-07 18:29:24 +00:00
if command -v extraMounts
then
extraMounts;
fi;
popd > /dev/null;