L'Ombre de l'Olivier

The Shadow of the Olive Tree

being the maunderings of an Englishman on the Côte d'Azur

30 April 2009 Blog Home : April 2009 : Permalink

Virtual Upon Virtual

The majority of readers of this blog will, I suspect, find this post to be tedious and boring. I'm putting it here mostly so that I have a reference for if I need to do it again and so that others can find it too.

I've just been installing an Ubuntu server as a VMware guest (said guest will, in the near future, be this very site amongst other things) and I've found a couple of neat tricks that make it easier to recover from mistakes.

The basic rule is virtualize whenever possible. So when I installed Ubuntu 9.04 server I chose to install with LVM. You might think this is a bit silly to have virtual volume support in VMware but it helps because it makes it simple to fix if you run out of space.

I started out with a 2GB hard disk for Ubuntu to use. The install process created a 250 MB boot partition, and allocated the remaining 1.75GB to LVM. Within that it created 144MB of swap and the remaining 1.6GB were assigned to the root filesystem. I ran out of space because when I did a few sums I forgot about the swap and the boot partitions and thus tried to upload about 1.8GB of stuff. Oops.

Fortunately it is very easy to add disks to VMware guests so once I'de cleaned out the mess made by the upload I stopped the machine and in the VMware server console added a new virtual disk drive - 4GB this time. The I started it up. Elapsed downtime about 5 minutes.

Now I logged into the server and checked to make sure linux knew about the new "hardware":

user@server:~$ sudo fdisk -l
[sudo] password for user: XXXX

Disk /dev/sda: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x000d9383

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         230     1847443+  8e  Linux LVM
/dev/sda2             231         261      249007+   5  Extended
/dev/sda5             231         261      248976   83  Linux

Disk /dev/sdb: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x00000000

Disk /dev/sdb doesn't contain a valid partition table

It did. So now create a basic partition table and single partition on it - don't worry about the partition type or anything at this point because LVM goes and splats it all anyway.

user@server:~$ sudo fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x34ff84ae.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4):
Value out of range.
Partition number (1-4): 1
First cylinder (1-522, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-522, default 522):
Using default value 522

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
user@server:~$

Check the LVM config to see what is there:

user@server:~$ sudo pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda1
  VG Name               server
  PV Size               1.76 GB / not usable 4.14 MB
  Allocatable           yes (but full)
  PE Size (KByte)       4096
  Total PE              450
  Free PE               0
  Allocated PE          450
  PV UUID               FNvtw6-Z06i-l48h-kKpG-gS25-01i8-vvju1T
  
user@server:~$ sudo vgdisplay
  --- Volume group ---
  VG Name               server
  System ID            
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               5.75 GB
  PE Size               4.00 MB
  Total PE              1473
  Alloc PE / Size       450 / 1.76 GB
  Free  PE / Size       1023 / 4.00 GB
  VG UUID               mQpOKH-CKVp-w24Q-00fs-UWwr-2O5D-5a0fre
  
user@server:~$ sudo lvdisplay
  --- Logical volume ---
  LV Name                /dev/server/root
  VG Name                server
  LV UUID                hO2d7u-IJ8t-UxN2-QArr-v3IL-M4gX-AGlZgV
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                1.62 GB
  Current LE             414
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0
  
  --- Logical volume ---
  LV Name                /dev/server/swap_1
  VG Name                server
  LV UUID                re64N8-iIjD-v5Tv-L6LS-KPcU-x6Xy-P2hKzK
  LV Write Access        read/write
  LV Status              available
  # open                 2
  LV Size                144.00 MB
  Current LE             36
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:1
  
user@server:~$

So now that we know the name of the logical volume etc. we can started adding the new space. First Add the new disk as to LVM a physical volume and verify that it worked:

user@server:~$ sudo pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created
user@server:~$ sudo pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda1
  VG Name               server
  PV Size               1.76 GB / not usable 4.14 MB
  Allocatable           yes (but full)
  PE Size (KByte)       4096
  Total PE              450
  Free PE               0
  Allocated PE          450
  PV UUID               FNvtw6-Z06i-l48h-kKpG-gS25-01i8-vvju1T
  
  "/dev/sdb1" is a new physical volume of "4.00 GB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name              
  PV Size               4.00 GB
  Allocatable           NO
  PE Size (KByte)       0
  Total PE              0
  Free PE               0
  Allocated PE          0
user@server:~$

Second add the new physical volume to the volume group and verify:

user@server:~$ sudo vgextend server /dev/sdb1
  Volume group "server" successfully extended
user@server:~$ sudo vgdisplay
  --- Volume group ---
  VG Name               server
  System ID            
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               5.75 GB
  PE Size               4.00 MB
  Total PE              1473
  Alloc PE / Size       450 / 1.76 GB
  Free  PE / Size       1023 / 4.00 GB
  VG UUID               mQpOKH-CKVp-w24Q-00fs-UWwr-2O5D-5a0fre
  
user@server:~$

Third (and final step) extend the root logical volume using the new space. I added 2Gb out of the possible four because that gives me some buffer in case of problems. And verify just to be sure

user@server:~$ sudo lvextend --size +2G /dev/server/root
  Extending logical volume root to 3.62 GB
  Logical volume root successfully resized
user@server:~$ sudo vgdisplay
  --- Volume group ---
  VG Name               server
  System ID            
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               5.75 GB
  PE Size               4.00 MB
  Total PE              1473
  Alloc PE / Size       962 / 3.76 GB
  Free  PE / Size       511 / 2.00 GB
  VG UUID               mQpOKH-CKVp-w24Q-00fs-UWwr-2O5D-5a0fre
user@server:~$ sudo lvdisplay
  --- Logical volume ---
  LV Name                /dev/server/root
  VG Name                server
  LV UUID                hO2d7u-IJ8t-UxN2-QArr-v3IL-M4gX-AGlZgV
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                3.62 GB
  Current LE             926
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0
  
  --- Logical volume ---
  LV Name                /dev/server/swap_1
  VG Name                server
  LV UUID                re64N8-iIjD-v5Tv-L6LS-KPcU-x6Xy-P2hKzK
  LV Write Access        read/write
  LV Status              available
  # open                 2
  LV Size                144.00 MB
  Current LE             36
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:1
  
user@server:~$

Boy that was difficult wasn't it?