Beaglebone black – Working with Debian(Linux) images

Beaglebone black – Working with Debian(Linux) images

In this article I hope to cover many different ways in which to work with a pre-existing Debian image. This image could be one of your own, or could be an official, or testing image for the Beaglebone black. Also hopefully, I’ll try to keep this information short, sweet, and to the point. However, I think it will also be necessary to modularize, or separate this information into several sections – Which again, hopefully means it is easier to read through as a reference. So without further adieu . . .

dd – creating, and restoring images

Using dd is pretty simple once you understand the basics of how it works. For example:

Copy existing image file to sdcard.

dd obviously is the command. Think of “if= . . .” as “in file”, “of= . . .” as “out file”. So we’re taking a previously created dd image as input, and writing that image to the /dev/sdb block device. Which in this case, /dev/sdb is the sdcard reader with an sdcard inserted on one of my laptops. Which is also running Debian Wheezy.

In order to create a block device image using dd, you must simply reverse the parameters above. Example:

Make a copy of block device to an image file

Pretty simple, Yes? Do keep in mind, that it would wise to create descriptive image file names. As the above does not really explain what the block device image *is*. Unless you happen to be Robert Nelson – Creating said image files from scratch. Another thing of note: You should be 100% absolutely sure which block device you’re writing to when using dd to write to it. Using the utility lsblk helps here. Granted you still need to understand what you’re looking for. Do also keep in mind that /dev/sda typically is the block device for the disk drive which your OS( Linux ) resides. In short, that means do *not* overwrite it. Unless you’re performing some sort of backup, and you know exactly what you’re doing. lsblk example:

You may also notice from the above output example. That each block device is divided into 1 or more sections. These are known as partitions. Often, but not always, these partitions are formatted with a specific file system. I say often, as some partition types do not use a filesystem – Such as [SWAP] listed above. Anyway, it is certainly possible to create / reinstate a partition image as well. Although typically, it can take a bit more work to achieve. So, in the case of /dev/sbd above. Using /dev/sdb as a dd parameter would create or write a byte for byte copy of the whole disk(device). Where using /dev/sb1 would only create a byte for byte copy of the root file system( or the sdb1 partition – Which does contain the root file system in this case). This is important to note – As modern Debian images for the Beaglebone black use the MBR( master boot record ) to store the first and second stage boot loaders( MLO, and uboot.img ). It is important because, just making a copy of the root file system will not help you if the whole disk were wiped clean. But it may perhaps help, if all you want to do is restore the same root file system to a previous state. Another thing to note. Typically it is best to create, and reinstate images on a disk / device that is currently not live. “Live” means you’re currently booted / running Debian, or any OS from this disk / device

Advanced usage of dd, and using tar

Using dd can certainly become very complicated depending what one wishes to achieve. What I intend on covering here however is just one possible advanced use case. Which is to create / reinstate a copy of the first / second stage boot loaders in the MBR. Typically, most would not need to use dd in such a way. Unless needing to make the root file system partition smaller. Another use case would conceivably be to strip the first / second stage boot loaders from a “shipping” image, to combine with another root file system. Which would / could be a very complex operation.

Also, in the case of re-sizing to a smaller partition. tar is more useful. Since dd at minimum would make a byte for byte copy of the device in question. tar only cares about files, and directory structure. This means, it does not matter how large the partition is. This does mean however, that extra steps are required in order to achieve the end goal. The example below takes a 2G( 1.7G usable ) Debian image, and downsizes it to 1G. In theory, partition size does not matter. Only the amount of storage needed to hold the files. I think in this case, the whole rootfs was only around 200M total disk space used.

Backup MBR
Backup rootfs
Wipe out the MBR, and partition table
Copy MBR ( MLO / uboot.img ) to disk
Check sfdisk version
Create new partition with sfdisk
Create file system ( format ) the new partition
Copy over rootfs

So in the above example. There is a lot of material to cover. Too much for me to go over step by step. But this should give intermediate to advanced Linux users an understanding of the steps required. Hopefully, for the less advanced users – It gives a good narration of various tools used, and a decent amount of utilities to keep in mind for different tasks. A couple things that I should probably mention however. First, depending on which version of sfdisk you have installed – determines how its command line parameters are used. What this means is: If you have a newer version of sfdisk than that which I demonstrated above. The demonstrated command above will likely not work. So, if you really *must* blame someone. Blame the maintainers of sfdisk. Yes, command line tool consistency, or lack there of bothers me too . . . Second, sudo tar -zcvf ~/backup/rootfs.tar.gz . <- Yes, there is a dot there, and it *is* required. The dot in this context roughly means "current directory". This is also why one must cd to that directory to begin with.

This post is turning out to be much larger than I originally expected. With that in mind, I may need to write another post, to cover more material. For instance, an image file created with dd can be mounted – Did you know that, and do you know how that can be useful ? There are also other things that I’d like to cover such as: Why one should keep multiple image files(variations) around for different uses. How that is important, etc.

Beaglebone black – Working with Debian(Linux) images