249 lines
No EOL
6.5 KiB
Bash
Executable file
249 lines
No EOL
6.5 KiB
Bash
Executable file
#!/bin/bash
|
|
### Konstanten ###
|
|
|
|
RED=$()
|
|
|
|
### DEFAULTS ###
|
|
|
|
DEFAULT_DEVICE="/dev/sda"
|
|
DEFAULT_NAME="ArchOS"
|
|
DEFAULT_VERSION="0.1"
|
|
DEFAULT_ESP_SIZE="512M"
|
|
DEFAULT_ESP_LABEL="EFI System Partition"
|
|
DEFAULT_SWAP_SIZE="1G"
|
|
DEFAULT_ROOT_SIZE="5G"
|
|
DEFAULT_USR_SIZE="10G"
|
|
DEFAULT_USRHASH_SIZE="1G"
|
|
|
|
### Helper ###
|
|
|
|
# shellcheck disable=SC2059 # $1 and $2 can contain the printf modifiers
|
|
out() { printf "$1 $2\n" "${@:3}"; }
|
|
error() { out "==> ERROR:" "$@"; } >&2
|
|
warning() { out "==> WARNING:" "$@"; } >&2
|
|
msg() { out "==>" "$@"; }
|
|
die() { error "${@:2}"; exit "$1"; }
|
|
|
|
### Funktionen ###
|
|
|
|
help () {
|
|
echo "Aufruf:"
|
|
echo " ${0##*/} [Optionen]"
|
|
echo ""
|
|
echo "Erstellen eines Systems mit Read-Only /usr-Baum"
|
|
echo ""
|
|
echo "Optionen:"
|
|
echo " -f Partitionen erstellen, auch wenn sie vorhanden sind"
|
|
echo " -d <device> Dieses Gerät als Basis verwenden [${DEFAULT_DEVICE}]"
|
|
echo " -v <system-version> Version des neuen Systemes [${DEFAULT_VERSION}]"
|
|
echo " -n <name> Name des neuen Systems [${DEFAULT_NAME}]"
|
|
echo " -r <size> Größe der Root-Partition [${DEFAULT_ROOT_SIZE}]"
|
|
echo " -s <size> Größe der Swap-Partition [${DEFAULT_SWAP_SIZE}]"
|
|
echo " -u <size> Größe der Usr-Partition [${DEFAULT_USR_SIZE}]"
|
|
echo " -h Diese Hilfe anzeigen"
|
|
echo ""
|
|
echo "Exit-Code:"
|
|
echo " 1: Ein Fehler ist aufgetreten"
|
|
echo " 2: Üngültiges Argument"
|
|
echo " 3: Gerät existiert nicht"
|
|
echo " 10: Noch nicht implementiert"
|
|
echo ""
|
|
exit 0
|
|
}
|
|
|
|
# erase <Gerät>
|
|
erase () {
|
|
if [[ "$#" -eq 1 ]]; then
|
|
echo "dd if=/dev/zero of=$1 status=progress"
|
|
#dd if=/dev/zero of="$1" status=progress
|
|
echo "sgdisk -og $1"
|
|
#sgdisk -og "$1"
|
|
else
|
|
die 1 "Kein Gerät zum Löschen angegeben"
|
|
fi
|
|
}
|
|
|
|
# create <Type>
|
|
create () {
|
|
case $1 in
|
|
"USR")
|
|
echo "sgdisk -n 0:0:+${USR_SIZE} -t 0:${GUIDS[USR]} -c 0:${USR_LABEL} ${DRIVE}"
|
|
#sgdisk -n 0:0:+"${USR_SIZE}" -t 0:"${GUIDS[USR]}" -c 0:"${USR_LABEL}" "${DRIVE}"
|
|
;;
|
|
"ROOT")
|
|
echo "sgdisk -n 0:0:+${ROOT_SIZE} -t 0:${GUIDS[ROOT]} -c 0:${ROOT_LABEL} ${DRIVE}"
|
|
#sgdisk -n 0:0:+"${ROOT_SIZE}" -t 0:"${GUIDS[ROOT]}" -c 0:"${ROOT_LABEL}" "${DRIVE}"
|
|
;;
|
|
"USRHASH")
|
|
echo "sgdisk -n 0:0:+${USRHASH_SIZE} -t 0:${GUIDS[USRHASH]} -c 0:${USRHASH_LABEL} ${DRIVE}"
|
|
#sgdisk -n 0:0:+"${USRHASH_SIZE}" -t 0:"${GUIDS[USRHASH]}" -c 0:"${USRHASH_LABEL}" "${DRIVE}"
|
|
;;
|
|
"ESP")
|
|
echo "sgdisk -n 0:0:+${ESP_SIZE} -t 0:${GUIDS[ESP]} -c 0:${ESP_LABEL} ${DRIVE}"
|
|
#sgdisk -n 0:0:+"${ESP_SIZE}" -t 0:"${GUIDS[ESP]}" -c 0:"${ESP_LABEL}" "${DRIVE}"
|
|
;;
|
|
"SWAP")
|
|
echo "sgdisk -n 0:0:+${SWAP_SIZE} -t 0:${GUIDS[SWAP]} -c 0:${SWAP_LABEL} ${DRIVE}"
|
|
#sgdisk -n 0:0:+"${SWAP_SIZE}" -t 0:"${GUIDS[SWAP]}" -c 0:"${SWAP_LABEL}" "${DRIVE}"
|
|
;;
|
|
"TABLE")
|
|
echo "sgdisk -og ${DRIVE}"
|
|
#sgdisk -og "${DRIVE}"
|
|
;;
|
|
*)
|
|
die 1 "Unbekannte Partition zum Erstellen angegeben"
|
|
;;
|
|
esac
|
|
}
|
|
|
|
# existing_partitions <Gerät> [<Type>]
|
|
existing_partitions () {
|
|
if [[ $# -eq 1 ]] # Genereller test auf Partitionen
|
|
then
|
|
echo "yes" # Partitionen vorhanden
|
|
#echo "no" # Partitionen nicht vorhanden
|
|
elif [[ $# -eq 2 ]] # Test auf eine bestimmte Partition
|
|
then
|
|
#echo "yes" # bestimmte Partition vorhanden
|
|
echo "no" # bestimmte Partition nicht vorhanden
|
|
else
|
|
die 1 "Kein Gerät zur Prüfung auf Partitionen angegeben"
|
|
fi
|
|
}
|
|
|
|
# format
|
|
format () {
|
|
if [[ "${FORCE}" == "yes" ]]
|
|
then
|
|
erase "${DRIVE}"
|
|
fi
|
|
|
|
EXISTING_PARTITIONS=$(existing_partitions "${DRIVE}")
|
|
if [[ "${EXISTING_PARTITIONS}" == "no" ]]
|
|
then
|
|
create "TABLE"
|
|
create "ESP"
|
|
create "USR"
|
|
create "ROOT"
|
|
create "USRHASH"
|
|
create "SWAP"
|
|
else
|
|
read -r -p "Partitionen überschreiben? [N/y]: " ask_overwrite
|
|
ask_overwrite=${ask_overwrite:="n"}
|
|
if [[ ${ask_overwrite} == [Nn] ]]
|
|
then
|
|
die 10 "Noch nicht implementiert"
|
|
EXISTING_ESP=$(existing_partitions "${DRIVE}" "ESP")
|
|
if [[ "${EXISTING_ESP}" == "no" ]]
|
|
then
|
|
create "ESP"
|
|
fi
|
|
else
|
|
erase "${DRIVE}"
|
|
create "TABLE"
|
|
create "ESP"
|
|
create "USR"
|
|
create "ROOT"
|
|
create "USRHASH"
|
|
create "SWAP"
|
|
fi
|
|
fi
|
|
}
|
|
|
|
### Start ###
|
|
|
|
# Optionen auslesen
|
|
while getopts :hfd:v:n:r:s:u: opt
|
|
do
|
|
case $opt in
|
|
h)
|
|
help
|
|
;;
|
|
f)
|
|
FORCE="yes"
|
|
;;
|
|
d)
|
|
DEVICE="$OPTARG"
|
|
;;
|
|
v)
|
|
VERSION=$OPTARG
|
|
;;
|
|
n)
|
|
NAME=$OPTARG
|
|
;;
|
|
r)
|
|
ROOT_SIZE=$OPTARG
|
|
;;
|
|
s)
|
|
SWAP_SIZE=$OPTARG
|
|
;;
|
|
u)
|
|
USR_SIZE=$OPTARG
|
|
;;
|
|
:)
|
|
die 2 '%s: Option benötigt ein Argument -- '\''%s'\' "${0##*/}" "$OPTARG"
|
|
;;
|
|
?)
|
|
die 2 '%s: Ungültige Option -- '\''%s'\' "${0##*/}" "$OPTARG"
|
|
;;
|
|
esac
|
|
done
|
|
|
|
DEVICE=${DEVICE:="${DEFAULT_DEVICE}"}
|
|
FORCE=${FORCE:="no"}
|
|
NAME=${NAME:="${DEFAULT_NAME}"}
|
|
VERSION=${VERSION:="${DEFAULT_VERSION}"}
|
|
ESP_SIZE="${DEFAULT_ESP_SIZE}"
|
|
ESP_LABEL="${DEFAULT_ESP_LABEL}"
|
|
SWAP_SIZE=${SWAP_SIZE:="${DEFAULT_SWAP_SIZE}"}
|
|
SWAP_LABEL="${NAME} Swap"
|
|
ROOT_SIZE=${ROOT_SIZE:="${DEFAULT_ROOT_SIZE}"}
|
|
ROOT_LABEL="${NAME} Root"
|
|
USR_SIZE=${USR_SIZE:="${DEFAULT_USR_SIZE}"}
|
|
USR_LABEL="${NAME}_${VERSION}"
|
|
USRHASH_SIZE=${USRHASH_SIZE:="${DEFAULT_USRHASH_SIZE}"}
|
|
USRHASH_LABEL="${NAME}_${VERSION}"
|
|
|
|
#--------------------------------------#
|
|
|
|
declare -A GUIDS=""
|
|
GUIDS=([ESP]="c12a7328-f81f-11d2-ba4b-00a0c93ec93b" [USR]="8484680c-9521-48c6-9c11-b0720656f69e" [USRHASH]="77ff5f63-e7b6-4633-acf4-1565b864c0e6" [ROOT]="4f68bce3-e8cd-4db1-96e7-fbcaf984b709" [SWAP]="0657fd6d-a4ab-43c4-84e5-0933c84b4f4f")
|
|
|
|
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
|
|
|
|
echo "### DEBUG ###"
|
|
echo "DEVICE: ${DEVICE}"
|
|
echo "FORCE: ${FORCE}"
|
|
echo "NAME: ${NAME}"
|
|
echo "VERSION: ${VERSION}"
|
|
echo "ESP_SIZE: ${ESP_SIZE}"
|
|
echo "ESP_LABEL: ${ESP_LABEL}"
|
|
echo "USR_SIZE: ${USR_SIZE}"
|
|
echo "USR_LABEL: ${USR_LABEL}"
|
|
echo "USRHASH_SIZE: ${USRHASH_SIZE}"
|
|
echo "USRHASH_LABEL: ${USRHASH_LABEL}"
|
|
echo "ROOT_SIZE: ${ROOT_SIZE}"
|
|
echo "ROOT_LABEL: ${ROOT_LABEL}"
|
|
echo "SWAP_SIZE: ${SWAP_SIZE}"
|
|
echo "SWAP_LABEL: ${SWAP_LABEL}"
|
|
echo "### DEBUG ###"
|
|
|
|
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
|
|
|
|
|
|
|
|
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
|
|
|
|
# Prüfen, ob DEVICE existiert
|
|
|
|
#CHECK_PARTITION_EXISTENCE_OUTPUT=$(lsblk -pldo NAME | grep -ic "${DRIVE}")
|
|
|
|
#if ! lsblk -d "${DEVICE}" > /dev/null 2>&1 ; then
|
|
if [ ! -b "${DEVICE}" ]; then
|
|
die 3 "Gerät existiert nicht, Abbruch!"
|
|
fi
|
|
|
|
# Prüfen, ob das Script mit Root-Rechten ausgeführt wird
|
|
#(( EUID == 0)) || die 3 "Dieses Script muss mit Root-Privilegien aufgerufen werden"
|
|
|
|
format |