Den Code zum erstellen refactort, um nicht notwendigerweise jedesmal die Partitionen und die Dateisysteme neu zu erstellen.

This commit is contained in:
eichehome 2023-07-23 20:18:58 +02:00
parent 2b440920fe
commit e8b2710514

View file

@ -70,7 +70,10 @@ fi
DRIVE="/dev/vda" DRIVE="/dev/vda"
# disklayout: ESP (512MiB), usr (15GiB), Root (100% - [512MiB + 15GiB]) # disklayout: ESP (512MiB), usr (15GiB), Root (100% - [512MiB + 15GiB])
ESP="512M" ESP="512M"
USR="15G" USR="10G"
ROOT_LABEL="root"
USR_LABEL="usr"
ESP_LABEL="EFI"
#--------------------------# #--------------------------#
@ -84,92 +87,203 @@ USR="15G"
# /usr /dev/vda2 # /usr /dev/vda2
# (usr-hash) /dev/vda3 # (usr-hash) /dev/vda3
# /(root) /dev/vda4 # /(root) /dev/vda4
# Reset Partition Table
sgdisk -og "${DRIVE}"
# Create EFI System Partition
sgdisk -n 0:0:+"${ESP}" -t 0:c12a7328-f81f-11d2-ba4b-00a0c93ec93b -c 0:"EFI System Partition" "${DRIVE}"
#sgdisk -n 0:0:+512M -t 0:ef00 -c 0:ESP "${DRIVE}"
# Create usr-Partition
sgdisk -n 0:0:+"${USR}" -t 0:8484680c-9521-48c6-9c11-b0720656f69e -c 0:"Usr-Partition" "${DRIVE}"
# Create usrhash-Partition
sgdisk -n 0:0:+512M -t 0:77ff5f63-e7b6-4633-acf4-1565b864c0e6 -c 0:"usrhash-Partition" "${DRIVE}"
# Create Root-Partition
sgdisk -n 0:0:0 -t 0:4f68bce3-e8cd-4db1-96e7-fbcaf984b709 -c 0:"System Root" "${DRIVE}"
# UUIDs finden #LSBLK_OPTIONEN="NAME,PARTUUID,UUID,PARTTYPE,PARTN,LABEL,MOUNTPOINTS"
UUID_ESP=""
UUID_ROOT=""
UUID_USR=""
UUID_USRHASH=""
# Anzahl der Partitionen ermitteln # Prüfen ob die Platte existiert
DRIVE_NAME=${DRIVE//\/dev\/} #declare -i CHECK_EXISTENCE_OUTPUT=""
declare -i NUMBER_OF_PARTITIONS; CHECK_EXISTENCE_OUTPUT=$(lsblk -o NAME | grep -c "^${DRIVE//\/dev\/}")
NUMBER_OF_PARTITIONS=$(lsblk | grep -c "${DRIVE_NAME}[0-9]") if [[ ${CHECK_EXISTENCE_OUTPUT} -lt 1 ]]; then
declare -i UNKNOWN_PARTITIONS echo "Platte existiert nicht, breche ab."
exit 0
NOCASEMATCH_UNSET=$(shopt -u | grep -c nocasematch)
if [[ $NOCASEMATCH_UNSET ]]; then
shopt -s nocasematch
fi fi
for (( i=NUMBER_OF_PARTITIONS; i > 0 ; i=i-1 )) ; do RECREATE_PARTITIONS="1"
OUTPUT=$(sgdisk -i "${i}" "${DRIVE}")
PARTITION_EXISTS=$(echo "${OUTPUT}" | grep -c "GUID") # Prüfe ob die Platte schon formatiert wurde und die Partitionen da sind
if [[ "${PARTITION_EXISTS}" != "0" ]]; then CHECK_PARTITIONS_OUTPUT=$(lsblk -o NAME,PARTTYPE | grep "${DRIVE//\/dev\/}" | grep "\(c12a7328-f81f-11d2-ba4b-00a0c93ec93b\|8484680c-9521-48c6-9c11-b0720656f69e\|77ff5f63-e7b6-4633-acf4-1565b864c0e6\|4f68bce3-e8cd-4db1-96e7-fbcaf984b709\)")
GUID_CODE=$(echo "${OUTPUT}" | grep "GUID code:" | cut -d ' ' -f 4) CHECK_ROOT=$(echo "${CHECK_PARTITIONS_OUTPUT}" | grep -c "4f68bce3-e8cd-4db1-96e7-fbcaf984b709")
UUID=$(echo "${OUTPUT}" | grep "unique GUID:" | cut -d ' ' -f 4) CHECK_ESP=$(echo "${CHECK_PARTITIONS_OUTPUT}" | grep -c "c12a7328-f81f-11d2-ba4b-00a0c93ec93b")
case $GUID_CODE in CHECK_USR=$(echo "${CHECK_PARTITIONS_OUTPUT}" | grep -c "8484680c-9521-48c6-9c11-b0720656f69e")
"8484680c-9521-48c6-9c11-b0720656f69e" ) CHECK_USRHASH=$(echo "${CHECK_PARTITIONS_OUTPUT}" | grep -c "77ff5f63-e7b6-4633-acf4-1565b864c0e6")
echo "USR-Partition, UUID: ${UUID}" if [[ ${CHECK_ESP} -eq 1 && ${CHECK_USR} -eq 1 && ${CHECK_USRHASH} -eq 1 && ${CHECK_ROOT} -eq 1 ]]; then
UUID_USR=${UUID} read -r "Alle Partitionen sind vorhanden. Trotzdem neu erstellen? (j/N): " confirm
;; if [[ "${confirm}" == [jJ] ]];then
"c12a7328-f81f-11d2-ba4b-00a0c93ec93b" ) echo "Partitionen werden neu erstellt"
echo "ESP-Partition, UUID: ${UUID}" RECREATE_PARTITIONS="1"
UUID_ESP=${UUID}
;;
"4f68bce3-e8cd-4db1-96e7-fbcaf984b709" )
echo "Root-Partition, UUID: ${UUID}"
UUID_ROOT=${UUID}
;;
"77ff5f63-e7b6-4633-acf4-1565b864c0e6" )
echo "Usrhash-Partition, UUID: ${UUID}"
UUID_USRHASH=${UUID}
;;
* )
echo "Unbekannte Partition"
UNKNOWN_PARTITIONS="${UNKNOWN_PARTITIONS}+1"
;;
esac
else else
echo "no partition found" echo "Partitionen werden nicht neu erstellt"
RECREATE_PARTITIONS="0"
fi fi
NUMBER_OF_PARTITIONS="${NUMBER_OF_PARTITIONS}-1" fi
if [[ ${RECREATE_PARTITIONS} -eq 1 ]]; then
# Reset Partition Table
sgdisk -og "${DRIVE}"
# EFI System Partition erstellen
sgdisk -n 0:0:+"${ESP}" -t 0:c12a7328-f81f-11d2-ba4b-00a0c93ec93b -c 0:"EFI System Partition" "${DRIVE}"
#sgdisk -n 0:0:+512M -t 0:ef00 -c 0:ESP "${DRIVE}"
# usr-Partition erstellen
sgdisk -n 0:0:+"${USR}" -t 0:8484680c-9521-48c6-9c11-b0720656f69e -c 0:"ArchOS_0.1" "${DRIVE}"
# usrhash-Partition erstellen
sgdisk -n 0:0:+512M -t 0:77ff5f63-e7b6-4633-acf4-1565b864c0e6 -c 0:"ArchOS_0.1" "${DRIVE}"
# Root-Partition erstellen
sgdisk -n 0:0:0 -t 0:4f68bce3-e8cd-4db1-96e7-fbcaf984b709 -c 0:"ArchOS Root" "${DRIVE}"
fi
# # UUIDs finden
# UUID_ESP=""
# UUID_ROOT=""
# UUID_USR=""
# UUID_USRHASH=""
# # Anzahl der Partitionen ermitteln
# DRIVE_NAME=${DRIVE//\/dev\/}
# declare -i NUMBER_OF_PARTITIONS;
# NUMBER_OF_PARTITIONS=$(lsblk | grep -c "${DRIVE_NAME}[0-9]")
# declare -i UNKNOWN_PARTITIONS
# NOCASEMATCH_UNSET=$(shopt -u | grep -c nocasematch)
# if [[ $NOCASEMATCH_UNSET ]]; then
# shopt -s nocasematch
# fi
# for (( i=NUMBER_OF_PARTITIONS; i > 0 ; i=i-1 )) ; do
# OUTPUT=$(sgdisk -i "${i}" "${DRIVE}")
# PARTITION_EXISTS=$(echo "${OUTPUT}" | grep -c "GUID")
# if [[ "${PARTITION_EXISTS}" != "0" ]]; then
# GUID_CODE=$(echo "${OUTPUT}" | grep "GUID code:" | cut -d ' ' -f 4)
# UUID=$(echo "${OUTPUT}" | grep "unique GUID:" | cut -d ' ' -f 4)
# case $GUID_CODE in
# "8484680c-9521-48c6-9c11-b0720656f69e" )
# echo "USR-Partition, UUID: ${UUID}"
# UUID_USR=${UUID}
# ;;
# "c12a7328-f81f-11d2-ba4b-00a0c93ec93b" )
# echo "ESP-Partition, UUID: ${UUID}"
# UUID_ESP=${UUID}
# ;;
# "4f68bce3-e8cd-4db1-96e7-fbcaf984b709" )
# echo "Root-Partition, UUID: ${UUID}"
# UUID_ROOT=${UUID}
# ;;
# "77ff5f63-e7b6-4633-acf4-1565b864c0e6" )
# echo "Usrhash-Partition, UUID: ${UUID}"
# UUID_USRHASH=${UUID}
# ;;
# * )
# echo "Unbekannte Partition"
# UNKNOWN_PARTITIONS="${UNKNOWN_PARTITIONS}+1"
# ;;
# esac
# else
# echo "no partition found"
# fi
# NUMBER_OF_PARTITIONS="${NUMBER_OF_PARTITIONS}-1"
# done
# if [[ $NOCASEMATCH_UNSET ]]; then
# shopt -u nocasematch
# fi
# if [[ "${UNKNOWN_PARTITIONS}" -gt "0" ]]; then
# if [[ "${UNKNOWN_PARTITIONS}" -gt "1" ]]; then
# read -r -p "Mehrere Partitionen unbekannt. Trotzdem fortfahren? (j/N): " confirm
# [[ "${confirm}" == [jJ] ]] || echo "Abbrechen" && exit 0
# else
# read -r -p "Eine Partition unbekannt. Trotzdem fortfahren? (j/N): " confirm
# [[ "${confirm}" == [jJ] ]] || echo "Abbrechen" && exit 0
# fi
# fi
# # Großbuchstaben in UUIDs zu kleinbuchstaben umwandeln
# UUID_ESP="${UUID_ESP@L}"
# UUID_ROOT="${UUID_ROOT@L}"
# UUID_USR="${UUID_USR@L}"
# UUID_USRHASH="${UUID_USRHASH@L}"
# Informationen aus den Partitionen auslesen
PARTITIONEN_OUTPUT=$(lsblk -o NAME,PARTTYPE,PARTUUID,UUID,PARTN,LABEL,MOUNTPOINTS | grep "${DRIVE//\/dev\/}[0-9]" | sed 's/[[:blank:]]\+/ /g')
echo "PARTITIONEN_OUTPUT:"
echo "${PARTITIONEN_OUTPUT}"
PARTITION_NUMBERS=$(echo "${PARTITIONEN_OUTPUT}" | cut -d ' ' -f 5)
echo "Partition-Numbers:"
echo "${PARTITION_NUMBERS}"
declare -A ROOT_PARTITION=()
declare -A ESP_PARTITION=()
declare -A USR_PARTITION=()
declare -A USRHASH_PARTITION=()
for i in ${PARTITION_NUMBERS}; do
PARTN=$(echo "${PARTITIONEN_OUTPUT}" | grep "${DRIVE//\/dev\/}${i}" | cut -d ' ' -f 5)
PARTUUID=$(echo "${PARTITIONEN_OUTPUT}" | grep "${DRIVE//\/dev\/}${i}" | cut -d ' ' -f 3)
PARTTYPE=$(echo "${PARTITIONEN_OUTPUT}" | grep "${DRIVE//\/dev\/}${i}" | cut -d ' ' -f 2)
case $PARTTYPE in
"c12a7328-f81f-11d2-ba4b-00a0c93ec93b" )
echo "ESP"
ESP_PARTITION+=([PARTUUID]="${PARTUUID}")
ESP_PARTITION+=([PARTTYPE]="${PARTTYPE}")
ESP_PARTITION+=([PARTNUMBER]="${PARTN}")
;;
"8484680c-9521-48c6-9c11-b0720656f69e" )
echo "USR"
USR_PARTITION+=([PARTUUID]="${PARTUUID}")
USR_PARTITION+=([PARTTYPE]="${PARTTYPE}")
USR_PARTITION+=([PARTNUMBER]="${PARTN}")
;;
"77ff5f63-e7b6-4633-acf4-1565b864c0e6")
echo "USRHASH"
USRHASH_PARTITION+=([PARTUUID]="${PARTUUID}")
USRHASH_PARTITION+=([PARTTYPE]="${PARTTYPE}")
USRHASH_PARTITION+=([PARTNUMBER]="${PARTN}")
;;
"4f68bce3-e8cd-4db1-96e7-fbcaf984b709" )
echo "ROOT"
ROOT_PARTITION+=([PARTUUID]="${PARTUUID}")
ROOT_PARTITION+=([PARTTYPE]="${PARTTYPE}")
ROOT_PARTITION+=([PARTNUMBER]="${PARTN}")
;;
* )
echo "Unknown"
;;
esac
done done
if [[ $NOCASEMATCH_UNSET ]]; then # Prüfen,ob die Dateisysteme schon korrekt sind
shopt -u nocasematch FILESYSTEM_OUTPUT=$(lsblk -o NAME,FSTYPE,FSVER,PARTUUID | grep "${DRIVE//\/dev\/}[0-9]")
fi CHECK_FS_ESP=$(echo "${FILESYSTEM_OUTPUT}" | grep "${ESP_PARTITION["PARTUUID"]}" | grep -i "vfat" | grep -i "fat32" | grep -c -i "${ESP_LABEL}")
CHECK_FS_USR=$(echo "${FILESYSTEM_OUTPUT}" | grep "${USR_PARTITION["PARTUUID"]}" | grep -i "ext4" | grep -c -i "${USR_LABEL}")
CHECK_FS_USRHASH=$(echo "${FILESYSTEM_OUTPUT}" | grep "${USRHASH_PARTITION["PARTUUID"]}" | grep -c -i "dm_verity_hash")
CHECK_FS_ROOT=$(echo "${FILESYSTEM_OUTPUT}" | grep "${ROOT_PARTITION["PARTUUID"]}" | grep -i "ext4" | grep -c -i "${ROOT_LABEL}")
if [[ "${UNKNOWN_PARTITIONS}" > "0" ]]; then RECREATE_FILESYSTEMS="0"
if [[ "${UNKNOWN_PARTITIONS}" > "1" ]]; then if [[ ${CHECK_FS_ESP} -eq 1 && ${CHECK_FS_USR} -eq 1 && ${CHECK_FS_USRHASH} -eq 1 && ${CHECK_FS_ROOT} -eq 1 ]]; then
read -r -p "Mehrere Partitionen unbekannt. Trotzdem fortfahren? (j/N): " confirm echo "Alles vorhanden"
[[ "${confirm}" == [jJ] ]] || echo "Abbrechen" && exit 0 read -r "Alle Dateisysteme vorhanden. Trotzdem neu erstellen? (j/N): " confirm
if [[ ${confirm} == [jJ] ]]; then
echo "Dateisysteme werden neuerstellt."
RECREATE_FILESYSTEMS="1"
else else
read -r -p "Eine Partition unbekannt. Trotzdem fortfahren? (j/N): " confirm echo "Dateisysteme werden nicht neu erstellt."
[[ "${confirm}" == [jJ] ]] || echo "Abbrechen" && exit 0 RECREATE_FILESYSTEMS="0"
fi fi
fi fi
# Großbuchstaben in UUIDs zu kleinbuchstaben umwandeln if [[ (${CHECK_ESP} -eq 0 || ${RECREATE_FILESYSTEMS} -eq 1) && -n ${ESP_PARTITION["PARTUUID"]} ]]; then
UUID_ESP="${UUID_ESP@L}" echo "Erstelle ESP"
UUID_ROOT="${UUID_ROOT@L}" mkfs.vfat -n "${ESP_LABEL}" -F 32 "/dev/disk/by-partuuid/${ESP_PARTITION["PARTUUID"]}"
UUID_USR="${UUID_USR@L}" fi
UUID_USRHASH="${UUID_USRHASH@L}"
mkfs.vfat -n EFI -F 32 "/dev/disk/by-partuuid/${UUID_ESP}" if [[ (${CHECK_USR} -eq 0 || ${RECREATE_FILESYSTEMS} -eq 1) && -n ${USR_PARTITION["PARTUUID"]} ]]; then
mkfs.ext4 -L usr "/dev/disk/by-partuuid/${UUID_USR}" echo "Erstelle USR"
mkfs.ext4 -L root "/dev/disk/by-partuuid/${UUID_ROOT}" mkfs.ext4 -L "${USR_LABEL}" "/dev/disk/by-partuuid/${USR_PARTITION["PARTUUID"]}"
fi
if [[ (${CHECK_ROOT} -eq 0 || ${RECREATE_FILESYSTEMS} -eq 1) && -n ${ROOT_PARTITION["PARTUUID"]} ]]; then
echo "Erstelle ROOT"
mkfs.ext4 -L "${ROOT_LABEL}" "/dev/disk/by-partuuid/${ROOT_PARTITION["PARTUUID"]}"
fi
mount /dev/disk/by-label/root /mnt mount /dev/disk/by-label/root /mnt
mkdir -p /mnt/{boot,usr} mkdir -p /mnt/{boot,usr}