How to resize virtual machine disk? Is there anyway to resize a virtual machine’s disk? Say increasing the disk size from 32GB to 64GB. I am running KVM/Qemu on Ubuntu server 11.10 64bit. Thanks.
· archived 5/18/2026, 12:39:59 AM screenshot cached html click to expand virtualization - How to resize virtual machine disk? - Ask Ubuntu body,.s-topbar{margin-top:1.9em} UbuntuCommunityAsk!DeveloperDesignHardwareInsightsJujuShopMore ›AppsHelpForumLaunchpadMAASCanonical Stack Exchange Network Stack Exchange network consists of 181 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. Visit Stack Exchange Loading… Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about our products current community Ask Ubuntu help chat Ask Ubuntu Meta your communities Sign up or log in to customize your list. more stack exchange communities company blog Log in Sign up Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up. Sign up to join this community Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top Home Public Questions Tags Users Companies Unanswered Teams Stack Overflow for Teams – Start collaborating and sharing organizational knowledge. Create a free Team Why Teams? Teams Create free Team Teams Q&A for work Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams How to resize virtual machine disk? Ask Question Asked 10 years, 10 months ago Modified 6 years, 4 months ago Viewed 79k times This question shows research effort; it is useful and clear 25 This question does not show any research effort; it is unclear or not useful Save this question. Show activity on this post. Is there anyway to resize a virtual machine's disk? Say increasing the disk size from 32GB to 64GB. I am running KVM/Qemu on Ubuntu server 11.10 64bit. Thanks. virtualizationkvmqemu ShareShare a link to this question Copy linkCC BY-SA 3.0 Improve this question Follow Follow this question to receive notifications edited Feb 24, 2012 at 13:58 Jorge Castro 69.8k123123 gold badges461461 silver badges652652 bronze badges asked Feb 24, 2012 at 8:26 Tong WangTong Wang 43322 gold badges55 silver badges99 bronze badges Add a comment | 5 Answers 5 Sorted by: Reset to default Highest score (default) Date modified (newest first) Date created (oldest first) This answer is useful 29 This answer is not useful Save this answer. Show activity on this post. On Debian based distro you should use virt-resize instead. This handle pretty much everything under the hood now. Let's assume your image is called Win7 (why not?). First thing make sure your VM is shut down: Install the tool: # apt-get install libguestfs-tools Get the location of your VM disk: # virsh dumpxml Win7 | xpath -e /domain/devices/disk/source Found 2 nodes in stdin: -- NODE -- <source file="/var/lib/libvirt/images/Win7.img" /> -- NODE -- <source file="/var/lib/libvirt/images/Win7.iso" /> You may need to adapt /var/lib/libvirt/images/Win7.img in the following: # virt-filesystems --long --parts --blkdevs -h -a /var/lib/libvirt/images/Win7.img Name Type MBR Size Parent /dev/sda1 partition 07 100M /dev/sda /dev/sda2 partition 07 32G /dev/sda /dev/sda device - 32G - Create your 64G disk: # truncate -s 64G /var/lib/libvirt/images/outdisk You'll need to expand /dev/sda2 (not the boot partition): # virt-resize --expand /dev/sda2 /var/lib/libvirt/images/Win7.img /var/lib/libvirt/images/outdisk Examining /var/lib/libvirt/images/Win7.img ... 100% [progress bar] --:-- ********** Summary of changes: /dev/sda1: This partition will be left alone. /dev/sda2: This partition will be resized from 32G to 64G. The filesystem ntfs on /dev/sda2 will be expanded using the 'ntfsresize' method. ********** Setting up initial partition table on outdisk ... Copying /dev/sda1 ... Copying /dev/sda2 ... 100% [progress bar] 00:00 100% [progress bar] 00:00 Expanding /dev/sda2 using the 'ntfsresize' method ... Resize operation completed with no errors. Before deleting the old disk, carefully check that the resized disk boots and works correctly. Make a backup just in case (or use mv if you do not want the backup): # cp /var/lib/libvirt/images/Win7.img /var/lib/libvirt/images/Win7.img.old # mv /var/lib/libvirt/images/outdisk /var/lib/libvirt/images/Win7.img Now boot ! For more info: man virt-resize ShareShare a link to this answer Copy linkCC BY-SA 3.0 Improve this answer Follow Follow this answer to receive notifications edited Aug 21, 2016 at 13:15 Lesmana 17.8k88 gold badges5151 silver badges4949 bronze badges answered Jun 11, 2014 at 15:36 malatmalat 44444 silver badges1313 bronze badges 6 3 Instead of making a backup in the last step (if the image is large and you don't want to/don't have the additional disk space) you could just do virsh edit virt_name and change the path to the disk's source to the new one. Then if it doesn't work, just change it back and try again. There's not really much point in having 3 images when you could do it with 2. – Mike Apr 20, 2015 at 23:26 1 Or you could simply use mv instead of cp for the first command. Besides saving disk space, it will also be dramatically faster this way. – Kevin Keane May 18, 2015 at 7:09 6 one important thing to notice is that even if the origin image has a type, the destination instance will be raw type. if you want to maintain for instance qcow2 type, you should apply a conversion like: qemu-img convert -O qcow2 /var/lib/libvirt/images/outdisk /var/lib/libvirt/images/outdisk.qcow2 – logoff May 21, 2015 at 12:38 1 And virt-resize even has a cute ncurses progress spinner. – David McNeill Apr 4, 2016 at 0:36 Note that to a Ubuntu bug, you need sudo for libguestfs-tools commands: askubuntu.com/questions/1046828/… – Ciro Santilli OurBigBook.com Apr 22, 2020 at 20:12 | Show 1 more comment This answer is useful 20 This answer is not useful Save this answer. Show activity on this post. I recommend before doing any of this you take a complete copy of the disk image as it is, then when it all breaks you can copy it back to start over. There's 3 things you need to do: 1) Make the disk image bigger. In your host: qemu-img resize foo.qcow2 +32G Now your guest can see a bigger disk, but still has old partitions and filesystems. 2) Make the partition inside the disk image bigger. You need to boot off a LiveCD in your guest for this, since you won't be able to mess with a mounted partition. This is quite involved and probably the most dangerous part. It's quite a lot to copy here, so I'll just link instead for now. You want to do something like this: http://www.howtoforge.com/linux_resizing_ext3_partitions_p2 OR 2b) creating a new partition would be simpler (and safer) if you just want more storage space. Use fdisk or cfdisk, or whatever you feel comfortable with - you should see a whole bunch of unallocated space on your guest disk now. 3) Finally, if you resized your existing partition, make the filesystem inside the new bigger partition bigger (this is actually in the guide linked above anyway). Inside your guest: resize2fs /dev/sda1 ShareShare a link to this answer Copy linkCC BY-SA 3.0 Improve this answer Follow Follow this answer to receive notifications edited Aug 21, 2016 at 13:16 Lesmana 17.8k88 gold badges5151 silver badges4949 bronze badges answered Feb 24, 2012 at 12:41 CaesiumCaesium 15.2k44 gold badges4040 silver badges4848 bronze badges 4 Thanks for the quick answer. It looks like a viable solution. I'll be out of town for a few days, but once I am back to office next week, I'll give it a try and report back if it works. – Tong Wang Feb 24, 2012 at 13:58 1 One more question: if I want to shrink the disk, do I perform the steps in a reversed order? Like: 1. shrink the file system; 2. shrink the partition; 3. shrink the disk image. Right? – Tong Wang Feb 24, 2012 at 14:40 Yes, resize2fs can shrink as well as grow, but it'll obviously need enough free space in the filesystem. When shrinking the partition I'd err on leaving a bit more than necessary so you don't accidentally chop the end off the filesystem :) – Caesium Feb 24, 2012 at 15:03 You absolutely CAN mess with a mounted partition – etherous May 20, 2017 at 0:24 Add a comment | This answer is useful 4 This answer is not useful Save this answer. Show activity on this post. I think Caesium's answer is fine, I'd just like to write down some other commands to achieve the same thing. Assume you have a file disk.img with a disk image, i.e. it has a partition table and one or more partitions, and say you want to make the last partition larger. What you have to do is to 1) make the whole file bigger, say 4GiB. A quick way to do it is to use dd dd if=/dev/zero of=disk.img bs=1c seek=4G count=0 2) make the partition bigger using fdisk (I wish I could do this in parted or some nicer tool... Anyone?) fdisk disk.img Type p to print out the partiton table and look for the start sector of your partition, for instance partition 2 starts at sector 106496. What you will do is to delete that partition from the partition table and create a new partition that starts at the exact same sector but ends at a later one. Then the partition will contain a valid file system. Type d and give the partition number to delete. (Gulp!) Type n and give the desired partition number, and then the start sector. You MUST use the same start sector as was used before. Finally give the end sector or just let fdisk pick the highest sector you can use. Type w to write the changes back to the disk image file, and exit fdisk. 3) Now you need to resize the filesystem. For this you need the offset to (= the position of the) file system inside the disk image. You can compute this from the sector number if you know the sector size (which is usually 512), or you can use parted parted disk.img u b p (parted can take commands as command line arguments, so this means 'unit bytes' and 'print', so it prints the partition table and uses bytes as size uint.) This prints out the partition starts and ends. Say that your partition starts at 54525952, then you make a loopback block devices with losetup. losetup -f --show -o 54525952 disk.img losetup tells you which dev it picked, for instance /dev/loop0. Now you can use resize2fs: resize2fs /dev/loop0 and finally remove the loop device losetup -d /dev/loop0 ShareShare a link to this answer Copy linkCC BY-SA 3.0 Improve this answer Follow Follow this answer to receive notifications answered Apr 18, 2013 at 12:23 larsrlarsr 9133 bronze badges Add a comment | This answer is useful 2 This answer is not useful Save this answer. Show activity on this post. Best solution found is here: http://www.linux-kvm.com/content/how-resize-your-kvm-virtual-disk I think what @Caesium said is given from the above official link I hope this will help. 3 Steps: 1.qemu-img resize windows.qcow2 +5GB 2.GParted live cd to resize 3.reboot and use os tools to resize ShareShare a link to this answer Copy linkCC BY-SA 3.0 Improve this answer Follow Follow this answer to receive notifications edited Dec 27, 2013 at 15:40 answered Dec 17, 2013 at 13:21 kamilkamil 7,20566 gold badges3939 silver badges6060 bronze badges 1 Thank you for the simple answer. Worked for me on Ubuntu 16.04 – phil Jun 14, 2016 at 17:47 Add a comment | This answer is useful -1 This answer is not useful Save this answer. Show activity on this post. #include <stdio.h> static unsigned long auxfilesize(FILE* fp) { unsigned long len=0; int c=0; while ( (c = fgetc(fp)) != -1 ) { len++; } return len; } static unsigned long aux_copyNBytesFromTo(FILE* from, FILE* to, unsigned long fromSize, unsigned long bytes) { unsigned long iter = 0; while ( iter++ < fromSize ) { int c = fgetc(from); fputc(c, to); } iter-=1; if ( fromSize < bytes ) { while ( iter++ < bytes ) { fputc(0, to); } } return iter; } int main(int argc, char **argv) { FILE *from = fopen(argv[1], "rb"); FILE *to = fopen(argv[2], "wb" ); unsigned long l = auxfilesize(from); rewind(from); aux_copyNBytesFromTo(from, to, l, (l + l/2)) ; fclose(from); fclose(to); } This simple program adds N bytes to the end of the virtual image. I used Paragon partition image for Windows XP to merge the newly created images. Works OK here. ShareShare a link to this answer Copy linkCC BY-SA 3.0 Improve this answer Follow Follow this answer to receive notifications edited Jul 8, 2015 at 16:12 A.B. 87.4k2121 gold badges239239 silver badges316316 bronze badges answered Jul 8, 2015 at 15:59 Ilian ZapryanovIlian Zapryanov 10122 bronze badges 4 Unused static function 'auxfilesize', 'aux_copyNBytesFromTo'. Why should that help? – A.B. Jul 8, 2015 at 16:15 Unused? I don`t understand the question. They are both used. – Ilian Zapryanov Jul 9, 2015 at 9:26 The result is an unallocated bytes and a new hard drive to the OS. You have to format it. If you are asking why the foo-s are static. It just saved me the prototypes and why not. It`s a simple program written in 2 mins in my spare time :) Nothing special or optimal. – Ilian Zapryanov Jul 9, 2015 at 9:37 4 You could have used existing tools like dd or truncate ... – rudimeier Aug 31, 2016 at 13:07 Add a comment | Your Answer Thanks for contributing an answer to Ask Ubuntu!Please be sure to answer the question. Provide details and share your research!But avoid …Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.To learn more, see our tips on writing great answers. Draft saved Draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Submit Post as a guest Name Email Required, but never shown Post as a guest Name Email Required, but never shown Post Your Answer Discard By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy Not the answer you're looking for? Browse other questions tagged virtualizationkvmqemu or ask your own question. The Overflow Blog Getting your data in shape for machine learning The future of software engineering is powered by AIOps and open source sponsored post Featured on Meta Navigation and UI research starting soon Announcement: AI generated content temporarily banned on Ask Ubuntu Linked 6 How to run libguestfs-tools tools such as virt-make-fs without sudo? 0 How to increase size of root partition at /dev/sda1 from /dev/sdb1? Related 12 Is my VM using KVM or QEMU? 2 How do I remotely shut down a virtual machine (over SSH)? 2 How to install KVM in Ubuntu virtual machine? 0 Configuring qemu/kvm on Ubuntu server machine to make local network 1 Is it possible to run qemu-system-x86_64 virtual machine from raw disk image located on external flash drive? 0 Why can't I connect to Internet from machine root, but can from virtual machine 5 How to create dynamic disks .qcow2 as it happens with VDI? 2 Resize partition from virtual machine without reboot 0 How to recover host disk space? Lost with KVM running out of memory while adding and removing virtual disks wtih Virtual Machine Manager Hot Network Questions Using 1206 capacitor on 0805 footprint for Ethernet testing How do you motivate people to post flyers around town? What is this pin? Help! Sanity in question... re: "No Blade of Grass" Can an n-dimentional being hurt and n+x dimentional being What are the main changes in the latest (January 2023) version of the FIDE Laws of Chess? Is a teacher necessary? Is Dantzig-Wolfe decomposition an example of a divide and conquer algorithm? Is mathematical creativity the same as artistic creativity? How to ignore an argument? What does the usable range depend on in this current sink circuit? American English idiom meaning "painful to resist the gods" Computational complexity theoretic incompleteness: is that a thing? Is there a clear boundary between states of matter? How much are "Nap & Gos" in Iceland? How to network with senior managers within the company? Students confusing "object types" in introductory proofs class What are the exact voting rules for electing the Speaker of the House of Representatives? Does modified server code, used in public website development, which is originally available under GPL2 have to be released to the public? Bathroom Fan Replacement With Blown Insulation Above Was cleavage first used for Jane Russell? Is it illegal to download passwords in bulk from the dark web to make a password checking tool to help people? Pressure difference in bottles connected by pipe Compass/magnetic directions in Middle-earth Question feed Subscribe to RSS Question feed To subscribe to this RSS feed, copy and paste this URL into your RSS reader. Ask Ubuntu Tour Help Chat Contact Feedback Company Stack Overflow Teams Advertising Collectives Talent About Press Legal Privacy Policy Terms of Service Cookie Settings Cookie Policy Stack Exchange Network Technology Culture & recreation Life & arts Science Professional Business API Data Blog Facebook Twitter LinkedIn Instagram Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. rev 2023.1.4.43135 Ubuntu and Canonical are registered trademarks of Canonical Ltd. Your privacy By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy. Accept all cookies Customize settings