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