Change partition sizes after Sysupgrade on x86

How do I change the partition sizes after Sysupgrade on x86 router to utilize more of the drive? I am using 22.03.2 with Ext4. The hard drive is 75 GB. I don't mind to try to change the partitions through terminal because it's headless but I do have access to USB ports.
I have :

root@OpenWrt:~# df
Filesystem           1K-blocks      Used Available  Use%   Mounted on
/dev/root               104788     21880     80780   21%      /
tmpfs                  4045312      1224   4044088    0%      /tmp
/dev/sda1                16084      5284     10476   34%      /boot
/dev/sda1                16084      5284     10476   34%      /boot
tmpfs                      512         0       512    0%      /dev

1 Like

GNU parted can do that.

2 Likes

Following https://teklager.se/en/knowledge-base/openwrt-installation-instructions/

root@OpenWrt:~# parted /dev/sda print
Model: ATA ST380815AS (scsi)
Disk /dev/sda: 80.0GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      262kB   17.0MB  16.8MB  primary  ext2         boot
 2      17.3MB  126MB   109MB   primary  ext2

root@OpenWrt:~# parted /dev/sda resizepart 2 10G
Information: You may need to update /etc/fstab.

root@OpenWrt:~# parted /dev/sda print
Model: ATA ST380815AS (scsi)
Disk /dev/sda: 80.0GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      262kB   17.0MB  16.8MB  primary  ext2         boot
 2      17.3MB  10.0GB  9983MB  primary  ext2

root@OpenWrt:~# resize2fs /dev/sda2
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/sda2 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
resize2fs: Invalid argument While checking for on-line resizing support

How do I proceed from here?

Before any specific advice. can be given, it is important to know what kind of system we're working with. You mentioned ext4; this is important. Also important (but I don't think I see it in your question; if I missed it, please point it out): is your system BIOS-based or UEFI-based? In other words, when you did sysupgrade, which ext4 image did you use, generic-ext4-combined-efi.img.gz (UEFI) or generic-ext4-combined.img.gz (BIOS)?.

Also, please run this:

fdisk -l

and post the output here. This will have specific information that we will need to give you specific recommendations. If you don't have fdisk on your system, install it:

opkg update && opkg install fdisk

For general preparedness, I also recommend reviewing the OpenWrt on x86 Hardware page:

https://openwrt.org/docs/guide-user/installation/openwrt_x86

There are sections there on partition resizing.

Basically, there are two possibilities for an ext4 system.

If you have an ext4 system with BIOS, you'll need to do two things, (1) resize the root partition, and (2) resize the root file system.

If you have an ext4 system with UEFI, it's slightly more complicated. The two tasks above still need to be done, but there are a couple more. So you will need to (0) make a note of your current partition layout, (1) resize the partition, (1.5) manually update the grub configuration, and (2) resize the root file system.

I don't want to confuse you with the details you may or may not need, so please respond with two things, (1) BIOS or UEFI?, and (2) the output of fdisk -l.

Thanks for looking at this. It is BIOS and I used generic-ext4-combined.img.gz.

 fdisk -l
Disk /dev/sda: 74.51 GiB, 80000000000 bytes, 156250000 sectors
Disk model: ST380815AS
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xc120d9f5

Device     Boot Start    End Sectors  Size Id Type
/dev/sda1  *      512  33279   32768   16M 83 Linux
/dev/sda2       33792 246783  212992  104M 83 Linux


Disk /dev/sdb: 74.51 GiB, 80000000000 bytes, 156250000 sectors
Disk model: WDC WD800JD-75MS
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xd0f4738c

Device     Boot     Start       End   Sectors  Size Id Type
/dev/sdb1              63     80324     80262 39.2M de Dell Utility
/dev/sdb2  *        80325 149934644 149854320 71.5G  7 HPFS/NTFS/exFAT
/dev/sdb3       149934645 156232124   6297480    3G db CP/M / CTOS / ...

I pasted the info about sdb so that you are aware of it but will ignore it going forward unless it is needed.

Great! Here's what you need to do.

First, let's take a look at the output of fdisk you posted. Device /dev/sda2 starts at sector 33792. This is an important number we will have to use.

Next, let's get all the utilities we need (you already have fdisk, so I am not including it in the list):

opkg update && opkg install losetup resize2fs

Now, run an interactive fdisk session. It will look something like the listing below. This will do three things, (1) delete the current partition 2 (aka /dev/sda2), (2) create a new partition 2 that will start where the old partition started and end at the boundary of available space, and (3) write the new partition information on disk. Since there won't be a write in-between (1) and (2), the net result of all this will be an extended partition 2 with a different partition ID. But, since we have BIOS and not UEFI, we needn't worry about the changed partition ID.

The manual inputs are shown [[in double square brackets]]; the [[]] sequence indicates pressing Enter with no visible input. Obviously, when you enter your data, you don't need any square brackets; I'm just using them as markers. It's a bit confusing, but I can't come up with a better formatting scheme right now. So here goes:

root@OpenWrt:~# [[fdisk /dev/sda]] 

Welcome to fdisk (util-linux 2.37.3).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.


Command (m for help): [[d]]
Partition number (1,2, default 2): [[2]]

Partition 2 has been deleted.

Command (m for help): [[n]]
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): [[p]]
Partition number (2-4, default 2): [[2]]
First sector (XXXX-XXXX, default XXXX): [[33792]]
Last sector, +/-sectors or +/-size{K,M,G,T,P} 
    (XXXX-XXXX, default XXXX): [[]]

Created a new partition 2 of type 'Linux' and of size XX.X GiB.
Partition #2 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: [[n]]

Command (m for help): [[w]]

The partition table has been altered.
Syncing disks.

root@OpenWrt:~# 

Now our root partition has been resized. You can run fdisk -l again to verify that the size of /dev/sda2 has changed.

The next (and final) step is to resize the filesystem that lives on that partition. Be sure to execute these commands one by one (meaning, don't paste all of them into the command line at once; allow one command to finish before entering the next one):

BOOT="$(sed -n -e "/\s\/boot\s.*$/{s///p;q}" /etc/mtab)"
DISK="${BOOT%%[0-9]*}"
PART="$((${BOOT##*[^0-9]}+1))"
ROOT="${DISK}${PART}"
LOOP="$(losetup -f)"
losetup ${LOOP} ${ROOT}
fsck.ext4 -y ${LOOP}
resize2fs ${LOOP}
reboot

After the device reboots, you should be able to use the entire sda drive.

Technically, you shouldn't do any of it on a live drive, as there's a possibility of corruption in both stages, but in practice it tends to work in overwhelming majority of cases, especially on a device that's essentially idle. If you're reluctant to do it this way, boot your device from a USB drive, umount your target drive and do the exact same thing, but while running from the USB drive. The only difference would be in the very end; you would do halt rather than reboot, take out the USB drive, and restart the device manually by pressing the power button...

That seemed to go very well! Here are my steps with the messages that resulted. Please let me know if anything would need additional attention.

Welcome to fdisk (util-linux 2.37.3).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.


Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (33280-156249999, default 34816): 33792
Last sector, +/-sectors or +/-size{K,M,G,T,P} (33792-156249999, default 156249999): +30G

Created a new partition 2 of type 'Linux' and of size 30 GiB.
Partition #2 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: N

Command (m for help): w

The partition table has been altered.
Syncing disks.

root@OpenWrt:~# fdisk -l
Disk /dev/sda: 74.51 GiB, 80000000000 bytes, 156250000 sectors
Disk model: ST380815AS
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xc120d9f5

Device     Boot Start      End  Sectors Size Id Type
/dev/sda1  *      512    33279    32768  16M 83 Linux
/dev/sda2       33792 62949375 62915584  30G 83 Linux

root@OpenWrt:~# BOOT="$(sed -n -e "/\s\/boot\s.*$/{s///p;q}" /etc/mtab)"
root@OpenWrt:~# DISK="${BOOT%%[0-9]*}"
root@OpenWrt:~# PART="$((${BOOT##*[^0-9]}+1))"
root@OpenWrt:~# ROOT="${DISK}${PART}"
root@OpenWrt:~# LOOP="$(losetup -f)"
root@OpenWrt:~# losetup ${LOOP} ${ROOT}
root@OpenWrt:~# fsck.ext4 -y ${LOOP}
e2fsck 1.46.5 (30-Dec-2021)
rootfs was not cleanly unmounted, check forced.
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
Free blocks count wrong (20958, counted=20912).
Fix? yes

Free inodes count wrong (5131, counted=5121).
Fix? yes

Padding at end of inode bitmap is not set. Fix? yes


rootfs: ***** FILE SYSTEM WAS MODIFIED *****
rootfs: 1535/6656 files (0.0% non-contiguous), 5712/26624 blocks
root@OpenWrt:~# resize2fs ${LOOP}
resize2fs 1.46.5 (30-Dec-2021)
Resizing the filesystem on /dev/loop0 to 7864320 (4k) blocks.
The filesystem on /dev/loop0 is now 7864320 (4k) blocks long.

Insert "Hold on to your butt meme" here

root@OpenWrt:~# reboot

Nothing jumps out at me. As a final check, you could log in to LuCI and see what LuCi thinks the disk size is. It would be on the Overview page, just scroll down past System and Memory sections.

Looks good in LuCi, about 29 Gb. Thank you so much for your help!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.