{"id":297,"date":"2018-06-21T16:32:33","date_gmt":"2018-06-21T23:32:33","guid":{"rendered":"http:\/\/staff.washington.edu\/ketcham\/?p=297"},"modified":"2019-05-14T10:30:40","modified_gmt":"2019-05-14T17:30:40","slug":"clone-physical-to-virtual-and-back-yet-another-variation","status":"publish","type":"post","link":"https:\/\/staff.washington.edu\/ketcham\/clone-physical-to-virtual-and-back-yet-another-variation\/","title":{"rendered":"Clone Physical to Virtual and Back, Revisited"},"content":{"rendered":"<p>Our beta-test kiosk-workstation is now tested and so this system is ready to clone to our other workstation.<\/p>\n<p>The following procedures take off from where, in <a href=\"http:\/\/staff.washington.edu\/ketcham\/from-physical-disk-to-virtual-machine\/\">my previous article<\/a>, I created a tar backup of system root filesystem (i.e. &#8216;\/&#8217;).<\/p>\n<p>In the mean time, the <a href=\"https:\/\/staff.washington.edu\/ketcham\/about-ricky\/\">buzzard<\/a> learned <a href=\"https:\/\/www.serverwatch.com\/server-tutorials\/using-a-physical-hard-drive-with-a-virtualbox-vm.html\">how to attach a physical disk<\/a> to a <a href=\"https:\/\/en.wikipedia.org\/wiki\/VirtualBox\">VirtualBox<\/a> <a href=\"https:\/\/en.wikipedia.org\/wiki\/Virtual_machine\">VM<\/a>.<\/p>\n<pre>#mount a physical disk\r\n VBoxManage internalcommands createrawvmdk -filename \"&lt;\/path\/to\/file&gt;.vmdk\" -rawdisk \/dev\/sda<\/pre>\n<p>This will be useful, because I can clone directly to a harddrive and boot it in a VM to test and configure, and then pop the HD into the physical machine, thereby minimizing downtime of the target workstation, with one less step where things could go wrong.<\/p>\n<p>I start by creating a VM with my physical disk attached, and pxe booting it from my CentOS 7 <a href=\"https:\/\/en.wikipedia.org\/wiki\/Kickstart_(Linux)\">kickstart<\/a> server.\u00a0 My <a href=\"http:\/\/staff.washington.edu\/ketcham\/kickstart\">kickstart configuration<\/a> partitions the physical drive with a boot partition,\u00a0 and an <a href=\"https:\/\/en.wikipedia.org\/wiki\/Logical_Volume_Manager_(Linux)\">LVM<\/a> <a href=\"https:\/\/en.wikipedia.org\/wiki\/Physical_volume\">PV<\/a> with a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Volume_group\">VG<\/a> on the remainder of the physical drive, from which I carve a small <a href=\"https:\/\/en.wikipedia.org\/wiki\/Logical_volume\">LV,<\/a> onto which I will install a minimal bootable <a href=\"https:\/\/en.wikipedia.org\/wiki\/Operating_system\">OS<\/a>, which, as I will show, comes in handy as I get the cloned system up and running.<\/p>\n<p>After the kickstart installation, I reboot my VM into this minimal CentOS 7 and I carve off another LV into which I will untar my clone backup.\u00a0 This will become my new, cloned, slash (\/) filesystem.<\/p>\n<pre>[root@C7-64-minimus ~]# lvcreate -a y -n avrootvol --size 20G -v vg_01\r\n[root@C7-64-minimus ~]# mkfs.ext4 -L avsysroot \/dev\/mapper\/vg_01-avrootvol\r\n[root@C7-64-minimus ~]# mkdir \/mnt\/avrootvol\r\n[root@C7-64-minimus ~]# mount \/dev\/mapper\/vg_01-avrootvol \/mnt\/avrootvol<\/pre>\n<p>(I prefer to create my LVM volumes small, because I can easily grow them later, as needed.)<\/p>\n<p>I now need my backup tar file.\u00a0 I have copied it to my VirtualBox host machine and I will use the VirtualBox file share feature to make it accessible from my booted VM.\u00a0 First I need to install VirtualBox Guest Additions to enable this feature. and then create my vbox share in the &#8220;devices&#8221; menu.<\/p>\n<pre>[root@C7-64-minimus ~]# mkdir \/mnt\/shared\r\n[root@C7-64-minimus ~]# mount -t vboxsf Shared \/mnt\/shared<\/pre>\n<p>Now I am set to untar:<\/p>\n<pre>[root@C7-64-minimus ~]# cd \/mnt\/avrootvol\r\n[root@C7-64-minimus avrootvol]# tar xzf \/mnt\/shared\/2018.06.19\/av301_root.tgz --numeric-owner<\/pre>\n<p>The &#8211;numeric-owner flag prevents tar from resetting file uid and gid numbers as it does by default, by matching names from \/etc\/passwd and \/etc\/group.\u00a0 These would then no longer match after I reboot into the cloned OS.<\/p>\n<p>Now I need to make changes in my boot partition so that I can boot my newly cloned system.\u00a0 This will require recreating \/boot\/grub\/grub.cfg and also \/boot\/initramfs-*.\u00a0 In particular, the existing <a href=\"https:\/\/en.wikipedia.org\/wiki\/Initial_ramdisk\">initramfs<\/a> contains references by <a href=\"https:\/\/en.wikipedia.org\/wiki\/Universally_unique_identifier\">UUID<\/a> to the kickstart-installed root filesystem (what was installed on the currently booted (\/) root partition) and so , short of cloning the UUID, it is tied to that partition.\u00a0 In <a href=\"http:\/\/staff.washington.edu\/ketcham\/from-physical-disk-to-virtual-machine\/\">my previous article<\/a>, I took a quick-and-dirty shortcut around this problem by un<a href=\"https:\/\/en.wikipedia.org\/wiki\/Tar_(computing)\">tar<\/a>ring the cloned system onto, and thus overwriting, that existing root filesystem, so that I could reuse the existing initramfs as-is.\u00a0\u00a0 Now, instead, I am going to fully recreate that initramfs, starting by <a href=\"https:\/\/en.wikipedia.org\/wiki\/Chroot\">chrooting<\/a> into the new root filesystem.<\/p>\n<pre> [root@C7-64-minimus avrootvol]# mount --bind \/proc .\/proc\r\n [root@C7-64-minimus avrootvol]# mount --bind \/sys .\/sys\r\n [root@C7-64-minimus avrootvol]# mount --bind \/dev .\/dev\r\n [root@C7-64-minimus avrootvol]# chroot \/mnt\/avrootvol\r\n bash-4.2#<\/pre>\n<p>As I will be rebooting between different OS installation instances, I want to use human-friendly partition labels instead of the default UUID labels (actually, these are file system labels correctly speaking, not partition labels) . Labels will keep things portable and less confusing to a <a href=\"http:\/\/staff.washington.edu\/ketcham\/about-ricky\/\">buzzard<\/a> like Ricky.<\/p>\n<pre> bash-4.2# e2label \/dev\/sda1 boot\r\n bash-4.2# e2label \/dev\/mapper\/vg_01-avrootvol avsysroot\r\n bash-4.2# e2label \/dev\/mapper\/vg_01-homevol home\r\n bash-4.2# e2label \/dev\/mapper\/vg_01-optvol opt\r\n bash-4.2# swaplabel -L swap1 \/dev\/mapper\/vg_01-swapvol<\/pre>\n<p>I am going to edit the \/etc\/fstab to use these labels (note that I am now working in my chrooted environment):<\/p>\n<pre> bash-4.2# vi \/etc\/fstab\r\n bash-4.2# cat \/etc\/fstab\r\n LABEL=boot \/boot ext3 defaults 0 0\r\n LABEL=avsysroot \/ ext4 defaults 0 0\r\n LABEL=home \/home ext4 defaults 0 0\r\n LABEL=opt \/opt ext4 defaults 0 0\r\n LABEL=swap1 swap swap defaults 0 0\r\n ## brukerdata disk\r\n #LABEL=brukerdata2011 \/brukerdata2011 ext3 defaults,usrquota 1 2\r\n ## bind mount for export\r\n #\/brukerdata2011 \/export\/avxxx bind bind,auto 0 0<\/pre>\n<p>The last two, commented out, lines are for an external hard drive that will be installed later and will be nfs4 exported.<\/p>\n<p>I am ready now to make changes to \/boot.<\/p>\n<p>Recall that I am operating on my physical disk that is booted as a VirtualBox VM and I have created a new partition into which and I have untarred the filesystem I am cloning, that I want to become my new system partition after I reboot.\u00a0 I am chrooted into this new system partition.\u00a0 My \/boot partition on\u00a0 \/dev\/sda1 will remain the same, so now I need to create a new initial ramdisk there and also a new grub configuration file, which will boot to my new system partition. I could, straightforwardly, mount the \/boot partition first:<\/p>\n<pre>bash-4.2# mount LABEL=boot \/boot<\/pre>\n<p>and write directly to it.\u00a0 But to be timidly cautious, I will leave my functional \/boot on \/dev\/sda1 unchanged for now and make a copy of it in the\u00a0 \/boot directory of my chrooted system filesystem.\u00a0 Then after I have created my new initramfs and grub.cfg in this copied \/boot directory I will copy these back to the \/boot\u00a0 filesystem on \/dev\/sda1:<\/p>\n<pre>bash-4.2# mkdir \/mnt\/boot\r\nbash-4.2# mount LABEL=boot \/mnt\/boot\r\nbash-4.2# cp -a \/mnt\/boot\/* \/boot\/<\/pre>\n<p>A less obvious advantage to this method is that a redundant copy of the boot system is created in the boot directory of the system partition, which will be safely hidden when it is over-mounted with the \/boot partition, but will be there in case needed for boot recovery emergency.<\/p>\n<p>The next steps are the same either way.<\/p>\n<p>Before recreating the boot system, I need to update my kernel so that the version of kernel modules in \/lib\/modules matches the version installed in \/boot.<\/p>\n<pre>bash-4.2# yum --disableexcludes=all update kernel\\*<\/pre>\n<p>(&#8216;&#8211;disableexcludes&#8217; because I have kernel updates excluded in \/etc\/yum.conf out of concern for system stability)<\/p>\n<p>Now create a new initramfs:<\/p>\n<pre>bash-4.2# dracut --force --no-hostonly --persistent-policy by-label<\/pre>\n<p>The &#8211;persistent-policy by-label flag should configure the new initramfs to use my human-friendly labels instead of UUIDs.<\/p>\n<p>and also re-create grub.cfg:<\/p>\n<pre>bash-4.2# grub2-mkconfig -o \/boot\/grub2\/grub.cfg\r\n\r\n<\/pre>\n<p>I shouldn&#8217;t need to reinstall grub because it is already installed onto my \/dev\/sda hard drive and \/dev\/sda1 boot partition, which recall, was created by my kickstart.\u00a0 That&#8217;s one less thing that could go wrong.<\/p>\n<p>So now I would have everything in place to reboot into the cloned\u00a0 system.\u00a0 Or, since I chose the timidly cautious method, there is the final step of copying\u00a0 my changes back to the real boot partition:<\/p>\n<p>[CTRL-D] (exit chroot)<\/p>\n<p>Recall that my new system partition is still mounted as \/mnt\/avrootvol.\u00a0 I am going to cautiously save my old initramfs vmlinuzes, and grub.cfg, leaving a trail of bread crumbs, just in case things go sideways when I attempt to reboot my new system.<\/p>\n<pre>cd \/mnt\/avrootvol\/boot\r\nmkdir -p \/boot.save\/grub2\r\nmv \/boot\/vmlinuz* \/boot.save\r\nmv \/boot\/initramfs-* \/boot.save\r\nmv \/boot\/grub2\/grub.* \/boot.save\/grub2\/\r\nrsync -av \/mnt\/avrootvol\/boot\/ \/boot<\/pre>\n<p>And now just<\/p>\n<pre>bash-4.2# reboot<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Our beta-test kiosk-workstation is now tested and so this system is ready to clone to our other workstation. The following procedures take off from where, in my previous article, I created a tar backup of system root filesystem (i.e. &#8216;\/&#8217;). In the mean time, the buzzard learned how to attach a physical disk to a&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/staff.washington.edu\/ketcham\/clone-physical-to-virtual-and-back-yet-another-variation\/\">Read More<i class=\"fa fa-angle-right\"><\/i><\/a><\/div>\n<p><br class=\"clear\" \/><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,11],"tags":[],"_links":{"self":[{"href":"https:\/\/staff.washington.edu\/ketcham\/wp-json\/wp\/v2\/posts\/297"}],"collection":[{"href":"https:\/\/staff.washington.edu\/ketcham\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/staff.washington.edu\/ketcham\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/staff.washington.edu\/ketcham\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/staff.washington.edu\/ketcham\/wp-json\/wp\/v2\/comments?post=297"}],"version-history":[{"count":44,"href":"https:\/\/staff.washington.edu\/ketcham\/wp-json\/wp\/v2\/posts\/297\/revisions"}],"predecessor-version":[{"id":364,"href":"https:\/\/staff.washington.edu\/ketcham\/wp-json\/wp\/v2\/posts\/297\/revisions\/364"}],"wp:attachment":[{"href":"https:\/\/staff.washington.edu\/ketcham\/wp-json\/wp\/v2\/media?parent=297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/staff.washington.edu\/ketcham\/wp-json\/wp\/v2\/categories?post=297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/staff.washington.edu\/ketcham\/wp-json\/wp\/v2\/tags?post=297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}