Extend Linux drive (XenServer)

Here’s a quick look up guide to extend a drive on Linux, hosted on XenServer.

Pre-steps

From XenServer, do the following:

  1. Take server offline.
  2. Select the VM you need to change.
  3. Click Storage.
  4. Right-Click the hard drive you want to extend, and click Properties.
  5. Click Size and Location.
  6. Resize to the desired size.

Take 1 of commands

root@xenservervm:~# df -h

Filesystem      Size Used Avail Use% Mounted on
/dev/xvda1       43G 41G 143M 100% /
udev             10M 0 10M 0% /dev
tmpfs           803M 8.7M 795M   2% /run
tmpfs           2.0G 0 2.0G   0% /dev/shm
tmpfs           5.0M 0 5.0M   0% /run/lock
tmpfs           2.0G 0 2.0G   0% /sys/fs/cgroup
tmpfs           402M 0 402M   0% /run/user/1001
tmpfs           402M 0 402M   0% /run/user/999
tmpfs           402M 0 402M   0% /run/user/0

 

root@xenservervm:~# fdisk -l /dev/xvda

Disk /dev/xvda: 145 GiB, 155692564480 bytes, 304087040 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
Disklabel type: dos
Disk identifier: 0xb3e06c00
Device     Boot Start      End Sectors Size Id Type
/dev/xvda1          2048 90449919 90447872 43.1G 83 Linux
/dev/xvda2      90451966 94369791  3917826 1.9G 5 Extended
/dev/xvda5      90451968 94369791  3917824  1.9G 82 Linux swap / Solaris

Note down the highlighted number above (End sector)

 

root@xenservervm:~# free -m

             total       used free     shared buffers   cached
Mem:          4014 2095       1918 8   23 422
-/+ buffers/cache:       1648 2365
Swap:         1912 0       1912

root@xenservervm:~# swapoff -a

root@xenservervm:~# free -m

             total       used free     shared buffers   cached
Mem:          4014 2095       1918 8   23 422
-/+ buffers/cache:       1648 2365
Swap:            0 0     0

root@xenservervm:~# fdisk /dev/xvda

Welcome to fdisk (util-linux 2.25.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

 

Command (m for help): p

Disk /dev/xvda: 145 GiB, 155692564480 bytes, 304087040 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
Disklabel type: dos
Disk identifier: 0xb3e06c00

Device     Boot Start      End Sectors Size Id Type
/dev/xvda1          2048 90449919 90447872 43.1G 83 Linux
/dev/xvda2      90451966 94369791  3917826 1.9G 5 Extended
/dev/xvda5      90451968 94369791  3917824 1.9G 82 Linux swap / Solaris

 

Command (m for help): m

Help:
  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table

 

Command (m for help): d

Partition number (1,2,5, default 5): 1
Partition 1 has been deleted.

Command (m for help): d

Partition number (2,5, default 5): 2

Partition 2 has been deleted.

Command (m for help): p

Disk /dev/xvda: 145 GiB, 155692564480 bytes, 304087040 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
Disklabel type: dos
Disk identifier: 0xb3e06c00

 

Command (m for help): n

Partition type
p   primary (0 primary, 0 extended, 4 free)
e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-304087039, default 2048): 2048
Last sector, +sectors or +size{K,M,G,T,P} (2048-304087039, default 304087039): 300169215

Created a new partition 1 of type ‘Linux’ and of size 143.1 GiB.

Command (m for help): n

Partition type
p   primary (1 primary, 0 extended, 3 free)
e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (300169216-304087039, default 300169216):
Last sector, +sectors or +size{K,M,G,T,P} (300169216-304087039, default 304087039): 304087039

Created a new partition 2 of type ‘Linux’ and of size 1.9 GiB.
Take note of highlighted numbers

Command (m for help): p

Disk /dev/xvda: 145 GiB, 155692564480 bytes, 304087040 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
Disklabel type: dos
Disk identifier: 0xb3e06c00
Device     Boot Start       End Sectors Size Id Type
/dev/xvda1           2048 300169215 300167168 143.1G 83 Linux
/dev/xvda2      300169216 304087039   3917824 1.9G 83 Linux

 

Command (m for help): t

Partition number (1,2, default 2): 2
Hex code (type L to list all codes): 82
Changed type of partition ‘Linux’ to ‘Linux swap / Solaris’.

Command (m for help): w

The partition table has been altered.

Calling ioctl() to re-read partition table.

Re-reading the partition table failed.: 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).

root@xenservervm:~# shutdown -r now

Take 2 of commands – after reboot

root@xenservervm:~# df -h

Filesystem      Size Used Avail Use% Mounted on
/dev/xvda1       43G 41G 138M 100% /
udev             10M 0 10M 0% /dev
tmpfs           803M 8.7M 795M   2% /run
tmpfs           2.0G 0 2.0G   0% /dev/shm
tmpfs           5.0M 0 5.0M   0% /run/lock
tmpfs           2.0G 0 2.0G   0% /sys/fs/cgroup
tmpfs           402M 0 402M   0% /run/user/1001
tmpfs           402M 0 402M   0% /run/user/999
tmpfs           402M 0 402M   0% /run/user/0

 

root@xenservervm:~# fdisk -l

 

Disk /dev/xvdb: 100 GiB, 107374182400 bytes, 209715200 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 /dev/xvda: 145 GiB, 155692564480 bytes, 304087040 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
Disklabel type: dos

Disk identifier: 0xb3e06c00

 

Device     Boot Start       End Sectors Size Id Type
/dev/xvda1           2048 300169215 300167168 143.1G 83 Linux
/dev/xvda2      300169216 304087039   3917824 1.9G 82 Linux swap / Solaris

 

root@xenservervm:~# awk ‘/swap/ { print $1 }’ /etc/fstab

#

UUID=8a694120-533e-4f4c-a973-9224e4a745a9

root@xenservervm:~# swapoff -a
swapoff: cannot find the device for UUID=8a694120-533e-4f4c-a973-9224e4a745a9

 

root@xenservervm:~# free -m

             total       used free     shared buffers   cached
Mem:          4014 2066       1947 8   24 423
-/+ buffers/cache:       1618 2395
Swap:            0 0     0

root@xenservervm:~# dd if=/dev/zero of=/dev/xvda2

dd: writing to ‘/dev/xvda2’: No space left on device
3917825+0 records in
3917824+0 records out
2005925888 bytes (2.0 GB) copied, 53.518 s, 37.5 MB/s

 

root@xenservervm:~# mkswap -U UUID=8a694120-533e-4f4c-a973-9224e4a745a9 /dev/xvda2

mkswap: error: parsing UUID failed

 

root@xenservervm:~# swapon -a

swapon: cannot find the device for UUID=8a694120-533e-4f4c-a973-9224e4a745a9

 

root@xenservervm:~# df -h

Filesystem      Size Used Avail Use% Mounted on
/dev/xvda1       43G 41G 139M 100% /
udev             10M 0 10M 0% /dev
tmpfs           803M 8.7M 795M   2% /run
tmpfs           2.0G 0 2.0G   0% /dev/shm
tmpfs           5.0M 0 5.0M   0% /run/lock
tmpfs           2.0G 0 2.0G   0% /sys/fs/cgroup
tmpfs           402M 0 402M   0% /run/user/1001
tmpfs           402M 0 402M   0% /run/user/999
tmpfs           402M 0 402M   0% /run/user/0

 

root@xenservervm:~# resize2fs /dev/xvda1

resize2fs 1.42.12 (29-Aug-2014)
Filesystem at /dev/xvda1 is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 9
The filesystem on /dev/xvda1 is now 37520896 (4k) blocks long.

 

root@xenservervm:~# df -h

Filesystem      Size Used Avail Use% Mounted on
/dev/xvda1      141G 41G 95G  30% /
udev             10M 0 10M 0% /dev
tmpfs           803M 8.7M 795M   2% /run
tmpfs           2.0G 0 2.0G   0% /dev/shm
tmpfs           5.0M 0 5.0M   0% /run/lock
tmpfs           2.0G 0 2.0G   0% /sys/fs/cgroup
tmpfs           402M 0 402M   0% /run/user/1001
tmpfs           402M 0 402M   0% /run/user/999
tmpfs           402M 0 402M   0% /run/user/0

 

Thank you for reading!

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *