{"id":2081,"date":"2011-12-31T14:42:16","date_gmt":"2011-12-31T19:42:16","guid":{"rendered":"http:\/\/sahd.lamafam.org\/?p=2081"},"modified":"2011-12-31T14:43:20","modified_gmt":"2011-12-31T19:43:20","slug":"moving-linux-to-a-new-hard-drive-a-howto-3","status":"publish","type":"post","link":"https:\/\/sahd.lamafam.org\/?p=2081","title":{"rendered":"Moving Linux to a New Hard Drive- a HowTo"},"content":{"rendered":"<p>About a week ago, I discovered that my primary hard drive was starting to enter its death throws.  I was consistently receiving I\/O errors and in one case, I had to reboot the system and perform an <code>fsck<\/code> on the root file system to repair the errors.  Fortunately, the system was stable enough for long enough that I was able to come up with a replacement drive.<\/p>\n<p>Yesterday, I set about migrating everything to the new hard drive.  FWIW, it&#8217;s a 500GB, 3Gb SATA Barracuda from Seagate.  Searching around, I came up with 2 articles (<a href=\"http:\/\/www.linuxquestions.org\/questions\/linux-general-1\/how-to-move-installation-to-a-new-hard-drive-520702\/\">here<\/a> and <a href=\"http:\/\/unix.stackexchange.com\/questions\/5297\/debian-grub2-moving-root-partition-to-new-drive\">here<\/a>) to guide me a bit.  The hard part ended up being getting <code>GRUB2<\/code> installed properly.<\/p>\n<p>I&#8217;m happy to report that I&#8217;ve got everything running.  After the jump is the process I used.  The advantage for this process is that everything is done from the shell.  There is no need to separately run an install program on the new drive or to figure out esoteric <code>grub<\/code> commands.  If that sounds good to you, then let&#8217;s proceed&#8230;<\/p>\n<p><!--more--><\/p>\n<p>I&#8217;m assuming that the drive is installed properly at this point &#8230; \ud83d\ude09<\/p>\n<p>To start, for my purposes, I created 3 partitions on the drive:<\/p>\n<ol>\n<li>A boot partition<\/li>\n<li>A root partition<\/li>\n<li>A swap partition<\/li>\n<\/ol>\n<p>I used <code>fdisk<\/code> for this part.  I divvied it up into a ~100MB boot partition, a 4GB swap partition, and the remainder to the root partition.  Remember to set the <strong>bootable<\/strong> flag on the boot partition.<\/p>\n<p>Next up, I used <code>mkfs<\/code> to format the partition appropriately.  A word to the wise:  <strong>these are powerful commands with no conscience<\/strong>.  They will wipe an existing partition without a seconds hesitation.  <strong>Make sure you invoke them properly and on the intended devices<\/strong>.  I just create and <code>ext2<\/code> on the boot partition and <code>ext4<\/code> on the root partition.  (Do I need to say I created a <code>swapfs<\/code> on the swap partition?)<\/p>\n<p>With the drive ready for data, proceed as follows (note that all of this must be done as <code>root<\/code>):<\/p>\n<ol>\n<li>\n<p>Mount the new <code>boot<\/code> partition and copy the <em>contents<\/em> of <code>\/boot<\/code> to it.  I     just used <code>cp -rpf \/boot\/* \/mnt\/newboot<\/code>.  (Strictly speaking, that isn&#8217;t     true.  That&#8217;s what I ended up doing.  Initially I copied the <code>boot<\/code>     directory over which wasn&#8217;t quite correct since I&#8217;m giving <code>\/boot<\/code> it&#8217;s own     partition.  The partition ultimately gets mounted to <code>\/boot<\/code>, which exists     on the <code>root<\/code> filesystem.  Got that?  Good.)<\/p>\n<\/li>\n<li>\n<p>Unmount the new <code>boot<\/code> partition and now mount the <code>root<\/code> partition.  (FWIW-     the device nodes were <code>\/dev\/sdb1<\/code> and <code>\/dev\/sdb3<\/code>, respectively, while I was     working.  The odds of them being the same in other cases are somewhere     around &#8220;not good.&#8221;)<\/p>\n<\/li>\n<li>\n<p>On the new <code>root<\/code> partition, create the following mount points:  <code>boot<\/code>,     <code>proc<\/code>, <code>dev<\/code>, <code>sys<\/code>, and <code>tmp<\/code>.  To be safe, you can also copy the contents     of the <code>\/dev<\/code> and <code>\/tmp<\/code> directories to the new partition, again using <code>copy<br \/>\n    -rpf<\/code>.<\/p>\n<\/li>\n<li>\n<p>Now, copy the remaining directories.  Again, I just used e.g.     <code>copy -rpf \/root\/ \/mnt\/newdrive\/<\/code>.  Be careful to account for other mount     points within your system.  For instance, my <code>\/usr\/local<\/code> directory has its     own drive.  So I need to copy everything from <code>\/usr<\/code> <em>except<\/em> <code>local<\/code>.  But     I did need to create the mount point using <code>mkdir \/mnt\/newdrive\/usr\/local<\/code>.     I had an identical situation with my <code>\/home<\/code> directory, which too has it&#8217;s     own dedicated drive. <\/p>\n<\/li>\n<li>\n<p>Once everything is copied over, the next thing is to setup     <code>newdrive\/etc\/fstab<\/code> with the proper UUID&#8217;s for the new drive.  The <code>blkid<\/code>     command will print out the UUID&#8217;s for all the partitions on the system.     Edit the <code>fstab<\/code> on the new drive accordingly, i.e., replace the UUID&#8217;s for     the old, failing drive with the UUID&#8217;s for the new one.<\/p>\n<\/li>\n<li>\n<p>We now have everything we need to be able to perform a <code>chroot<\/code>.  The     <code>chroot<\/code> will look <em>exactly<\/em> like the new system, and we&#8217;ll be able to     perform a <code>grub-install<\/code> to get the bootloader properly installed.  The     instructions for setting up for the <code>chroot<\/code> are in the 2nd link above, but     I&#8217;ll reproduce them here for convenience (use the following sequence of     commands):<\/p>\n<pre><code>root:\/$ mount \/dev\/sdb1 \/mnt\/newdrive\/boot  #&lt;--- this mounts the new boot \n                                            #     partition like it will be\n                                            #     in the new system\nroot:\/$ mount -o bind \/dev \/mnt\/newdrive\/dev\nroot:\/$ mount -t proc none \/mnt\/newdrive\/proc\nroot:\/$ mount -t sysfs none \/mnt\/newdrive\/sys\nroot:\/$ chroot \/mnt\/newdrive\n<\/code><\/pre>\n<\/li>\n<li>\n<p>Execute <code>grub-install \/dev\/sdb<\/code> to install <code>grub<\/code> to the new drive so it is     bootable.   (Substitute the appropriate <code>\/dev<\/code> device node for your system.)<\/p>\n<\/li>\n<\/ol>\n<p>With that, the <code>chroot<\/code> can be exited, the dummy system unmounted and then shutdown the system.  For my purposes, I unplugged the old drive completely (data and power) and plugged in the new drive in it&#8217;s place.  I didn&#8217;t physically remove the old drive, just in case.  Turned out everything worked out splendidly and I&#8217;m now running with the new hard drive in place.<\/p>\n<p>I did all of this while my system was live.  I suppose it might have been safer to perform the steps from a maintenance mode or some other minimally running system.  That would likely change some of the steps as it might be necessary to mount some drives for copying purposes.  But the basic process of format, create mount points, copy data, adjust UUID&#8217;s, <code>chroot<\/code> and finally <code>grub-install<\/code> within the <code>chroot<\/code> should hold up.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>About a week ago, I discovered that my primary hard drive was starting to enter its death throws. I was consistently receiving I\/O errors and in one case, I had to reboot the system and perform an fsck on the root file system to repair the errors. Fortunately, the system was stable enough for long [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[191,10],"class_list":["post-2081","post","type-post","status-publish","format-standard","hentry","category-computers","tag-chroot","tag-debian"],"_links":{"self":[{"href":"https:\/\/sahd.lamafam.org\/index.php?rest_route=\/wp\/v2\/posts\/2081","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sahd.lamafam.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sahd.lamafam.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sahd.lamafam.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sahd.lamafam.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2081"}],"version-history":[{"count":2,"href":"https:\/\/sahd.lamafam.org\/index.php?rest_route=\/wp\/v2\/posts\/2081\/revisions"}],"predecessor-version":[{"id":2083,"href":"https:\/\/sahd.lamafam.org\/index.php?rest_route=\/wp\/v2\/posts\/2081\/revisions\/2083"}],"wp:attachment":[{"href":"https:\/\/sahd.lamafam.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sahd.lamafam.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sahd.lamafam.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}