设为首页 收藏本站
查看: 1150|回复: 0

[经验分享] 在xen下调整基于文件的lvm大小

[复制链接]

尚未签到

发表于 2015-10-11 13:22:47 | 显示全部楼层 |阅读模式
  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!
             版权声明:本文为博主原创文章,未经博主允许不得转载。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-125467-1-1.html 上篇帖子: Xen API简介 下篇帖子: 云管理之虚拟化-Xen简介
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表