docs: resize: Not just limited to resizing filesystems in partitions.
[libguestfs.git] / resize / virt-resize.pod
1 =encoding utf8
2
3 =head1 NAME
4
5 virt-resize - Resize a virtual machine disk
6
7 =head1 SYNOPSIS
8
9  virt-resize [--resize /dev/sdaN=[+/-]<size>[%]]
10    [--expand /dev/sdaN] [--shrink /dev/sdaN]
11    [--ignore /dev/sdaN] [--delete /dev/sdaN] [...] indisk outdisk
12
13 =head1 DESCRIPTION
14
15 Virt-resize is a tool which can resize a virtual machine disk, making
16 it larger or smaller overall, and resizing or deleting any partitions
17 contained within.
18
19 Virt-resize B<cannot> resize disk images in-place.  Virt-resize
20 B<should not> be used on live virtual machines - for consistent
21 results, shut the virtual machine down before resizing it.
22
23 If you are not familiar with the associated tools:
24 L<virt-filesystems(1)> and L<virt-df(1)>, we recommend you go and read
25 those manual pages first.
26
27 =head1 EXAMPLES
28
29 Copy C<olddisk> to C<newdisk>, extending one of the guest's partitions
30 to fill the extra 5GB of space.
31
32  truncate -r olddisk newdisk; truncate -s +5G newdisk
33  virt-filesystems --long -h --all -a olddisk
34  # Note "/dev/sda2" is a partition inside the "olddisk" file.
35  virt-resize --expand /dev/sda2 olddisk newdisk
36
37 As above, but make the /boot partition 200MB bigger, while giving the
38 remaining space to /dev/sda2:
39
40  virt-resize --resize /dev/sda1=+200M --expand /dev/sda2 olddisk newdisk
41
42 As above, but the output format will be uncompressed qcow2:
43
44  qemu-img create -f qcow2 newdisk.qcow2 15G
45  virt-resize --expand /dev/sda2 olddisk newdisk.qcow2
46
47 =head1 DETAILED USAGE
48
49 =head2 EXPANDING A VIRTUAL MACHINE DISK
50
51 =over 4
52
53 =item 1. Shut down the virtual machine
54
55 =item 2. Locate input disk image
56
57 Locate the input disk image (ie. the file or device on the host
58 containing the guest's disk).  If the guest is managed by libvirt, you
59 can use C<virsh dumpxml> like this to find the disk image name:
60
61  # virsh dumpxml guestname | xpath /domain/devices/disk/source
62  Found 1 nodes:
63  -- NODE --
64  <source dev="/dev/vg/lv_guest" />
65
66 =item 3. Look at current sizing
67
68 Use L<virt-filesystems(1)> to display the current partitions and
69 sizes:
70
71  # virt-filesystems --long --parts --blkdevs -h -a /dev/vg/lv_guest
72  Name       Type       Size  Parent
73  /dev/sda1  partition  101M  /dev/sda
74  /dev/sda2  partition  7.9G  /dev/sda
75  /dev/sda   device     8.0G  -
76
77 (This example is a virtual machine with an 8 GB disk which we would
78 like to expand up to 10 GB).
79
80 =item 4. Create output disk
81
82 Virt-resize cannot do in-place disk modifications.  You have to have
83 space to store the resized output disk.
84
85 To store the resized disk image in a file, create a file of a suitable
86 size:
87
88  # rm -f outdisk
89  # truncate -s 10G outdisk
90
91 Or use L<lvcreate(1)> to create a logical volume:
92
93  # lvcreate -L 10G -n lv_name vg_name
94
95 Or use L<virsh(1)> vol-create-as to create a libvirt storage volume:
96
97  # virsh pool-list
98  # virsh vol-create-as poolname newvol 10G
99
100 =item 5. Resize
101
102 virt-resize takes two mandatory parameters, the input disk (eg. device
103 or file) and the output disk.  The output disk is the one created in
104 the previous step.
105
106  # virt-resize indisk outdisk
107
108 This command just copies disk image C<indisk> to disk image C<outdisk>
109 I<without> resizing or changing any existing partitions.  If
110 C<outdisk> is larger, then an extra, empty partition is created at the
111 end of the disk covering the extra space.  If C<outdisk> is smaller,
112 then it will give an error.
113
114 More realistically you'd want to expand existing partitions in the
115 disk image by passing extra options (for the full list see the
116 L</OPTIONS> section below).
117
118 L</--expand> is the most useful option.  It expands the named
119 partition within the disk to fill any extra space:
120
121  # virt-resize --expand /dev/sda2 indisk outdisk
122
123 (In this case, an extra partition is I<not> created at the end of the
124 disk, because there will be no unused space).
125
126 L</--resize> is the other commonly used option.  The following would
127 increase the size of /dev/sda1 by 200M, and expand /dev/sda2
128 to fill the rest of the available space:
129
130  # virt-resize --resize /dev/sda1=+200M --expand /dev/sda2 \
131      indisk outdisk
132
133 If the expanded partition in the image contains a filesystem or LVM
134 PV, then if virt-resize knows how, it will resize the contents, the
135 equivalent of calling a command such as L<pvresize(8)>,
136 L<resize2fs(8)> or L<ntfsresize(8)>.  However virt-resize does not
137 know how to resize some filesystems, so you would have to online
138 resize them after booting the guest.
139
140 Other options are covered below.
141
142 =item 6. Test
143
144 Thoroughly test the new disk image I<before> discarding the old one.
145
146 If you are using libvirt, edit the XML to point at the new disk:
147
148  # virsh edit guestname
149
150 Change E<lt>source ...E<gt>, see
151 L<http://libvirt.org/formatdomain.html#elementsDisks>
152
153 Then start up the domain with the new, resized disk:
154
155  # virsh start guestname
156
157 and check that it still works.  See also the L</NOTES> section below
158 for additional information.
159
160 =item 7. Resize LVs etc inside the guest
161
162 (This can also be done offline using L<guestfish(1)>)
163
164 Once the guest has booted you should see the new space available, at
165 least for filesystems that virt-resize knows how to resize, and for
166 PVs.  The user may need to resize LVs inside PVs, and also resize
167 filesystem types that virt-resize does not know how to expand.
168
169 =back
170
171 =head2 SHRINKING A VIRTUAL MACHINE DISK
172
173 Shrinking is somewhat more complex than expanding, and only an
174 overview is given here.
175
176 Firstly virt-resize will not attempt to shrink any partition content
177 (PVs, filesystems).  The user has to shrink content before passing the
178 disk image to virt-resize, and virt-resize will check that the content
179 has been shrunk properly.
180
181 (Shrinking can also be done offline using L<guestfish(1)>)
182
183 After shrinking PVs and filesystems, shut down the guest, and proceed
184 with steps 3 and 4 above to allocate a new disk image.
185
186 Then run virt-resize with any of the I<--shrink> and/or I<--resize>
187 options.
188
189 =head2 IGNORING OR DELETING PARTITIONS
190
191 virt-resize also gives a convenient way to ignore or delete partitions
192 when copying from the input disk to the output disk.  Ignoring a
193 partition speeds up the copy where you don't care about the existing
194 contents of a partition.  Deleting a partition removes it completely,
195 but note that it also renumbers any partitions after the one which is
196 deleted, which can leave some guests unbootable.
197
198 =head2 QCOW2 AND NON-SPARSE RAW FORMATS
199
200 If the input disk is in qcow2 format, then you may prefer that the
201 output is in qcow2 format as well.  Alternately, virt-resize can
202 convert the format on the fly.  The output format is simply determined
203 by the format of the empty output container that you provide.  Thus to
204 create qcow2 output, use:
205
206  qemu-img create [-c] -f qcow2 outdisk [size]
207
208 instead of the truncate command (use I<-c> for a compressed disk).
209
210 Similarly, to get non-sparse raw output use:
211
212  fallocate -l size outdisk
213
214 (on older systems that don't have the L<fallocate(1)> command use
215 C<dd if=/dev/zero of=outdisk bs=1M count=..>)
216
217 =head1 OPTIONS
218
219 =over 4
220
221 =item B<--help>
222
223 Display help.
224
225 =item B<-d>
226
227 =item B<--debug>
228
229 Enable debugging messages.
230
231 =item B<--delete part>
232
233 Delete the named partition.  It would be more accurate to describe
234 this as "don't copy it over", since virt-resize doesn't do in-place
235 changes and the original disk image is left intact.
236
237 Note that when you delete a partition, then anything contained in the
238 partition is also deleted.  Furthermore, this causes any partitions
239 that come after to be I<renumbered>, which can easily make your guest
240 unbootable.
241
242 You can give this option multiple times.
243
244 =item B<--expand part>
245
246 Expand the named partition so it uses up all extra space (space left
247 over after any other resize changes that you request have been done).
248
249 If virt-resize knows how, it will expand the direct content of the
250 partition.  For example, if the partition is an LVM PV, it will expand
251 the PV to fit (like calling L<pvresize(8)>).  Virt-resize leaves any
252 other content it doesn't know about alone.
253
254 Currently virt-resize can resize:
255
256 =over 4
257
258 =item *
259
260 ext2, ext3 and ext4 filesystems.
261
262 =item *
263
264 NTFS filesystems, if libguestfs was compiled with support for NTFS.
265
266 The filesystem must have been shut down consistently last time it was
267 used.  Additionally, L<ntfsresize(8)> marks the resized filesystem as
268 requiring a consistency check, so at the first boot after resizing
269 Windows will check the disk.
270
271 =item *
272
273 LVM PVs (physical volumes).  virt-resize does not usually resize
274 anything inside the PV, but see the I<--LV-expand> option.  The user
275 could also resize LVs as desired after boot.
276
277 =back
278
279 Note that you cannot use I<--expand> and I<--shrink> together.
280
281 =item B<--format> raw
282
283 Specify the format of the input disk image.  If this flag is not
284 given then it is auto-detected from the image itself.
285
286 If working with untrusted raw-format guest disk images, you should
287 ensure the format is always specified.
288
289 Note that this option I<does not> affect the output format.
290 See L</QCOW2 AND NON-SPARSE RAW FORMATS>.
291
292 =item B<--ignore part>
293
294 Ignore the named partition.  Effectively this means the partition is
295 allocated on the destination disk, but the content is not copied
296 across from the source disk.  The content of the partition will be
297 blank (all zero bytes).
298
299 You can give this option multiple times.
300
301 =item B<--LV-expand logvol>
302
303 This takes the logical volume and, as a final step, expands it to fill
304 all the space available in its volume group.  A typical usage,
305 assuming a Linux guest with a single PV C</dev/sda2> and a root device
306 called C</dev/vg_guest/lv_root> would be:
307
308  virt-resize indisk outdisk \
309    --expand /dev/sda2 --LV-expand /dev/vg_guest/lv_root
310
311 This would first expand the partition (and PV), and then expand the
312 root device to fill the extra space in the PV.
313
314 The contents of the LV are also resized if virt-resize knows how to do
315 that.  You can stop virt-resize from trying to expand the content by
316 using the option I<--no-expand-content>.
317
318 Use L<virt-filesystems(1)> to list the filesystems in the guest.
319
320 You can give this option multiple times, I<but> it doesn't
321 make sense to do this unless the logical volumes you specify
322 are all in different volume groups.
323
324 =item B<-n>
325
326 =item B<--dryrun>
327
328 Print a summary of what would be done, but don't do anything.
329
330 =item B<--no-copy-boot-loader>
331
332 By default, virt-resize copies over some sectors at the start of the
333 disk (up to the beginning of the first partition).  Commonly these
334 sectors contain the Master Boot Record (MBR) and the boot loader, and
335 are required in order for the guest to boot correctly.
336
337 If you specify this flag, then this initial copy is not done.  You may
338 need to reinstall the boot loader in this case.
339
340 =item B<--no-extra-partition>
341
342 By default, virt-resize creates an extra partition if there is any
343 extra, unused space after all resizing has happened.  Use this option
344 to prevent the extra partition from being created.  If you do this
345 then the extra space will be inaccessible until you run fdisk, parted,
346 or some other partitioning tool in the guest.
347
348 Note that if the surplus space is smaller than 10 MB, no extra
349 partition will be created.
350
351 =item B<--no-expand-content>
352
353 By default, virt-resize will try to expand the direct contents
354 of partitions, if it knows how (see I<--expand> option above).
355
356 If you give the I<--no-expand-content> option then virt-resize
357 will not attempt this.
358
359 =item B<--ntfsresize-force>
360
361 Pass the I<--force> option to L<ntfsresize(8)>, allowing resizing
362 even if the NTFS disk is marked as needing a consistency check.
363 You have to use this option if you want to resize a Windows
364 guest multiple times without booting into Windows between each
365 resize.
366
367 =item B<--output-format> raw
368
369 Specify the format of the output disk image.  If this flag is not
370 given then it is auto-detected from the image itself.
371
372 If working with untrusted raw-format guest disk images, you should
373 ensure the format is always specified.
374
375 Note that this option I<does not create> the output format.  This
376 option just tells libguestfs what it is so it doesn't try to guess it.
377 You still need to create the output disk with the right format.  See
378 L</QCOW2 AND NON-SPARSE RAW FORMATS>.
379
380 =item B<-q>
381
382 =item B<--quiet>
383
384 Don't print the summary.
385
386 =item B<--resize part=size>
387
388 Resize the named partition (expanding or shrinking it) so that it has
389 the given size.
390
391 C<size> can be expressed as an absolute number followed by
392 b/K/M/G to mean bytes, Kilobytes, Megabytes, or Gigabytes;
393 or as a percentage of the current size;
394 or as a relative number or percentage.
395 For example:
396
397  --resize /dev/sda2=10G
398
399  --resize /dev/sda4=90%
400
401  --resize /dev/sda2=+1G
402
403  --resize /dev/sda2=-200M
404
405  --resize /dev/sda1=+128K
406
407  --resize /dev/sda1=+10%
408
409  --resize /dev/sda1=-10%
410
411 You can increase the size of any partition.  Virt-resize will expand
412 the direct content of the partition if it knows how (see I<--expand>
413 below).
414
415 You can only I<decrease> the size of partitions that contain
416 filesystems or PVs which have already been shrunk.  Virt-resize will
417 check this has been done before proceeding, or else will print an
418 error (see also I<--resize-force>).
419
420 You can give this option multiple times.
421
422 =item B<--resize-force part=size>
423
424 This is the same as I<--resize> except that it will let you decrease
425 the size of any partition.  Generally this means you will lose any
426 data which was at the end of the partition you shrink, but you may not
427 care about that (eg. if shrinking an unused partition, or if you can
428 easily recreate it such as a swap partition).
429
430 See also the I<--ignore> option.
431
432 =item B<--shrink part>
433
434 Shrink the named partition until the overall disk image fits in the
435 destination.  The named partition B<must> contain a filesystem or PV
436 which has already been shrunk using another tool (eg. L<guestfish(1)>
437 or other online tools).  Virt-resize will check this and give an error
438 if it has not been done.
439
440 The amount by which the overall disk must be shrunk (after carrying
441 out all other operations requested by the user) is called the
442 "deficit".  For example, a straight copy (assume no other operations)
443 from a 5GB disk image to a 4GB disk image results in a 1GB deficit.
444 In this case, virt-resize would give an error unless the user
445 specified a partition to shrink and that partition had more than a
446 gigabyte of free space.
447
448 Note that you cannot use I<--expand> and I<--shrink> together.
449
450 =item B<-V>
451
452 =item B<--version>
453
454 Display version number and exit.
455
456 =back
457
458 =head1 NOTES
459
460 =head2 "Partition 1 does not end on cylinder boundary."
461
462 Virt-resize aligns partitions to multiples of 64 sectors.  Usually
463 this means the partitions will not be aligned to the ancient CHS
464 geometry.  However CHS geometry is meaningless for disks manufactured
465 since the early 1990s, and doubly so for virtual hard drives.
466 Alignment of partitions to cylinders is not required by any modern
467 operating system.
468
469 =head2 RESIZING WINDOWS VIRTUAL MACHINES
470
471 In Windows Vista and later versions, Microsoft switched to using a
472 separate boot partition.  In these VMs, typically C</dev/sda1> is the
473 boot partition and C</dev/sda2> is the main (C:) drive.  We have not
474 had any luck resizing the boot partition.  Doing so seems to break the
475 guest completely.  However expanding the second partition (ie. C:
476 drive) should work.
477
478 Windows may initiate a lengthy "chkdsk" on first boot after a resize,
479 if NTFS partitions have been expanded.  This is just a safety check
480 and (unless it find errors) is nothing to worry about.
481
482 =head2 GUEST BOOT STUCK AT "GRUB"
483
484 If a Linux guest does not boot after resizing, and the boot is stuck
485 after printing C<GRUB> on the console, try reinstalling grub.  This
486 sometimes happens on older (RHEL 5-era) guests, for reasons we don't
487 fully understand, although we think is to do with partition alignment.
488
489  guestfish -i -a newdisk
490  ><fs> cat /boot/grub/device.map
491  # check the contents of this file are sensible or
492  # edit the file if necessary
493  ><fs> grub-install / /dev/vda
494  ><fs> exit
495
496 For more flexible guest reconfiguration, including if you need to
497 specify other parameters to grub-install, use L<virt-rescue(1)>.
498
499 =head1 ALTERNATIVE TOOLS
500
501 There are several proprietary tools for resizing partitions.  We
502 won't mention any here.
503
504 L<parted(8)> and its graphical shell gparted can do some types of
505 resizing operations on disk images.  They can resize and move
506 partitions, but I don't think they can do anything with the contents,
507 and they certainly don't understand LVM.
508
509 L<guestfish(1)> can do everything that virt-resize can do and a lot
510 more, but at a much lower level.  You will probably end up
511 hand-calculating sector offsets, which is something that virt-resize
512 was designed to avoid.  If you want to see the guestfish-equivalent
513 commands that virt-resize runs, use the I<--debug> flag.
514
515 =head1 SHELL QUOTING
516
517 Libvirt guest names can contain arbitrary characters, some of which
518 have meaning to the shell such as C<#> and space.  You may need to
519 quote or escape these characters on the command line.  See the shell
520 manual page L<sh(1)> for details.
521
522 =head1 SEE ALSO
523
524 L<virt-filesystems(1)>,
525 L<virt-df(1)>,
526 L<guestfs(3)>,
527 L<guestfish(1)>,
528 L<lvm(8)>,
529 L<pvresize(8)>,
530 L<lvresize(8)>,
531 L<resize2fs(8)>,
532 L<ntfsresize(8)>,
533 L<virsh(1)>,
534 L<parted(8)>,
535 L<truncate(1)>,
536 L<fallocate(1)>,
537 L<grub(8)>,
538 L<grub-install(8)>,
539 L<virt-rescue(1)>,
540 L<http://libguestfs.org/>.
541
542 =head1 AUTHOR
543
544 Richard W.M. Jones L<http://people.redhat.com/~rjones/>
545
546 =head1 COPYRIGHT
547
548 Copyright (C) 2010-2011 Red Hat Inc.
549
550 This program is free software; you can redistribute it and/or modify
551 it under the terms of the GNU General Public License as published by
552 the Free Software Foundation; either version 2 of the License, or
553 (at your option) any later version.
554
555 This program is distributed in the hope that it will be useful,
556 but WITHOUT ANY WARRANTY; without even the implied warranty of
557 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
558 GNU General Public License for more details.
559
560 You should have received a copy of the GNU General Public License
561 along with this program; if not, write to the Free Software
562 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.