PortValhalla/scripts/Common/OS/partition.sh

109 lines
2.3 KiB
Bash
Executable file

#!/bin/bash
pushd "${BASH_SOURCE%/*}" > /dev/null;
source "../Scripts/hooks.sh";
source "../Scripts/is-truthy.sh";
source "../Scripts/choose-disk.sh";
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"}";
SKIP_FORMAT_EFI="${SKIP_FORMAT_EFI}";
memory="$(cat /proc/meminfo | awk -F " " '/^MemTotal/ { print $2 }')";
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" ]
then
chooseDisk OS_DISK "Which disk do you wish to format for installing the OS?";
fi;
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" \
runHook initializePartitions \
partitionDisk;
while true
do
disks=($(bash -c "echo $OS_DISK*"));
[ "${#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");
if ! isTruthy "$SKIP_FORMAT_EFI"
then
mkfs.fat -F 32 -n "$BOOT_LABEL" "$bootDisk";
labels+=("$BOOT_LABEL");
fi
mkswap -L "$SWAP_LABEL" "$swapDisk";
mkfs.ext4 -L "$OS_LABEL" -F "$linuxDisk";
for label in ${labels[*]};
do
while true;
do
[ -b "/dev/disk/by-label/$label" ] && break;
done;
done
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";
swapon "/dev/disk/by-label/$SWAP_LABEL";
if command -v extraMounts
then
extraMounts;
fi;
popd > /dev/null;