diff options
Diffstat (limited to 'server/modules/partitioner/opt/openslx/scripts/partitioner')
-rwxr-xr-x | server/modules/partitioner/opt/openslx/scripts/partitioner | 281 |
1 files changed, 281 insertions, 0 deletions
diff --git a/server/modules/partitioner/opt/openslx/scripts/partitioner b/server/modules/partitioner/opt/openslx/scripts/partitioner new file mode 100755 index 00000000..e5e94f4e --- /dev/null +++ b/server/modules/partitioner/opt/openslx/scripts/partitioner @@ -0,0 +1,281 @@ +#!/bin/bash + +# set -e + +PARTITIONSPATH="/proc/partitions" +CONFIGPATH="/opt/openslx/config" + +# BLOCKSIZE=$(sfdisk -l /dev/$CHOSENDISK | grep -o -e "blocks of [0-9]* bytes" | cut -d ' ' -f3) +BLOCKSIZE=1024 + +. $CONFIGPATH +# testing if the sizes and ID's of the disk is defined in the config file + + +# picking disk that will be used +DISKS=$(cat $PARTITIONSPATH | tr -s ' ' | cut -d ' ' -f5 | grep -e "[a-z]$") +if [ -z "$DISKS" ]; then + dialog --msgbox "ERROR: Can't find an hard disk." 10 40 + exit 1 +fi +if [ -z "$SLX_CHOOSEN_DISK" ]; then + DIALOGSTR="This computer has the following partitions:\n" + for disk in $DISKS; do + DISKSIZE=$(($(cat $PARTITIONSPATH | grep -e $disk$ | tr -s ' ' | cut -d ' ' -f4)*$BLOCKSIZE)) + PARTS=$(cat $PARTITIONSPATH | grep -e $disk[0-9] | tr -s ' ' | cut -d ' ' -f5) + GBSIZE=$(echo "scale=2; $DISKSIZE/1024/1024/1024" | bc -l) + DIALOGSTR=$DIALOGSTR"$disk $DISKSIZE Bytes ($GBSIZE GB)\n" + USED=0 + for PART in $PARTS; do + PARTSIZE=$(($(cat $PARTITIONSPATH | grep -e $PART$ | tr -s ' ' | cut -d ' ' -f4)*1024)) + USED=$(($USED+$PARTSIZE)) + GBSIZE=$(echo "scale=2; $PARTSIZE/1024/1024/1024" | bc -l) + DIALOGSTR=$DIALOGSTR" $PART $PARTSIZE ($GBSIZE GB)\n" + done + DIALOGSTR=$DIALOGSTR" ----------\n" + FREESPACE=$(($DISKSIZE-$USED)) + GBSIZE=$(echo "scale=2; $USED/1024/1024/1024" | bc -l) + DIALOGSTR=$DIALOGSTR" Used $USED ($GBSIZE GB)\n" + GBSIZE=$(echo "scale=2; $FREESPACE/1024/1024/1024" | bc -l) + DIALOGSTR=$DIALOGSTR" Free $FREESPACE ($GBSIZE GB)\n\n" + done + if [ $(echo $DISKS | tr ' ' \\n | wc -l) -gt 1 ]; then + DIALOGSTR=$DIALOGSTR"Which of these disks you want to use?\n" + SLX_CHOOSEN_DISK=$(dialog --no-collapse --cr-wrap --inputbox "$DIALOGSTR" 0 0 $(echo $DISKS | cut -d ' ' -f1) 3>&1 1>&2 2>&3) + if [ $? -eq 1 ]; then + exit 1; + fi + # verify if the choosen disk is a valid disk + echo $DISKS | tr ' ' \\n | grep -e ^$SLX_CHOOSEN_DISK$ > /dev/null 2>/dev/null + while [ $? -ne 0 ]; do + dialog --msgbox "Invalid option.\nTry again." 0 0 + SLX_CHOOSEN_DISK=$(dialog --no-collapse --cr-wrap --inputbox "$DIALOGSTR" 0 0 $(echo $DISKS | cut -d ' ' -f1) 3>&1 1>&2 2>&3) + if [ $? -eq 1 ]; then + exit 1; + fi + echo $DISKS | tr ' ' \\n | grep -e ^$SLX_CHOOSEN_DISK$ > /dev/null 2>/dev/null + done + # ask a confirmation (all your data will be lost etc) + elif [ $(echo $DISKS | tr ' ' \\n | wc -l) -eq 1 ]; then + SLX_CHOOSEN_DISK=$(echo $DISKS | tr ' ' \\n | head -n1) + else + dialog --msgbox "ERROR: Can't find an hard disk." 10 40 + exit 1 + fi +fi +# if SLX_PARTITION_TABLE is set then put his content in a associative array +declare -A PARTTBL +COUNTER=1 +if [ -n "$SLX_PARTITION_TABLE" ]; then + for PARTITION in $SLX_PARTITION_TABLE; do + IFS=, + set $PARTITION + if [ $COUNTER -eq 4 ]; then + COUNTER=$(($COUNTER+1)) + fi + PARTTBL["$SLX_CHOOSEN_DISK$COUNTER/id"]=$1 + PARTTBL["$SLX_CHOOSEN_DISK$COUNTER/size"]=$(echo $2 | egrep -o "[0-9]*") + PARTTBL["$SLX_CHOOSEN_DISK$COUNTER/mountpoint"]=$3 + if [ -e $4 ]; then + PARTTBL["$SLX_CHOOSEN_DISK$COUNTER/bootable"]=0 + else + PARTTBL["$SLX_CHOOSEN_DISK$COUNTER/bootable"]=1 + fi + # set the partitions to non-existents + PARTTBL["$SLX_CHOOSEN_DISK$COUNTER/exists"]=0 + COUNTER=$(($COUNTER+1)) + done + unset IFS +else + dialog --msgbox "ERROR: You didn't set a valid partition table. Please check your config file." 10 40 + exit 1 +fi +# PARTTBL starts at 1 and ends at COUNTER-1 + +# check if the choosen disk is greater than the new partition table size. +DISKSIZE=$(($(cat $PARTITIONSPATH | grep -e $SLX_CHOOSEN_DISK$ | tr -s ' ' | cut -d ' ' -f4)*$BLOCKSIZE)) +NEEDED_SPACE=0 +for (( i = 1; i < $COUNTER; i++ )); do + if [[ $i -ne 4 ]]; then + NEEDED_SPACE=$(($NEEDED_SPACE+${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]})) + fi +done +NEEDED_SPACE=$(($NEEDED_SPACE*1024*1024*1024+$COUNTER*1024*1024)) +echo "$DISKSIZE -lt $NEEDED_SPACE" +if [ $DISKSIZE -lt $NEEDED_SPACE ]; then + dialog --msgbox "ERROR: Insufficient space in disk /dev/$SLX_CHOOSEN_DISK." 6 40 + exit 1 +fi + +# choosing the partition type (MSDOS or GPT) if it wasn't set in config file +if [ -z "$SLX_PARTITION_TYPE" ]; then + SLX_PARTITION_TYPE=msdos + ANS=$(dialog --no-tags --menu "Choose the partitions type" 0 0 0 "1" " MSDOS " "2" " GPT " 3>&1 1>&2 2>&3) + if [ $? -eq 1 ]; then + exit 1 + fi + if [ $ANS -eq 1 ]; then + SLX_PARTITION_TYPE=msdos + else + SLX_PARTITION_TYPE=GPT + fi +fi + +GBSIZE=$(echo "scale=2; $DISKSIZE/1024/1024/1024" | bc -l) +DIALOGSTR="$SLX_CHOOSEN_DISK $DISKSIZE Bytes ($GBSIZE GB)\n" +PARTS=$(cat $PARTITIONSPATH | grep -e $SLX_CHOOSEN_DISK[0-9] | tr -s ' ' | cut -d ' ' -f5) +USED=0 +for PART in $PARTS; do + PARTSIZE=$(($(cat $PARTITIONSPATH | grep -e $PART$ | tr -s ' ' | cut -d ' ' -f4)*$BLOCKSIZE)) + USED=$(($USED+$PARTSIZE)) + GBSIZE=$(echo "scale=2; $PARTSIZE/1024/1024/1024" | bc -l) + DIALOGSTR=$DIALOGSTR" $PART $PARTSIZE ($GBSIZE GB)\n" +done +DIALOGSTR=$DIALOGSTR" ----------\n" +FREESPACE=$(($DISKSIZE-$USED)) +GBSIZE=$(echo "scale=2; $USED/1024/1024/1024" | bc -l) +DIALOGSTR=$DIALOGSTR" Used $USED ($GBSIZE GB)\n" +GBSIZE=$(echo "scale=2; $FREESPACE/1024/1024/1024" | bc -l) +DIALOGSTR=$DIALOGSTR" Free $FREESPACE ($GBSIZE GB)\n\n" +DIALOGSTR=$DIALOGSTR"All /dev/$SLX_CHOOSEN_DISK partitions will be deleted, ok?" + +# asking confirmation to create the partitions +if [[ $SLX_AUTOMATIC_PARTITIONING = "no" && SLX_DELETE_PARTITION_TABLE = 'yes' ]]; then + dialog --defaultno --yesno "$DIALOGSTR" 0 0 + if [ $? -eq 1 ]; then + exit 1 + fi +fi + +# pick the size of a sector (usually 512 bytes) +# SECTORSIZE=$(sfdisk /dev/$SLX_CHOOSEN_DISK -l -u S 2> /dev/null | grep Units | cut -d ' ' -f5) +SECTORSIZE=512 + +if [[ $USED -eq 0 ]]; then + SLX_DELETE_PARTITION_TABLE="yes" +fi + +if [ $SLX_DELETE_PARTITION_TABLE = "yes" ]; then + # delete partition table + sgdisk -Z /dev/$SLX_CHOOSEN_DISK > /dev/null 2> /dev/null # erase all partitions + + # constructing the sfdisk input file + echo "unit: sectors + "> /tmp/partitiontable.tmp + + START=$((1*1024*1024/$SECTORSIZE)) + for (( i = 1; i < $COUNTER; i++ )); do + if [ $i -ne 4 ]; then + SIZE=$((${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}*1024*1024*1024/$SECTORSIZE)) + ID=${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]} + if [ ${PARTTBL["$SLX_CHOOSEN_DISK$i/bootable"]} -eq 1 ]; then + echo "/dev/"$SLX_CHOOSEN_DISK$i" : start= "$START", size= "$SIZE", Id= "$ID", bootable " >> /tmp/partitiontable.tmp + else + echo "/dev/"$SLX_CHOOSEN_DISK$i" : start= "$START", size= "$SIZE", Id= "$ID >> /tmp/partitiontable.tmp + fi + START=$(($START+$SIZE+1*1024*1024/$SECTORSIZE)) + else + echo "/dev/"$SLX_CHOOSEN_DISK$i" : start= "$START", size= "$(($DISKSIZE/$SECTORSIZE-1))", Id= 5" >> /tmp/partitiontable.tmp + START=$(($START+1*1024*1024/$SECTORSIZE)) + fi + done + sfdisk -q -f /dev/$SLX_CHOOSEN_DISK < /tmp/partitiontable.tmp > /dev/null + # rm -f /tmp/partitiontable.tmp +elif [ $(sfdisk -d /dev/$SLX_CHOOSEN_DISK 2> /dev/null | grep $SLX_CHOOSEN_DISK'1' | tr -s ' ' | cut -d ' ' -f7 | cut -d '=' -f2) = 'ee' ]; then +# finding existent partitions in GPT + echo 'GPT' + for PART in $PARTS; do + for (( i = 1; i < $COUNTER; i++ )); do + if [ $i -ne 4 ]; then + if [ ${PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]} = 0 ]; then + sgdisk /dev/$SLX_CHOOSEN_DISK -i ${PART:3} | grep ${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}"000000-0000-0000-0000-000000000000" > /dev/null + if [ $? -eq 0 ]; then + echo "the partition $PART is the ${PARTTBL["$SLX_CHOOSEN_DISK$i/mountpoint"]} partition." + PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]=$PART + NEEDED_SPACE=$(($NEEDED_SPACE-${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}*1024*1024*1024)) + break + fi + fi + fi + done + done + echo "FREESPACE="$FREESPACE + echo "NEEDED_SPACE="$NEEDED_SPACE + if [[ $FREESPACE -lt $NEEDED_SPACE ]]; then + dialog --msgbox "ERROR: Insufficient free space in disk /dev/$SLX_CHOOSEN_DISK." 6 40 + exit 1 + fi +else +# finding existent partitions in MS-DOS + echo 'MSDOS' + for PART in $PARTS; do + for (( i = 1; i < $COUNTER; i++ )); do + if [ $i -ne 4 ]; then + if [ ${PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]} = 0 ]; then + sfdisk -d /dev/$SLX_CHOOSEN_DISK 2> /dev/null | grep $PART | tr -s ' ' | cut -d ' ' -f7 | grep ${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]} > /dev/null + if [ $? -eq 0 ]; then + echo "the partition $PART is the ${PARTTBL["$SLX_CHOOSEN_DISK$i/mountpoint"]} partition." + PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]=$PART + NEEDED_SPACE=$(($NEEDED_SPACE-${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}*1024*1024*1024)) + break + fi + fi + fi + done + done + echo "FREESPACE="$FREESPACE + echo "NEEDED_SPACE="$NEEDED_SPACE + if [[ $FREESPACE -lt $NEEDED_SPACE ]]; then + dialog --msgbox "ERROR: Insufficient free space in disk /dev/$SLX_CHOOSEN_DISK." 6 40 + exit 1 + fi +fi + +DIALOGSTR="New partitions created:\n\n" +if [ $SLX_PARTITION_TYPE = 'GPT' ]; then + # create the command (CMD) that will convert the MSDOS type to GPT and change the unique GUIDs + CMD="sgdisk -g /dev/$SLX_CHOOSEN_DISK" + for (( i = 1; i < $COUNTER; i++ )); do + if [ $i -ne 4 ]; then + if [ ${PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]} = 0 ]; then + CMD=$CMD" -t "$i":"${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}"000000-0000-0000-0000-000000000000" + fi + CMD=$CMD" -c "$i":\""${PARTTBL["$SLX_CHOOSEN_DISK$i/mountpoint"]}"\"" + fi + done + CMD=$CMD" > /dev/null" + $CMD + + for (( i = 1; i < $COUNTER; i++ )); do + if [ $i -ne 4 ]; then + SIZE=$((${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}*1024*1024*1024)) + ID=${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]} + MOUNTPOINT=${PARTTBL["$SLX_CHOOSEN_DISK$i/mountpoint"]} + DIALOGSTR=$DIALOGSTR$MOUNTPOINT" (/dev/"$SLX_CHOOSEN_DISK$i")\n" + DIALOGSTR=$DIALOGSTR" GUID: "$ID"000000-0000-0000-0000-000000000000\n" + DIALOGSTR=$DIALOGSTR" Size: "$SIZE" Bytes ("${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}" GB)\n" + fi + done +else + for (( i = 1; i < $COUNTER; i++ )); do + if [ $i -lt 4 ]; then + SIZE=$((${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}*1024*1024*1024)) + ID=${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]} + MOUNTPOINT=${PARTTBL["$SLX_CHOOSEN_DISK$i/mountpoint"]} + DIALOGSTR=$DIALOGSTR$MOUNTPOINT" (/dev/"$SLX_CHOOSEN_DISK$i")\n" + DIALOGSTR=$DIALOGSTR" Type: Primary\n" + DIALOGSTR=$DIALOGSTR" ID: "$ID"\n" + DIALOGSTR=$DIALOGSTR" Size: "$SIZE" Bytes ("${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}" GB)\n" + elif [ $i -gt 4 ]; then + SIZE=$((${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}*1024*1024*1024)) + ID=${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]} + MOUNTPOINT=${PARTTBL["$SLX_CHOOSEN_DISK$i/mountpoint"]} + DIALOGSTR=$DIALOGSTR$MOUNTPOINT" (/dev/"$SLX_CHOOSEN_DISK$i")\n" + DIALOGSTR=$DIALOGSTR" Type: Logical\n" + DIALOGSTR=$DIALOGSTR" ID: "$ID"\n" + DIALOGSTR=$DIALOGSTR" Size: "$SIZE" Bytes ("${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}" GB)\n" + fi + done +fi + +dialog --msgbox "$DIALOGSTR" 0 0 |