summaryrefslogtreecommitdiff
path: root/kvm.html
blob: fc90ea3cb0334cc2c90cff2914ca72d52f01e082 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<!DOCTYPE html>

<html lang="en-us">
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<meta name="author" content="Rafael Marçalo">
		<meta name="description" content="Create a Virtual Machine using KVM">
		<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon">
		<link rel="stylesheet" type="text/css" href="styles/style.css">
		<title>Create a Virtual Machine using KVM</title>
	</head>
	<body>
		<div id="header">
			<h1>Rafa's Website</h1>
			<nav>
				<a href="index.html">home</a>
				|
				<a href="projects.html">projects</a>
				|
				<a href="donate.html">donate</a>
			</nav>
		</div>

		<div id="content">

			<h2>What is KVM</h2>
			<p><em>"Kernel-based Virtual Machine (KVM) is an open source virtualization technology built into Linux®. Specifically, KVM lets you turn Linux into a hypervisor that allows a host machine to run multiple, isolated virtual environments called guests or virtual machines (VMs)."</em> - <a href="https://www.redhat.com/en/topics/virtualization/what-is-KVM">Red Hat, What is KVM?</a></p>

			<h2>Install required packages</h2>
			<p>To run a KVM virtual machine you need to have a Intel VT/AMD-V supported machine and you need to be running any sort of Linux distribution which provides virt-manager, libvirt and qemu packages.</p>
			<p>For this instance I'm currently running a Artix GNU/Linux distribution(same as Arch Linux), but all the steps should be significantly the same for other distributions excluding the package names</p>

			<h3>Required Packages</h3>
			<ul>
				<li><code>libvirt</code>;</li>
				<li><code>virt-manager</code>;</li>
				<li><code>qemu-desktop</code>;</li>
				<li><code>edk2-ovmf</code> (Optional, but required if you're trying to virtualize Windows 11);</li>
				<li><code>swtpm</code> (Optional, but required if you're trying to virtualize Windows 11).</li>
			</ul>

			<h2>Prepairing Virt-Manager</h2>

			<h3>Setting up a user session</h3>
			<p>After installing these packages according the OS you're running, you must now open virt-manager and create a new connection to a user qemu/kvm session. You do this by going into the file menu and by clicking on the <code>Add Connection...</code> option.</p>
			<p>Now in the <code>Hypervisor</code>, select <code>QEMU/KVM user session</code>.</p>

			<h3>Enabling XML Editing (Optional, but required if installing Windows 11)</h3>
			<p>Since each VM is configured in XML in th backend, XML editing will allow you to have more control over your VM.</p>
			<p>To do this, all you have to do is, go to the Edit menu, and select <code>Preferences</code>, now just tick on the option that says <code>Enable XML editing</code>.</p>

			<h2>Setting up the Virtual Machine</h2>
			<p>In this section I'll be going over the steps to install Windows 11 on KVM. Some of the steps will be marked with a optional marker, which means, that the referred stage is specific to Windows 11 only.</p>

			<h3>Creating the Virtual Machine</h3>
			<p>Firstly you must click the "➕" icon and select the <code>Local install media (ISO image or CDROM)</code> option.</p>

			<h3>Install media</h3>
			<p>Now go ahead and choose your installation ISO, if the OS is not automatically detected, select it manually or choose something similar to what you are trying to virtualize.</p>
			<p><small><strong>Tip:</strong> When browsing for the iso, there's no need to create a pool, you can just click on <code>Browse local</code> and click on the desired ISO file</small></p>

			<h3>Ram and CPU</h3>
			<p>After selecting the disk, you'll be prompted with the memory and CPU settings, in this stage my only advice is to use at least 2GiB of ram.</p>

			<h3>Storage</h3>
			<p>Next up you'll be prompted with virtual storage, I recommend at least 25GiB if you're running Windows, if you're running any Linux distro I'd give it at least 15GiB.</p>

			<h3>Customize Installation (Optional, but required if installing Windows 11)</h3>
			<p>For a Windows 11 installation you need to have a UEFI, secure boot capable machine with the TPM(Trusted Platform Module) minimum version 2.0. Ahead I'll show how to take care of each instance.</p>

			<h4>TPM Module</h4>
			<p>After selecting the desired storage, go ahead and tick  on the option that says <code>Customize configuration before install</code>.</p>
			<p>Here you must click on <code>Add Hardware</code> and select the TPM category</p>
			<p>Under TPM, the type must be <code>Emulated</code>. In advanced options, the model must be <code>TIS</code> and the version <code>2.0</code>.</p>

			<h4>Secure Boot</h4>
			<p>Still in the <code>Customize configuration before install</code> window, go over to the <code>Overview</code> category and set the firmware to something like <code>UEFI x86_64: /usr/share/edk2-ovmf/x64/OVMF_CODE.secboot.fd</code></p>
			<p>After changing the firmware there are some hardware modules that won't working, so we must remove them to evade conflicts. To do this, still in the <code>Overview</code> category, go over to the <code>XML</code> tab and remove the lines below:</p>

			<pre><code>&lt;controller type="pci" model="pcie-root"/&gt;
&lt;controller type="pci" model="pcie-root-port"/&gt;
&lt;controller type="pci" model="pcie-root-port"/&gt;
&lt;controller type="pci" model="pcie-root-port"/&gt;
&lt;controller type="pci" model="pcie-root-port"/&gt;
&lt;controller type="pci" model="pcie-root-port"/&gt;
&lt;controller type="pci" model="pcie-root-port"/&gt;
&lt;controller type="pci" model="pcie-root-port"/&gt;
&lt;controller type="pci" model="pcie-root-port"/&gt;
&lt;controller type="pci" model="pcie-root-port"/&gt;
&lt;controller type="pci" model="pcie-root-port"/&gt;
&lt;controller type="pci" model="pcie-root-port"/&gt;
&lt;controller type="pci" model="pcie-root-port"/&gt;
&lt;controller type="pci" model="pcie-root-port"/&gt;
&lt;controller type="pci" model="pcie-root-port"/&gt;</code></pre>

			<h3>Finishing Installation</h3>
			<p>Now that you have everything setup to run the VM, just hit <code>Finish</code> or <code>Begin Installation</code>.</p>

			<h3>Extra: Shared Linux VM folder</h3>
			<h4>Create a shared folder</h4>
			<p>In order to use a shared folder between the host machine and the guest VM, you must first create a folder in your filesystem and `chmod 777` it.</p>
			<pre><code>mkdir ~/KVM_Share</code></pre>

			<h4>Adding the shared folder has a filesystem</h4>
			<p>Now open virt-manager, got to your virtual machine and under the virtual hardware tab, click on `Add Hardware`, select filesystem and in the driver option, select the virtio-9p option.</p>
			<p>In the source path you are going to put the complete path to the host machine shared folder and in the target path you should type where the filesystem should exist inside the guest VM.</p>
			<p>For this instance we are going to set the source path to <code>~/KVM_Share</code> and the target path to <code>/shared</code>.</p>

			<h4>Mounting the shared folder</h4>
			<p>Boot your guest VM and create a folder anywhere in your filesystem where you will mount the shared folder to.</p>
			<pre><code>mkdir ~/hostfiles</code></pre>
			<p>Now, if you type: <code>sudo mount -t 9p -o trans=virtio /shared ~/hostfiles</code>, the shared folder will be mounted.</p>

			<h4>Auto mount on startup</h4>
			<p>Now if you want the guest VM to auto mount the shared folder everytime you boot it, you just have to add the following line to your guest VM <code>/etc/fstab</code> file:</p>
			<p><code>/shared /home/user/hostfiles 9p trans=virtio,version=9p2000.L,rw 0 0</code></p>
			<p><strong>WARNING!!! The mounted folder path must be an absolute path!</strong></p>

			<h3>References:</h3>
			<ul>
				<li><a href="https://youtu.be/p1d_b_91YlU"><em>DistroTube</em> - Virt-Manager Is The Better Way To Manage VMs</a>;</li>
				<li><a href="https://computingforgeeks.com/enable-tpm-on-kvm-and-install-windows/"><em>Öteyo, Klinsmann</em> at Computing for Geeks - How To Enable TPM 2.0 on KVM and install Windows 11</a>;</li>
				<li><a href="https://ostechnix.com/setup-a-shared-folder-between-kvm-host-and-guest/"><em>Sk</em> at OSTECHNIX - Setup A Shared Folder Between KVM Host And Guest</a>.</li>
			</ul>
		</div>

		<div id="footer">
			<div id="contact">
				<p>Email: <a href="mailto:rafa@rafaelmarcalo.xyz">rafa@rafaelmarcalo.xyz</a></p>
				<p>Github: <a href="https://github.com/rafa-99">rafa-99</a></p>
				<p>GPG: <a href="files/etc/rafael.gpg">Public Key</a></p>
				<p>License: <a href="https://www.gnu.org/licenses/gpl-3.0.txt">GPLv3</a></p>
			</div>
		</div>
	</body>
</html>