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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
[% topdir = "../.." -%]
[% PROCESS globals -%]
[% WRAPPER page
title = "Installing a virtual machine using virt-install"
h1 = "Installing a virtual machine using virt-install"
section = "learning"
local_stylesheet = 1
%]
<p>
Creating a virtual machine with virt-install involves the same steps
as doing it <a href="../start-install-with-virt-manager/">graphically
with virt-manager</a>.
</p>
<p>
First you need to download an ISO of the
operating system you want to install or you can do a
<a href="#network">network install</a>.
</p>
[% WRAPPER h2 h2="Create space for the virtual disk" anchor="disk" %]
<p>
There are several decisions you have to make about how
and where you want to create the virtual hard disk for
the new guest:
</p>
<ol>
<li> <b>How large?</b>
Decide how large you want the disk image to be. This is the
maximum amount of disk space that the virtual machine will be
able to use, unless you take manual steps to increase it
(using <code>virt-resize</code>), so choose this wisely.
Even if you choose a format which <q>grows</q> (like sparse or
qcow2), this is a hard limit that you cannot easily grow beyond.
Below are some rough guides for minimum <i>operating system only</i>
space. Remember if you want to store significant amounts of
data in the guest, you must add that requirement on top of
these figures.
<table>
<tr>
<th> Minimal </th>
<td> eg. Debian or FreeBSD, text only, configured with just the
essential components. </td>
<td> 2048 MB </td>
</tr>
<tr>
<th> Linux </th>
<td> Typical Linux distribution, base install with graphical
components. </td>
<td> 8192 MB </td>
</tr>
<tr>
<th> Windows </th>
<td> Recent version of Windows. </td>
<td> 16384 MB </td>
</tr>
</table>
An example calculation: a Linux virtual machine for developers
— 8192 (base OS) + 1000 (compiler tools) + 20000 (space to check
out and compile software in home directory) = 29192 MB.
</li>
<li> <b>File, logical volume or SAN?</b>
Storing the disk as a plain host file is the easiest option. As
long as you have enough free space, you can put the file anywhere,
and move it around later. However file access isn't very
fast, so for the best performance you should choose an LVM2 logical
volume (LV) or (if you have it) a dedicated LUN on your SAN storage.
Using a host partition or host disk directly is not recommended
for security reasons.
</li>
<li> <b>Fully allocated or sparse?</b>
If you choose <q>file</q> then you also have the option of using
a sparse file. Sparse files are allocated on demand (as the guest
writes to them), which means you don't need to dedicate disk space
up-front. On the other hand, sparse allocation is slow, and
if you allow the host to run out of disk space this can cause dangerous
data loss in the guest.
</li>
<li> <b>Raw or qcow2?</b>
Again for file storage, you can choose a raw disk (just a massive
file which is a direct image of what is in the guest's disk), or
the more flexible <a href="http://en.wikipedia.org/wiki/Qcow2">qcow2
format</a>. qcow2 images grow on-demand (a bit like sparse files),
and there are other features like snapshotting. However out of all
the file options, qcow2 is the slowest.
</li>
<li> <b>Snapshot?</b>
If you chose LV, qcow2 or SAN, then you might want to clone an
existing guest. Snapshots are out of scope for this document.
For more information we suggest reading the <code>lvcreate(8)</code>
and/or <code>qemu-img(1)</code> man pages.
</li>
<li> <b>libvirt storage pools?</b>
Libvirt now features storage management, and you can store your
VM disk images in a volume in a libvirt storage pool. This is out
of scope for the current document, but we suggest
<a href="http://libvirt.org/storage.html">reading about libvirt storage
management here</a>.
</li>
</ol>
<p>
With these decisions made, you can now go ahead and create the
storage for the guest. The examples below all assume that the
disk will be 8192 MB in size (as the hard upper limit). Adjust
the number as required.
</p>
<p>
To create a fully-allocated (non-sparse) raw file:
</p>
<pre>
dd if=/dev/zero of=/var/lib/libvirt/images/guest.img bs=1M count=8192
</pre>
<p>
or for newer versions of Linux, use the faster <code>fallocate(1)</code>
program:
</p>
<pre>
fallocate -l 8192M /var/lib/libvirt/images/guest.img
</pre>
<p>
To create a sparse raw file:
</p>
<pre>
rm -f /var/lib/libvirt/images/guest.img
truncate --size=8192M /var/lib/libvirt/images/guest.img
</pre>
<p>
To create a qcow2 file:
</p>
<pre>
qemu-img create -f qcow2 /var/lib/libvirt/images/guest.qcow2 8192
</pre>
<p>
To create an LVM2 logical volume in the volume group
called <code>vg_host</code>:
</p>
<pre>
lvcreate -n lv_guest -L 8192M /dev/vg_host
</pre>
<p>
SAN LUN creation depends on your SAN, and you should consult that
documentation.
</p>
<p>
To create a libvirt volume in the <code>default</code>
storage pool, do:
</p>
<pre>
virsh vol-create-as default guest 8192M
</pre>
<p>
The libvirt <code>default</code> storage pool is a directory
<code>/var/lib/libvirt/images</code>, and you'll find the disk image
under there. <code>virsh vol-create-as</code> has several other
options, and you might want to consult the <code>virsh(1)</code> man
page.
</p>
[% END %]
[% WRAPPER h2 h2="Create the virtual machine" anchor="create-vm" %]
<p>
Now you can create the virtual machine itself from the ISO
which you downloaded and the disk image that you created.
</p>
<p>
This is the basic virt-install command:
</p>
<pre>
virt-install -r 1024 --accelerate -n Fedora14 \
-f /path/to/guest.img \
--cdrom Fedora-14-x86_64-Live.iso
</pre>
<p>
The <code>-r</code> option specifies the amount of RAM (in megabytes).
This depends on the operating system, but 768 MB is a good
starting point these days, and I use 1024 MB for modern
graphical Linux and Windows guests.
</p>
<p>
<code>--accelerate</code> indicates you want to use hardware
acceleration. Recent versions of virt-install default to this.
</p>
<p>
<code>-n</code> specifies the name of the virtual machine
(as known to libvirt), and this is the name you will see
in listings and use when <a href="../start-stop-vm-with-command-line/">starting
and stopping the VM</a>.
</p>
<p>
<code>-f</code> is the full path to the disk image you created before.
For LVs, use the device path,
eg. <code>-f /dev/vg_host/lv_guest</code>
</p>
<p>
<code>--cdrom</code> is the path to the ISO file that you downloaded.
The ISO is only needed during installation, and can be deleted
after that.
</p>
<p>
Other virt-install options that might be useful (read
<code>virt-install(1)</code> for the full list) include:
</p>
<ul>
<li> <code>--vcpus=N</code>
Specify an SMP guest with N virtual CPUs.
</li>
<li> <code>--description</code>
Give a description string which appears in the libvirt XML.
</li>
<li> <code>-l</code>
Use this to install from a network URL (instead of needing to
download an ISO). See <a href="#network">network installs below</a>.
</li>
<li> <code>--disk</code>
This option lets you specify other aspects of the disk such as
the format (qcow2 instead of raw). See the man page for the
full details.
</li>
<li> <code>--soundhw ac97</code>
Give the guest a (virtual) AC'97 soundcard. Without this option
no soundcard is provided for the guest.
</li>
</ul>
[% END %]
[% WRAPPER h2 h2="Advanced topic: Installing over the network" anchor="network" %]
<p>
Instead of downloading the ISO, you can install from public
repositories over HTTP. To do this, remove the <code>--cdrom</code>
option and instead specify the
<code>-l URL</code> option. Some common locations that virt-install
knows how to handle:
</p>
<ul>
<li> <b>Fedora:</b>
<code>-l http://download.fedoraproject.org/pub/fedora/linux/releases/13/Fedora/i386/os/</code>
Change <q>13</q> to the Fedora release, and <q>i386</q>
to <code>x86_64</code> for a 64 bit guest.
</li>
<li> <b>Debian:</b>
<code>-l http://ftp.us.debian.org/debian/dists/stable/main/installer-i386/</code>
Change <q>us</q> to your country code (for faster access to a local
mirror), and <q>i386</q> to <code>amd64</code> for a 64 bit guest.
</li>
<li> <b>Ubuntu:</b>
<code>-l http://ftp.ubuntu.com/ubuntu/dists/maverick/main/installer-i386/</code>
Change <q>maverick</q> to the name of the version of Ubuntu to
install, and <q>i386</q> to <code>amd64</code> for a 64 bit guest.
</li>
</ul>
[% END %]
[% END -%]
|