{"id":13,"date":"2010-01-26T21:22:34","date_gmt":"2010-01-27T03:22:34","guid":{"rendered":"http:\/\/www.linuxpoweruser.com\/?p=13"},"modified":"2010-01-26T21:22:34","modified_gmt":"2010-01-27T03:22:34","slug":"disk-and-partition-imaging-using-dd","status":"publish","type":"post","link":"https:\/\/www.linuxpoweruser.com\/?p=13","title":{"rendered":"Disk and Partition Imaging using dd"},"content":{"rendered":"<p>Linux provides an abundance of advanced command line tools to manage and modify just about anything on your system.\u00a0 Today we will explore the use of dd, the primary tool on linux for creating and restoring disk images, among other things.<\/p>\n<p>The dd (diskdump) on Linux can be used to backup an entire disk or partition to an image file. Several caveats apply to this method:<\/p>\n<ol>\n<li> The disk in question can not be in use by an operating system<\/li>\n<li>A destination medium or network resource must be present that is large enough to hold the image.<\/li>\n<\/ol>\n<p>To backup a disk using dd, the following procedure can be used.<\/p>\n<ol>\n<li>Boot the computer with the disk in question from a Linux Live CD, such as Ubuntu or Knoppix<\/li>\n<li>Mount a destination disk (such as a usb disk drive or nfs mount)<\/li>\n<li>Run dd command to backup disk<\/li>\n<li>Note the size of the disk partition if partitioning a new device is necessary when restoring the image<\/li>\n<\/ol>\n<p>Here is an example session, to back up a single partition (sda1) containing a Windows XP installation to a USB hard disk mounted at \/mnt\/sdb1:<\/p>\n<p>As a root user, do the following:<\/p>\n<p>Mount USB disk drive<br \/>\n<code><br \/>\n# mount -t ext3 \/dev\/sdb1 \/mnt\/sdb1<br \/>\n<\/code><br \/>\nRun dd command (piping output through gzip to save space):<br \/>\n<code><br \/>\n# dd if=\/dev\/sda1 conv=sync,noerror bs=64k | gzip -c &gt; \/mnt\/sdb1\/windowsxp-c.img.gz<br \/>\n<\/code><br \/>\nDefinition of the dd command parameters:<\/p>\n<p>&#8220;if=\/dev\/sda1&#8221; is the input file for the dd command, in this case, its linux device sda1<br \/>\n&#8220;conv=sync,noerror instructs dd that if it can&#8217;t read a block due to an error then it should at least write something to its output of the correct length.<\/p>\n<p>Even if your Hard disk exhibits no errors, dd will read every single block, including any which the OS avoids because it has marked them as bad.<\/p>\n<p>&#8220;bs=64k&#8221; is the block size of 64 kilobytes. Using a large block size speeds up the copy process. The output of this is then passed to gzip for compression and storage in a file on the destination device.<\/p>\n<p>Noting Partition configuration:<\/p>\n<p>Using the command fdisk -l \/dev\/&lt;device&gt; where &lt;device&gt; is the device node of the disk being backed up, make note of the number of blocks used to create the partition:<br \/>\n<code><br \/>\n# fdisk -l \/dev\/sda<br \/>\n<\/code><br \/>\n<code><br \/>\nDisk \/dev\/sda: 959.9 GB, 959966085120 bytes<br \/>\n255 heads, 63 sectors\/track, 116709 cylinders<br \/>\nUnits = cylinders of 16065 * 512 = 8225280 bytes<br \/>\nDisk identifier: 0x2e2d2e2d<br \/>\n<\/code><br \/>\n<code><br \/>\nDevice Boot      Start\u00a0 End\u00a0\u00a0      Blocks\u00a0\u00a0\u00a0\u00a0   Id  System<br \/>\n\/dev\/sda1*\u00a0\u00a0\u00a0\u00a0\u00a0           1\u00a0      15298 122881153+   7\u00a0  HPFS\/NTFS<br \/>\n\/dev\/sda2\u00a0\u00a0 15299\u00a0      51771 292969372+  83  Linux<br \/>\n\/dev\/sda3\u00a0\u00a0 51772\u00a0      52767     \u00a0 8000370   \u00a082  Linux swap \/ Solaris<br \/>\n\/dev\/sda4\u00a0\u00a0       52768      116709 513614115   \u00a083  Linux<br \/>\n<\/code><br \/>\nThe destination disk should have a partition defined identical to the source partition, total number of blocks is the important parameter here.<\/p>\n<p>The partition geometry information can be backed up the the USB hard disk with the following command:<br \/>\n<code><br \/>\n# fdisk -l \/dev\/sda &gt; \/mnt\/sdb1\/sda_fdisk.txt<br \/>\n<\/code><br \/>\nRestoring a dd image to a disk\/partition<\/p>\n<p>The steps are similar to the backup process:<\/p>\n<ol>\n<li>Boot computer with destination disk from a Linux Live CD<\/li>\n<li>Partition the destination disk, if needed<\/li>\n<li>Mount the source media (usb disk or nfs mount)<\/li>\n<li>Use gunzip and dd to restore image to disk or partition<\/li>\n<\/ol>\n<p>Here is an example session, to restore the image taken with the above steps:<\/p>\n<p>As a root user, do the following:<\/p>\n<p>Mount image source (USB hard disk at \/dev\/sdb1)<br \/>\n<code><br \/>\n# mount -t ext3 \/dev\/sdb1 \/mnt\/sdb1<br \/>\n<\/code><br \/>\nPartition destination disk:<br \/>\n<code><br \/>\n# fdisk \/dev\/&lt;device node in question&gt;; in our case, sda.<\/code><\/p>\n<p>&lt;create partition if needed&gt;<\/p>\n<p>Restore image (destination partition is \/dev\/sda1)<br \/>\n<code><br \/>\n# gunzip -c \/mnt\/sdb1\/windowsxp-c.img.gz | dd of=\/dev\/sda1 conv=sync,noerror bs=64k<br \/>\n<\/code><br \/>\nNote: On a fast machine, ie C2Q 6600, and 3ware RAID disk array, a 120GB image takes 25 minutes to create.<\/p>\n<p>In order to have a bootable system, some other configuration may be needed such as restoring a boot block.  Check out the next post for details on boot sector management with dd.<\/p>\n<p>A excellent guide on using fdisk for disk partitioning can be found <a href=\"http:\/\/tldp.org\/HOWTO\/Partition\/fdisk_partitioning.html\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux provides an abundance of advanced command line tools to manage and modify just about anything on your system.\u00a0 Today we will explore the use of dd, the primary tool on linux for creating and restoring disk images, among other things. The dd (diskdump) on Linux can be used to backup an entire disk or [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6,10],"tags":[16,25,27,37,53,58],"_links":{"self":[{"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=\/wp\/v2\/posts\/13"}],"collection":[{"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13"}],"version-history":[{"count":0,"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=\/wp\/v2\/posts\/13\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}