How to extend the root filesystem of a 12.04 UBUNTU running on Citrix Xenserver
To extend the root filesystem please follow the following steps:
Check the filesystem setup
root@ubuntu:~# df -h
Filesystem     Size Used Avail Use% Mounted on
/dev/xvda1Â Â Â Â Â Â 9.0GÂ 2.7GÂ 5.9GÂ 32% /
udev           488M 4.0K 488M  1% /dev
tmpfs          199M 800K 198M  1% /run
none           5.0M    0 5.0M  0% /run/lock
none           497M  76K 496M  1% /run/shm
Check the partition layout
root@ubuntu:# fdisk -l /dev/xvda
Disk /dev/xvda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders, total 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00001dec
  Â
Device Boot     Start        End     Blocks  Id System
/dev/xvda1Â Â *Â Â Â Â Â Â Â 2048Â Â Â 18874367Â Â Â Â 9436160Â Â 83Â Linux
/dev/xvda2Â Â Â Â Â Â Â 18876414Â Â Â 20969471Â Â Â Â 1046529Â Â Â 5Â Extended
/dev/xvda5Â Â Â Â Â Â Â 18876416Â Â Â 20969471Â Â Â Â 1046528Â Â 82Â Linux swap / Solaris
Make a note of how many blocks the Linux swap is using, in this case it is 1046528. If the Linux swap had been on its own device (/dev/sdb for example) things would have been a lot easier as we would not need to worry about the swap partition at all and could just extend /dev/xvda. In this example, the Linux swap is also located in /dev/xvda and thus also requires relocation.
Shutdown the the VM in Citrix Xencenter and increase the diskspace -> Click on the VM and go to the Storage Tab and increase the amount you wish to add.
In this example we extend the disk space to 15GB.
To create new partitions, all old partiotions have to be deleted.
To be able to delete the swap partitions the swap has to be turned off:
root@ubuntu:~# free -m
            Â
total      used      free    shared   buffers    cached
Mem:Â Â Â Â Â Â Â Â Â Â 992Â Â Â Â Â Â Â 924Â Â Â Â Â Â Â Â 67Â Â Â Â Â Â Â Â Â 0Â Â Â Â Â Â Â Â 43Â Â Â Â Â Â Â 426
-/+ buffers/cache:Â Â Â Â Â Â Â 454Â Â Â Â Â Â Â 537
Swap:Â Â Â Â Â Â Â Â 1021Â Â Â Â Â Â Â Â Â 0Â Â Â Â Â Â 1021
root@ubuntu:~# swapoff -a
root@ubuntu:~# free -m
            Â
total      used      free    shared   buffers    cached
Mem:Â Â Â Â Â Â Â Â Â Â 992Â Â Â Â Â Â Â 924Â Â Â Â Â Â Â Â 67Â Â Â Â Â Â Â Â Â 0Â Â Â Â Â Â Â Â 43Â Â Â Â Â Â Â 426
-/+ buffers/cache:Â Â Â Â Â Â Â 454Â Â Â Â Â Â Â 537
Swap:Â Â Â Â Â Â Â Â Â Â Â 0Â Â Â Â Â Â Â Â Â 0Â Â Â Â Â Â Â Â Â 0
In the next step delete both /dev/xvda1 and /dev/xvda2. It is important to note that the start of the partition is 2048 (in this example).
root@ubuntu:~# fdisk /dev/xvda
Command (m for help): p
Disk /dev/xvda: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 cylinders, total 31457280 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00001dec
  Â
Device Boot     Start        End     Blocks  Id System
/dev/xvda1Â Â *Â Â Â Â Â Â Â 2048Â Â Â 18874367Â Â Â Â 9436160Â Â 83Â Linux
/dev/xvda2Â Â Â Â Â Â Â 18876414Â Â Â 20969471Â Â Â Â 1046529Â Â Â 5Â Extended
/dev/xvda5Â Â Â Â Â Â Â 18876416Â Â Â 20969471Â Â Â Â 1046528Â Â 82Â Linux swap / Solaris
Command (m for help): d
Partition number (1-5): 1
Command (m for help): d
Partition number (1-5): 2
Command (m for help): p
Disk /dev/xvda: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 cylinders, total 31457280 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00001dec
  Â
Device Boot     Start        End     Blocks  Id System
Command (m for help):
Do not quit fdisk just yet! New partitions have to be created.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-31457279, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-31457279, default 31457279): 30410751
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (1-4, default 2):
Using default value 2
First sector (30410752-31457279, default 30410752):
Using default value 30410752
Last sector, +sectors or +size{K,M,G} (30410752-31457279, default 31457279):
Using default value 31457279
You might notice, the end of the first primary partition is not 31457279 but 30410751. This number was calculated by subtracting the amount of blocks used by the swap from the block count of the new disk (in our example: 31457279-1046528 = 30410751).
The primary partition is now correct but the swap partition is not. The type of the partition should not be “83� but “82� (marking it as a partition for the Linux swap)
Command (m for help): p
Disk /dev/xvda: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 cylinders, total 31457280 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00001dec
  Â
Device Boot     Start        End     Blocks  Id System
/dev/xvda1Â Â Â Â Â Â Â Â Â Â Â 2048Â Â Â 30410751Â Â Â 15204352Â Â 83Â Linux
/dev/xvda2Â Â Â Â Â Â Â 30410752Â Â Â 31457279Â Â Â Â Â 523264Â Â 83Â Linux
Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 82
Changed system type of partition 2 to 82 (Linux swap / Solaris)
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
Now reboot the VM (virtual machine).
root@ubuntu:~# shutdown -r now
The swap partition will be mounted using an UUID identifier. After creating a new partition for swap the UUID will not match and upon reboot, no swap will be available. At this point two possibilites are available: Either enter the new UUID into the /etc/fstab or just “slap� the old UUID onto the new partition. In the example we chose the latter.
For some strange reason, Ubuntu actually finds the swap after reboot, although the UUID does not (yet) match. It did somehow find the “chunk� of swap space within the new /dev/xvda1 and used that. As this could not have been used by data anyway, no damage should occur but in any case setting up swap properly is the way to go.
The awk command is to show the UUID which is used for mkswap.
The dd is just to make sure that there is no data (usable by the kernel) at the beginning of the partition.
root@ubuntu:~# awk '/swap/ { print $1 }' /etc/fstab
#
UUID=8bb62351-4436-47df-92fe-af2865f03461
root@ubuntu:~# swapoff -a
root@ubuntu:~# free -m
total used free shared buffers cached
Mem: 992 695 296 0 23 325
-/+ buffers/cache: 346 645
Swap: 0 0 0
root@ubuntu:~# dd if=/dev/zero of=/dev/xvda2
dd: writing to `/dev/xvda2': No space left on device
1046529+0 records in
1046528+0 records out
535822336 bytes (536 MB) copied, 11.9388 s, 44.9 MB/s
root@ubuntu:~# mkswap -U 8bb62351-4436-47df-92fe-af2865f03461 /dev/xvda2
Setting up swapspace version 1, size = 523260 KiB
no label, UUID=8bb62351-4436-47df-92fe-af2865f03461
root@ubuntu:~# swapon -a
root@ubuntu:~# free -m
total used free shared buffers cached
Mem: 992 693 298 0 23 325
-/+ buffers/cache: 345 646
Swap: 510 7 503
root@ubuntu:~#
Now The final and easiest step is to resize the filesystem
root@ubuntu:~# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 9.0G 2.8G 5.8G 33% /
udev 488M 4.0K 488M 1% /dev
tmpfs 199M 788K 198M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 497M 200K 496M 1% /run/shm
root@ubuntu:~# resize2fs /dev/xvda1
resize2fs 1.42 (29-Nov-2011)
Filesystem at /dev/xvda1 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/xvda1 to 3801088 (4k) blocks.
The filesystem on /dev/xvda1 is now 3801088 blocks long.
root@ubuntu:~# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 15G 2.8G 11G 21% /
udev 488M 4.0K 488M 1% /dev
tmpfs 199M 788K 198M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 497M 200K 496M 1% /run/shm
Now you are done.