BBB – mount and shrink a disk image

This describes how to take an image file (origBBB.img) that has been extracted from a Beaglebone Black (BBB) – (see here for how to do that) – and shrink it so that it will fit onto a smaller SD card.

First we need to be able to inspect the file system locally.  We can inspect the image file with:

file origBBB.img
origBBB.img: x86 boot sector; partition 1: ID=0xc, active, starthead 1, startsector 63, 144522 sectors; partition 2: ID=0x83, starthead 45, startsector 147456, 6979584 sectors, code offset 0x0

we can see that there are two partitions (1 = FAT which is boot and 2 = EXT4 which is the linux file system). On the SD card, the sectors are 512 bytes in size. Partition 2 starts at sector 147456 which means that the offset from the start of the file is (512 bytes * 147456) which is 75497472 bytes.

In order to access the partition, we can create a linux loop device using losetup as follows:

sudo losetup -f --offset 75497472 origBBB.img

Once that is done, we can find out what name to loop device has by issuing:

sudo losetup -a

which gives:

/dev/loop0: [0805]:49318322 (/home/martin/Documents/path/to/file/origBBB.img), offset 75497472

Once that is done, it would be wise to check it to ensure that the system had been shutdown cleanly

sudo e2fsck -f /dev/loop0

Assuming no errors, you should get something like:

e2fsck 1.42 (29-Nov-2011)
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
Angstrom-Cloud9-: 50821/218160 files (0.1% non-contiguous), 321878/872448 blocks

So now in order to access it, we need to mount it. If you have Nautilus, it may appear in the Devices pane so you can mount it by just clicking on it but alternatively you can issue:

sudo mkdir /home/martin/origBBB
sudo mount /dev/loop0 /home/martin/origBBB

It’s useful to be able to mount an image like this in case you want to read or write to the image – remember though, you probably need to use sudo on most commands when accessing the image as pretty much everything will be owned by root.

when you are finished, you can unmount it with:

umount /home/martin/origBBB

We know the FAT partition is just under 100MB and we are aiming at a 2GB SD card image so we need to resize the linux partition to be say 1700MB. The space available in the BBB eMMC is apparently 1923088384 bytes so we will make the image that size.

sudo resize2fs /dev/loop0 1700M

That reports:

resize2fs 1.42 (29-Nov-2011)
Resizing the filesystem on /dev/loop0 to 435200 (4k) blocks.
The filesystem on /dev/loop0 is now 435200 blocks long.

In a 4k block, there are 8 x 512 bytes and there are 512 bytes per sector. So we need at least 8 x 435200 = 3481600 sectors in partition 2.

Now we disconnect the loop device:

sudo losetup -d /dev/loop0

Now we need to change the partition table. We can use fdisk to do this:

sudo fdisk origBBB.img

If we press command “p” to print the current partition table:

Command (m for help): p

Disk origBBBsm.img: 7914 MB, 7914651648 bytes
255 heads, 63 sectors/track, 962 cylinders, total 15458304 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
origBBBsm.img1 * 63 144584 72261 c W95 FAT32 (LBA)
origBBBsm.img2 147456 7127039 3489792 83 Linux

We can therefore see our Linux partition starts at sector 147456 and currently ends at sector 7127039. We need to adjust it so that while it still starts at sector 147456, it has a length of 3481600 sectors which means the last sector should be (147456 + 3481600 – 1) = 3629055.

Use the fdisk commands:
“d” to delete a partition
“2″ to specify that we want to delete partition 2
“n” to create a new partition
“p” to specify that is it to be a primary partition
“2″ to specify that it is to be the second partition
“147456″ to specify the starting sector (ignore the default offered)
“3629055″ to specift the last sector (ignore the default offered)
“w” to write the new table to disk

After that, if we issue the file command again, we can view the new partition table info:

file origBBB.img
origBBBsm.img: x86 boot sector; partition 1: ID=0xc, active, starthead 1, startsector 63, 144522 sectors; partition 2: ID=0x83, starthead 45, startsector 147456, 3481600 sectors, code offset 0x0

Which shows that we now have 3481600 sectors in partition 2 as we wished.

We need to truncate the file now and we know the last sector number (3629055) and the number of bytes per sector (512) so we can calculate the required file size which in this case is ((3629055 + 1) * 512) = 1858076672 bytes. The +1 is because it is zero based so we have one more sector than the last sector number. We can therefore truncate the image file as follows:

sudo truncate -s 1858076672 origBBB.img

Note, we could have chosen a slightly larger size for the root file system when we resized it so that we made better use of the 2GB SD card but this is adequate for demonstration. Now this image can be written to the SD card. Locate the device name of your SD card (e.g. /dev/sdb) and then use the dd command:

sudo dd if=origBBB.img of=/dev/sdb bs=4MB

alternatively use imagewriter to write the image to the SD card.

Lastly put it into the BBB and see if it boots :-)

Comments are closed.