{"id":41,"date":"2014-12-30T16:01:10","date_gmt":"2014-12-30T16:01:10","guid":{"rendered":"http:\/\/weegreenblobbie.com\/?p=41"},"modified":"2015-03-08T18:29:50","modified_gmt":"2015-03-08T18:29:50","slug":"how-to-clone-linux-disk-of-different-sizes-large-hdd-to-smaller-ssd","status":"publish","type":"post","link":"http:\/\/weegreenblobbie.com\/?p=41","title":{"rendered":"How to clone Linux disk of different sizes; large HDD to smaller SSD"},"content":{"rendered":"<p><strong>WARNING: MISUSE OF THE COMMANDS IN THIS TUTORIAL CAN CAUSE DATA LOSS!<br \/>\nBACKUP YOUR DATA!<br \/>\nYOU&#8217;VE BEEN WARNED!<\/strong><\/p>\n<hr \/>\n<p><strong>Step 1: Clone original HDD to larger, spare HDD<\/strong><\/p>\n<p>1.1. Connect SRC and DST disk to computer<\/p>\n<p>1.2. Disconnect external drives that you may be using for backups, you won&#8217;t want to accidentally overwrite them!<\/p>\n<p>1.3. Boot off of Ubuntu 14.04 live USB stick or your Ubuntu tower with SRC and DST connected<\/p>\n<p>1.4. Use gparted to identify your boot OS, original SRC, and DST disk device ids, for example:<\/p>\n<pre class=\"lang:default highlight:0 decode:true \">OS = \/dev\/sda\r\nSRC = \/dev\/sdc\r\nDST = \/dev\/sdb<\/pre>\n<p><code><\/code>Write these down! If you mix these up you will LOSE ALL YOU DATA! Using all different sized disks makes this easier. If you have multiple disks of the same size, connect one disk at a time and label them. You don&#8217;t want to make a mistake here.<\/p>\n<p>1.5. Clone SRC disk to DST disk using the pv &amp; dd utilities (pv is available in the Ubuntu &#8220;universe&#8221; apt-get repo):<\/p>\n<pre class=\"theme:shell-default lang:sh decode:true \">sudo su\r\npv -tpreb {SRC} | dd of={DST} bs=4K conv=notrunc,noerror,sync<\/pre>\n<p>where:<br \/>\n<code><\/code><\/p>\n<pre class=\"font:liberation-mono toolbar:2 nums:false lang:default highlight:0 decode:true\">{SRC} = source device id\r\n{DST} = destination device id<\/pre>\n<p>for example on my system:<\/p>\n<pre class=\"theme:shell-default lang:sh decode:true\">pv -tpreb \/dev\/sdc | dd of=\/dev\/sdb bs=4K conv=notrunc,noerror,sync<\/pre>\n<p>This may take several hours depending on your disk sizes, using pipe view (pv) will give you an estimate for how long it will take.<\/p>\n<p>1.6. Power off your computer after the disk is cloned, the Linux kernel may not read the newly cloned disk correctly.<\/p>\n<hr \/>\n<p><strong>Step 2: Resize Cloned HDD<\/strong><\/p>\n<p>2.1. After powering off, disconnect the original drive to keep it safe from modification<\/p>\n<p>2.2. Boot computer again, use gparted to resize and move all partitions so that they will fit on the new SSD<\/p>\n<p>You can launch gparted with the DST device id to operate on just that disk, for example:<\/p>\n<pre class=\"theme:shell-default lang:sh decode:true\">sudo gparted \/dev\/sdb<\/pre>\n<p><strong>NOTE<\/strong>: gparted should display all partitions on the new disk without any warnings like \/!\\ next to a partition. All partitions should be readable by gparted with partition size and used listed next to each partition. If you are booting off a live USB stick, you will probably need to turn off the swap partition by right-clicking and selecting swapoff in the menu.<\/p>\n<p>2.3. After resizing everything power off the computer<\/p>\n<hr \/>\n<p><strong>Step 3: Clone HDD To New SSD<\/strong><\/p>\n<p>3.1. With the power off, connect the new SSD to the computer<\/p>\n<p>3.2. Boot computer with the resized HDD and the new SSD<\/p>\n<p>3.3. Use gparted to identify your boot OS, original SRC, and DST disk device ids, for example:<\/p>\n<pre class=\"lang:default highlight:0 decode:true\">OS = \/dev\/sda\r\nSRC = \/dev\/sdb\r\nDST = \/dev\/sdc<\/pre>\n<p>Write these down! If you mix these up you will LOSE ALL YOU DATA!<\/p>\n<p>3.4. Clone SRC disk to DST disk using the pv &amp; dd utilities:<\/p>\n<pre class=\"theme:shell-default lang:sh decode:true\">sudo su\r\npv -tpreb {SRC} | dd of={DST} bs=4K conv=notrunc,noerror,sync<\/pre>\n<p>where:<br \/>\n<code><\/code><\/p>\n<pre class=\"lang:default highlight:0 decode:true\">{SRC} = source device id\r\n{DST} = destination device id<\/pre>\n<p>for example on my system:<br \/>\n<code><\/code><\/p>\n<pre class=\"theme:shell-default lang:sh decode:true\">pv -tpreb \/dev\/sdb | dd of=\/dev\/sdc bs=4K conv=notrunc,noerror,sync<\/pre>\n<p>This may take several hours depending on your disk sizes, using pipe view (pv) will give you an estimate for how long it will take.<\/p>\n<p>Since the destination SSD is likely smaller than the source HDD, dd will error out at the end stating the destination disk is out of space:<br \/>\n<code>dd: error writing \u2018\/dev\/sdc\u2019: No space left on device<\/code><br \/>\nThis is OKAY.<\/p>\n<p>3.5. Power off the computer and disconnect the spare HDD, so moving forward we are only modifying the SDD<\/p>\n<hr \/>\n<p><strong>Step 4: Repair The Partition Table<\/strong><\/p>\n<p>The original partition table that got cloned to the SSD is now incorrect, the physical size of the disk has changed, we need to repair it. With Ubuntu 14.04, the partition table being used is something called a GPT. We will repair the partition using the tool gdisk.<\/p>\n<p>4.1. Boot the computer without the spare HDD<\/p>\n<p>4.2. Use gparted to identify your boot OS, and the new SSD device ids, for example:<\/p>\n<pre class=\"lang:default highlight:0 decode:true \">OS = \/dev\/sda\r\nSSD = \/dev\/sdb<\/pre>\n<p>Write these down! If you mix these up you will LOSE ALL YOU DATA!<\/p>\n<p>4.3. Use gdisk to repair the GUID Partition Table (GPT):<\/p>\n<pre class=\"theme:shell-default lang:sh decode:true\">sudo su\r\ngdisk \/dev\/sdb\r\nCommand (? for help): x                  # enter Expert mode\r\nExpert command (? for help): e           # relocate backup to end of disk\r\nExpert command (? for help): w           # write table to disk and exit\r\nOK; writing new GUID partition table (GPT) to \/dev\/sdb.\r\nThe operation has completed successfully.<\/pre>\n<p>4.4. Reboot the computer so the Linux kernel sees the new partition table<\/p>\n<hr \/>\n<p><strong>Step 5: Repair Boot Image<\/strong><\/p>\n<p>After all the work of resizing the partitions and repairing the partition table, the GRUB boot image is likely non functional. From my experience the BIOS can try to boot from the disk but we are left with a single &#8216;-&#8216; blinking character and nothing happens. The second stage boot loader can&#8217;t be found. Here we&#8217;ll repair it.<\/p>\n<p>5.1. Boot computer<\/p>\n<p>5.2. Use cgdisk to create a tiny partition to install grub2 on:<\/p>\n<pre class=\"theme:shell-default lang:sh decode:true\">sudo su\r\ncgdisk \/dev\/sdb<\/pre>\n<p>5.3. Select the free space at the beginning of the disk to create the new partition, it MUST be the first partition on the disk:<\/p>\n<pre class=\"theme:shell-default lang:sh decode:true \">[ New ]\r\nFirst sector (34-2047, default = 34): &lt;return&gt;\r\nSize in sectors or {KMGTP} (default = 2014): &lt;return&gt;\r\nCurrent type is 8300 (Linux filesystem)\r\nHex code or GUID (L to show codes, Enter = 8300): ef02          # bios boot\r\nCurrent partition name is ''\r\nEnter new partition name, or to use the current name:\r\ngrub_boot&lt;return&gt;\r\n[ Write ]<\/pre>\n<hr \/>\n<p><strong>Step 6. Install Grub2<\/strong><\/p>\n<p>Now that we have a dedicated grub boot partition, we can proceed to install grub2 on the disk.<\/p>\n<p>6.1. Mount the data partition disk for chroot operations:<\/p>\n<pre class=\"theme:shell-default lang:sh decode:true \">sudo su\r\nmount \/dev\/sdb1 \/mnt\r\nmount --bind \/dev \/mnt\/dev\r\nmount --bind \/dev\/pts \/mnt\/dev\/pts\r\nmount --bind \/proc \/mnt\/proc\r\nmount --bind \/sys \/mnt\/sys<\/pre>\n<p>6.2. Change root into mounted disk:<\/p>\n<pre class=\"theme:shell-default lang:sh decode:true \">chroot \/mnt<\/pre>\n<p>6.3. Reinstall grub2, recheck, recreate menu:<\/p>\n<pre class=\"theme:shell-default lang:sh decode:true \">grub-install \/dev\/sdb\r\ngrub-install --recheck \/dev\/sdb\r\nupdate-grub<\/pre>\n<p>6.4. Exit the chroot shell and unmount everything:<\/p>\n<pre class=\"theme:shell-default lang:sh decode:true  \">exit\r\numount \/mnt\/sys\r\numount \/mnt\/proc\r\numount \/mnt\/dev\/pts\r\numount \/mnt\/dev\r\numount \/mnt<\/pre>\n<p>6.5. Shutdown the computer and install the SSD into the new computer, it should now boot!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>WARNING: MISUSE OF THE COMMANDS IN THIS TUTORIAL CAN CAUSE DATA LOSS! BACKUP YOUR DATA! YOU&#8217;VE BEEN WARNED! Step 1: Clone original HDD to larger, spare HDD 1.1. Connect SRC and DST disk to computer 1.2. Disconnect external drives that &hellip; <a href=\"http:\/\/weegreenblobbie.com\/?p=41\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[10],"class_list":["post-41","post","type-post","status-publish","format-standard","hentry","category-computing","tag-linux"],"_links":{"self":[{"href":"http:\/\/weegreenblobbie.com\/index.php?rest_route=\/wp\/v2\/posts\/41","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/weegreenblobbie.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/weegreenblobbie.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/weegreenblobbie.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/weegreenblobbie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=41"}],"version-history":[{"count":29,"href":"http:\/\/weegreenblobbie.com\/index.php?rest_route=\/wp\/v2\/posts\/41\/revisions"}],"predecessor-version":[{"id":70,"href":"http:\/\/weegreenblobbie.com\/index.php?rest_route=\/wp\/v2\/posts\/41\/revisions\/70"}],"wp:attachment":[{"href":"http:\/\/weegreenblobbie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/weegreenblobbie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/weegreenblobbie.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}