Need to expand rootfs online. Looking for alternatives

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Need to expand rootfs online. Looking for alternatives

145 Views
bgaurav1718
Contributor III
I am currently working with NXP's iMX8MP-EVK and I created a custom image using yocto. The image is 6.6GB in size and after flashing this image into eMMC and booting through it, I found the remaining portion of eMMC (32GB - 6.6GB) is unallocated.

I could use a SD to boot into the device, unmount the eMMC's rootfs, extend the partition and filesystem to utilize that space. However, the SD card slot won't be available in the future custom design. So I don't find this idea useful.

Another solution I found was to use bitbake's IMAGE_ROOTFS_EXTRA_SPACE variable to build a image with necessary size. Again, the image size will be large and writing a single image into a eMMC could take much longer time, which I guess won't be efficient.

I am trying to expand the rootfs size using a script that runs during first boot up (shown below) of the system only. Although the same script (with slight modifications) works fine when trying to expand rootfs contained in an SD card, in case of eMMC, it doesn't work and complains about the mounted file system which is expected. I don't know why it worked on SD card.

To sum up, I have a root filesystem which needs to be expanded to fill the available space when the device boots up. I cannot use alternative boot media like USB or SD card. Is there any suitable solution that could work for the situation?

 

#!/bin/bash

# Script to expand root partition and file system during first boot

# Check if this is the first boot by looking for a marker file
marker_file="/root/.firstboot_done"
if [ -f "$marker_file" ]; then
    echo "First boot operations already completed. Exiting."
    exit 0
fi

# Identify the root disk and partition
root_mount=$(mount | grep ' / ' | cut -d' ' -f1)
root_disk=$(echo "$root_mount" | awk '{print $1}' | sed 's/[0-9]*$//;s/p$//')
root_partition=$(echo "$root_mount" | awk '{print $1}' | grep -o '[0-9]$')

#echo "Root disk: $root_disk"

# Expand the root partition (assuming /dev/sda and partition 2, adjust as needed)
echo "Expanding partition $root_disk"
parted $root_disk resizepart $root_partition 100%

# Inform the kernel about the partition table changes
#echo "Informing kernel about partition table changes"
partprobe $root_disk

# Resize the file system (assuming ext4, adjust as needed)
echo "Resizing file system on $root_disk"
sudo resize2fs ${root_mount}

# Create a marker file to prevent repeated execution
echo "Creating marker file: $marker_file"
touch $marker_file

echo "Root partition and file system expansion completed."
Tags (3)
0 Kudos
Reply
2 Replies

110 Views
Zhiming_Liu
NXP TechSupport
NXP TechSupport

Hi

Using IMAGE_ROOTFS_EXTRA_SPACE to change rootfs size is most recommended. Also you can use script to extend it, you can try to use parted tool to resize your rootfs.

https://www.gnu.org/software/parted/manual/parted.html

Best Regards
Zhiming

88 Views
bgaurav1718
Contributor III
Thank you for the response.
As I mentioned previously in the question, using this method is not efficient for our case. We are supposed to write the image into hundreds of devices, for which, using 25-30 GB image takes nearly 1 hour. However, using image of 6-7GB will reduce this time to nearly 15 mins, which is a massive advantage I guess.
0 Kudos
Reply