diff options
author | Jonathan Bauer | 2015-01-14 15:26:18 +0100 |
---|---|---|
committer | Jonathan Bauer | 2015-01-14 15:26:18 +0100 |
commit | 5748aae930485cad805c4594367b8b024bafc564 (patch) | |
tree | 3d1740d7c8e8b2c9696b19416045367e0f149f4a | |
parent | [vbox] xml filter died long ago... (diff) | |
parent | [setup-partitions] fix syntax (diff) | |
download | tm-scripts-5748aae930485cad805c4594367b8b024bafc564.tar.gz tm-scripts-5748aae930485cad805c4594367b8b024bafc564.tar.xz tm-scripts-5748aae930485cad805c4594367b8b024bafc564.zip |
Merge remote-tracking branch 'origin/partitioner' into test
10 files changed, 690 insertions, 58 deletions
diff --git a/doc/boot_config_vars b/doc/boot_config_vars index 36293d87..07866031 100644 --- a/doc/boot_config_vars +++ b/doc/boot_config_vars @@ -52,3 +52,34 @@ remote logging: SLX_REMOTE_LOG URL where to post log to, eg. 'http://server/log.php' POST variables: type, description, longdesc +partitioner & setp partitions: + +SLX_PARTITION_TYPE='GPT|msdos' #Choose partition table type, if variable is not set partitioner + #will ask user or in silent mode use 'msdos' as default + +SLX_CHOOSEN_DISK='sda' #If no HDD is specified, the partitioner will either ask the user + #or in silent mode choose the only HDD available or abort if more + #then one HDD is present. + +SLX_AUTOMATIC_PARTITIONING='yes' #silent mode, restarts the machine automatically after partitioning + +SLX_PARTITION_TABLE=' #The partition table variable is used by both the partitioner and + 44,1G,/tmp #setup-partition script. The values per partition are as follows: + 43,1G,/boot,bootable #unique ID, size, [mountpoint], [options] + 42,1G,/cache/export/dnbd3 #The ID is used as is for msdos and for GPT concatenated with trailing + 41,1G,/home #zeros, example: msdos: 44 GPT: 44000000-0000-0000-0000-000000000000 + 82,1G' #Attention: without a mount point, the setup-partition script won't mount + #the partition. Swap partition doesn't need a mount point + #Existing linux partitions will automatically be mountet under /media + #and don't need to be specified here. + #The only optional argument currently used at the moment is 'bootable' + #to mark a partition as such + +#If no partition table variable was specifies, the partitioner and setup-scripts will assume +#the following default partition table: + +SLX_PARTITION_TABLE=' + 44,10G,/tmp + 45,10G,/var/scratch + 82,4G' + diff --git a/doc/kcl_config_vars b/doc/kcl_config_vars index 66166299..4c51d5dc 100644 --- a/doc/kcl_config_vars +++ b/doc/kcl_config_vars @@ -26,3 +26,5 @@ LABEL u1304x64 APPEND slxsrv=10.254.221.6 slxbase=u1304x64 vga=current splash quiet IPAPPEND 3 +To start the partitioner, the partitioner module must be included in the configuration +and 'partitioner' must be specifies in the KCL diff --git a/remote/modules/busybox/openslx-busybox-config b/remote/modules/busybox/openslx-busybox-config index 29bb29ae..0f884d2b 100644 --- a/remote/modules/busybox/openslx-busybox-config +++ b/remote/modules/busybox/openslx-busybox-config @@ -365,7 +365,7 @@ CONFIG_WHICH=y # Editors # CONFIG_AWK=y -# CONFIG_FEATURE_AWK_LIBM is not set +CONFIG_FEATURE_AWK_LIBM=y CONFIG_FEATURE_AWK_GNU_EXTENSIONS=y # CONFIG_CMP is not set CONFIG_DIFF=y diff --git a/remote/rootfs/rootfs-stage32/data/etc/systemd/system/setup-partitions.service b/remote/rootfs/rootfs-stage32/data/etc/systemd/system/setup-partitions.service index 6bb3a986..67466fae 100644 --- a/remote/rootfs/rootfs-stage32/data/etc/systemd/system/setup-partitions.service +++ b/remote/rootfs/rootfs-stage32/data/etc/systemd/system/setup-partitions.service @@ -4,6 +4,7 @@ DefaultDependencies=no Before=basic.target After=systemd-udev-settle.service Wants=tmp.target +ConditionKernelCommandLine=!partitioner [Service] Type=oneshot diff --git a/remote/rootfs/rootfs-stage32/data/opt/openslx/scripts/systemd-setup_partitions b/remote/rootfs/rootfs-stage32/data/opt/openslx/scripts/systemd-setup_partitions index f9640589..8fc7535c 100755 --- a/remote/rootfs/rootfs-stage32/data/opt/openslx/scripts/systemd-setup_partitions +++ b/remote/rootfs/rootfs-stage32/data/opt/openslx/scripts/systemd-setup_partitions @@ -14,8 +14,8 @@ ############################################################################# -# Mount point for persistent scratch partition (type 45) -PERSISTENT="/opt/openslx/persistent" +# read global OpenSLX config +. /opt/openslx/config || { echo "Could not source config!"; exit 23; } # General formatter for the /tmp partition on a local harddisk diskfm () { @@ -57,7 +57,8 @@ diskfm () { mopt="-o noexec" ;; esac - mkfs.$fs ${fopt} "${target}" + echo "formatting ${target} with $fs..." + mkfs.$fs ${fopt} "${target}" > /dev/null 2>&1 fi [ -n "$found" ] && break fi @@ -73,7 +74,7 @@ mount_temp () { mount $@ /tmp || return 1 chmod a+rwxt /tmp # Move stuff from working directory, which is old /tmp, to new /tmp just mounted - mv ./* ./.[!.]* ./..?* /tmp/ 2> /dev/null + mv ./* ./.[!.]* ./..?* /tmp/ > /dev/null 2>&1 local OLD=$(LANG=C ls -alh | grep -v -E ' \.\.?$' | grep -v '^total') [ -n "$OLD" ] && echo -- "Leftovers:" && echo -- "$OLD" cd "$PRE" @@ -81,7 +82,7 @@ mount_temp () { mount_temp_fallback () { mkdir -p /tmptmp - mv /tmp/* /tmp/.* /tmptmp/ 2> /dev/null + mv /tmp/* /tmp/.* /tmptmp/ > /dev/null 2>&1 mount $@ /tmp || return 1 chmod a+rwxt /tmp mv /tmptmp/* /tmptmp/.* /tmp/ @@ -89,72 +90,142 @@ mount_temp_fallback () { return 0 } -fdisk -l | sed -n "/^\/dev\//p" > "/etc/disk.partition" +mount_partition () { + local mountpoint="$1" + local partition="$2" -echo "Partitions:" -cat "/etc/disk.partition" - -# Check for standard swap partitions and make them available to the system -HAVE_SWAP=no -for hdpartnr in $(sed -n -e "/ 82 /p" "/etc/disk.partition" | sed -e "s/[[:space:]].*//"); do - echo -e "$hdpartnr\tswap\t\tswap\t\tdefaults\t 0 0" >> "/etc/fstab" - swapon "$hdpartnr" -p 10 && HAVE_SWAP=yes # low priority, in case we have zram swap, prefer that) -done - -# We use special non assigned partition type (id44) for harddisk scratch -# space, thus no normal filesystem will be incidentally deleted or -# corrupted -HAVE_TEMP=no -for hdpartnr in $(sed -n -e "/ 44 /p" "/etc/disk.partition" | sed -e "s/[[:space:]].*//"); do - # check for supported filesystem and formatter - if diskfm "$hdpartnr"; then - # echo "$hdpartnr is mounted to /mnt/tmp at $(sysup)" >/tmp/tmpready - mount_temp "$mopt" "$hdpartnr" || continue - echo -e "${hdpartnr}\t/tmp\t\tauto\t\tnoexec\t 0 0" >> "/etc/fstab" - HAVE_TEMP=yes - break + mkdir -p "$mountpoint" + if ! blkid -s TYPE | grep "${partition}" ; then + echo "No fs found for ${partition}, formating..." + diskfm "$partition" "jfs xfs ext3" || return $? + mount -t auto -o noexec "$partition" "$mountpoint" || return $? else - echo "formatting failed for some reason" - fi # Made this non-forking, systemd should handle it - 2013-05-28 -done + if ! mount -t auto -o noexec "$partition" "$mountpoint" ; then + echo "Mount of $partition on $mountpoint failed with code $?, trying fsck..." + fsck "$partition" || return $? + mount -t auto -o noexec "$partition" "$mountpoint" || return $? + fi + fi + echo "${partition}\t${mountpoint}\tauto\t\tnoauto,noexec\t\t 0 0" >> "/etc/fstab" +} -# Put detected linux partitions (83) into /etc/fstab with "noauto", special -# partition 45 (persistent scratch) to /var/scratch and 46 to /var/openslx -HAVE_PERSISTENT=no -for partid in 83 45 46 ; do - for hdpartnr in $(sed -n -e "/ ${partid} /p" "/etc/disk.partition" | sed -e "s/[[:space:]].*//"); do - if [ "${partid}" -eq 83 ]; then - mkdir -p "/media/${hdpartnr#/dev/*}" - echo -e "${hdpartnr}\t/media/${hdpartnr#/dev/*}\tauto\t\tnoauto,noexec\t 0 0" >> "/etc/fstab" - elif [ "${partid}" -eq 45 -a "$HAVE_PERSISTENT" = "no" ]; then - mkdir -p "$PERSISTENT" - if ! mount -t auto -o noexec "${hdpartnr}" "$PERSISTENT"; then - diskfm "$hdpartnr" "jfs xfs ext3" || continue - mount -t auto -o noexec "${hdpartnr}" "$PERSISTENT" || continue - fi - HAVE_PERSISTENT=yes - echo -e "${hdpartnr}\t${PERSISTENT}\tauto\t\tnoauto,noexec\t\t 0 0" >> "/etc/fstab" - elif [ "${partid}" -eq 46 ]; then - mkdir -p "/media/${hdpartnr#/dev/*}" - #mount -t auto ${hdpartnr} /mnt/media/${hdpartnr#/dev/*} \n\ - #test -d /mnt/media/${hdpartnr#/dev/*}/home && \ - # ln -sf /media/${hdpartnr#/dev/*} /var/home - echo -e "${hdpartnr}\t/media/${hdpartnr#/dev/*}\tauto\t\tnoauto\t\t 0 0" >> "/etc/fstab" +read_partitions () { + # create the /etc/disk.partition file with all the partitions and respective id's (MSDOS and GPT) + echo "PARTITION - ID" > "/etc/disk.partition" + for hd in $(cat /proc/partitions | tr -s ' ' | cut -d ' ' -f5 | grep -o -e "[a-z]*$"); do + echo -n "$hd (" >> "/etc/disk.partition" + sfdisk -d /dev/$hd 2>&1 | grep 'GPT' > /dev/null + if [ $? -eq 1 ]; then + echo "MSDOS)" >> "/etc/disk.partition" + fdisk /dev/$hd -l | sed -n "/^\/dev\//p" | tr -d '*' | tr -s ' ' | cut -d ' ' -f1,5 >> "/etc/disk.partition" + else + echo "GPT)" >> "/etc/disk.partition" + for part in $(cat /proc/partitions | tr -s ' ' | cut -d ' ' -f5 | grep -o -e "$hd[0-9][0-9]*$"); do + LINE="/dev/$part " + LINE=${LINE}$(sgdisk /dev/$hd -i ${part:3} | grep 'GUID code' | cut -d ' ' -f4) + echo $LINE >> "/etc/disk.partition" + done fi done + + echo "Partitions:" + cat "/etc/disk.partition" + echo "------------------------------------------------------" +} + +# default partitions, if not specifies in config (note: size is irrelevant for setup_partitions) +if [ -z "${SLX_PARTITION_TABLE}" ]; then + SLX_PARTITION_TABLE=' + 44,10G,/tmp + 45,10G,/var/scratch + 82,4G' +fi + +read_partitions + +for PARTITION in $SLX_PARTITION_TABLE; do + IFS=, + set $PARTITION + id=$1 + shift + size=$1 + shift + mountpoint="$1" + shift + options="$*" + unset IFS + + case $id in + 44) + HAVE_TEMP="no" + TMP_SIZE=$size + for tmppart in $(grep -e "44$\|44000000-0000-0000-0000-000000000000$" /etc/disk.partition | cut -d ' ' -f1); do + echo "tmp partition found, formatting and mounting..." + if diskfm "$tmppart"; then + # echo "$tmppart is mounted to /mnt/tmp at $(sysup)" >/tmp/tmpready + mount_temp "$mopt" "$tmppart" || continue + echo "${tmppart}\t/tmp\t\tauto\t\tnoexec\t 0 0" >> "/etc/fstab" + HAVE_TEMP="yes" + echo "$tmppart mounted on /tmp" + else + echo "formatting tmp partition failed for some reason" + fi # Made this non-forking, systemd should handle it - 2013-05-28 + done + ;; + 46) + for openslxpart in $(grep -e "46$\|46000000-0000-0000-0000-000000000000$" /etc/disk.partition | cut -d ' ' -f1); do + mkdir -p "/media/${openslxpart#/dev/*}" + #mount -t auto ${openslxpart} /mnt/media/${openslxpart#/dev/*} \n\ + #test -d /mnt/media/${openslxpart#/dev/*}/home && \ + #ln -sf /media/${openslxpart#/dev/*} /var/home + echo "${openslxpart}\t/media/${openslxpart#/dev/*}\tauto\t\tnoauto\t\t 0 0" >> "/etc/fstab" + echo "special partition ${openslxpart} mounted on /media/${openslxpart#/dev/*}" + done + ;; + 82) + # Check for standard swap partitions and make them available to the system + HAVE_SWAP=no + for swppart in $(grep -e "82$\|0657FD6D-A4AB-43C4-84E5-0933C84B4F4F$" /etc/disk.partition | cut -d ' ' -f1); do + echo -e "$swppart\tswap\t\tswap\t\tdefaults\t 0 0" >> "/etc/fstab" + mkswap "$swppart" && swapon "$swppart" -p 10 && HAVE_SWAP=yes # low priority, in case we have zram swap, prefer that) + [ $HAVE_SWAP = "yes" ] && echo "swap partition found and activated" + done + ;; + *) + for hdpartnr in $(grep -e "${id}$\|${id}000000-0000-0000-0000-000000000000$" /etc/disk.partition | cut -d ' ' -f1); do + mount_partition "${mountpoint}" $hdpartnr + mount_status=$? + if [ $mount_status -ne 0 ]; then + echo "Mount of partition $hdpartnr on ${mountpoint} failed with exit code: $mount_status" + [ -d "${mountpoint}" ] && rm -r "${mountpoint}" + else + echo "special partition $hdpartnr mounted on ${mountpoint}" + fi + done + ;; + esac + echo "------------------------------------------------------" +done + +#Put detected linux partitions (83) into /etc/fstab with "noauto" +for linuxpart in $(grep -e "83$\|0FC63DAF-8483-4772-8E79-3D69D8477DE4$" /etc/disk.partition | cut -d ' ' -f1); do + mountpoint="/media" + mkdir -p "${mountpoint}/${linuxpart#/dev/*}" + echo "${linuxpart}\t${mountpoint}/${linuxpart#/dev/*}\tauto\t\tnoauto,noexec\t 0 0" >> "/etc/fstab" + echo "linux partition ${linuxpart} mounted on ${mountpoint}/${linuxpart#/dev/*}" + echo "------------------------------------------------------" done -[ "$HAVE_PERSISTENT" = "no" -a -d "$PERSISTENT" ] && rm -f "$PERSISTENT" mount -a # Make huge tmpfs if nothing could be mounted for /tmp if [ "$HAVE_TEMP" = "no" ]; then - mount_temp -t tmpfs -o size=20G none - slxlog "partition-temp" "Running /tmp on tmpfs only!" "/etc/disk.partition" + mount_temp -t tmpfs -o size=${TMP_SIZE} none + slxlog "partition-temp" "Running /tmp on tmpfs only!" "/etc/disk.partition" fi + if [ "$HAVE_SWAP" = "no" ]; then slxlog "partition-swap" "Have no (formatted) swap partition, using zram swap only!" "/etc/disk.partition" fi exit 0 - diff --git a/remote/rootfs/rootfs-stage32/module.conf b/remote/rootfs/rootfs-stage32/module.conf index 3a160a36..eab2b8f3 100644 --- a/remote/rootfs/rootfs-stage32/module.conf +++ b/remote/rootfs/rootfs-stage32/module.conf @@ -18,6 +18,11 @@ REQUIRED_BINARIES=" mkfs.xfs mkfs.ext3 mkfs.ext4 + fsck.ext3 + fsck.ext4 + fsck.jfs + fsck.xfs + fsck blkid modprobe ps @@ -38,6 +43,9 @@ REQUIRED_BINARIES=" getent ldconfig grep + sfdisk + sgdisk + dialog " REQUIRED_LIBRARIES=" libcap diff --git a/server/configs/curitiba/partitioner b/server/configs/curitiba/partitioner new file mode 120000 index 00000000..b45bce02 --- /dev/null +++ b/server/configs/curitiba/partitioner @@ -0,0 +1 @@ +../../modules/partitioner
\ No newline at end of file diff --git a/server/modules/partitioner/etc/systemd/system/multi-user.target.wants/partitioner.service b/server/modules/partitioner/etc/systemd/system/multi-user.target.wants/partitioner.service new file mode 120000 index 00000000..48defa3e --- /dev/null +++ b/server/modules/partitioner/etc/systemd/system/multi-user.target.wants/partitioner.service @@ -0,0 +1 @@ +../partitioner.service
\ No newline at end of file diff --git a/server/modules/partitioner/etc/systemd/system/partitioner.service b/server/modules/partitioner/etc/systemd/system/partitioner.service new file mode 100644 index 00000000..85b41597 --- /dev/null +++ b/server/modules/partitioner/etc/systemd/system/partitioner.service @@ -0,0 +1,20 @@ +[Unit] +Description=Runs the OpenSLX Partitioning Tool +After=killsplash.service +After=systemd-vconsole-setup.service +Before=display-manager.service getty@tty1.service getty@ttyUSB0.service +Before=serial-getty@ttyS0.service serial-getty@ttyO0.service serial-getty@ttyO2.service +Before=serial-getty@ttyAMA0.service serial-getty@ttymxc0.service serial-getty@ttymxc3.service +Conflicts=killsplash.service +ConditionKernelCommandLine=partitioner + +[Service] +Type=oneshot +ExecStart=/opt/openslx/scripts/systemd-partitioner +TimeoutSec=0 +RemainAfterExit=yes +SysVStartPriority=99 +StandardInput=tty +StandardOutput=tty +StandardError=syslog + diff --git a/server/modules/partitioner/opt/openslx/scripts/systemd-partitioner b/server/modules/partitioner/opt/openslx/scripts/systemd-partitioner new file mode 100755 index 00000000..d8fa0c6d --- /dev/null +++ b/server/modules/partitioner/opt/openslx/scripts/systemd-partitioner @@ -0,0 +1,497 @@ +#!/bin/ash +# Copyright (c) 2013 - OpenSLX GmbH +# +# This program is free software distributed under the GPL version 2. +# See http://openslx.org/COPYING +# +# If you have any feedback please consult http://openslx.org/feedback and +# send your feedback to feedback@openslx.org +# +# General information about OpenSLX can be found under http://openslx.org +# +# Local hard disk autodetection script for OpenSLX linux stateless clients, +# detecting swap and special partitions + +############################################################################# + +perror () { + echo "ERROR: $@" 1>&2 + if [ "$SLX_AUTOMATIC_PARTITIONING" != "yes" ]; then + dialog --title "ERROR" --stdout --msgbox "$@" 15 60 + else + dialog --title "ERROR" --no-cancel --stdout --pause "$@\n\nReboot in:" 15 60 600 + reboot + fi + exit 1 +} + +pwarning () { + echo "WARNING: $@" 1>&2 + if [ "$SLX_AUTOMATIC_PARTITIONING" != "yes" ]; then + dialog --title "WARNING" --stdout --msgbox "$@" 15 60 + else + dialog --title "WARNING" --stdout --infobox "$@" 15 60 + sleep 5 + fi +} + +. /opt/openslx/config || perror "Could not source config." + +#set -x +#exec > /log 2>&1 + + +#-------------------------------------------------------------------------------------- +#Gathering partition information + +# BLOCKSIZE=$(sfdisk -l /dev/$SLX_CHOOSEN_DISK | grep -o -e "blocks of [0-9]* bytes" | cut -d ' ' -f3) +BLOCKSIZE=1024 + +# 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 + +# Partition that should be set as extended partition (only partitions 2-4 can be extended partitions) +EXTENDED_PARTITION_NR=4 + +PARTITIONSPATH="/proc/partitions" +# picking disk that will be used +DISKS=$(cat $PARTITIONSPATH | tr -s ' ' | cut -d ' ' -f5 | grep -e "[a-z]$") +if [ -z "$DISKS" ]; then + perror "Can't find an hard disk." +fi +#-------------------------------------------------------------------------------------- + +define_partition_table() { + if [ -z "$SLX_PARTITION_TABLE" ]; then + SLX_PARTITION_TABLE=' + 44,10G,/tmp + 45,10G,/var/scratch + 82,4G' + pwarning "You didn't define a partition table in config. Using default partition table:\n$SLX_PARTITION_TABLE" + fi +} + +size_conv() { + [ $(echo "$1<1024" | bc) -eq 1 ] && { echo "$1 B"; exit 0; } + local result="$(echo $1 | awk '{ sum=$1; hum[1024^3]="GB"; hum[1024^2]="MB"; hum[1024]="KB"; + for (x=1024^3; x>=1024; x/=1024){ + if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x]; break } + } + }')" + echo $result +} + +choose_disk() { + local disk_number=$(echo $DISKS | tr ' ' \\n | wc -l) + if [ "$SLX_AUTOMATIC_PARTITIONING" = "yes" ]; then + if [ -n "$SLX_CHOOSEN_DISK" ]; then + echo $DISKS | grep -wq "$SLX_CHOOSEN_DISK" || perror "ERROR: Automatic partitioning enabled, but in config specified $SLX_CHOOSEN_DISK was not found." + elif [ "$disk_number" -eq 1 ]; then + SLX_CHOOSEN_DISK=$DISKS + pwarning "Automatic partitioning enabled, but SLX_CHOOSEN_DISK not specified in config. Choosing only existing disk: $DISKS for partitioning." + else + perror "Automatic partitioning enabled, but SLX_CHOOSEN_DISK not specified in config. More than one disk exists, please specify disk in config and run again." + fi + else + local dialog_string="Existing disks with partition tables:\n\n" + local disksize="" + local freespace=0 + local parts="" + local partsize=0 + local used=0 + local options="" + + for disk in $DISKS; do + disksize=$(echo "$(cat $PARTITIONSPATH | grep -e $disk$ | tr -s ' ' | cut -d ' ' -f4)*$BLOCKSIZE" | bc) + parts=$(cat $PARTITIONSPATH | grep -e $disk[0-9] | tr -s ' ' | cut -d ' ' -f5) + dialog_string="${dialog_string}$disk $(size_conv $disksize)\n" + used=0 + options="${options}$disk $disk " + + for part in $parts; do + partsize=$(echo "$(cat $PARTITIONSPATH | grep -e $part$ | tr -s ' ' | cut -d ' ' -f4)*$BLOCKSIZE" | bc) + used=$(echo "$used+$partsize" | bc) + dialog_string="${dialog_string} $part $(size_conv $partsize)\n" + done + + dialog_string=$dialog_string"--------------------------------\n" + freespace=$(echo "$disksize-$used" | bc) + dialog_string="${dialog_string}Used $(size_conv $used)\n" + dialog_string="${dialog_string}Free $(size_conv $freespace)\n\n" + done + + if [ $(echo $DISKS | tr ' ' \\n | wc -l) -gt 0 ]; then + SLX_CHOOSEN_DISK=$(echo $options | xargs dialog --title "Choose a disk to partition:" --no-tags --cr-wrap --no-collapse --stdout --menu "${dialog_string}" 0 0 0) + if [ -z $SLX_CHOOSEN_DISK ]; then + pwarning "Partitioning aborted by user." + exit 1 + fi + else + perror "Can't find a hard disk." + fi + fi + CHOOSEN_DISK_SIZE=$(echo "$(cat $PARTITIONSPATH | grep -e $SLX_CHOOSEN_DISK$ | tr -s ' ' | cut -d ' ' -f4)*$BLOCKSIZE" | bc) + CHOOSEN_DISK_PARTS=$(cat $PARTITIONSPATH | grep -e $SLX_CHOOSEN_DISK[0-9] | tr -s ' ' | cut -d ' ' -f5) +} + +check_disk_size() { + # check if the choosen disk is greater than the new partition table size. + local part_number=$(echo $SLX_PARTITION_TABLE | wc -w) + local part_space=$(echo $SLX_PARTITION_TABLE | grep -oE '[0-9]+G' | awk '{ SUM += $0 } END { print SUM }') + NEEDED_DISK_SPACE=$(echo "$part_space*1024*1024*1024+$part_number*1024*1024" | bc) + + if [ $(echo "$CHOOSEN_DISK_SIZE<$NEEDED_DISK_SPACE" | bc) -eq 1 ]; then + perror "Insufficient space on disk /dev/$SLX_CHOOSEN_DISK\n DISK SIZE: $(size_conv $CHOOSEN_DISK_SIZE)\n REQUIRED SIZE: $(size_conv $NEEDED_DISK_SPACE)" + fi +} + +select_partition_type() { + # choosing the partition type (MSDOS or GPT) if it wasn't set in config file + if [ -z "$SLX_PARTITION_TYPE" ]; then + if [ "$SLX_AUTOMATIC_PARTITIONING" = "yes" ]; then + pwarning "SLX_PARTITION_TYPE not defined in config, using default: msdos" + SLX_PARTITION_TYPE=msdos + else + SLX_PARTITION_TYPE=$(dialog --no-tags --title "Choose a partition type:" --menu --stdout "Partitions types:" 0 0 0 "msdos" " MSDOS " "GPT" " GPT ") + if [ -z $SLX_PARTITION_TYPE ]; then + pwarning "Partitioning aborted by user." + exit 1 + fi + fi + fi +} + +confirm_partitioning() { + local dialog_string="New partition table after partitioning:\n\n" + local counter=1 + local id=0 + local size=0 + local mountpoint="" + local bootable=0 + local part_type="Primary" + for part in $SLX_PARTITION_TABLE; do + IFS=, + set $part + + id=$1 + shift + size=${1%G} + shift + [ -n $1 ] && mountpoint=$1 + shift + case "$*" in + *bootable*) bootable=1 ;; + *) bootable=0 ;; + esac + + [ "$id" = "82" ] && mountpoint="swap" + + if [ $SLX_PARTITION_TYPE = 'GPT' ]; then + #update dialog status + dialog_string="${dialog_string}${mountpoint} (/dev/"${SLX_CHOOSEN_DISK}${counter}")\n" + if [ "${id}" = "82" ]; then + dialog_string="${dialog_string} GUID: 0657FD6D-A4AB-43C4-84E5-0933C84B4F4F\n" + elif [ "${id}" = "83" ]; then + dialog_string="${dialog_string} GUID: 0FC63DAF-8483-4772-8E79-3D69D8477DE4\n" + else + dialog_string="${dialog_string} GUID: "$id"000000-0000-0000-0000-000000000000\n" + fi + dialog_string="${dialog_string} Size: "$size" GB\n" + else + if [ $counter -eq $EXTENDED_PARTITION_NR ]; then + part_type="Logical" + counter=$(($counter+1)) + fi + #update dialog + dialog_string="${dialog_string}$mountpoint (/dev/${SLX_CHOOSEN_DISK}$counter)\n" + dialog_string="${dialog_string} Type: $part_type\n" + dialog_string="${dialog_string} ID: $id\n" + dialog_string="${dialog_string} Size: $size GB\n" + fi + + counter=$(($counter+1)) + done + unset IFS + + dialog_string="${dialog_string}\nAll existing partitions on /dev/$SLX_CHOOSEN_DISK will be deleted, continue?" + + # asking confirmation to create the partitions + dialog --title "WARNING" --stdout --defaultno --yesno "$dialog_string" 0 0 + if [ $? -eq 1 ]; then + pwarning "Partitioning aborted by user." + exit 1 + fi +} + +# function to create gpt type partition tables (uses sgdisk) +partition_disk_gpt() { + # delete partition table + sgdisk -Z /dev/$SLX_CHOOSEN_DISK > /dev/null 2>&1 || perror "Error erasing old partition table" + + #set dialog + dialog_string="Partitions created:\n\n" + echo "0" | dialog --title "Partitioner" --stdout --gauge "$dialog_string" 0 0 0 + + # loop that will create each GPT partition, change GUIDS, change names, and set bootable flags. + local counter=1 + local part_number=$(echo $SLX_PARTITION_TABLE | wc -w) + local id=0 + local size=0 + local mountpoint="" + local bootable=0 + for part in $SLX_PARTITION_TABLE; do + IFS=, + set $part + + id=$1 + shift + size=${1%G} + shift + [ -n $1 ] && mountpoint=$1 + shift + case "$*" in + *bootable*) bootable=1 ;; + *) bootable=0 ;; + esac + + #create boot partition and set boot flag + if [ "$bootable" -eq 1 ]; then + sgdisk /dev/${SLX_CHOOSEN_DISK} -n ${counter}:0:+1M 1>&2 || perror "Error setting size of GPT partition ${SLX_CHOOSEN_DISK}${counter}" + sgdisk /dev/${SLX_CHOOSEN_DISK} -t ${counter}:21686148-6449-6E6F-744E-656564454649 1>&2 || perror "Error setting id ${id} of GPT partition ${SLX_CHOOSEN_DISK}${counter}" + sgdisk /dev/${SLX_CHOOSEN_DISK} -A ${counter}:set:2 1>&2 || perror "Error setting boot flag for GPT partition ${SLX_CHOOSEN_DISK}${counter}" + dialog_string="${dialog_string}Special bootloader partition (/dev/${SLX_CHOOSEN_DISK}${counter})\n" + dialog_string="${dialog_string} GUID: 21686148-6449-6E6F-744E-656564454649\n" + dialog_string="${dialog_string} Size: 1 MB\n" + counter=$(($counter+1)) + part_number=$(($part_number+1)) + fi + + #set size of partition + sgdisk /dev/${SLX_CHOOSEN_DISK} -n ${counter}:0:+${size}G 1>&2 || perror "Error setting size of GPT partition ${SLX_CHOOSEN_DISK}${counter}" + + #set id of partition + if [ "${id}" = "82" ] || [ "${id}" = "83" ]; then + sgdisk /dev/${SLX_CHOOSEN_DISK} -t ${counter}:${id}00 1>&2 || perror "Error setting id ${id} of GPT partition ${SLX_CHOOSEN_DISK}${counter}" + else + sgdisk /dev/${SLX_CHOOSEN_DISK} -t ${counter}:${id}000000-0000-0000-0000-000000000000 -c ${counter}:\"${mountpoint}\" 1>&2 || perror "Error setting id ${id} of GPT partition ${SLX_CHOOSEN_DISK}${counter}" + fi + + #update dialog status + [ "$id" = "82" ] && mountpoint="swap" + dialog_string="${dialog_string}${mountpoint} (/dev/${SLX_CHOOSEN_DISK}${counter})\n" + if [ "${id}" = "82" ]; then + dialog_string="${dialog_string} GUID: 0657FD6D-A4AB-43C4-84E5-0933C84B4F4F\n" + elif [ "${id}" = "83" ]; then + dialog_string="${dialog_string} GUID: 0FC63DAF-8483-4772-8E79-3D69D8477DE4\n" + else + dialog_string="${dialog_string} GUID: ${id}000000-0000-0000-0000-000000000000\n" + fi + dialog_string="${dialog_string} Size: ${size} GB\n" + echo $(echo "(100/$part_number)*$counter" | bc) | dialog --title "Partitioner" --stdout --gauge "${dialog_string}" 0 0 0 + + counter=$(($counter+1)) + done + unset IFS + + if [ "$SLX_AUTOMATIC_PARTITIONING" != "yes" ]; then + dialog --title "Partitioner" --stdout --msgbox "$dialog_string" 0 0 + fi +} + +#function to create msdos type partition tables (uses sfdisk) +partition_disk_msdos(){ + # delete partition table + sgdisk -Z /dev/$SLX_CHOOSEN_DISK > /dev/null 2>&1 || perror "Error erasing old partition table" + + #set dialog + dialog_string="Partitions created:\n\n" + + # constructing the sfdisk input file + echo "unit: sectors"> /tmp/partitiontable.tmp + + # loop that will create each GPT partition, change GUIDS, change names, and set bootable flags. + local counter=1 + local id=0 + local size=0 + local mountpoint="" + local bootable=0 + local part_type="Primary" + local start=$(echo "1*1024*1024/$SECTORSIZE" | bc) + for part in $SLX_PARTITION_TABLE; do + IFS=, + set $part + + id=$1 + shift + size=${1%G} + shift + [ -n $1 ] && mountpoint=$1 + shift + case "$*" in + *bootable*) bootable=1 ;; + *) bootable=0 ;; + esac + + if [ $counter -eq $EXTENDED_PARTITION_NR ]; then + echo "/dev/${SLX_CHOOSEN_DISK}$counter : start= $start, size= $(echo "${CHOOSEN_DISK_SIZE}/$SECTORSIZE-1" | bc), Id= 5" >> /tmp/partitiontable.tmp + part_type="Logical" + start=$(echo "$start+1*1024*1024/$SECTORSIZE" | bc) + counter=$(($counter+1)) + fi + + size_bytes=$(echo "${size}*1024*1024*1024/$SECTORSIZE" | bc) + + if [ "$bootable" -eq 1 ]; then + echo "/dev/${SLX_CHOOSEN_DISK}$counter : start= $start, size= $size_bytes, Id= $id, bootable " >> /tmp/partitiontable.tmp + else + echo "/dev/${SLX_CHOOSEN_DISK}$counter : start= $start, size= $size_bytes, Id= $id" >> /tmp/partitiontable.tmp + fi + + start=$(echo "$start+$size_bytes+1*1024*1024/$SECTORSIZE" |bc) + + #update dialog + [ "$id" = "82" ] && mountpoint="swap" + dialog_string="${dialog_string}$mountpoint (/dev/${SLX_CHOOSEN_DISK}$counter)\n" + dialog_string="${dialog_string} Type: $part_type\n" + dialog_string="${dialog_string} ID: $id\n" + dialog_string="${dialog_string} Size: $size GB\n" + + counter=$(($counter+1)) + done + unset IFS + + sfdisk -q --no-reread -f /dev/${SLX_CHOOSEN_DISK} < /tmp/partitiontable.tmp 1>&2 || return 0 + # rm -f /tmp/partitiontable.tmp + + if [ "$SLX_AUTOMATIC_PARTITIONING" != "yes" ]; then + dialog --title "Partitioner" --stdout --msgbox "$dialog_string" 0 0 + else + dialog --title "Partitioner" --stdout --infobox "$dialog_string" 0 0 + sleep 5 + fi +} + + +define_partition_table +choose_disk +select_partition_type +if [ "$SLX_AUTOMATIC_PARTITIONING" != "yes" ]; then + confirm_partitioning +fi +check_disk_size + +if [ $SLX_PARTITION_TYPE = 'GPT' ]; then + partition_disk_gpt +else + partition_disk_msdos +fi + +if [ "$SLX_AUTOMATIC_PARTITIONING" != "yes" ]; then + #reboot prompt + dialog --title "reboot computer" --stdout --yes-label "Reboot" --no-label "Continue" --yesno "For changes to take effect you have to reboot your machine." 5 65 + [ $? -eq 0 ] && reboot || exit 0 +else + reboot +fi + +####################### CODE FOR THE PRESERVATION OF EXISTING PARTITIONS (EXPERIMENTAL) ######################### +#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' +# NONEXISTENT_PARTITIONS=0 +# for (( i = 1; i < $COUNTER; i++ )); do +# if [ $i -ne 4 ]; then +# for PART in $PARTS; do +# if [ "${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}" = "82" ]; then +# sgdisk /dev/$SLX_CHOOSEN_DISK -i ${PART:3} 2> /dev/null | grep "0657FD6D-A4AB-43C4-84E5-0933C84B4F4F" > /dev/null +# elif [ "${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}" = "83" ]; then +# sgdisk /dev/$SLX_CHOOSEN_DISK -i ${PART:3} 2> /dev/null | grep "0FC63DAF-8483-4772-8E79-3D69D8477DE4" > /dev/null +# else +# sgdisk /dev/$SLX_CHOOSEN_DISK -i ${PART:3} 2> /dev/null | grep ${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}"000000-0000-0000-0000-000000000000" > /dev/null +# fi +# if [ $? -eq 0 ]; then +# PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]=$PART +# NEEDED_SPACE=$(($NEEDED_SPACE-${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}*1024*1024*1024)) +# break +# fi +# done +# if [[ ${PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]} = "0" ]]; then +# NONEXISTENT_PARTITIONS=$(($NONEXISTENT_PARTITIONS+1)) +# fi +# fi +# done +# # checking if there's sufficient space for the new partitions. +# if [[ $FREESPACE -lt $NEEDED_SPACE ]]; then +# dialog --msgbox "ERROR: Insufficient free space in disk /dev/$SLX_CHOOSEN_DISK." 6 40 +# exit 1 +# fi +# if [[ $NONEXISTENT_PARTITIONS -eq 0 ]]; then +# dialog_string="All your requested partitions already exists.\n\n" +# dialog_string=$dialog_string"Partition ID\n" +# for (( i = 1; i < $COUNTER; i++ )); do +# if [[ $i -ne 4 ]]; then +# dialog_string=$dialog_string"${PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]} ${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}\n" +# fi +# done +# else +# dialog_string="Creating partitions...\n\n" +# echo "0" | dialog --gauge "$dialog_string" 0 0 0 +# CREATED=0 +# for (( i = 1; i < $COUNTER; i++ )); do +# j=1 +# if [[ $i -ne 4 ]]; then +# if [[ "${PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]}" = "0" ]]; then +# sgdisk /dev/$SLX_CHOOSEN_DISK -n $j:0:+${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}G 2> /dev/null > /dev/null +# while [[ $? -eq 4 ]]; do +# j=$(($j+1)) +# sgdisk /dev/$SLX_CHOOSEN_DISK -n $j:0:+${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}G 2> /dev/null > /dev/null +# done +# PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]=$SLX_CHOOSEN_DISK$j +# SIZE=$((${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}*1024*1024*1024)) +# ID=${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]} +# MOUNTPOINT=${PARTTBL["$SLX_CHOOSEN_DISK$i/mountpoint"]} +# dialog_string=$dialog_string$MOUNTPOINT" (/dev/"${PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]}")\n" +# if [ "${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}" = "82" ]; then +# dialog_string=$dialog_string" GUID: 0657FD6D-A4AB-43C4-84E5-0933C84B4F4F\n" +# elif [ "${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}" = "83" ]; then +# dialog_string=$dialog_string" GUID: 0FC63DAF-8483-4772-8E79-3D69D8477DE4\n" +# else +# sgdisk /dev/$SLX_CHOOSEN_DISK -t $j:$ID"000000-0000-0000-0000-000000000000" -c $j:$MOUNTPOINT >/dev/null 2>/dev/null +# dialog_string=$dialog_string" GUID: "$ID"000000-0000-0000-0000-000000000000\n" +# fi +# dialog_string=$dialog_string" Size: "$SIZE" Bytes ("${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}" GB)\n" +# CREATED=$(($CREATED+1)) +# echo $(( $CREATED*100/($NONEXISTENT_PARTITIONS) )) | dialog --gauge "$dialog_string" 0 0 0 +# fi +# if [ ${PARTTBL["$SLX_CHOOSEN_DISK$i/bootable"]} -eq 1 ]; then +# sgdisk /dev/$SLX_CHOOSEN_DISK -A $j:set:2 >/dev/null 2>/dev/null +# fi +# fi +# done +# 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 |