diff --git a/src/Core/System/PBXInstaller.php b/src/Core/System/PBXInstaller.php index fcbba279..954973cf 100644 --- a/src/Core/System/PBXInstaller.php +++ b/src/Core/System/PBXInstaller.php @@ -205,7 +205,6 @@ private function proceedInstallation(): void // Start the installation process echo Util::translate("Installing PBX...").PHP_EOL; $this->unmountPartitions(); - $this->convertDiscLayout(); $this->unpackImage(); $this->mountStorage(); $this->copyConfiguration(); @@ -215,44 +214,6 @@ private function proceedInstallation(): void System::reboot(); } - /** - * Converting the disk layout - * @return void - */ - private function convertDiscLayout():void - { - $disk = $this->target_disk; - if (!file_exists($disk)) { - $disk = "/dev/$disk"; - } - if (!file_exists($disk)) { - return; - } - $gDiskPath = Util::which('gdisk'); - if (empty($gDiskPath)) { - return; - } - $echoPath = Util::which('echo'); - $partedPath = Util::which('parted'); - $command = "$partedPath --script $disk print 2>/dev/null | grep 'Partition Table' | awk '{print $3}'"; - exec($command, $partitionTypeOutput, $partedStatus); - if ($partedStatus !== 0 || empty($partitionTypeOutput)) { - return; - } - $partitionType = trim($partitionTypeOutput[0]); - if ($partitionType === "msdos") { - echo " - Converting from MBR to GPT...\n"; - $gDiskCommand = "$echoPath -e \"w\\nY\\n\" | $gDiskPath $disk > /dev/null 2>&1"; - exec($gDiskCommand, $gDiskOutput, $gDiskStatus); - if ($gDiskStatus === 0) { - echo " - The conversion to GPT has been completed successfully.\n"; - } else { - echo " - Error converting to GPT.\n"; - } - } - } - - /** * Unmount the partitions of the selected disk. */ diff --git a/src/Core/System/RootFS/sbin/initial_storage_part_four b/src/Core/System/RootFS/sbin/initial_storage_part_four index 458b0632..d9ac3594 100644 --- a/src/Core/System/RootFS/sbin/initial_storage_part_four +++ b/src/Core/System/RootFS/sbin/initial_storage_part_four @@ -20,7 +20,7 @@ # Check whether the script has been run with the "create" argument; if not, exit with status code 6 action="$1" if [ "$action" != "create" ] && [ "$action" != "update" ]; then - echo "Invalid argument: $1. Please use 'create' or 'update'." + echo " - Invalid argument: $1. Please use 'create' or 'update'." exit 6 fi @@ -40,7 +40,7 @@ fi; # Check if the device exists and is a block device if [ ! -b "${dev}" ];then - echo "Device ${dev} doesn't exist or isn't a block device." + echo " - Device ${dev} doesn't exist or isn't a block device." exit 4; # If the device doesn't exist or isn't a block device, exit with status code 4 fi; @@ -48,27 +48,40 @@ fi; totalMb=$(($(busybox blockdev --getsize64 "${dev}") / 1024 / 1024)); if [ "$totalMb" -le "600" ]; then - echo "Device ${dev} size is less than or equal to 600 MB, there's no point in creating a 4th partition." + echo " - Device ${dev} size is less than or equal to 600 MB, there's no point in creating a 4th partition." exit 0; fi # Looking for a way to the fourth section -partitionFour=$(/bin/lsblk -r "$dev" -p -o NAME,TYPE| grep "part$" | /bin/busybox cut -d ' ' -f 1 | /bin/busybox grep "4$"); +if /sbin/parted -s "${dev}" print 2> /dev/null | /usr/bin/awk '{$1=$1; print}' | /bin/busybox grep -w '^4' > /dev/null; then + partitionFour=$(/bin/lsblk -r "$dev" -p -o NAME,TYPE | grep "part$" | /bin/busybox cut -d ' ' -f 1 | /bin/busybox grep "4$"); +else + partitionFour='' +fi + # Get partition info for the device -count=$(/sbin/sfdisk -l "${dev}" | /bin/busybox grep "^/dev" | /bin/busybox wc -l); +count=$(/sbin/sfdisk -l "${dev}" 2>/dev/null | /bin/busybox grep -c "^/dev"); if [ -n "$partitionFour" ]; then # The fourth partition already exists, format it and resize - echo "Formatting and resizing the existing fourth partition ${partitionFour}..." - /sbin/parted --script "${dev}" resizepart 4 100% + echo " - Formatting and resizing the existing fourth partition ${partitionFour}..." + /sbin/parted --script "${dev}" resizepart 4 100% 2>/dev/null; elif [ "$count" -eq 3 ] || [ "$count" -eq 4 ]; then + echo " - Fix the secondary GPT header;" + echo -e "x\ne\nm\nw\nY\n" | /bin/gdisk "$dev" > /dev/null 2>&1; # There are only 3 partitions, add a new one - echo "Adding a new partition №4..." + echo " - Adding a new partition №4..." # Use parted to create a new partition on the device. # The new partition is primary, has ext4 filesystem, starts from 600MiB, and occupies the rest of the disk. - /sbin/parted --script --align optimal "${dev}" 'mkpart primary ext4 600MiB 100%'; + /sbin/parted --script --align optimal "${dev}" 'mkpart primary ext4 600MiB 100%' > /dev/null 2>&1; + if command -v gdisk > /dev/null 2>&1; then + # correcting the gap between the main table and the first used sector + echo -e "x\ne\nj\nm\nw\nY\n" | /bin/gdisk "$dev" > /dev/null 2>&1; + # fix the partition table and use all the space + echo "fix" | parted "$dev" print free ---pretend-input-tty > /dev/null 2>&1; + fi; else # Not the expected number of partitions; exit - echo "Unexpected number of partitions. Exiting." + echo " - Unexpected number of partitions. Exiting." exit 0 fi diff --git a/src/Core/System/RootFS/sbin/pbx_firmware b/src/Core/System/RootFS/sbin/pbx_firmware index d7731a74..94ed71bb 100644 --- a/src/Core/System/RootFS/sbin/pbx_firmware +++ b/src/Core/System/RootFS/sbin/pbx_firmware @@ -108,12 +108,30 @@ else fi # Inform the user of the installation process +DISK="/dev/${systemDevice}"; if [ -r "${img_file}" ]; then if command -v gdisk > /dev/null 2>&1; then - PARTITION_TYPE=$(/sbin/parted --script "$DISK" print 2>/dev/null | grep "Partition Table" | awk '{print $3}') - echo " - Performing an action for the msdos section" + PARTITION_TYPE=$(/sbin/parted --script "$DISK" print 2>/dev/null | grep "Partition Table" | awk '{print $3}'); + echo " - Performing an action for the $PARTITION_TYPE section"; if [ "$PARTITION_TYPE" == "msdos" ]; then - echo -e "w\nY\n" | gdisk "/dev/${systemDevice}" > /dev/null 2>&1; + echo " - At the end of the 4th partition, 1 Mb of free space will be required..."; + PARTITION="4" + REDUCE_BY=1 + LAST_ENTRY=$(parted -s "$DISK" unit MiB print free 2>/dev/null | tail -n 1) + FREE_SPACE=$(echo "$LAST_ENTRY" | awk '{print $3}' | grep -o '[0-9]*') + if [[ -z "$FREE_SPACE" || "$FREE_SPACE" -lt "$REDUCE_BY" ]]; then + echo " - Reducing the 4th section..." + SECTOR_SIZE=$(blockdev --getss "$DISK") + SECTORS_IN_MIB=$((1024 * 1024 / SECTOR_SIZE)) + sfdisk -d "$DISK" 2> /dev/null > disk_layout.txt + CURRENT_SIZE=$(grep "^$DISK$PARTITION" disk_layout.txt | grep -o 'size=[ ]*[0-9]*' | tr -d ' ' | cut -d= -f2) + NEW_SIZE=$((CURRENT_SIZE - REDUCE_BY * SECTORS_IN_MIB)) + sed -i "s|^\($DISK$PARTITION.*size=\s*\)[0-9]*\(.*\)$|\1$NEW_SIZE\2|" disk_layout.txt + sfdisk "$DISK" > /dev/null 2>&1 < disk_layout.txt + rm disk_layout.txt; + fi + echo " - Converting markup to GPT." + echo -e "w\nY\n" | /bin/gdisk "$DISK" > /dev/null 2>&1; resultGDisk="$?"; if [ "$resultGDisk" -eq 0 ]; then echo " - The conversion to GPT has been completed successfully." @@ -123,23 +141,23 @@ if [ -r "${img_file}" ]; then fi fi - echo " - Installing new image on /dev/${systemDevice}" + echo " - Installing new image on $DISK" # Use pv to monitor the progress of the data being piped to the dd command - if test -w /dev/ttyS0 && ! /bin/busybox setserial -g /dev/ttyS0 | /bin/grep -q unknown; then - /bin/pv -p "${img_file}" | /bin/gunzip | dd of="/dev/${systemDevice}" bs=4M 2>&1 | /bin/busybox tee /dev/ttyS0 >/dev/null - else - /bin/pv -p "${img_file}" | /bin/gunzip | dd of="/dev/${systemDevice}" bs=4M >/dev/null 2>&1; - fi + if test -w /dev/ttyS0 && ! /bin/busybox setserial -g /dev/ttyS0 | /bin/grep -q unknown; then + /bin/pv -p "${img_file}" | /bin/gunzip | dd of="$DISK" bs=4M 2>&1 | /bin/busybox tee /dev/ttyS0 >/dev/null + else + /bin/pv -p "${img_file}" | /bin/gunzip | dd of="$DISK" bs=4M >/dev/null 2>&1; + fi resultPv="$?"; # If the pv command fails or there aren't three primary partitions, restore the system image from the backup - if [ ! "$resultPv" = '0' ] || [ ! "$(/sbin/parted --script --align optimal "/dev/${systemDevice}" "print" | grep primary | /bin/busybox wc -l)" = '3' ]; then - if [ -f "$backupImg" ]; then + if [ ! "$resultPv" = '0' ] || [ ! "$(/bin/lsblk -l "$DISK" | /bin/busybox grep -c part)" = '4' ]; then + if [ -f "$backupImg" ]; thenif [ ! "$resultPv" = '0' ] || [ ! "$(/bin/lsblk -l "$DISK" | /bin/busybox grep -c part)" = '4' ]; then echo " - There are some errors, restoring system from backup to /dev/${systemDevice}" if test -w /dev/ttyS0 && ! /bin/busybox setserial -g /dev/ttyS0 | /bin/grep -q unknown; then - /bin/pv -p "$backupImg" | /bin/gunzip | dd of="/dev/${systemDevice}" bs=4M 2>&1 | /bin/busybox tee /dev/ttyS0 >/dev/null + /bin/pv -p "$backupImg" | /bin/gunzip | dd of="$DISK" bs=4M 2>&1 | /bin/busybox tee /dev/ttyS0 >/dev/null else - /bin/pv -p "$backupImg" | /bin/gunzip | dd of="/dev/${systemDevice}" bs=4M >/dev/null 2>&1; + /bin/pv -p "$backupImg" | /bin/gunzip | dd of="$DISK" bs=4M >/dev/null 2>&1; fi fi; fi; @@ -147,12 +165,12 @@ if [ -r "${img_file}" ]; then echo " - The execution of the dd command was finished..."; # Update the partition table - /sbin/initial_storage_part_four update "/dev/${systemDevice}"; + /sbin/initial_storage_part_four update "$DISK"; echo " - Check if it necessary to update 4-th storage partition. The result is ${?}..."; fi # Reread the partition table of the system device -/sbin/blockdev --rereadpt "/dev/${systemDevice}" > /dev/null 2>&1 +/sbin/blockdev --rereadpt "$DISK" > /dev/null 2>&1 sleep 3; # Get the name of the 3rd partition