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:
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:
Once that is done, we can find out what name to loop device has by issuing:
Once that is done, it would be wise to check it to ensure that the system had been shutdown cleanly
Assuming no errors, you should get something like:
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:
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:
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.
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:
Now we need to change the partition table. We can use fdisk to do this:
If we press command “p” to print the current partition table:
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:
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:
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:
alternatively use imagewriter to write the image to the SD card.
Lastly put it into the BBB and see if it boots 🙂