虚拟机LVM扩容

Posted 若水小筑

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了虚拟机LVM扩容相关的知识,希望对你有一定的参考价值。

1.现在Vmware上,把虚拟机硬盘做扩展

2.现在打开虚拟机发现系统的磁盘空间已经扩了,但是硬盘分区可用空间没变,还是原来的30G

[[email protected] ~]# fdisk -l

Disk /dev/sda: 53.6 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        3916    31350847+  8e  Linux LVM

3.使用 fdisk 新建分区,为lvm扩容做准备

[[email protected] ~]# fdisk /dev/sda

The number of cylinders for this disk is set to 6527.
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/sda: 53.6 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        3916    31350847+  8e  Linux LVM

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

Command (m for help): p

Disk /dev/sda: 53.6 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        3916    31350847+  8e  Linux LVM
/dev/sda3            3917        6527    20972857+  83  Linux

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.
Syncing disks.

如果Re-reading the partition table 失败,可能分区还未识别,重启下再看。

[[email protected] ~]# reboot

Broadcast message from root (pts/1) (Wed Mar  9 10:01:32 2016):

The system is going down for reboot NOW!
[[email protected] ~]# fdisk -l

Disk /dev/sda: 53.6 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        3916    31350847+  8e  Linux LVM
/dev/sda3            3917        6527    20972857+  83  Linux

调整磁盘分区/dev/sda3为LVM格式,防止重装系统是不能识别(感觉不调整格式也能用)

[[email protected] ~]# fdisk /dev/sda

The number of cylinders for this disk is set to 6527.
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): t
Partition number (1-4): 3
Hex code (type L to list codes): 8e
Changed system type of partition 3 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sda: 53.6 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        3916    31350847+  8e  Linux LVM
/dev/sda3            3917        6527    20972857+  8e  Linux LVM

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.
Syncing disks.
[[email protected] ~]# reboot

Broadcast message from root (pts/1) (Wed Mar  9 11:00:43 2016):

The system is going down for reboot NOW!
[[email protected] ~]# fdisk -l

Disk /dev/sda: 53.6 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        3916    31350847+  8e  Linux LVM
/dev/sda3            3917        6527    20972857+  8e  Linux LVM
[[email protected] ~]# 

4.建立 pv

[[email protected] ~]# pvcreate /dev/sda3
  Physical volume "/dev/sda3" successfully created
[[email protected] ~]# pvdisplay 
  /dev/cdrom: open failed: No medium found
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               VolGroup00
  PV Size               29.90 GB / not usable 24.06 MB
  Allocatable           yes 
  PE Size (KByte)       32768
  Total PE              956
  Free PE               2
  Allocated PE          954
  PV UUID               o9H0fA-136u-HmOM-Ga3O-7R0i-VRAS-F1mNxZ
   
  --- NEW Physical volume ---
  PV Name               /dev/sda3
  VG Name               
  PV Size               20.00 GB
  Allocatable           NO
  PE Size (KByte)       0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               Qib59l-uCF5-5fo0-6oqH-5Url-yuSA-Y6N0E0

5.扩展 vg

[[email protected] ~]# vgextend VolGroup00 /dev/sda3
  Volume group "VolGroup00" successfully extended
[[email protected] ~]# vgdisplay 
  --- Volume group ---
  VG Name               VolGroup00
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               49.88 GB
  PE Size               32.00 MB
  Total PE              1596
  Alloc PE / Size       954 / 29.81 GB
  Free  PE / Size       642 / 20.06 GB
  VG UUID               8D7xnq-UPBK-LW2U-OCmc-Dl23-cpP1-qLLSHn

6.扩展lv

[[email protected] ~]# lvextend -l +640 /dev/VolGroup00/LogVol00
  Extending logical volume LogVol00 to 47.88 GB
  Logical volume LogVol00 successfully resized
[[email protected] ~]# lvdisplay 
  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol00
  VG Name                VolGroup00
  LV UUID                QDtSfz-1SlB-xl3P-mRZp-H3dx-erAx-mAwrt2
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                47.88 GB
  Current LE             1532
  Segments               3
  Allocation             inherit
  Read ahead sectors     0
  Block device           253:0
   
  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol01
  VG Name                VolGroup00
  LV UUID                6nv9PJ-ho10-LPkA-0GBg-TBZt-z1Tf-F6S2Hu
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                1.94 GB
  Current LE             62
  Segments               1
  Allocation             inherit
  Read ahead sectors     0
  Block device           253:1
   

7.调整文件系统

[[email protected] ~]# resize2fs  /dev/mapper/VolGroup00-LogVol00
resize2fs 1.35 (28-Feb-2004)
/dev/mapper/VolGroup00-LogVol00 is mounted; cant resize a mounted filesystem!

不能调整,使用 ext2online,我这边文件系统估计有点不对,所有会有些inode调整,忽略。

[[email protected] ~]# ext2online -v /dev/mapper/VolGroup00-LogVol00
ext2online v1.1.18 - 2001/03/18 for EXT2FS 0.5b
new filesystem size 12550144
group 2 inode table has offset 2, not 1029
group 4 inode table has offset 2, not 1029
...
using 1024 reserved group descriptor blocks
creating group 382 with 32768 blocks (rsvd = 1023, newgd =  3)

cache direct hits: 160, indirect hits: 1, misses: 4

8.打完收工

[[email protected] ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                       48G   27G   19G  59% /
/dev/sda1              99M   13M   81M  14% /boot
none                  772M     0  772M   0% /dev/shm

 

以上是关于虚拟机LVM扩容的主要内容,如果未能解决你的问题,请参考以下文章

给linux虚拟机硬盘扩容(LVM分区)

lvm虚拟机扩容虚拟机根目录

lvm虚拟机扩容虚拟机根目录

markdown Linux的虚拟机扩容(用分手在GPT分区盘创建新分区,在LVM上扩容)

Centos7下,LVM根分区扩容

如何给VirtualBox虚拟机的ubuntu LVM分区扩容