Skip to content

Resizing Bootable uSD card

Adam YH Lee edited this page Jul 30, 2015 · 5 revisions

Here is an example of how one might resize the root partition to make the most use of the available space in a uSD card. This is helpful as the default wic kickstart file generates a DD-able image that fits into a 2GiB large uDS card. For example, about 6GiB of space would be left unused if your uSD is 8GiB large.

Note that everything described in this page is done from your workstation, not from your Gumstix. Also, resizing cannot be done while the partition is mounted and being used.

In summary, we will use:

  1. parted tool to resize the root partition.
  2. resize2fs to expand the root filesystem to fit into the newly resized partition.

1. Resize the Root Partition

Launch parted utility like below. Make sure to use the correct path to your uSD. It is /dev/sdb here. The optimal option aligns partitions for better performance.

$ sudo parted /dev/sdb -a optimal
GNU Parted 2.3
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.

Remove the swap partition with rm 3 command. We can't grow the size of the root partition if the swap partition is located right after it. Check the partition table with the p command. You should only see 2 partitions.

(parted) rm 3                                                             
(parted) p                                                                
Model: Generic- SD/MMC (scsi)
Disk /dev/sdb: 8010MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
 
Number  Start   End     Size    Type     File system  Flags
 1      65.5kB  16.8MB  16.8MB  primary  fat16        boot, lba
 2      25.2MB  1421MB  1396MB  primary  ext4

Now also remove the root partition. Note that removal of the partition does not actually wipe the data.

(parted) rm 2

Create a new root partition with a larger size with mkpart command. The partition type is primary, and the start of the partition is 25.2MB, and the end is 7500MB. Here the start value is taken from the default partition table that we observed from step 1.

(parted) mkpart primary 25.2MB 7500MB                                     
(parted) p
Model: Generic- SD/MMC (scsi)
Disk /dev/sdb: 8010MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
 
Number  Start   End     Size    Type     File system  Flags
 1      65.5kB  16.8MB  16.8MB  primary  fat16        boot, lba
 2      25.2MB  7500MB  7475MB  primary  ext4

Now we can also recreate the swap partition. This time, we will fill it up to the end of the disk by specifying 100%.

(parted) mkpart primary 7501MB 100%
(parted) p                                                                
Model: Generic- SD/MMC (scsi)
Disk /dev/sdb: 8010MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
 
Number  Start   End     Size    Type     File system  Flags
 1      65.5kB  16.8MB  16.8MB  primary  fat16        boot, lba
 2      25.2MB  7500MB  7475MB  primary  ext4
 3      7500MB  8010MB  510MB   primary

Quit out of parted by pressing q.

2. Resize the Root Filesystem

Now that we have resized the partition, we need to adjust the filesystem to make the use of the newly available space. Run the resize2fs tool on the root partition. It will automatically grow the filesystem to take 100% of the available space.

$ sudo resize2fs /dev/sdb2
resize2fs 1.42 (29-Nov-2011)
Resizing the filesystem on /dev/sdb2 to 1825024 (4k) blocks.
The filesystem on /dev/sdb2 is now 1825024 blocks long.

Now you can make the swap filesystem with mkswap tool:

$ sudo mkswap /dev/sdb3
Setting up swapspace version 1, size = 497660 KiB
no label, UUID=aa659e16-54cd-4b56-bd4e-ff20a9cc6adf

And you are done!

Optional: Remove Swap Partition

If you decided not to make a swap partition above, it is necessary to remove the entry for the swap partition in /etc/fstab in the root directory of the bootable uSD card (not your host system). Open up the fstab file, and delete the entry for the swap partition (the last line in this case):

root@overo:~# vim /etc/fstab
# stock fstab - you probably want to override this with a machine specific one

/dev/root            /                    auto       defaults              1  1
proc                 /proc                proc       defaults              0  0
devpts               /dev/pts             devpts     mode=0620,gid=5       0  0
usbdevfs             /proc/bus/usb        usbdevfs   noauto                0  0
tmpfs                /run                 tmpfs      mode=0755,nodev,nosuid,strictatime 0  0
tmpfs                /var/volatile        tmpfs      defaults              0  0

# uncomment this if your device has a SD/MMC/Transflash slot
#/dev/mmcblk0p1       /media/card          auto       defaults,sync,noauto  0  0

/dev/mmcblk0p3  swap    swap    defaults        0       0