在xen下调整基于文件的lvm大小

One of the best references for getting started using virtual machines using XEN and CentOS can be found here. It is well written, very easy to follow and can quickly get you up and running.

Recently I had the task of revisiting a virtual machine that was provisioned with a file based file system as outlined in the tutorial. The main task I needed to achieve was to expand the size of the filesystem. Given that the tutorial has you build the virtual machine using tools that have this capability I figured it would not be that hard.

What is not obvious is that although the filesystem is made in such a way as to easily support this, that the actual practice of making it happen is a little more complicated. Having spent a lot of time googling all the various technologies and not coming up with a solution, I thought I would share what I learned, and give a recipe to perform the same operation on your own virtual server. All the usual caveats apply (use at your own risk, etc.)

If you are attempting to do something similar, or you are using a different Linux distribution it still may be useful to look at the following, and translate the steps accordingly.

Here is the recipe, feel free to let me know if it works for you:

1. Shutdown the VM

2. Backup the disk image file:

cp /xen/client8.img /xen/client8.img.backup

3. Restart the VM so that it can continue to serve while we work

4. Okay now make a working image from the backup:

cp client8.img.backup client8ext.img

5. Use dd to append more space to the end of the file:

dd if=/dev/zero of=/xen/client8xen.img bs=1M seek=10000 count=5000

This assumes an initial filesystem that is 10G and is expanding it to 15G

6. Check to see that the disk image file has grown the correct amount

ls -l /xen/client8ext.img

Should look something like

-rw-r–r– 1 root root 15728640000 Oct 26 02:22 client8ext.img

7. Mount the disk image on the loopback device:

/sbin/losetup /dev/loop1 /xen/client8ext.img

Here I am mounting it on loop1 because my initial loop0 was already in use. Just make sure that you mount it on an unused loop device and you should be fine.

Check that it has mounted okay:

/sbin/fdisk -l -u /dev/loop1

Should look something like:

Disk /dev/loop1: 15.7 GB, 15728640000 bytes
255 heads, 63 sectors/track, 1912 cylinders, total 30720000 sectors
Units = sectors of 1 * 512 = 512 bytes

Device Boot      Start         End      Blocks   Id  System
/dev/loop1p1   *          63      208844      104391   83  Linux
/dev/loop1p2          208845    20466809    10128982+  8e  Linux LVM

7. Find the start of the primary root partition. In the above example the LVM partition starts at 208845. Use the following formula to find the offset in the file where the LVM starts.

START * 512

8. Mount the volume group using the -o parameter and the calculated start value:

/sbin/losetup -o 106928640 /dev/loop2 /dev/loop1

9. Edit lvm.conf to filter out any other devices. If you are not actively using lvm on your working server it may be easy enough to just filter out everything but the loop devices. and return it back after you have finished. Here is a good reference on how to do this. In the end I just ended up changing the filter setting to

filter = [ "a/loop.*/", "r/.*/" ]

10. Rerun vgscan and check that it finds the volumes

/sbin/vgscan

You should see something like:

Reading all physical volumes.  This may take a while…
Found volume group “VolGroup00″ using metadata type lvm2

11. Okay now to resize the physical volume. Check to see that you can see it:

/usr/sbin/pvdisplay

Should get something like:

— Physical volume —
PV Name               /dev/loop2
VG Name               VolGroup00
PV Size               9.66 GB / not usable 3.58 MB
Allocatable           yes (but full)
PE Size (KByte)       32768
Total PE              309
Free PE               0
Allocated PE          309
PV UUID               IUefv0-aFx9-iPQn-p2nb-VOGO-ioQo-4XsfUc

12. Okay now to resize the physical volume:

/usr/sbin/pvresize /dev/loop2

Check that it worked:

/usr/sbin/pvdisplay

Output should now look something like:

— Physical volume —
PV Name               /dev/loop2
VG Name               VolGroup00
PV Size               14.55 GB / not usable 17.84 MB
Allocatable           yes
PE Size (KByte)       32768
Total PE              465
Free PE               156
Allocated PE          309
PV UUID               IUefv0-aFx9-iPQn-p2nb-VOGO-ioQo-4XsfUc

13. Now to resize the logical volumes:

/usr/sbin/lvdisplay

Should get something like:

— Logical volume —
LV Name                /dev/VolGroup00/LogVol00
VG Name                VolGroup00
LV UUID                4vMcGJ-jOdo-idMa-Q3qR-31Hg-YKHs-2ppTQA
LV Write Access        read/write
LV Status              NOT available
LV Size                9.16 GB
Current LE             293
Segments               1
Allocation             inherit
Read ahead sectors     0

— Logical volume —
LV Name                /dev/VolGroup00/LogVol01
VG Name                VolGroup00
LV UUID                wRtiQA-dZX1-fKI0-4yv2-FfPw-0F3C-8RThgX
LV Write Access        read/write
LV Status              NOT available
LV Size                512.00 MB
Current LE             16
Segments               1
Allocation             inherit
Read ahead sectors     0

14. Start by extending the swap space:

/usr/sbin/lvextend -L +512M /dev/VolGroup00/LogVol01

Then extend the root partition. Let’s use all available space this time.

Look up free extents:

/usr/sbin/vgdisplay

Get Something like:

— Volume group —
VG Name               VolGroup00
System ID
Format                lvm2
Metadata Areas        1
Metadata Sequence No  5
VG Access             read/write
VG Status             resizable
MAX LV                0
Cur LV                2
Open LV               0
Max PV                0
Cur PV                1
Act PV                1
VG Size               14.53 GB
PE Size               32.00 MB
Total PE              465
Alloc PE / Size       325 / 10.16 GB
Free  PE / Size       140 / 4.38 GB
VG UUID               ecCfkb-oPKd-Tf4S-VTLj-2M9c-LJJj-BHBPh2

15. Free extents are 140. Use this to extend the root partition:

/usr/sbin/lvextend -l+140 /dev/VolGroup00/LogVol00

16. Activate the root logical volume so that we can resize it:

/usr/sbin/lvchange -ay /dev/VolGroup00/LogVol00

17. Now we need to resize the file system. The docs I found suggest that you run a filesystem check both before and after you have resized the partiion:

/sbin/e2fsck -f /dev/VolGroup00/LogVol00
/sbin/resize2fs /dev/VolGroup00/LogVol00

Check the filesystem again:

/sbin/e2fsck -f /dev/VolGroup00/LogVol00

18. Okay now deactivate the root partition:

/usr/sbin/lvchange -an /dev/VolGroup00/LogVol00

19. Activate the swap partition:

/usr/sbin/lvchange -ay /dev/VolGroup00/LogVol01

20. Run mkswap on the extended swap partition:

/sbin/mkswap /dev/VolGroup00/LogVol01

21. Everything okay, now deactivate it again:

/usr/sbin/lvchange -an /dev/VolGroup00/LogVol01

22. Change the size of the LVM partition on /dev/loop1 using fdisk. This step caught me out. The disk partition information needs to be updated to reflect the new LVM size. Just launch fdisk and find the last partition. Then change it to reflect the full size of the drive.

   

         fdisk /dev/loop1

         fdisk>d

         fdisk>2

         fdisk>n

         fdisk>2

         fdisk>enter

         fdisk>enter

         fdisk>w

23. Deactivate the loop devices:

/sbin/losetup -d /dev/loop2
/sbin/losetup -d /dev/loop1

Your file based filesystem should now be ready to use with your current virtual machine. Change the appropriate line in your server’s XEN configuration and restart your server and enjoy!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值