<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>IONCANNON &#187; virtual box</title>
	<atom:link href="http://www.ioncannon.net/tag/virtual-box/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ioncannon.net</link>
	<description>Thoughts on Software Development and Engineering</description>
	<lastBuildDate>Tue, 03 Jan 2012 13:59:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
	<atom:link rel='hub' href='http://www.ioncannon.net/?pushpress=hub'/>
		<item>
		<title>How to transfer a Linux image from VirtualBox to Xen</title>
		<link>http://www.ioncannon.net/system-administration/80/how-to-transfer-linux-from-virtualbox-to-xen/</link>
		<comments>http://www.ioncannon.net/system-administration/80/how-to-transfer-linux-from-virtualbox-to-xen/#comments</comments>
		<pubDate>Sun, 04 Jan 2009 19:19:11 +0000</pubDate>
		<dc:creator>carson</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[system administration]]></category>
		<category><![CDATA[ec2]]></category>
		<category><![CDATA[virtual box]]></category>

		<guid isPermaLink="false">http://www.ioncannon.net/?p=80</guid>
		<description><![CDATA[There have been times recently when I wanted to pull a VirtualBox Linux instance I had into Xen. I kept thinking it had to be fairly easy but I kept putting off trying it until recently when I ran into something I wanted to install from a CD image into an Amazon EC2 AMI. It [...]]]></description>
			<content:encoded><![CDATA[<p>There have been times recently when I wanted to pull a <a href="http://www.virtualbox.org/">VirtualBox</a> Linux instance I had into Xen. I kept thinking it had to be fairly easy but I kept putting off trying it until recently when I ran into something I wanted to install from a CD image into an Amazon EC2 AMI. It turns out the main hurdle in transferring an image is lack of documentation.</p>
<p><span id="more-80"></span></p>
<p>I&#039;m using VirtualBox 2.1.0 so some of the following commands may not work with older versions. I learned the hard way that they have changed a number of tools for VirtualBox and some of the older tools where probably easier to use and documented better. I installed the package I was using from an ISO image and then started trying to extract the part that I needed from the VDI that was created.</p>
<p>My first attempt at extracting the partition required me to convert my dynamic VDI into a static image. To dump a dynamic VDI into a static image you run this command:</p>
<div class="codesnip-container" >
<div class="bash codesnip" style="font-family:monospace;">VBoxManage convertdd <span class="re5">-static</span> abox.vdi <span class="sy0">/</span>tmp<span class="sy0">/</span>abox.img</div>
</div>
<p>I thought I could find the image by hand in the VDI after I had it in a raw format. There were a number of hints that I found that made me think I could just pull the partition out without much of a problem: <a href="http://forensicir.blogspot.com/2008/01/virtualbox-and-forensics-tools.html">VirtualBox and forensics tools</a> and <a href="http://forums.virtualbox.org/viewtopic.php?t=52&#038;start=0&#038;postdays=0&#038;postorder=asc&#038;highlight=">a forum post</a>. However I found that just looking around wasn&#039;t easy enough to find where the partition started so I moved on to trying to find something else that could scan the disk and find it.</p>
<p>I rand into <a href="http://www.cgsecurity.org/wiki/TestDisk">TestDisk</a> and gave it a try. When it would scan the disk it found the /boot partition but for some reason it wasn&#039;t finding the root partition so I moved on.</p>
<p>I then took a look at the <a href="http://forums.virtualbox.org/viewtopic.php?t=8046">format for VDI disks</a> to see if it was possible to pull it out given the header information with a simple program but that looked like it would be a lot of work so it was back to square one.</p>
<p>Along the way I happened to came across information about an undocumented command to <a href="http://www.virtualbox.org/changeset/12775">export</a> raw <a href="http://techokarma.blogspot.com/2008/10/v2p-virtual-to-physical-for-virtualbox.html">disk image</a>. This turned out to be the break I needed because running the following command will result in only the disk image itself without any VirtualBox residue:</p>
<div class="codesnip-container" >
<div class="bash codesnip" style="font-family:monospace;">VBoxManage internalcommands converttoraw myosimage.vdi <span class="sy0">/</span>tmp<span class="sy0">/</span>myosimage.img</div>
</div>
<p>At this point things became a lot easier. There were multiple partitions on the resulting disk image but I only needed the / partition. To extract the root partition I first listed the partitions with this command:</p>
<div class="codesnip-container" >
<div class="bash codesnip" style="font-family:monospace;">fdisk <span class="re5">-lu</span> myosimage.img</div>
</div>
<p>This output the following for my image:</p>
<div class="codesnip-container" >
<div class="text codesnip" style="font-family:monospace;">You must set cylinders.<br />
You can do this from the extra functions menu.</p>
<p>Disk myosimage.img: 0 MB, 0 bytes<br />
255 heads, 63 sectors/track, 0 cylinders, total 0 sectors<br />
Units = sectors of 1 * 512 = 512 bytes<br />
Disk identifier: 0x0003f47f</p>
<p>&nbsp; &nbsp; &nbsp; Device Boot &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Start &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End &nbsp; &nbsp; &nbsp; &nbsp; Blocks &nbsp; &nbsp;Id &nbsp;System<br />
myosimage.img1 &nbsp; * &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;63 &nbsp; &nbsp; &nbsp; 208844 &nbsp; &nbsp; &nbsp; &nbsp;104391 &nbsp; 83 &nbsp;Linux<br />
myosimage.img2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;208845 &nbsp; &nbsp; &nbsp;3662819 &nbsp; &nbsp; 1726987+ &nbsp;83 &nbsp;Linux<br />
myosimage.img3 &nbsp; &nbsp; &nbsp; &nbsp; 3662820 &nbsp; &nbsp; 4192964 &nbsp; &nbsp; &nbsp; 265072+ &nbsp;82 &nbsp;Linux swap / Solaris</div>
</div>
<p>To figure out where the root partition starts I just multiplied the start sector by the number of bytes per sector:  208845 * 512 = 106928640</p>
<p>I then did a quick test to make sure I had the correct partition:</p>
<div class="codesnip-container" >
<div class="bash codesnip" style="font-family:monospace;"><span class="kw2">mount</span> <span class="re5">-o</span> loop,<span class="re2">offset</span>=<span class="nu0">106928640</span> myosimage.img <span class="sy0">/</span>mnt<span class="sy0">/</span></div>
</div>
<p>This looked good so I extracted the partition from the disk and did a filesystem check on it:</p>
<div class="codesnip-container" >
<div class="bash codesnip" style="font-family:monospace;"><span class="kw2">dd</span> <span class="kw1">if</span>=myosimage.img <span class="re2">of</span>=mypartimage.img <span class="re2">bs</span>=512 <span class="re2">skip</span>=208845 <span class="re2">count</span>=<span class="nu0">3453974</span><br />
e2fsck mypartimage.img</div>
</div>
<p>Extracting the partition you want is about 80% of the battle. Getting it to run under Xen after extraction is just a matter of fixing anything that was left out because the install was done under a &#034;real&#034; machine. </p>
<p>I add a nosegneg ld.so.conf directive and move /lib/tls directory out of the way first:</p>
<div class="codesnip-container" >
<div class="bash codesnip" style="font-family:monospace;"><span class="kw3">echo</span> <span class="st0">&quot;hwcap 0 nosegneg&quot;</span> <span class="sy0">&gt;</span> &nbsp;<span class="sy0">/</span>mnt<span class="sy0">/</span>etc<span class="sy0">/</span>ld.so.conf.d<span class="sy0">/</span>nosegneg.conf<br />
<span class="kw2">mv</span> <span class="sy0">/</span>mnt<span class="sy0">/</span>lib<span class="sy0">/</span>tls <span class="sy0">/</span>mnt<span class="sy0">/</span>lib<span class="sy0">/</span>tls.disabled</div>
</div>
<p>Next the base device entries needed to be created:</p>
<div class="codesnip-container" >
<div class="bash codesnip" style="font-family:monospace;"><span class="kw1">for</span> i <span class="kw1">in</span> console null zero ; <span class="kw1">do</span> <span class="sy0">/</span>sbin<span class="sy0">/</span>MAKEDEV <span class="re5">-d</span> <span class="sy0">/</span>mnt<span class="sy0">/</span>dev <span class="re5">-x</span> <span class="re1">$i</span> ; <span class="kw1">done</span></div>
</div>
<p>I then removed the disk label from the partition using e2label:</p>
<div class="codesnip-container" >
<div class="bash codesnip" style="font-family:monospace;">e2label mypartimage.img <span class="st0">&quot;&quot;</span></div>
</div>
<p>Because I was sending this image to EC2 I recreated the fstab with the following entries that are specific to the way EC2 allocates disks to a node:</p>
<div class="codesnip-container" >
<div class="bash codesnip" style="font-family:monospace;"><span class="sy0">/</span>dev<span class="sy0">/</span>sda1 &nbsp;<span class="sy0">/</span> &nbsp; &nbsp; &nbsp; &nbsp; ext3 &nbsp; &nbsp;defaults &nbsp; &nbsp; &nbsp; &nbsp;<span class="nu0">1</span> <span class="nu0">1</span><br />
<span class="sy0">/</span>dev<span class="sy0">/</span>sda2 &nbsp;<span class="sy0">/</span>mnt &nbsp; &nbsp; &nbsp;ext3 &nbsp; &nbsp;defaults &nbsp; &nbsp; &nbsp; &nbsp;<span class="nu0">1</span> <span class="nu0">2</span><br />
<span class="sy0">/</span>dev<span class="sy0">/</span>sda3 &nbsp;swap &nbsp; &nbsp; &nbsp;swap &nbsp; &nbsp;defaults &nbsp; &nbsp; &nbsp; &nbsp;<span class="nu0">0</span> <span class="nu0">0</span></div>
</div>
<p>The finally, again because I was going to EC2 I added a few scripts and created rc.local to let me in when the instance was started. </p>
<p>This seems to be a fairly easy process now that I have done it from start to finish once.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ioncannon.net/system-administration/80/how-to-transfer-linux-from-virtualbox-to-xen/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>

