User:Maffblaster/Infrastructure/Disk images

When working with small systems (such as SoCs, or other types of embedded systems), it is useful to know how to manipulate disk images. This article will attempt to flush out different capture methods, how to maintain them, mount them, convert them to virtual machines, view data inside, etc.

Capture
There are many tools available in Gentoo to capture disk images. Capturing an image is nothing more than making backup of the system. Generally this involves physically connecting the disk to a system and creating a file from the disk image. Starting from easier and faster methods, and moving to harder and slower methods:


 * 1) Connect the disk by inserting it into an external USB device, then connecting the USB device to a system.
 * 2) Connect the disk directly to the system's motherboard.
 * 3) Connect the disk to another system, and then send the data to a new system.

See the Backup article for more information on creating backups in general.

Once the disk is connected choose a tool to use to capture the disk image. For this example, a variant of dd will be used to ensure the integrity of the backup.

Mount
After the disk image has been captured, it is typical to mount the image as loop devices.

Attaching an image
It is wise to first check to see what loop devices are presently in use:

The following command can be used to attach to either a single or multi-partition disk image to a loop device:

Listing the loop devices again should show something like the following:

With the image attached to a loop device, is it now possible to run commands on the image, or mount the image at a mount point.

Presuming the file is a btrfs filesystem, the image can be checked or repaired just like any other btrfs device. See the Troubleshooting section below on repairing dirty filesystems.

Mount the image:

Converting raw disk image to VM disk image
Converting the raw image file to qcow2 format (requires ):

Compress
To get the best backup to space ratio, compressing a raw image after the capture will be highly beneficial.

Filesystem image will not mount
Filesystems occasionally have problems. For example, presume in the example below that is a btrfs formatted filesystem image and is already attached to.

Oh, no! Looks like there's a problem with the filesystem image. No to worry, this may be correctable. Filesystem images can be checked just like actual devices.

Now that the filesystem has been repaired, proceed to mount as normal:

Yay! The issue has been fixed!