From 8ae6e417ea7651a8b257009893285dde87a17879 Mon Sep 17 00:00:00 2001 From: Martin Pietsch <martin.pietsch@tu-dresden.de> Date: Fri, 11 Oct 2019 16:09:49 +0200 Subject: [PATCH] update diskspreparation.sh script --- files/diskspreparation.sh | 268 +++++++++++++++++++++++++++++++------- 1 file changed, 220 insertions(+), 48 deletions(-) diff --git a/files/diskspreparation.sh b/files/diskspreparation.sh index 17db039..bf574a8 100755 --- a/files/diskspreparation.sh +++ b/files/diskspreparation.sh @@ -1,16 +1,16 @@ #!/usr/bin/env bash -export DISK_SDA_SCHEME="msdos" +export DISK_SDA_SCHEME="gpt" declare -a DISK_SDA_PARTITIONS=() -DISK_SDA_PARTITIONS+=("1GB:fat32:/boot:defaults:0:0:False") -DISK_SDA_PARTITIONS+=("12.5GB:ext4:/:defaults:0:0:True") +DISK_SDA_PARTITIONS+=("1GB:fat16:/boot:defaults:0:0:False:boot,esp") +DISK_SDA_PARTITIONS+=("12.5GB:ext4:/:defaults:0:0:True:") export DISK_SDA_PARTITIONS export DISK_SDB_SCHEME="mbr" declare -a DISK_SDB_PARTITIONS=() -DISK_SDB_PARTITIONS+=("12G:swap::defaults:0:0:true") +DISK_SDB_PARTITIONS+=("12GB:linux-swap::defaults:0:0:true") export DISK_SDB_PARTITIONS -declare -a DISK_DEVICES=("sda") +declare -a DISK_DEVICES=("sda" "sdb") export DISK_DEVICES # convertSizeToByte - converts a given size to byte @@ -23,43 +23,43 @@ convertSizeToByte() { local value=$(echo $1 | sed "s/[a-zA-Z%]*//g") local factor=1 - case ${unit} in - "B") factor=1;; - "kB") factor=1000 ;; - "MB") factor=1000000 ;; - "GB") factor=1000000000 ;; - "TB") factor=1000000000000 ;; - "kiB") factor=1024 ;; - "MiB") factor=1048576 ;; - "GiB") factor=1073741824 ;; - "TiB") factor=1099511627776 ;; + case $(echo ${unit} | tr [A-Z] [a-z]) in + "b") factor=1 ;; + "kb") factor=1000 ;; + "mb") factor=1000000 ;; + "gb") factor=1000000000 ;; + "tb") factor=1000000000000 ;; + "kib") factor=1024 ;; + "mib") factor=1048576 ;; + "gib") factor=1073741824 ;; + "tib") factor=1099511627776 ;; *) factor=1 ;; esac res=$(dc -e "0k ${value} ${factor} * p") - + echo "${res%.*}" } # convertByteToSize - converts a byte size to a given unit # parameter: # $1 - size in byte -# $2 - unit like "kB", "kiB", "MB", "MiB", "GB", "GiB", "TB" or "TiB" +# $2 - unit like "B", "kB", "kiB", "MB", "MiB", "GB", "GiB", "TB" or "TiB" # return: # rounded size with unit convertByteToSize() { local factor=1 - case $2 in - "B") factor=1;; - "kB") factor=1000 ;; - "MB") factor=1000000 ;; - "GB") factor=1000000000 ;; - "TB") factor=1000000000000 ;; - "kiB") factor=1024 ;; - "MiB") factor=1048576 ;; - "GiB") factor=1073741824 ;; - "TiB") factor=1099511627776 ;; + case $(echo $2 | tr [A-Z] [a-z]) in + "b") factor=1 ;; + "kb") factor=1000 ;; + "mb") factor=1000000 ;; + "gb") factor=1000000000 ;; + "tb") factor=1000000000000 ;; + "kib") factor=1024 ;; + "mib") factor=1048576 ;; + "gib") factor=1073741824 ;; + "tib") factor=1099511627776 ;; *) factor=1 ;; esac @@ -71,13 +71,13 @@ convertByteToSize() { # parameter: # $1 - disk device name # return: -# 0 - no changes -# 1 - label has changed +# 0 - no differences +# 1 - current label differs from given label validateScheme() { - local lbl=$(echo $diskdata[0] | cut -f 6 -d ":") + local lbl=$(echo ${diskdata[0]} | cut -f 6 -d ":") local retval=1 local scheme=$(eval echo \$DISK_$(echo $1 | tr [a-z] [A-Z])_SCHEME) - + if [ "x${scheme}" = "x${lbl}" ]; then retval=0 @@ -104,21 +104,21 @@ initialDiskScheme() { return } -# validateDiskPartitions - checks the partitions of a disk device +# validateDiskPartitionLayout - checks the partition layout of a disk device # parameter: # $1 - disk device name # return: -# 0 - no changes -# 1 - partions has changed -validateDiskPartitions() { +# 0 - no differences +# 1 - current partition layout differs from given partition layout +validateDiskPartitionLayout() { declare -a parts=() local retval=0 - + for part in $(eval "for p in \${DISK_$(echo $1 | tr [a-z] [A-Z])_PARTITIONS[@]}; do echo \$p; done ") do parts+=(${part}) done - + if [ ${#parts[@]} -eq $((${#diskdata[@]} - 1)) ]; then for idx in $(seq 1 1 ${#parts[@]}) @@ -126,13 +126,13 @@ validateDiskPartitions() { gsize=$(convertSizeToByte $(echo ${parts[$((${idx} - 1))]} | cut -f 1 -d ":")) dsize=$(convertSizeToByte $(echo ${diskdata[${idx}]} | cut -f 4 -d ":")) diffperc=$(dc -e "2k $dsize $gsize - 100 * $gsize / p" | sed -e "s/^-//g") - + if [ -n "x${diffperc%.*}" ]; then diffperc=0 fi - - #if difference between current disk size and given disk size more than 1% then partitions has changed + + #if difference between current disk size and given disk size more than 1% then the partition layout has changed if [ ${diffperc%.*} -gt 1 ]; then retval=1 @@ -141,30 +141,195 @@ validateDiskPartitions() { else retval=1 fi - + return ${retval} } -# initialDiskPartitions - initial the partitions of a disk device +# initialDiskPartitionLayout - initial the partition layout of a disk device # parameter: # $1 - disk device name # return: # none -initialDiskPartitions() { +initialDiskPartitionLayout() { declare -a parts=() + local scheme=$(eval echo \$DISK_$(echo $1 | tr [a-z] [A-Z])_SCHEME) local start=$(convertSizeToByte "1MiB") local fs="" local size="" local end="" - + local idx=1 + + # create partitions for part in $(eval "for p in \${DISK_$(echo $1 | tr [a-z] [A-Z])_PARTITIONS[@]}; do echo \$p; done ") do fs=$(echo ${part} | cut -f 2 -d ":") fsize=$(echo ${part} | cut -f 1 -d ":") size=$(convertSizeToByte $(echo ${part} | cut -f 1 -d ":")) end=$((${start} + ${size})) - parted -a optimal -s -m /dev/$1 mkpart primary ${fs} $(convertByteToSize ${start} "MiB") $(convertByteToSize ${end} "MiB") + case ${scheme} in + "mbr" | "msdos") + parted -a optimal -s -m /dev/$1 mkpart primary ${fs} $(convertByteToSize ${start} "MiB") $(convertByteToSize ${end} "MiB") + ;; + + "gpt") + parted -a optimal -s -m /dev/$1 mkpart "Part${idx}" ${fs} $(convertByteToSize ${start} "MiB") $(convertByteToSize ${end} "MiB") + ;; + esac start=${end} + idx=$(($idx + 1)) + done +} + +# setDiskPartitionFlags - set the flags of a disk partitions +# parameter: +# $1 - disk device name +# return: +# none +setDiskPartitionFlags() { + local idx + local flags + + # reset all flags of every partition + for part in $(parted -s -m /dev/$1 print | sed -e "1,2d;s/[\ ;]//g" | cut -f 1,7 -d ":") + do + idx=$(echo ${part} | cut -f 1 -d ":") + for flag in $(echo ${part} | sed "s/^[^:]*:\(.*\)$/\1/g;s/,/ /g") + do + parted -s /dev/$1 set ${idx} ${flag} off + done + done + + # set all given flags of a partition + idx=1 + for part in $(eval "for p in \${DISK_$(echo $1 | tr [a-z] [A-Z])_PARTITIONS[@]}; do echo \$p; done ") + do + flags=$(echo ${part} | cut -f 8 -d ":") + if [ -n "${flags}" ]; + then + for flag in $(echo ${flags} | sed "s/,/ /g") + do + parted -s /dev/$1 set ${idx} ${flag} on + done + fi + idx=$(($idx + 1)) + done + + return +} + +# formatDiskPartion - format a partition +# parameter: +# $1 - disk device name +# $2 - disk partition index +# $3 - filesystem name +# return: +# 0 - no error +# 1 - error occurred +# 2 - unknown filesystem +formatDiskPartion() { + local device="/dev/$1$2" + local retval=0 + + case $(echo $3 | tr "[A-Z]" "[a-z]") in + "ext4") mkfs.ext4 -q -F ${device} >/dev/null|| retval=1 ;; + "ext3") mkfs.ext3 -q -F ${device} >/dev/null || retval=1 ;; + "fat32") mkfs.vfat ${device} >/dev/null || retval=1 ;; + "fat16") mkfs.msdos -F 16 ${device} >/dev/null || retval=1 ;; + "swap") mkswap ${device} >/dev/null || retval=1 ;; + "ntfs") mkntfs -q -f ${device} >/dev/null || retval=1 ;; + "btrfs") mkfs.btrfs -q -f ${device} >/dev/null || retval=1 ;; + "reiserfs") mkreiserfs -q ${device} >/dev/null || retval=1 ;; + *) retval=2 ;; + esac + + return ${retval} +} + +# formatDiskPartitions - format partitions of a disk if necessary +# parameter: +# $1 - disk device name +# return: +# none +formatDiskPartitions() { + declare -a parts=() + local idx + local gfs + local dfs + local format + + for part in $(eval "for p in \${DISK_$(echo $1 | tr [a-z] [A-Z])_PARTITIONS[@]}; do echo \$p; done ") + do + parts+=(${part}) + done + + for part in $(parted -s -m /dev/$1 print | sed -e "1,2d;s/[\ ;]//g" | cut -f 1,5 -d ":") + do + idx=$(echo ${part} | cut -f 1 -d ":") + dfs=$(echo ${part} | cut -f 2 -d ":") + gfs=$(echo ${parts[$((${idx} - 1))]} | cut -f 2 -d ":") + format=$(echo ${parts[$((${idx} - 1))]} | cut -f 7 -d ":" | tr "[A-Z]" "[a-z]") + + if [ "${dfs}" != "${gfs}" -o "${format}" = "true" ]; + then + formatDiskPartion $1 ${idx} ${gfs} + fi + done +} + +# generateFSTab - generate an fstab file +# parameter: +# $1 - path of fstab file +# $2 - mount path prefix (optional) +# return: +# none +generateFSTab() { + local idx + local uuid + local blkidout + local mntpnt + local mntpntpfx="" + local fs + local opts + local dmp + local pass + + if [ $# -eq 2 ]; + then + mntpntpfx="$2" + fi + + echo -e "#device mount point filesystem options dump pass\n" >$1 + + for disk in ${DISK_DEVICES[@]} + do + idx=1 + for part in $(eval "for p in \${DISK_$(echo ${disk} | tr [a-z] [A-Z])_PARTITIONS[@]}; do echo \$p; done ") + do + blkidout=$(blkid /dev/${disk}${idx}) + uuid=$(echo ${blkidout} | sed "s/^.*UUID=\([^\ ]*\).*$/\1/g;s/\"//g") + fs=$(echo ${blkidout} | sed "s/^.*TYPE=\([^\ ]*\).*$/\1/g;s/\"//g") + mntpnt=$(echo ${part} | cut -f 3 -d ":") + opts=$(echo ${part} | cut -f 4 -d ":") + dmp=$(echo ${part} | cut -f 5 -d ":") + pass=$(echo ${part} | cut -f 6 -d ":") + + if [ ${fs} = "swap" -o ${fs} = "linux-swap" ]; + then + mntpnt="none" + fi + + if [ -n ${fs} -a -n ${mntpnt} -a -n ${uuid} ]; + then + echo "#/dev/${disk}${idx}" >>$1 + if [ ${mntpnt} != "none" ]; + then + echo -e "UUID=${uuid} ${mntpntpfx}${mntpnt} ${fs} ${opts} ${dmp} ${pass}\n" >>$1 + else + echo -e "UUID=${uuid} ${mntpnt} ${fs} ${opts} ${dmp} ${pass}\n" >>$1 + fi + fi + idx=$((${idx} + 1)) + done done } @@ -181,14 +346,21 @@ do if [ $? -eq 0 ]; then - validateDiskPartitions ${disk} + validateDiskPartitionLayout ${disk} if [ $? -ne 0 ]; then - initialDiskPartitions ${disk} + initialDiskPartitionLayout ${disk} fi + else initialDiskScheme ${disk} - initialDiskPartitions ${disk} + initialDiskPartitionLayout ${disk} fi + + setDiskPartitionFlags ${disk} + formatDiskPartitions ${disk} done + +generateFSTab "/tmp/fstab.local" +generateFSTab "/tmp/fstab.target" "/target" -- GitLab