{"id":1240,"date":"2016-09-02T05:14:38","date_gmt":"2016-09-02T04:14:38","guid":{"rendered":"http:\/\/cdblog.cdstealer.com\/?p=1240"},"modified":"2017-05-13T06:12:19","modified_gmt":"2017-05-13T05:12:19","slug":"convert-hdd-to-vdi","status":"publish","type":"post","link":"https:\/\/cdblog.cdstealer.com\/?p=1240","title":{"rendered":"Convert HDD to VDI"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1249 alignnone\" src=\"http:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/09\/Virtualbox_logo.png\" alt=\"Virtualbox_logo\" width=\"512\" height=\"512\" srcset=\"https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/09\/Virtualbox_logo.png 512w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/09\/Virtualbox_logo-150x150.png 150w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/09\/Virtualbox_logo-300x300.png 300w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/p>\n<p>I've only had to do this a couple of times in my life, but it's useful to know ;) \u00a0There are 2 ways this can be achieved. \u00a0I'll state the PROS\/CONS at the end of each guide.<\/p>\n<p>Details:<br \/>\nHost system - Gentoo X86_64 with 24Tb storage :), VirtualBox<br \/>\nGuest HDD - Gentoo X86_64 500Gb<\/p>\n<p>I'm going to assume that you have good Linux knowledge using the cli with mounting, tarballing, fdisk and formatting filesystems.<\/p>\n<h1>#1 (pure disk dump)<\/h1>\n<p>Obviously connect the drive you wish to convert.<\/p>\n<p>Create a drive image using dd:<\/p>\n<pre>dd if=\/dev\/sdX of=\/path\/to\/output\/file.dd<\/pre>\n<p>Then convert it to a VDI (Virtual Disk Image):<\/p>\n<pre>VBoxManage convertfromraw ImageFile.dd OutputFile.vdi<\/pre>\n<p>Create a new virtual machine and attach the vdi as the main drive.<\/p>\n<p>Once booted into the VM, you will need to install virtualbox guest addititions and may need to reconfigure\/recompile the kernel.<\/p>\n<p>Done!<\/p>\n<p><strong>PROS<\/strong>:<\/p>\n<ul>\n<li>Not complicated.<\/li>\n<li>Relatively quick to do.<\/li>\n<\/ul>\n<p><strong>CONS<\/strong>:<\/p>\n<ul>\n<li>Uses a lot of space as dd reads every sector of the source drive.<\/li>\n<li>If you delete any data from the VM, it will not reduce the VDI size. \u00a0Dynamic disks only grow, not shrink.<\/li>\n<\/ul>\n<h1>#2 (only copy files)<\/h1>\n<p>Connect the HDD.<\/p>\n<p>Open a terminal.<\/p>\n<p>su to root<\/p>\n<p>Mount each filesystem. \u00a0(I did the following)<\/p>\n<pre>mkdir -p \/mnt\/{oldhdd,newhdd}\/{bootfs,rootfs,homefs}\r\n\r\nmount \/dev\/sdb1 \/mnt\/oldhdd\/bootfs\r\n\r\nmount \/dev\/sdb2 \/mnt\/oldhdd\/rootfs\r\n\r\nmount \/dev\/sdb3 \/mnt\/oldhdd\/homefs<\/pre>\n<p>cd into the place you wish to save the data.<\/p>\n<pre>cd \/mnt\/newhdd\/bootfs\r\n\r\ntar czvf bootfs.tgz \/mnt\/oldhdd\/bootfs\/.\r\n\r\ncd ..\/rootfs\r\n\r\ntar czvf rootfs.tgz \/mnt\/oldhdd\/rootfs\/.\r\n\r\ncd ..\/homefs\r\n\r\ntar czvf homefs.tgz \/mnt\/oldhdd\/homefs\/.<\/pre>\n<p>If you don't have an existing Linux VM, create one including an empty HDD and boot it with your favourite Linux CD\/DVD. \u00a0Ensure the tarballs you have just created are available to the VM. \u00a0hint: \u00a0setup a shared folder in the VBoxManager GUI and mount it in the VM.<\/p>\n<p>You'll need to partition the empty HDD appropriately to accommodate the data you'll be restoring and format each filesystem. \u00a0Mount each filesystem you've just created and and the shared directory then unpack the tarballs to the new HDD.<\/p>\n<p>So we'll mount the prepared HDD.<\/p>\n<pre>mount \/dev\/sda2 \/mnt\/newhdd\/rootfs\r\n\r\nmount \/dev\/sda1 \/mnt\/newhdd\/rootfs\/boot<\/pre>\n<p>eg<\/p>\n<pre>tar zxvf \/path\/to\/share\/rootfs.tgz -C \/mnt\/newhdd\/rootfs\r\n\r\ntar zxvf \/path\/to\/share\/bootfs.tgz -C \/mnt\/newhdd\/rootfs\/boot<\/pre>\n<p>The fun doesn't end yet :) \u00a0Now we have restored our data, we now need to reinstall the boot loader. \u00a0In this case, grub2. \u00a0But first we need to chroot into our restored data.<\/p>\n<pre>cd \/mnt\/newhdd\/rootfs\r\n\r\nmount -t proc none \/mnt\/newhdd\/rootfs\/etc\/proc\r\n\r\nchroot \/mnt\/newhdd\/rootfs \/bin\/bash\r\n\r\nenv-update\r\n\r\nsource \/etc\/profile\r\n\r\ngrub-install \/dev\/sda\r\n\r\nedit \/etc\/fstab if required\r\n\r\ngrub-mkconfig -o \/boot\/grub\/grub.cfg\r\n\r\nexit<\/pre>\n<p>Unmount the filesystems and reboot the VM. \u00a0If everything went to plan, you should now be booted into your VM. \u00a0You will need to install guest additions and may need to reconfigure and recompile your kernel.<\/p>\n<p><strong>PROS<\/strong>:<br \/>\nThe VDI only uses the space required.<\/p>\n<p><strong>CONS<\/strong>:<br \/>\nMore involved and takes longer.<\/p>\n<h1>#3 (existing VM)<\/h1>\n<p>This is a combination of #1 &amp; #2 methods.<\/p>\n<p>Create a drive image using dd:<\/p>\n<pre>dd if=\/dev\/sdX of=\/path\/to\/output\/file.dd<\/pre>\n<p>Then convert it to a VDI (Virtual Disk Image):<\/p>\n<pre>VBoxManage convertfromraw ImageFile.dd OutputFile.vdi<\/pre>\n<p>Add the new VDI as an additional HDD to your VM. \u00a0Also create a new HDD and partition appropriately.<\/p>\n<p>Within the VM,<\/p>\n<pre>tar clf - -C \/mnt\/oldhdd\/bootfs .| tar xf - -C \/mnt\/newhdd\/bootfs<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I've only had to do this a couple of times in my life, but it's useful to know ;) \u00a0There are 2 ways this can be achieved. \u00a0I'll state the PROS\/CONS at the end of each guide. Details: Host system - Gentoo X86_64 with 24Tb storage :), VirtualBox Guest HDD - Gentoo X86_64 500Gb I'm &hellip; <a href=\"https:\/\/cdblog.cdstealer.com\/?p=1240\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Convert HDD to VDI<\/span> <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":[1],"tags":[18,239,238],"class_list":["post-1240","post","type-post","status-publish","format-standard","hentry","category-gentoo","tag-linux","tag-migrate","tag-virtualbox"],"_links":{"self":[{"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/posts\/1240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1240"}],"version-history":[{"count":11,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/posts\/1240\/revisions"}],"predecessor-version":[{"id":1575,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/posts\/1240\/revisions\/1575"}],"wp:attachment":[{"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}