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