{"id":1079,"date":"2014-02-05T19:59:45","date_gmt":"2014-02-06T02:59:45","guid":{"rendered":"http:\/\/www.threshold-zero.com\/cblog\/?p=1079"},"modified":"2018-12-02T19:12:21","modified_gmt":"2018-12-03T01:12:21","slug":"converting-your-existing-ubuntu-installation-into-a-virtualbox-virtual-machine","status":"publish","type":"post","link":"https:\/\/www.threshold-zero.com\/cblog\/2014\/02\/converting-your-existing-ubuntu-installation-into-a-virtualbox-virtual-machine\/","title":{"rendered":"Converting Your Existing Ubuntu Installation Into a VirtualBox Virtual Machine"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Update for 2018: some of the commands have been changed\u00c2\u00a0 below to reflect new\u00c2\u00a0 possibilities present in Ubuntu 18.04, namely the excellent <code>losetup<\/code> command.)<br><\/p><\/blockquote>\n\n\n<p>I often find myself in the position of having to transfer all my files, applications, and other configurations that make my laptop &#8220;mine&#8221; onto a new laptop.<\/p>\n<p>What&#8217;s so strange about that, you might add? Well, I go through all of this once every six months.\u00c2\u00a0 It&#8217;s not that I keep buying new computers &#8212; I don&#8217;t.\u00c2\u00a0 But I often obtain them in other ways &#8212; I trade, I help someone buy a new computer and in turn get their old one, etc.<\/p>\n<p>So, tired of having to constantly re-install everything (or, at the very least, if I&#8217;ve imaged one laptop to another, having to spend a week or so having to get everything running <em>just<\/em> right), I decided to just convert my current main computer into a VM that I could just run on any computer, running any sort of OS that&#8217;s enough to run VirtualBox.<\/p>\n<p>(This tutorial was created with VirtualBox in mind, but other VM&#8217;s have similar ways of converting the final file after you get to about step 3 or so.)<\/p>\n<p>It seems like it should be easy, and after a little bit of work, I found out that it&#8217;s not too hard.<\/p>\n<ol>\n<li>First, you&#8217;ve got to make an image of your current installation.\u00c2\u00a0 (This is much easier if you have your entire Ubuntu install on one partition, i.e., no \/home partitions on another hard drive or partition.\u00c2\u00a0 You can probably figure out how to manage that, but this tutorial will be just for one-partition installs).\n<ul>\n<li>Boot your computer with another startup disk (CD, jump drive, whatever), and then perform the following command:\n<ul>\n<li><strong><code>dd if=\/dev\/sda1 of=image.bin<\/code><\/strong><\/li>\n<\/ul>\n<\/li>\n<li>&#8220;\/dev\/sda1&#8221; refers to the partition name that your main install is on &#8212; you can find this by doing a &#8220;sudo blkid&#8221; or &#8220;sudo fdisk -l&#8221;<\/li>\n<li>&#8220;image.bin&#8221; refers to the output file that the image will be contained in &#8212; this can be anywhere you want, but set it to a location that&#8217;s not on the hard drive you&#8217;re trying to image.<\/li>\n<\/ul>\n<\/li>\n<li>At this point, I tried to turn the image.bin file into a .vdi file so that VirtualBox could use it for a virtual machine &#8212; the problem is, at this point, your .bin file is just a partition, and not a real &#8220;virtual&#8221; hard drive.\u00c2\u00a0 There&#8217;s no partition table, etc. &#8212; you have to simulate these things.\n<ul>\n<li>You do this by creation an empty &#8220;sparse image&#8221; where we&#8217;ll copy the image, simulating a hard disk, and then create a partition table:\n<ul>\n<li><strong><code>dd if=\/dev\/zero of=newhd.img bs=1G count=0 seek=100<\/code><\/strong><\/li>\n<li>In this, &#8220;newhd.img&#8221;represents the location of the file we&#8217;re creating, and &#8220;100&#8221; represents the size of the virtual hard drive we&#8217;re creating, in gigabytes.\u00c2\u00a0 You may want to make this larger or smaller depending on the image you made.<\/li>\n<\/ul>\n<\/li>\n<li>Now, edit the image with &#8220;<strong>fdisk newhd.img<\/strong>&#8220;, and, following the commands presented in the fdisk interface, create a new partition table, and create a partition as large as the image you created. (The commands inside fdisk are pretty self-explanatory.)<\/li>\n<li>Now, make the partitions available as individual devices to your system.\n<ul>\n<li><strong><code>sudo kpartx -a newhd.img<\/code><\/strong><\/li>\n<\/ul>\n<\/li>\n<li>Now, copy the original .bin file you made in step 1 onto the newly mounted partition:\n<ul>\n<li><strong><code>sudo cp image.bin \/dev\/mapper\/loop0p1<\/code><\/strong><\/li>\n<\/ul>\n<\/li>\n<li>Now, run a disk check, and expand the copied partition to fill all of the available space, and then finally close the mounted partitions:\n<ul>\n<li><strong>sudo e2fsck -f \/dev\/mapper\/loop0p1<\/strong><\/li>\n<li><strong><code>sudo resize2fs \/dev\/mapper\/loop0p1<\/code><\/strong><\/li>\n<li><strong><code>sudo kpartx -d newhd.img<\/code><\/strong><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>At this point, you should have a newhd.img file, which represents the entire hard drive you&#8217;ll virtually mount in your VM &#8212; the only step left is to convert it from a raw image of a hard drive into a .vdi file for use in VirtualBox:\n<ul>\n<li><strong>VBoxManage convertfromraw -format VDI newhd.img newhd.vdi<\/strong><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>The only steps left at this point are to create your new VM in VirtualBox, and then start it using this HD.\u00c2\u00a0 It more than likely won&#8217;t boot, so what you&#8217;ll need to do is start it with a livecd of your choice, and then fix the boot (I used the wonderful boot-repair utility available to Ubuntu).<\/p>\n<p>\u00c2\u00a0<\/p>\n<p>\u00c2\u00a0<\/p>\n<p>Sources:<\/p>\n<p><a href=\"https:\/\/unix.stackexchange.com\/questions\/41137\/convert-image-of-a-partition-into-image-of-a-disk-with-partition-table\">https:\/\/unix.stackexchange.com\/questions\/41137\/convert-image-of-a-partition-into-image-of-a-disk-with-partition-table<\/a><\/p>\n<p><a href=\"https:\/\/superuser.com\/questions\/554862\/how-to-convert-img-to-usable-virtualbox-format\">https:\/\/superuser.com\/questions\/554862\/how-to-convert-img-to-usable-virtualbox-format<\/a><\/p>\n<p><a href=\"https:\/\/askubuntu.com\/questions\/69363\/mount-single-partition-from-image-of-entire-disk-device\">https:\/\/askubuntu.com\/questions\/69363\/mount-single-partition-from-image-of-entire-disk-device<\/a><\/p>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Update for 2018: some of the commands have been changed\u00c2\u00a0 below to reflect new\u00c2\u00a0 possibilities present in Ubuntu 18.04, namely the excellent losetup command.) I often find myself in the position of having to transfer all my files, applications, and other configurations that make my laptop &#8220;mine&#8221; onto a new laptop. What&#8217;s so strange about [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[10,11,31],"tags":[139,140,132,130,131],"class_list":["post-1079","post","type-post","status-publish","format-standard","hentry","category-life","category-linux","category-ubuntu","tag-linux","tag-ubuntu","tag-vdi","tag-virtual-machine","tag-virtualbox"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2ZUZG-hp","_links":{"self":[{"href":"https:\/\/www.threshold-zero.com\/cblog\/wp-json\/wp\/v2\/posts\/1079","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.threshold-zero.com\/cblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.threshold-zero.com\/cblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.threshold-zero.com\/cblog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.threshold-zero.com\/cblog\/wp-json\/wp\/v2\/comments?post=1079"}],"version-history":[{"count":2,"href":"https:\/\/www.threshold-zero.com\/cblog\/wp-json\/wp\/v2\/posts\/1079\/revisions"}],"predecessor-version":[{"id":2205,"href":"https:\/\/www.threshold-zero.com\/cblog\/wp-json\/wp\/v2\/posts\/1079\/revisions\/2205"}],"wp:attachment":[{"href":"https:\/\/www.threshold-zero.com\/cblog\/wp-json\/wp\/v2\/media?parent=1079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.threshold-zero.com\/cblog\/wp-json\/wp\/v2\/categories?post=1079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.threshold-zero.com\/cblog\/wp-json\/wp\/v2\/tags?post=1079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}