Skip to content

Instantly share code, notes, and snippets.

@sposmen
Last active October 27, 2019 20:50
Show Gist options
  • Select an option

  • Save sposmen/9253930 to your computer and use it in GitHub Desktop.

Select an option

Save sposmen/9253930 to your computer and use it in GitHub Desktop.

To make a full hard disk image in linux we will use several small programs and a livecd.

Next to this we need a place to store the image (external hard disk?). Which livecd you use is not that important. Just make sure that you access the place where you want to store the image and that you have access to gzip, dd and swapoff (if there is a swap partition).

So let us see the steps to take:

  1. Boot into the livecd
  2. Make sure you can access the place where you want to store the image
  3. (1) Disable the swap partition on the hard disk you want to image
  4. (2) Start the image-making process
  5. Check if the image is stored on the requested locations without errors

So let us see which commands we have to use to perform actions (1) and (2):

(1)# sudo swapoff -a
(2)# sudo dd if=/dev/hd_ | gzip > /mnt/hd__/image_name.dd.gz

Restoring the image is as easy as making it. Let us see these steps as well

  1. Boot into the livecd
  2. Make sure you can access the place where your image is stored
  3. (3) Disable the swap partition on the hard disk you want to image
  4. (4) Start the image-restoring process
  5. Check if the image is restored on the requested location without errors

So let us see which commands we have to use to perform actions (3) and (4):

(3)# sudo swapoff -a
(4)# sudo gzip -dc /mnt/hd__/image_name.dd.gz | dd of=/dev/hd_

That is it. I hope it is/was helpful.

I have backed up my system to an external ximeta drive using "dd" and the well-known linux live cd distribution, Knoppix to boot from. Below are the steps in brief:

  1. Boot from the live cdrom distribution.

  2. Switch to root.

  3. Make sure NO partitions are mounted from the source hard drive.

  4. Mount the external HD.

    mount -t vfat /dev/sda1 /mnt/sda1

  5. Backup the drive.

    dd if=/dev/hda conv=sync,noerror bs=64K | gzip -c > /mnt/sda1/hda.img.gz

"dd" is the command to make a bit-by-bit copy of "if=/dev/hda" as the "Input File" to "of=/mnt/sda1/hda.img.gz" as the "Output File". Everything from the partition will go into an "Output File" named "hda.img.gz". "conv=sync,noerror" tells dd that if it can't read a block due to a read error, then it should at least write something to its output of the correct length. Even if your hard disk exhibits no errors, remember that dd will read every single block, including any blocks which the OS avoids using because it has marked them as bad. "bs=64K" is the block size of 64x1024 Bytes. Using this large of block size speeds up the copying process. The output of dd is then piped through gzip to compress it.

  1. To restore your system:

    gunzip -c /mnt/sda1/hda.img.gz | dd of=/dev/hda conv=sync,noerror bs=64K

NOTE: I've had much success leaving out "conv=sync,noerror" during restore.

  1. Store extra information about the drive geometry necessary in order to interpret the partition table stored within the image. The most important of which is the cylinder size.

    fdisk -l /dev/hda > /mnt/sda1/hda_fdisk.info

Notes:

One of the disadvantages of the dd method over software specifically designed for the job such as Ghost or partimage is that dd will store the entire partition, including blocks not currently used to store files, whereas the likes of Ghost understand the filesystem and don't store these unallocated blocks. The overhead isn't too bad as long as you compress the image and the unallocated blocks have low entropy. In general this will not be the case because the emtpy blocks contain random junk from bygone files. To rectify this, it's best to blank all unused blocks before making the image. After doing that, the unallocated blocks will contain mostly zeros and will therefore compress down to almost nothing.

Mount the partition, then create a file of zeros which fills the entire disk, then delete it again.

# dd if=/dev/zero of=/tmp/delete.me bs=8M; rm delete.me

VirtualBox - convert RAW image to VDI and otherwise

Posted on _ April, 29 2012 _ by _ milosz _

VirtualBox command-line interface (VBoxManage) provides an easy way to convert raw disk image to the VDI/VMDK format and otherwise.

Let's assume that we have raw image of the sdb device:

$ sudo dd if=/dev/sdb of=./sdb.raw

To use it with VirtualBox we need to convert it to the VDI format:

$ VBoxManage convertdd sdb.raw sdb.vdi --format VDI

To use it with VMware we need to convert it to the VMDK format:

$ VBoxManage convertdd sdb.raw sdb.vmdk --format VMDK

Convert between VDI/VMDK formats:

$ VBoxManage clonehd sdb.vdi sdb.vmdk --format VMDK


$ VBoxManage clonehd sdb.vmdk sdb.vdi --format VDI

Convert to the RAW image:

$ VBoxManage clonehd sdb.vdi sdb.raw --format RAW

Alternative solution to get back raw image after applying modifications is to use qemu-img command from qemu package:

$ qemu-img convert -f vmdk sdb.vmdk -O raw sdb.raw

Now we can write image to the device:

$ sudo dd if=./sdb.raw of=/dev/sdb

Posted in Linux, Backup, Recovery, VirtualBox


Bookmark the [ permalink](http://blog.sleeplessbeastie.eu/2012/04/29 /virtualbox-convert-raw-image-to-vdi-and-otherwise/).

Related stories

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment