From 2666c3b1bcdf018a060ebbeb63a8dc76ff3b9452 Mon Sep 17 00:00:00 2001 From: Nikolay Beketov Date: Fri, 12 Apr 2024 09:55:18 +0700 Subject: [PATCH] Fix storage disk extension for NVME devices --- src/Core/System/ConsoleMenu.php | 1 + .../System/RootFS/etc/rc/resize_storage_part | 228 ++++++++++-------- 2 files changed, 133 insertions(+), 96 deletions(-) diff --git a/src/Core/System/ConsoleMenu.php b/src/Core/System/ConsoleMenu.php index 0288549ae..5e6183154 100644 --- a/src/Core/System/ConsoleMenu.php +++ b/src/Core/System/ConsoleMenu.php @@ -642,6 +642,7 @@ public function validate(string $input): bool echo "resize storage return $return_var"; sleep(2); if ($return_var === 0) { + file_put_contents('/tmp/ejectcd', ''); $pbx_rebootPath = Util::which('pbx_reboot'); Processes::mwExecBg($pbx_rebootPath); } diff --git a/src/Core/System/RootFS/etc/rc/resize_storage_part b/src/Core/System/RootFS/etc/rc/resize_storage_part index 3575991a7..cff203ae2 100755 --- a/src/Core/System/RootFS/etc/rc/resize_storage_part +++ b/src/Core/System/RootFS/etc/rc/resize_storage_part @@ -19,71 +19,92 @@ # disk="${1}"; -disk_name="$(basename "$disk")"; +disk_name=$(basename $disk); +SYSTEM_SIZE_MB=600 # Check if the block device exists -if [ ! -b "${disk}" ]; then - echo " - ERROR block devise ${disk} not found"; - exit 1; -fi; +check_disk() { + if [ ! -b "$1" ]; then + echo " - ERROR: block device $1 not found"; + exit 1; + fi; +} -# Determine the partition number based on whether it's a system disk or a storage disk -part=4; -storageDev="$(/bin/lsblk -r -p | /bin/busybox grep "$(/bin/busybox basename "$disk")" | /bin/busybox cut -d ' ' -f 1 | /bin/busybox grep "4$" | /bin/busybox sort -u)"; -systemPart="$storageDev"; -if [ ! -b "${storageDev}" ]; then - part=1; - storageDev="$(/bin/lsblk -r -p | /bin/busybox grep "$(/bin/busybox basename "$disk")" | /bin/busybox cut -d ' ' -f 1 | /bin/busybox grep "1$" | /bin/busybox sort -u)"; -fi; +# Check block device and partition +check_disk "$disk" -if [ ! -b "${storageDev}" ]; then - echo " - ERROR block devise ${disk} not found"; - exit 4; -fi; -tmp_dir='/storage/usbdisk1'; -mkdir -p "$tmp_dir"; +# Define partition name and number +determine_partition() { + local base_name="$1" + # Determine the partition number based on whether it's a system disk or a storage disk + mountedStorage=$(mount | /bin/busybox grep storage | /bin/busybox awk '{ print $1}'); -# Mount the partition -mount "${storageDev}" "$tmp_dir"; -if [ "$?" != "0" ]; then - echo " - ERROR mount storage disk ${storageDev}..."; - rm -rf "$tmp_dir"; - exit 2; -fi; + # Get the name of the mounted storage and the 4th partition (if it exists) + partitionName=$(/bin/lsblk -r -p -o NAME,TYPE | /bin/busybox grep ' part' | /bin/busybox sort -u | cut -d ' ' -f 1 | /bin/busybox grep "$base_name" | /bin/busybox grep "4$") + # If the 4th partition is not the same as the mounted storage + if [ "$partitionName" == "$mountedStorage" ]; then + echo " - Storage on the same disk as the system ..." + partitionNumber=4 + else + echo " - Storage on the separate disk from the system one..." + partitionName=$(/bin/lsblk -r -p -o NAME,TYPE | /bin/busybox grep ' part' | /bin/busybox sort -u | cut -d ' ' -f 1 | /bin/busybox grep "$base_name" | /bin/busybox grep "1$") + partitionNumber=1 + fi +} -# Get the total size of the disk in megabytes -total_byte=`busybox blockdev --getsize64 $disk`; -total=`expr $total_byte / 1024 / 1024`; +determine_partition "$disk_name" -# Get the size of the partition in megabytes -disk_size=`/bin/busybox df -P -m | /bin/busybox grep "${storageDev}" | busybox head -n 1 | /bin/busybox awk '{ print $2}'`; +# Check block device and partition +check_disk "$partitionName" -# Unmount the partition -umount "${storageDev}"; +# Function to get disk size in megabytes +get_disk_size() { + local disk_device=$1 + total_byte=$(busybox blockdev --getsize64 "$disk_device") + total=$((total_byte / 1024 / 1024)) + echo $total +} -# Calculate the difference in size (delta) between the total size and partition size -delta=`expr $total - $disk_size`; +# Function to get partition size in megabytes +get_partition_size() { + local partition=$1 + disk_size=$(/bin/busybox df -P -m | /bin/busybox grep "$partition" | busybox head -n 1 | /bin/busybox awk '{print $2}') + echo $disk_size +} -echo " - disk: '${storageDev}'"; -echo " - total_size: ${total}Mb"; -echo " - partition_size: ${disk_size}Mb"; -if [ -b "${systemPart}" ]; then - echo " - is SYSTEM disk: '${storageDev}'"; - system_size=600; - if [ "$delta" -le "$system_size" ]; then - delta=0; - else - # Decrease delta by the size of the system image - delta=`expr $delta - $system_size`; - fi; -fi; -echo " - delta: ${delta}Mb"; +# Get the total size of the disk in megabytes +total=$(get_disk_size "$disk") +# Get the size of the partition in megabytes +disk_size=$(get_partition_size "$partitionName") +# Calculate the difference in size (delta) between the total size and partition size +delta=$((total - disk_size)) + +# Check the partition number and manage delta accordingly +calculate_delta() { + local delta=$1 + local partition_number=$2 + local disk=$3 + if [[ "$partition_number" == "4" ]]; then + if (( delta <= SYSTEM_SIZE_MB )); then + delta=0 + else + delta=$((total - disk_size)) + fi + fi + echo $delta +} +delta=$(calculate_delta "$delta" "$partitionNumber" "$disk") + +echo " - Disk: '$disk'" +echo " - Total size: ${total}Mb" +echo " - Partition size: ${disk_size}Mb" +echo " - Delta: ${delta}Mb" # Calculate the portion as a percentage multiplied by 100 -portion=`expr $delta \* 10000 / $total `; -if [ "$portion" -le "500" ]; then +portion=$((delta * 10000 / total)) +if [[ "$portion" -le 500 ]]; then # The delta is less than or equal to 5 percent, so no resizing will be performed echo " - delta is less than five percent."; echo " - the partition size will not be changed."; @@ -92,59 +113,74 @@ if [ "$portion" -le "500" ]; then fi; # Check if the disk is currently mounted -is_mounted=$(mount | grep "$disk"); -if [ "${is_mounted}x" != "x" ]; then - echo ' - free storage...'; - /sbin/freestorage; -fi; - -echo " - delete part ${storageDev} / add new part ${storageDev}..."; - -if [ -b "${disk}4" ]; then - # Clear existing configuration for system disk - /sbin/freeupoffload; - fdisk "$disk" > /dev/null 2> /dev/null << EOF +check_mount_and_free() { + local disk=$1 + local partition_name=$2 + is_mounted=$(mount | grep "$disk") + if [ "${is_mounted}x" != "x" ]; then + echo " - Free storage partition $partition_name ..." + /sbin/freestorage + fi +} + +check_mount_and_free "$disk" "$partitionName" + +# Partition operations based on partition number +partition_operations() { + local disk=$1 + local partition_number=$2 + local partition_name=$3 + if [[ "$partition_number" == "4" ]]; then + # Clear existing configuration for system disk + /sbin/freeupoffload + echo " - Delete 4-th partition $partition_name from the system disk ..." + fdisk "$disk" > /dev/null 2>&1 << EOF d -${part} +$partition_number w EOF - # Create new partition for system disk - /sbin/initial.storage.part.four create "$disk"; - sleep 2; - - need_reboot=1; -else - start="$(/bin/busybox fdisk -l "$disk" | /bin/busybox grep -v sectors | /bin/busybox grep "$disk" | /bin/busybox awk '{ print $4 }')" - fdisk "$disk" > /dev/null 2> /dev/null << EOF + echo " - Create the new 4-th partition on the system disk ..." + /sbin/initial_storage_part_four create "$disk" + sleep 2 + else + echo " - Delete 1-st partition $partition_name from the storage disk and create new with whole available space ..." + start="$(/bin/busybox fdisk -l "$disk" | /bin/busybox grep -v sectors | /bin/busybox grep "$disk" | /bin/busybox awk '{ print $4 }')" + fdisk "$disk" > /dev/null 2>&1 << EOF d n p -${part} -${start} +$partition_number +$start w EOF -fi; - -# Update the partition table -/sbin/blockdev --rereadpt "${disk}" > /dev/null 2>&1; -sleep 2; - -# Get the file system type for the partition -fs_type=$(/sbin/blkid -ofull "${storageDev}"); -if [ "${fs_type}x" != "x" ]; then - echo " - e2fsck ${disk} part ${part}..."; - e2fsck -f -p "${storageDev}"; - sleep 2; - echo " - resize2fs ${disk} part ${part}..." - resize2fs "${storageDev}"; - sleep 2; -else - echo " - error blkid $?" -fi; + fi +} + +partition_operations "$disk" "$partitionNumber" "$partitionName" + +echo " - Update the partition table on $disk ..." +/sbin/blockdev --rereadpt "$disk" > /dev/null 2>&1 +sleep 2 + +# Get the file system type for the partition and perform file system operations +perform_fs_operations() { + local partition_name=$1 + fs_type=$(/sbin/blkid -ofull "$partition_name") + if [ "${fs_type}x" != "x" ]; then + echo " - Start e2fsck $partition_name ..." + e2fsck -f -p "$partition_name" + sleep 2 + echo " - Start resize2fs $partition_name ..." + resize2fs "$partition_name" + sleep 2 + else + echo " - Error blkid $?" + exit 1 + fi +} -if [ "${need_reboot}x" != "x" ]; then - touch /tmp/ejectcd; - /sbin/pbx_reboot; -fi; +perform_fs_operations "$partitionName" +echo " - Resize of the storage disk was successfully done." +exit 0 \ No newline at end of file