Resize linux partitions (physical and virtual using VMware)

Resize linux partitions (physical and virtual using VMware)

Resize linux partitions (physical and virtual using VMware) 150 150 Roderick Derks

The reason why I wanted to change the sizes of the partitions of one of my linux systems was the lack of space on the root partition. I'm running this linux system as a Virtual Machine (VM) using 256MB of RAM.

I could have choosen to enlarge the vmdk file (the virtual disk), but I didn't. I'll tell you why. In a VM you can have a swap partition of half the size of the amount of memory (VMware recommendation). As I sticked to the 'linux-out-of-the-box-installation' 512MB was used for the swap partition. So I decided to shrink the swap and use it to extend the root partition.

System used: Fedora Core 6 (but the procedure does not only apply to this linux flavour) and VMware server, though the latter is not quite relevant in this situation.

Resize the swap partition 

Check current swap space:
# free
             total       used       free     shared    buffers     cached
Mem:        376544     340732      35812          0      47608     186532
-/+ buffers/cache:     106592     269952
Swap:       528904          0     528904

# swapoff /dev/VolGroup00/LogVol01
# lvreduce -L -256M /dev/VolGroup00/LogVol01
# mkswap /dev/VolGroup00/LogVol01
# swapon /dev/VolGroup00/LogVol01
# free
             total       used       free     shared    buffers     cached
Mem:        376544     340732      35812          0      47608     186532
-/+ buffers/cache:     106592     269952
Swap:       294904          0     294904

Extend the Logical Volume containing the root partition

# lvdisplay
  — Logical volume —
  LV Name                /dev/VolGroup00/LogVol00
  VG Name                VolGroup00
  LV UUID                hriQEB-DgnX-Rj2U-wVtJ-LpZr-5Poi-BMu12M
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                1.31 GB
  Current LE             42
  Segments               1
  Allocation             inherit
  Read ahead sectors     0
  Block device           253:0
   
  — Logical volume —
  LV Name                /dev/VolGroup00/LogVol01
  VG Name                VolGroup00
  LV UUID                a8v3Ss-MiGo-7TaU-Hv0F-qEwq-R0qx-0FMzze
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                288.00 MB
  Current LE             9
  Segments               1
  Allocation             inherit
  Read ahead sectors     0
  Block device           253:1

The output of the next command shows us that there are 9 free Physical Extends on the physical volume. These will be added to the LV that contains the / partition:

# pvdisplay
  — Physical volume —
  PV Name               /dev/sda2
  VG Name               VolGroup00
  PV Size               1.88 GB / not usable 0   
  Allocatable           yes
  PE Size (KByte)       32768
  Total PE              60
  Free PE               9
  Allocated PE          51
  PV UUID               waZQIQ-MR05-1wjy-wYQ9-PYGF-q9EB-BJjdvb

Extending: 

# lvextend -l +9 /dev/VolGroup00/LogVol00
  /dev/hdc: open failed: No medium found
  Attempt to close device '/dev/hdc' which is not open.
  Extending logical volume LogVol00 to 1.59 GB
  Logical volume LogVol00 successfully resized

Current Logical Extends (LE) is now 51 (before it was 42) 

# lvdisplay
  /dev/hdc: open failed: No medium found
  Attempt to close device '/dev/hdc' which is not open.
  — Logical volume —
  LV Name                /dev/VolGroup00/LogVol00
  VG Name                VolGroup00
  LV UUID                hriQEB-DgnX-Rj2U-wVtJ-LpZr-5Poi-BMu12M
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                1.59 GB
  Current LE             51
  Segments               2
  Allocation             inherit
  Read ahead sectors     0
  Block device           253:0

No free PE's anymore: 

# pvdisplay
  — Physical volume —
  PV Name               /dev/sda2
  VG Name               VolGroup00
  PV Size               1.88 GB / not usable 0  
  Allocatable           yes (but full)
  PE Size (KByte)       32768
  Total PE              60
  Free PE               0
  Allocated PE          60
  PV UUID               waZQIQ-MR05-1wjy-wYQ9-PYGF-q9EB-BJjdvb

Extend the partiontable

Now we still have to extend the partiontable for the / partition:

Today it is possible to extend root filesystem online in Fedora! This is really cool.
Before you had to boot the server using the rescue cd to the "linux rescue" mode.

# resize2fs /dev/VolGroup00/LogVol00
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/VolGroup00/LogVol00 is mounted on /; on-line resizing required
Performing an on-line resize of /dev/VolGroup00/LogVol00 to 417792 (4k) blocks.
The filesystem on /dev/VolGroup00/LogVol00 is now 417792 blocks long.

Extra: Summary of how to resize a vmdk and extend the logical volume

On the host:

VMware ESX 3.0.0 and higher:
# vmkfstools -X 20G "/path/to/vmdkfile.vmdk"
(10Gb is the final size you want to have)
VMware server:
# vmware-vdiskmanager.exe -x 20Gb vmdkfile.vmdk

In the guest OS:
# df -h /
# fdisk /dev/sda
(add a new partition and change the type to lvm (8e) using the t option in fdisk)
# shutdown -r 0
# vgdisplay VolGroup00
# pvcreate /dev/sda3

# shutdown -r 0

# vgextend VolGroup00 /dev/sda3
# vgdisplay VolGroup00
# lvextend -l+89 /dev/VolGroup00/LogVol00
# vgdisplay VolGroup00
# resize2fs /dev/VolGroup00/LogVol00
# df -h /

Thank you and goodbye!

( The best thing would be to recreate the partitions exactly the same on
the new disks first to make sure everything is still working fine.  Then
increase the size of the partitions through fdisk and expand the
filesystem with resize2fs.  )

Roderick Derks

Liefhebber van fietsen, van het oplossen van IT puzzels, en van het delen van informatie om anderen te helpen.

All stories by:Roderick Derks

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Your Name (required)

Your Email (required)

Subject

Your Message

Your Name (required)

Your Email (required)

Subject

Your Message