Expanding a xen disk image’s space

August 12th, 2009

I documented this a while back and never posted it. Had to use it again today so I figured it would be fun to post it.

Compiled these steps using these urls:

http://www.mail-archive.com/centos@centos.org/msg08928.html

http://www.howtoforge.com/linux_resizing_ext3_partitions_p2

1. use dd to create a 1 GB file

[root@virtserver os]# cd /var/lib/xen/images/os
[root@virtserver os]# dd if=/dev/zero of=Tempfile bs=1024 count=1000000

3.  backup your disk image

[root@virtserver os]# cp somehost.example.com-disk0 somehost.example.com-disk0.bkup

4.  append the tmp file to virtual image file

cat Tempfile >> somehost.example.com-disk0

5. attach to the disk image

[root@virtserver os]# modprobe xenblk
[root@virtserver os]# pwd
/var/lib/xen/images/os
[root@virtserver os]# xm block-attach 0 ‘file:/var/lib/xen/images/os/somehost.example.com-disk0′ xvda w

if you try and mount a file that doesn’t exist you’ll get output like this:

Error: Device 51712 (vbd) could not be connected.
File /var/lib/xen/images/os/notreal.example.com-disk0 is read-only, and so I will not
mount it read-write in a guest domain.
Usage: xm block-attach <Domain> <BackDev> <FrontDev> <Mode>

Create a new virtual block device.

if you copy and paste you may have to fix your single quotes. when I copy and pasted this I got a weird ascii error.

6. fsck

[root@virtserver os]# e2fsck -f /dev/xvda1
e2fsck 1.39 (29-May-2006)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/boot: 42/524288 files (2.4% non-contiguous), 28805/524112 blocks

[root@virtserver os]# e2fsck -f /dev/xvda2
e2fsck 1.39 (29-May-2006)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/: 43523/2097152 files (0.9% non-contiguous), 487769/2096482 blocks

7. Strip out the ext3 fournal

[root@virtserver os]# tune2fs -O^has_journal /dev/xvda1
tune2fs 1.39 (29-May-2006)

[root@virtserver os]# tune2fs -O^has_journal /dev/xvda2
tune2fs 1.39 (29-May-2006)

8. delete and re-add the last partition on the disk using the new end cylinder

[root@virtserveros]# fdisk /dev/xvda

The number of cylinders for this disk is set to 1156.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): p

Disk /dev/xvda: 9514 MB, 9514450944 bytes
255 heads, 63 sectors/track, 1156 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *           1         261     2096451   83  Linux
/dev/xvda2             262         783     4192965   83  Linux

Command (m for help): d
Partition number (1-4): 2

Command (m for help): p

Disk /dev/xvda: 9514 MB, 9514450944 bytes
255 heads, 63 sectors/track, 1156 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *           1         261     2096451   83  Linux

Command (m for help): n
Command action
e   extended
p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (262-1156, default 262):
Using default value 262
Last cylinder or +size or +sizeM or +sizeK (262-1156, default 1156):
Using default value 1156

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

Calling ioctl() to re-read partition table.
Syncing disks.

8. fsck and recreate the ext3 journals

[root@virtserver os]# e2fsck /dev/xvda2
e2fsck 1.39 (29-May-2006)
/: clean, 62190/1048576 files, 860683/1048241 blocks

[root@virtserver os]# tune2fs -j /dev/xvda2
tune2fs 1.39 (29-May-2006)
Creating journal inode: done
This filesystem will be automatically checked every -1 mounts or
0 days, whichever comes first.  Use tune2fs -c or -i to override.

[root@virtserver os]# tune2fs -j /dev/xvda1
tune2fs 1.39 (29-May-2006)
Creating journal inode: done
This filesystem will be automatically checked every -1 mounts or
0 days, whichever comes first.  Use tune2fs -c or -i to override.

9. Resize the filesystem

[root@virtserver os]# resize2fs -f /dev/xvda2
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/xvda2 to 1797271 (4k) blocks.
The filesystem on /dev/xvda2 is now 1797271 blocks long.

10. detach the disk image and get on with your day with more space

[root@virtserver os]# xm block-detach 0 xvda

Leave a Reply

You must be logged in to post a comment.