Pages

Categories

Expanding x86 OpenWrt Root Partition

November 15, 2011 — macbruins

The root partition of the official x86 OpenWrt image is not very big, about 50 MiB. Many find it too small after installing a few add-on packages. Here I will cover the steps to expand it. The resultant image can be used in a live USB (see Easy Live USB for x86 OpenWRT) or copied to a hard disk.

Procedure Outline

Get an uncompressed disk image.

Pad image to desired size

Attach the image file to a loop device

Edit image partition table to enlarge the root partition

Resize the file system in root partition

Detach the image from the loop device.

All commands below are run in Bash.

Uncompress Image File

Use whichever method you like to download an image file from OpenWrt (http://downloads.openwrt.org) and uncompress it using gzip. For example, these two commands download and uncompress the 10.03.1-rc6 disk image.

The first command uses “losetup -f” to find an unused device and stores the result in the shell variable loop_dev. The “echo” command shows the device found. Finally “losetup” attaches the device to the disk image.

Edit Partition Table

To expand a disk partition, it needs to be deleted first. A new, larger partition is then created to take its place. This new partition must start from the same sector as the old to prevent loss of data.

fdisk is used to manipulate the disk partition table.

bash$ fdisk -u=sectors -c=dos $loop_dev

The -u option asks fdisk to list partitions in sectors. The -c option tells fdisk to operate in DOS compatibility mode. $loop_dev is the loop device attached to the image file.

fdisk shows /dev/loop3 has 209839 sectors. It also lists two partitions. The first one, /dev/loop3p1, is a small boot partition. The second, /dev/loop3p2, is the root partition. The root partition starts from sector 9135. Make a note of this number.

Now delete the root partition and create a new one that covers all available space.

The “d” command asks fdisk to delete a partition, and “2” selects the second partition for deletion. The “n” command asks fdisk to create a new partition. “p” specifies a primary partition, and “2” selects the second primary partition. The first sector of this partition is sector 9135, same as the deleted partition. Its last sector is sector 209838, the default choice. This is also the last sector on /dev/loop3. Finally, the “w” command writes the new partition table through /dev/loop3 to the disk image.

Resize Root File System

The following commands will expand the root file system to the size of the root partition.

The “kpartx -a” command creates device nodes for the partitions in the disk image. The output of “kpartx –a” (“mknod for loop3p1 failed”) seems to be a bug in my system. As far as I can tell, the creation and deletion of loop3p1 occur normally.

Another thing worth noting: kpartx and fdisk use different naming conventions. kpartx uses “/dev/mapper/device_name”, for example “/dev/mapper/loop3p1”. fdisk uses “/dev/device_name”, such as “/dev/loop3p1”. This is because kpartx works with the device mapper.

Now run “fsck” to check the file system before resizing it. In fact, some file systems can’t be resized until they are checked.