<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.mkcs.at/wikien/index.php?action=history&amp;feed=atom&amp;title=Ubuntu_QEMU_VM</id>
	<title>Ubuntu QEMU VM - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.mkcs.at/wikien/index.php?action=history&amp;feed=atom&amp;title=Ubuntu_QEMU_VM"/>
	<link rel="alternate" type="text/html" href="https://wiki.mkcs.at/wikien/index.php?title=Ubuntu_QEMU_VM&amp;action=history"/>
	<updated>2026-05-03T19:54:29Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.12</generator>
	<entry>
		<id>https://wiki.mkcs.at/wikien/index.php?title=Ubuntu_QEMU_VM&amp;diff=215&amp;oldid=prev</id>
		<title>MkWikiEnSysOp: 16 revisions imported</title>
		<link rel="alternate" type="text/html" href="https://wiki.mkcs.at/wikien/index.php?title=Ubuntu_QEMU_VM&amp;diff=215&amp;oldid=prev"/>
		<updated>2018-06-28T17:19:39Z</updated>

		<summary type="html">&lt;p&gt;16 revisions imported&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;So far I installed operating systems in virtual machines to its virtual hard disk using a CD or DVD image (an ISO file), similar to what I would do on a real PC. Some years ago I noticed that Qemu has options to directly load a kernel and initial ramdisk (initrd) into the virtual ROM of the virtual computer, so it can directly start the kernel without needing a firmware to detect hard disks, seek for boot loader and the like (similar to what many embedded systems do as well, PCs normally don&amp;#039;t do that). It&amp;#039;s quite clear that this reduces the time for the machine to come up and also reduces the complexity and number of things that could go wrong. So I decided to make an experiment and see which steps are necessary to get such a virtual machine running.&lt;br /&gt;
&lt;br /&gt;
== Creating files for VM ==&lt;br /&gt;
&lt;br /&gt;
First, I created a directory that contains all the files needed and copied my currently used kernel and initrd to that directory.&lt;br /&gt;
&lt;br /&gt;
 mkdir ubuntu_qemu&lt;br /&gt;
 cp -v /boot/*$(uname -r)* ubuntu_qemu/&lt;br /&gt;
 cd ubuntu_qemu&lt;br /&gt;
&lt;br /&gt;
Next, I created a file that would represent the virtual hard disk (a raw image) and formatted that file with an ext3 file system. The size of that virtual hard disk is just 512 MiB, which would be enough for that experiment. For real world scenarios I strongly recommend a larger file!&lt;br /&gt;
&lt;br /&gt;
 dd if=/dev/zero of=testimage bs=1048576 count=512 # create 512 MiB image&lt;br /&gt;
 mkfs.ext4 -L testpart testimage # format image with ext4&lt;br /&gt;
&lt;br /&gt;
An alternative is to create an QCOW image and format it:&lt;br /&gt;
&lt;br /&gt;
 qemu-img create -f qcow2 drive0.img 1G&lt;br /&gt;
 modprobe nbd max_part=16&lt;br /&gt;
 qemu-nbd -c /dev/nbd0 drive0.img&lt;br /&gt;
 partprobe /dev/nbd0&lt;br /&gt;
 mkfs.ext4 -L &amp;#039;&amp;#039;label&amp;#039;&amp;#039; /dev/nbd0&lt;br /&gt;
&lt;br /&gt;
If you want to have some spare disks, let them create in a batch:&lt;br /&gt;
&lt;br /&gt;
 for i in $(seq 1 4); do echo &amp;quot;Creating spare disk $i&amp;quot;; dd if=/dev/zero of=sparedisk$i bs=1048576 count=128; done&lt;br /&gt;
&lt;br /&gt;
== Installing Ubuntu ==&lt;br /&gt;
&lt;br /&gt;
Ubuntu won&amp;#039;t be installed using ubiquity as usual, but just by using &amp;quot;debootstrap&amp;quot; on my host system (which is Xubuntu 14.04 x64 with Linux 4.2.0-36 at time of writing).&lt;br /&gt;
&lt;br /&gt;
=== dnsmasq on host ===&lt;br /&gt;
&lt;br /&gt;
My bridge &amp;quot;br0&amp;quot; has an IP address of 10.92.92.253 and dnsmasq is listening on that address. YMMV - change as needed.&lt;br /&gt;
&lt;br /&gt;
 dnsmasq --interface=br0 --bind-interfaces --dhcp-range=10.92.92.1,10.92.92.128 # on host, start dnsmasq&lt;br /&gt;
&lt;br /&gt;
=== debootstrap and network configuration ===&lt;br /&gt;
&lt;br /&gt;
So we install Ubuntu using debootstrap and configure the installation.&lt;br /&gt;
&lt;br /&gt;
 mkdir mnt&lt;br /&gt;
 mount testimage mnt/&lt;br /&gt;
 debootstrap --arch=amd64 trusty mnt/ http://archive.ubuntu.com/ubuntu/ # downloads about 40 MiB; takes about 30 min&lt;br /&gt;
 echo &amp;quot;/dev/sda	/	ext4	errors=remount-ro	0	1&amp;quot; &amp;gt; mnt/etc/fstab&lt;br /&gt;
 echo &amp;quot;ubuntu_qemu&amp;quot; &amp;gt; mnt/etc/hostname # set a meaningful hostname&lt;br /&gt;
 echo &amp;quot;auto eth0&amp;quot; &amp;gt; mnt/etc/network/interfaces&lt;br /&gt;
&lt;br /&gt;
If dnsmasq (or some other DHCP server) is running on the host and listening on the bridge used, use this line&lt;br /&gt;
&lt;br /&gt;
 echo &amp;quot;iface eth0 inet dhcp&amp;quot;  &amp;gt;&amp;gt; mnt/etc/network/interfaces&lt;br /&gt;
&lt;br /&gt;
Otherwise, use a static configuration:&lt;br /&gt;
&lt;br /&gt;
 echo &amp;quot;iface eth0 inet static&amp;quot; &amp;gt;&amp;gt; mnt/etc/network/interfaces&lt;br /&gt;
 echo &amp;quot;    address 10.92.92.123&amp;quot; &amp;gt;&amp;gt; mnt/etc/network/interfaces&lt;br /&gt;
 echo &amp;quot;    netmask 255.255.255.0&amp;quot; &amp;gt;&amp;gt; mnt/etc/network/interfaces&lt;br /&gt;
 echo &amp;quot;    gateway 10.92.92.253&amp;quot; &amp;gt;&amp;gt; mnt/etc/network/interfaces&lt;br /&gt;
&lt;br /&gt;
For more information on network configuration, see [https://wiki.debian.org/NetworkConfiguration NetworkConfiguration in Debian Wiki].&lt;br /&gt;
&lt;br /&gt;
Tell the system which DNS resolver you would like to use:&lt;br /&gt;
&lt;br /&gt;
 echo &amp;quot;nameserver 10.92.92.253&amp;quot; &amp;gt;  mnt/etc/resolv.conf&lt;br /&gt;
&lt;br /&gt;
=== Setting passwords ===&lt;br /&gt;
&lt;br /&gt;
Next, we&amp;#039;ll chroot into the system:&lt;br /&gt;
&lt;br /&gt;
 chroot mnt/&lt;br /&gt;
&lt;br /&gt;
In the chrooted environment:&lt;br /&gt;
&lt;br /&gt;
 passwd # set root password&lt;br /&gt;
 useradd someuser # add a user&lt;br /&gt;
 passwd someuser # set user&amp;#039;s password&lt;br /&gt;
&lt;br /&gt;
Then leave with Ctrl+D.&lt;br /&gt;
&lt;br /&gt;
== Booting the VM ==&lt;br /&gt;
&lt;br /&gt;
 qemu-system-x86_64 -kernel vmlinuz-4.2.0-36-generic -initrd initrd.img-4.2.0-36-generic \&lt;br /&gt;
   -boot d testimage -net nic,macaddr=52:54:e:2:11:10 -enable-kvm -name ubuntu1404qemu -net none -net bridge,br=br0 -m 512&lt;br /&gt;
&lt;br /&gt;
== Statistics ==&lt;br /&gt;
&lt;br /&gt;
=== Disk usage ===&lt;br /&gt;
&lt;br /&gt;
Right after the installation, df reported this:&lt;br /&gt;
&lt;br /&gt;
 Filesystem      Size  Used Avail Use% Mounted on&lt;br /&gt;
 /dev/sda        488M  253M  210M  55% /&lt;br /&gt;
&lt;br /&gt;
Then I installed the openssh server, which loaded a bunch of dependencies on the system:&lt;br /&gt;
&lt;br /&gt;
 Filesystem      Size  Used Avail Use% Mounted on&lt;br /&gt;
 /dev/sda        488M  319M  144M  69% /&lt;br /&gt;
&lt;br /&gt;
=== Memory ===&lt;br /&gt;
&lt;br /&gt;
With 512 MiB RAM, /proc/meminfo reported this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
MemTotal:         500844 kB&lt;br /&gt;
MemFree:          408108 kB&lt;br /&gt;
MemAvailable:     464828 kB&lt;br /&gt;
Buffers:            5936 kB&lt;br /&gt;
Cached:            51096 kB&lt;br /&gt;
SwapCached:            0 kB&lt;br /&gt;
Active:            40088 kB&lt;br /&gt;
Inactive:          25776 kB&lt;br /&gt;
Active(anon):       8868 kB&lt;br /&gt;
Inactive(anon):      264 kB&lt;br /&gt;
Active(file):      31220 kB&lt;br /&gt;
Inactive(file):    25512 kB&lt;br /&gt;
Unevictable:           0 kB&lt;br /&gt;
Mlocked:               0 kB&lt;br /&gt;
SwapTotal:             0 kB&lt;br /&gt;
SwapFree:              0 kB&lt;br /&gt;
Dirty:                 0 kB&lt;br /&gt;
Writeback:             0 kB&lt;br /&gt;
AnonPages:          8856 kB&lt;br /&gt;
Mapped:             9200 kB&lt;br /&gt;
Shmem:               304 kB&lt;br /&gt;
Slab:              17504 kB&lt;br /&gt;
SReclaimable:      10548 kB&lt;br /&gt;
SUnreclaim:         6956 kB&lt;br /&gt;
KernelStack:        1040 kB&lt;br /&gt;
PageTables:         1212 kB&lt;br /&gt;
NFS_Unstable:          0 kB&lt;br /&gt;
Bounce:                0 kB&lt;br /&gt;
WritebackTmp:          0 kB&lt;br /&gt;
CommitLimit:      250420 kB&lt;br /&gt;
Committed_AS:      38900 kB&lt;br /&gt;
VmallocTotal:   34359738367 kB&lt;br /&gt;
VmallocUsed:        3304 kB&lt;br /&gt;
VmallocChunk:   34359730020 kB&lt;br /&gt;
HardwareCorrupted:     0 kB&lt;br /&gt;
AnonHugePages:         0 kB&lt;br /&gt;
CmaTotal:              0 kB&lt;br /&gt;
CmaFree:               0 kB&lt;br /&gt;
HugePages_Total:       0&lt;br /&gt;
HugePages_Free:        0&lt;br /&gt;
HugePages_Rsvd:        0&lt;br /&gt;
HugePages_Surp:        0&lt;br /&gt;
Hugepagesize:       2048 kB&lt;br /&gt;
DirectMap4k:       36856 kB&lt;br /&gt;
DirectMap2M:      487424 kB&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Boot time ===&lt;br /&gt;
&lt;br /&gt;
6 seconds after starting Qemu, Linux starts writing its output. After 20, I see the login prompt.&lt;br /&gt;
&lt;br /&gt;
On my host, btrfs-tools is installed, so the initrd contains the code to seek for btrfs partitions, which takes about 3 seconds. Apparently, the boot time could be improved if btrfs was not used in the VM and the code was removed.&lt;br /&gt;
&lt;br /&gt;
== Outlook ==&lt;br /&gt;
&lt;br /&gt;
This can be done also with a virtual machine using ARM architecture. Further information can be found here:&lt;br /&gt;
&lt;br /&gt;
* [https://wiki.ubuntu.com/ARM/RootfsFromScratch/QemuDebootstrap Ubuntu Wiki: QemuDebootstrap]&lt;br /&gt;
&lt;br /&gt;
[[Category:Linux]]&lt;/div&gt;</summary>
		<author><name>MkWikiEnSysOp</name></author>
	</entry>
</feed>