Categories
Linux/Unix

Resize QEMU image for Openwrt

While using ext4 combine image from Openwrt website directly, you will find the openwrt system only about 100MB disk. The disk has to be resized before resize the partition and filesystem. Here is the steps you can follow.

First, resize the qemu image. Reference.

# in host OS, resize the qemu image
> qemu-img resize vm-100-disk-0.raw 32G

After Openwrt system booted up. Reference.

fdisk /dev/sda

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


Command (m for help): p
Disk /dev/sda: 32 GiB, 34359738368 bytes, 67108864 sectors
Disk model: QEMU HARDDISK
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: 0x529e51f2

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

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

Partition 2 has been deleted.

Command (m for help): p
Disk /dev/sda: 32 GiB, 34359738368 bytes, 67108864 sectors
Disk model: QEMU HARDDISK
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: 0x529e51f2

Device     Boot Start   End Sectors Size Id Type
/dev/sda1  *      512 33279   32768  16M 83 Linux

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p):

Using default response p.
Partition number (2-4, default 2):
First sector (33280-67108863, default 34816): 33792
Last sector, +/-sectors or +/-size{K,M,G,T,P} (33792-67108863, default 67108863):

Created a new partition 2 of type 'Linux' and of size 32 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.

Resize ext4 filesystem after partition was resized. Reference.

opkg update
opkg install losetup resize2fs
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 VM rebooted, now you have get Openwrt with full-size disk. Remember, after a sysupgrade, you have to resize partition and filesystem again.

# fdisk -l
Disk /dev/sda: 32 GiB, 34359738368 bytes, 67108864 sectors
Disk model: QEMU HARDDISK
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: 0x529e51f2

Device     Boot Start      End  Sectors Size Id Type
/dev/sda1  *      512    33279    32768  16M 83 Linux
/dev/sda2       33792 67108863 67075072  32G 83 Linux

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.