Smokes your problems, coughs fresh air.

Tag: disk image

Increasing (encrypted) disk image size with dd

When you have a raw disk image file, you may need to increase its size at some point. With dd, you can do this:

dd conv=notrunc oflag=append if=/dev/zero of=backups bs=1M count=1024

This will add 1GB of zero data to the image file ‘backups’. You can then use resize2fs on it to increase the file system size (that is, if you use ext2/3/4):

e2fsck (-f) backups
resize2fs backups

However, if the disk image is a volume encrypted with cryptsetup + luks, first open it:

losetup /dev/loop/0 backups
cryptsetup luksOpen /dev/loop/0 encrypted-backups

Then resize the encrypted volume:

cryptsetup resize encrypted-backups
e2fsck (-f) /dev/mapper/encrypted-backups
resize2fs /dev/mapper/encrypted-backups

And I know, I still have to add LVMs to my set of skills…

Clonezilla as disk imager

I was always using partimage to make backups of the hard disks of workstations. When trying to restore one the other day, it just started giving CRC errors. After recreating the image and running a simulated restore, it gave CRC errors again. So, apparently, it’s not incidental. It must be due to the experimental NTFS support. So, I set out to find a new tool. And that’s how I found Clonezilla, which gives me a very good first impression.

Clonezilla is a wizard-style live CD. It uses several tools, including partimage. However, it prefers ntfsclone over partimage at it’s default settings. Besides its handy interface, it has some very cool features: It doesn’t only create the image, but also some other useful files, such as the MBR (first 512 bytes of a disk), the data between the MBR and the first partition, an sfdisk dump of the partition table and some files with info about the PCI devices and other hardware. All this ensures you can properly restore the image.

Also, it has support for leaving out the page- and hybernation files on Windows systems, which can save you several gigabytes of space. And, to top it off, it calls “sync” when everything is done. They must have read my backup article :).

And the last major thing: it allows you to remove the CD when rebooting or halting. It is so incredibly annoying when live CD’s don’t do that…

A must have live CD for any sysadmin, I would say.

Mounting partititions residing inside a disk image

When you’ve read a disk with dd or ddrescue into an image file, one of the things you want to do is mount the partitions inside it. But, how does one do that? The answer is using losetup, with a bit of knowledge of partition offsets.

First, you have to map the disk image to a loopback device:

# losetup /dev/loop/0 disk_image_file

Then use fdisk to print the partition table, displaying offsets in sectors:

# fdisk -lu /dev/loop/0

For my disk, that results in:

Disk /dev/sda: 250.0 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders, total 488397168 sectors
Units = sectors of 1 * 512 = 512 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *          63   482415884   241207911   fd  Linux raid autodetect
/dev/sda2       482415885   488392064     2988090   fd  Linux raid autodetect

You can see that the first partition begins at sector 63. Now we’re going to setup a new loop device, with the proper offset. A disk sector is 512 bytes long, so an offset of 63 sectors is 32256 bytes. Therefore, you setup the first partition with this command:

# losetup -o 32256 /dev/loop/1 disk_image_file

Then you can mount that loop device:

# mount /dev/loop/1 /mnt/mountpoint

© 2022 BigSmoke

Theme by Anders NorenUp ↑