Resizing filesystem at first boot

This commit is contained in:
baiywt 2022-01-06 14:51:49 +08:00
parent 15212ed11b
commit c7ef92382c
3 changed files with 66 additions and 0 deletions

View File

@ -0,0 +1,9 @@
#!/bin/sh /etc/rc.common
START=22
start() {
rm -rf /etc/rc.d/S22resize-rootfs
#service resize-rootfs disable
/usr/bin/resize-rootfs.sh
}

View File

@ -0,0 +1 @@
../init.d/resize-rootfs

View File

@ -0,0 +1,56 @@
#!/bin/bash
FDISK=$(which fdisk) || { echo "E: You must have fdisk" && exit 1; }
LOSETUP=$(which losetup) || { echo "E: You must have losetup" && exit 1; }
roottype=$(findmnt -n -o SOURCE / -o FSTYPE)
case ${roottype} in
overlay)
FSCKEXT4=$(which fsck.ext4) || { echo "E: You must have fsck.ext4" && exit 1; }
RESIZE2FS=$(which resize2fs) || { echo "E: You must have resize2fs" && exit 1; }
rootsource=$(findmnt -n -o SOURCE /rom) # i.e. /dev/mmcblk0p2
rootdevice=${rootsource%p*} # i.e. /dev/mmcblk0
partitions=${rootsource##*p}
# Resizing partitions
lastsector=$(${FDISK} -l ${rootdevice} |grep "Disk ${rootdevice}" |awk '{print $7}')
lastsector=$(( $lastsector - 1 ))
startfrom=$(${FDISK} -l ${rootdevice} |grep ${rootsource} |awk '{print $2}')
partend=$(${FDISK} -l ${rootdevice} |grep ${rootsource} |awk '{print $3}')
[[ $lastsector -eq $partend ]] && exit 0
(echo d; echo $partitions; echo n; echo p; echo ; echo $startfrom; echo $lastsector ; echo w;) | fdisk $rootdevice
# Start resizing filesystem
LOOP="$(losetup -n -O NAME | sort | sed -n -e "1p")"
ROOT="$(losetup -n -O BACK-FILE ${LOOP} | sed -e "s|^|/dev|")"
OFFS="$(losetup -n -O OFFSET ${LOOP})"
LOOP="$(losetup -f)"
${LOSETUP} -o ${OFFS} ${LOOP} ${ROOT}
${FSCKEXT4} -y ${LOOP}
${RESIZE2FS} ${LOOP}
reboot
;;
ext4)
FSCKEXT4=$(which fsck.ext4) || { echo "E: You must have fsck.ext4" && exit 1; }
RESIZE2FS=$(which resize2fs) || { echo "E: You must have resize2fs" && exit 1; }
rootsource=$(findmnt -n -o SOURCE / | sed 's~\[.*\]~~') # i.e. /dev/mmcblk0p2
rootdevice=${rootsource%p*} # i.e. /dev/mmcblk0
partitions=${rootsource##*p}
# Resizing partitions
lastsector=$(${FDISK} -l ${rootdevice} |grep "Disk ${rootdevice}" |awk '{print $7}')
lastsector=$(( $lastsector - 1 ))
startfrom=$(${FDISK} -l ${rootdevice} |grep ${rootsource} |awk '{print $2}')
partend=$(${FDISK} -l ${rootdevice} |grep ${rootsource} |awk '{print $3}')
[[ $lastsector -eq $partend ]] && exit 0
(echo d; echo $partitions; echo n; echo p; echo ; echo $startfrom; echo $lastsector ; echo w;) | fdisk $rootdevice
# Start resizing filesystem
LOOP="$(losetup -f)"
${LOSETUP} ${LOOP} ${rootsource}
${FSCKEXT4} -y ${LOOP}
${RESIZE2FS} ${LOOP}
reboot
;;
esac
exit 0