Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Original Question: How to resize root partition without live CD?

I have a virtual machine that I need to make bigger. I made the VM img larger and now I need to resize the partitions so I can use the extra space.

This is my current partition table and the goal is to combine /dev/vda1 and /dev/vda2.
vda1 is my current root partition
vda2 is an extended partition that contains vda5(swap)

Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    18874367     9436160   83  Linux
/dev/vda2        18876414    20969471     1046529    5  Extended
/dev/vda5        18876416    20969471     1046528   82  Linux swap / Solaris

This would be simple with Gparted and a live CD, but I can only ssh to the server, or open it in virt-manager but it is still command line only. How can I possibly resize the root partition?

The VM is Ubuntu server 12.04.

share|improve this question
    
So you can delete/remove vda2 and vda5? –  Salem Aug 21 '13 at 14:00

4 Answers 4

You need to connect your VM with the iso of a live CD - the file you can download. I assume you are not using a GUI-Tool (like virt-manager Install virt-manager), so you will have to do it from the command line.

You can use virsh to get an XML file of the VM config (I called my VM qemu1)

virsh dumpxml qemu1 > qemu1-config.xml

Now edit that file, look for something like

<disk type='file' device='cdrom'>
  <driver name='qemu' type='raw'/>
  <target dev='hdc' bus='ide' tray='open'/>
  <readonly/>
  <alias name='ide0-1-0'/>
  <address type='drive' controller='0' bus='1' target='0' unit='0'/>
</disk>

and replace it with

<disk type='file' device='cdrom'>
  <driver name='qemu' type='raw'/>
  <source file='/media/data/isos/gparted-live-0.13.0-1.iso'/>
  <target dev='hdc' bus='ide'/>
  <readonly/>
  <address type='drive' controller='0' bus='1' target='0' unit='0'/>
</disk>

The important points are

  • adapt the paths and file names
  • the line <source file=.../> contains the iso-file
  • the line <target .../> should not contain tray open now

And now you put the new settings into effect with

virsh define qemu1-config.xml

The VM will need a restart.

share|improve this answer
up vote 1 down vote accepted

Here is what worked in the end:

Enlage the img(virtual disk image) file for the VM:

1.sudo qemu-img create -f raw addon.raw 10G Create raw disc file

2.sudo mv domain.img domain.old.img backup current img

3.sudo cat domain.old.img addon.raw >> domain.img combine original and extra to create img file of new larger size.

Now we have a larger image but the VM still has old partition table. Now the tricky part

The next steps are done on the host machine with VM shutdown

Download iso file for gparted:

wget -c http://sourceforge.net/projects/gparted/files/gparted-live-stable/0.6.4-1/gparted-live-0.6.4-1.iso

Boot the iso with the virtual image as the harddrive in the virtual system

The command: kvm -m 512 -hda domain.img -cdrom gparted-live-0.6.4-1.iso -boot d

We are basically launching a virtual system kvm that will boot from the cdrom -boot d and gparted is in the cdrom -cdrom gparted-live-0.6.4-1.iso and in this virtual system treat domain.img as the harddrive -hda domain.img and allocate 512mb memory for this virtual system-m 512.

Now gparted pops up with a nice GUI (there is probably a command line option as well) ready to repartition your virtual disk.

Now you can manipulate the partition table just like you were using a liveCD on a real machine.

Most of this information was taken from this blog by Chris Walden.

Thanks @guntbert for pointing me in the right direction (i.e. it is possible to boot a VM from a liveCD).

share|improve this answer

You can use gParted and the resize from command line. Type...

sudo -i
parted

It will show

GNU Parted 1.7.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)

list will show a lot of commands ...

(parted) list
  check NUMBER                             do a simple check on the file system
  cp [FROM-DEVICE] FROM-NUMBER TO-NUMBER   copy file system to another partition
  help [COMMAND]                           prints general help, or help on COMMAND
  mklabel LABEL-TYPE                       create a new disklabel (partition table)
  mkfs NUMBER FS-TYPE                      make a FS-TYPE file system on partititon NUMBER
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  mkpartfs PART-TYPE FS-TYPE START END     make a partition with a file system
  move NUMBER START END                    move partition NUMBER
  name NUMBER NAME                         name partition NUMBER as NAME
  print [free|NUMBER|all]                  display the partition table, a partition, or all devices
  quit                                     exit program
  rescue START END                         rescue a lost partition near START and END
  resize NUMBER START END                  resize partition NUMBER and its file system
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  displays the current version of GNU Parted and copyright information

  • print will show the disc lay-out and add a number to a disc;
  • resize {number} will let you resize a disc.
  • This will only work on umounted partitions ;)

I never used it this way myself so be careful when issuing commands.

share|improve this answer
    
"This will only work on unmounted partitions". That is my hangup. I can't unmount the root partition. –  dan08 Aug 21 '13 at 14:24
    
So all you can do is create a directory on the 2nd partition and symlink to it ;) –  Rinzwind Aug 21 '13 at 14:58

You can use parted command line tool for this.

First You need to delete extended partition.

list partition using parted

parted /dev/vda

Note down your extended partition no. with starting and ending blocks and delete it .

(parted) rm 2  ( Where is 2 your extended partition no.)  

Check it without using

 (parted) print

You will get your extended partition removed. Now make primary partition in remaining space.

(parted) mkpart primary <starting block no.>   <ending block no.>

as starting and ending blocks no. Previously noted. check it with again

 (parted) print

reboot your system to take affects.

Resize your root partition

 resize2fs /dev/vda

You have to just check disk space using.

df -lh

You will get your root partition extended.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.