Wednesday, June 25, 2014

Resize partitions for SoC computers and other linux devices (raspberry pi, beaglebone black, etc...)

This only uses fdisk and resize2fs.  I ran out of space on the beaglebone black so I could not use parted for partition editing but the following works just as well.  The same procedure below applies to raspberry pi's too.  The bbb is running debian wheezy burned on a 32GB micro sd card.  Raspi-config simplifies the following with one step, but it also used parted for it's file recreation.  We don't have that package at our disposal so here is the alternative.

enter: df -h

root@beaglebone:/media/Angstrom# df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          1.6G  1.6G     0 100% /
udev             10M     0   10M   0% /dev
tmpfs           100M  3.1M   97M   4% /run
/dev/mmcblk0p2  1.6G  1.6G     0 100% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   70M   27M  73% /boot/uboot
/dev/mmcblk1p2  1.7G  1.1G  519M  68% /media/Angstrom
/dev/mmcblk1p1   70M   54M   16M  78% /media/

*************************************************************************
Clearly the rootfs has no more space available.  We want to find the partition that the rootfs is referencing by looking at the filesystems that start with "/dev/" and are mounted on the same location as the rootfs.  In this case (and it most cases with SoC computers) it appears here as /dev/mmcblk0p2.
*************************************************************************


enter: fdisk -l

root@beaglebone:~# fdisk -l

Disk /dev/mmcblk0: 31.9 GB, 31914983424 bytes
4 heads, 16 sectors/track, 973968 cylinders, total 62333952 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

        Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1   *        2048      198655       98304    e  W95 FAT16 (LBA)
/dev/mmcblk0p2          198656    62333951    31067648   83  Linux

Disk /dev/mmcblk1: 1920 MB, 1920991232 bytes
255 heads, 63 sectors/track, 233 cylinders, total 3751936 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

        Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk1p1   *          63      144584       72261    c  W95 FAT32 (LBA)
/dev/mmcblk1p2          144585     3743144     1799280   83  Linux

Disk /dev/mmcblk1boot1: 1 MB, 1048576 bytes
4 heads, 16 sectors/track, 32 cylinders, total 2048 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mmcblk1boot1 doesn't contain a valid partition table

Disk /dev/mmcblk1boot0: 1 MB, 1048576 bytes
4 heads, 16 sectors/track, 32 cylinders, total 2048 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mmcblk1boot0 doesn't contain a valid partition table

*************************************************************************
This will tell us where the /dev/mmcblk0p2 partition is located.  It appears under the disk /dev/mmcblk0 which makes sense because it has 31.9 GB of total usable space.  We have made a direct correlation with the disk needed to be modified and the 32GB card installed in the bbb.
*************************************************************************


enter: fdisk /dev/mmcblk0

root@beaglebone:/media/Angstrom# fdisk /dev/mmcblk0  

Command (m for help): p

Disk /dev/mmcblk0: 31.9 GB, 31914983424 bytes
4 heads, 16 sectors/track, 973968 cylinders, total 62333952 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

        Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1   *        2048      198655       98304    e  W95 FAT16 (LBA)
/dev/mmcblk0p2          198656     3481599     1641472   83  Linux

Command (m for help): d
Partition number (1-4): 2

Command (m for help): p

Disk /dev/mmcblk0: 31.9 GB, 31914983424 bytes
4 heads, 16 sectors/track, 973968 cylinders, total 62333952 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

        Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1   *        2048      198655       98304    e  W95 FAT16 (LBA)

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (1-4, default 2): 2
First sector (198656-62333951, default 198656):  (press enter here)
Using default value 198656
Last sector, +sectors or +size{K,M,G} (198656-62333951, default 62333951):  (press enter here)
Using default value 62333951

Command (m for help): p

Disk /dev/mmcblk0: 31.9 GB, 31914983424 bytes
4 heads, 16 sectors/track, 973968 cylinders, total 62333952 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

        Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1   *        2048      198655       98304    e  W95 FAT16 (LBA)
/dev/mmcblk0p2          198656    62333951    31067648   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
root@beaglebone:/media/Angstrom# reboot

Broadcast message from root@beaglebone (pts/0) (Wed Jun 25 15:37:07 2014):
The system is going down for reboot NOW!

*************************************************************************
What we did was delete the partition from the partition table, not the actual partition itself so the data still remains intact when we create a new partition.  After the second partition has been deleted and recreated, we reboot.
*************************************************************************

log in again and reenter: df -h

root@beaglebone:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          1.6G  1.6G     0 100% /
udev             10M     0   10M   0% /dev
tmpfs           100M  540K   99M   1% /run
/dev/mmcblk0p2  1.6G  1.6G     0 100% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   70M   27M  73% /boot/uboot

*************************************************************************
We have free space! ... wait.... huh?
The available space is still 0???

... remember, we just modified the partition table, not the partition itself.  Now we have resize the actual partition in accordance with the partition table.
*************************************************************************

enter: resize2fs /dev/mmcblk0p2

root@beaglebone:~# resize2fs /dev/mmcblk0p2
resize2fs 1.42.5 (29-Jul-2012)
Filesystem at /dev/mmcblk0p2 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 2
The filesystem on /dev/mmcblk0p2 is now 7766912 blocks long.
root@beaglebone:~# reboot

*************************************************************************
From the previous output of "df -h", we want to modify the partition itself which would be /dev/mmcblk0p2 since that is the rootfs described in the first step.  Now the physical partition is resized.
*************************************************************************

enter: df -h

root@beaglebone:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs           30G  1.6G   27G   6% /
udev             10M     0   10M   0% /dev
tmpfs           100M  540K   99M   1% /run
/dev/mmcblk0p2   30G  1.6G   27G   6% /
tmpfs           249M     0  249M   0% /dev/shm
tmpfs           249M     0  249M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/mmcblk0p1   96M   70M   27M  73% /boot/uboot

*************************************************************************
Bam! We have copious amounts of free space.  Enjoy!
*************************************************************************