Skip to content

Commit

Permalink
fix fs-resize would break on 4GB drives, and EEROMS was not properly …
Browse files Browse the repository at this point in the history
…formatted on USB drives (#921)

* fs-resize: fix disk smaller than 4GiB won't be resized, and part3 of USB drive aren't properly formated
* fs-resize: fix some typo and add disclaimer
* fs-resize: use sector size 512 bytes instead of retriving from device
* fs-resize: reformat the codes to improve readability
* fs-resize: reformat the codes to improve readability, again
* fs-resize: omit EEROMS for 4GB drives, and adjust order of the size figuring logic
  • Loading branch information
7Ji committed May 15, 2022
1 parent 14a9dc1 commit bf4536c
Showing 1 changed file with 68 additions and 46 deletions.
114 changes: 68 additions & 46 deletions packages/sysutils/busybox/scripts/fs-resize
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ if [ -e /storage/.please_resize_me ] ; then
case $PART in
"/dev/mmcblk"* | "/dev/nvme"*)
DISK=$(echo $PART | sed s/p2$//g)
ROMS_PART_NAME="${DISK}p3"
;;
*)
DISK=$(echo $PART | sed s/2$//g)
ROMS_PART_NAME="${DISK}3"
;;
esac

Expand All @@ -38,6 +40,23 @@ if [ -e /storage/.please_resize_me ] ; then
if [ ! -z "$DISK" -a ! -z "$PART" ] ; then
umount $PART

DISK_NAME=$(basename $DISK)
DISK_SECTORS=$(cat "/sys/block/$DISK_NAME/size") # Obtain the disk sectors count, each sector is always 512 Bytes large, independent of the underlying device, according to https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/types.h#n117
DISK_SIZE=$(( $DISK_SECTORS * 512 )) # Calculate the disk actual size, in byte
if [ $DISK_SIZE -gt 4294967296 ]; then # The optimal behaviour, this will leave 2GiB to storage, and create the EEROMS partition
ROMS_CREATE='yes'
STORAGE_END='4GiB'
elif [ $DISK_SIZE -gt 3221225472 ]; then # Optionaly omit the EEROMS partition for 4GB cards/drives
ROMS_CREATE=''
STORAGE_END='100%'
else
# The bare minumum a EmuELEC image itself needs is 2186280960, but after adding in user configs things become uncontrollable, so we don't care about disks with size between 2085MiB and 3GiB
echo 'ERROR: You disk is too small! You need to use a USB drive/SD card that is at least 4GB!'
echo 'ERROR: You should NOT try to boot up this drive as it is NOT properly resized!'
StartProgress countdown "Powering off in 30s... " 30 "NOW"
poweroff
fi

echo "PARTITION RESIZING IN PROGRESS"
echo ""
echo "Please do not reboot or turn off your @DISTRONAME@ device!"
Expand All @@ -49,55 +68,58 @@ if [ -e /storage/.please_resize_me ] ; then
StartProgress spinner "Checking layout... " "sgdisk -e $DISK &>/dev/null"
fi

# EmueELEC Get EEROMS filetype
ROM_FS_TYPE="vfat"
PARTED_FS_TYPE="fat32"
# EmueELEC Get EEROMS filetype
ROM_FS_TYPE="vfat"
PARTED_FS_TYPE="fat32"

if [ -e "/flash/ee_fstype" ]; then
EE_FS_TYPE=$(cat "/flash/ee_fstype")

case $EE_FS_TYPE in
"ntfs")
ROM_FS_TYPE="ntfs"
PARTED_FS_TYPE="ntfs"
;;
"ext4")
ROM_FS_TYPE="ext4"
PARTED_FS_TYPE="ext4"
;;
"exfat")
ROM_FS_TYPE="exfat"
PARTED_FS_TYPE="ntfs"
;;
*)
# Failsafe
ROM_FS_TYPE="vfat"
PARTED_FS_TYPE="fat32"
;;
esac

fi
StartProgress spinner "Resizing partition... " "parted -s -a optimal -m $DISK resizepart 2 4GiB &>/dev/null"
if [ -e "/flash/ee_fstype" ]; then
EE_FS_TYPE=$(cat "/flash/ee_fstype")

case $EE_FS_TYPE in
"ntfs")
ROM_FS_TYPE="ntfs"
PARTED_FS_TYPE="ntfs"
;;
"ext4")
ROM_FS_TYPE="ext4"
PARTED_FS_TYPE="ext4"
;;
"exfat")
ROM_FS_TYPE="exfat"
PARTED_FS_TYPE="ntfs"
;;
*)
# Failsafe
ROM_FS_TYPE="vfat"
PARTED_FS_TYPE="fat32"
;;
esac

fi

StartProgress spinner "Resizing partition... " "parted -s -a optimal -m $DISK resizepart 2 $STORAGE_END &>/dev/null"
StartProgress spinner "Checking file system... " "e2fsck -f -p $PART &>/dev/null"
StartProgress spinner "Resizing file system... " "resize2fs $PART &>/dev/null"
StartProgress spinner "Creating EEROMS partition..." "parted -s -a optimal -m $DISK mkpart primary ${PARTED_FS_TYPE} 4296MB 100% &>/dev/null"
partprobe &>/dev/null

case $ROM_FS_TYPE in
"ntfs")
StartProgress spinner "Formatting EEROMS partition as NTFS..." "mkfs.ntfs -L EEROMS -f ${DISK}p3 &>/dev/null"
;;
"ext4")
StartProgress spinner "Formatting EEROMS partition as EXT4..." "mkfs.ext4 -L EEROMS -t ext4 -O 64bit ${DISK}p3 &>/dev/null"
;;
"exfat")
StartProgress spinner "Formatting EEROMS partition as EXFAT..." "mkfs.exfat -n EEROMS ${DISK}p3 &>/dev/null"
;;
*)
StartProgress spinner "Formatting EEROMS partition as FAT32..." "mkfs.vfat -n EEROMS ${DISK}p3 &>/dev/null"
;;
esac

if [ "$ROMS_CREATE" ]; then
StartProgress spinner "Creating EEROMS partition..." "parted -s -a optimal -m $DISK mkpart primary ${PARTED_FS_TYPE} 4GiB 100% &>/dev/null"
partprobe &>/dev/null
case $ROM_FS_TYPE in
"ntfs")
StartProgress spinner "Formatting EEROMS partition as NTFS..." "mkfs.ntfs -L EEROMS -f ${ROMS_PART_NAME} &>/dev/null"
;;
"ext4")
StartProgress spinner "Formatting EEROMS partition as EXT4..." "mkfs.ext4 -L EEROMS -t ext4 -O 64bit ${ROMS_PART_NAME} &>/dev/null"
;;
"exfat")
StartProgress spinner "Formatting EEROMS partition as EXFAT..." "mkfs.exfat -n EEROMS ${ROMS_PART_NAME} &>/dev/null"
;;
*)
StartProgress spinner "Formatting EEROMS partition as FAT32..." "mkfs.vfat -n EEROMS ${ROMS_PART_NAME} &>/dev/null"
;;
esac
else
echo 'WARNING: EEROMS partition is omitted for 4GB drives'
fi
StartProgress countdown "Rebooting in 5s... " 5 "NOW"
fi
fi
Expand Down

0 comments on commit bf4536c

Please sign in to comment.