linux 将现有分区转换为lvm2分区并扩展放大容量

葫芦的运维日志

下一篇 搜索 上一篇

2014/02/08 17:49


此次主要进行了/dev/sdb从默认系统转换到lvm系统模式。期间备份恢复数据,不同磁盘下的不同分区创建pv加入同一vg组,放大lv容量,从vg中删除单个pv,通过实验验证lvm2突破了在lvm1版本时pe size大小限制vg大小的限制,自动挂载lv等。
期间经历重启由于fstab文件忘记修改导致无法开机,后进入单用户救援模式修改/etc/fstab后恢复,经过此番折腾更加深入了解了linux的磁盘文件系统模式。
1、查看现有系统信息。
[root@localhost ~]# df
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/sda2 99190032 4077672 89992368 5% /
/dev/sdb1 567161764 246748 537640172 1% /opt
/dev/sda1 99098 12238 81743 14% /boot
tmpfs 8196244 0 8196244 0% /dev/shm

[root@localhost ~]# fdisk /dev/sdb

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

Device Boot Start End Blocks Id System
/dev/sdb1 * 1 72891 585496926 83 Linux

2、备份数据,将原先/opt下的文件复制到新建的mkdir /optbak下,已做备份。
[root@localhost ~]# ll /opt/
总计 32
drwxr-xr-x 2 root root 4096 01-27 15:20 disk
drwx------ 2 root root 16384 01-22 15:40 lost+found
drwxr-xr-x 3 root root 4096 01-23 12:54 svn
drwxrwxrwx 3 root root 4096 02-02 16:50 svntongbu
drwxr-xr-x 5 root root 4096 01-30 14:43 wzcs
执行cp -r /opt /optbak后
[root@localhost ~]# ll /optbak/opt/
总计 20
drwxr-xr-x 2 root root 4096 02-08 15:48 disk
drwx------ 2 root root 4096 02-08 15:48 lost+found
drwxr-xr-x 3 root root 4096 02-08 15:48 svn
drwxr-xr-x 3 root root 4096 02-08 15:48 svntongbu
drwxr-xr-x 5 root root 4096 02-08 15:48 wzcs

3、执行umount /opt 写在文件挂载,这时/opt 目录下的文件已经看不到了,因为它所挂载的硬盘已被从文件系统卸载了。但是/opt目录仍然存在,只是成空文件夹了。
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda2 95G 4.0G 86G 5% /
/dev/sda1 97M 12M 80M 14% /boot
tmpfs 7.9G 0 7.9G 0% /dev/shm

4、现在开始执行将原有磁盘系统转化为lvm,因为服务器原先有两块磁盘sda sdb,sda为系统盘 其中sda3已经设置为了lvm分区,sdb这块磁盘由于我只分了一个区现在我需要把它删了重建。
[root@localhost ~]# fdisk /dev/sdb

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

Device Boot Start End Blocks Id System
/dev/sdb1 * 1 72891 585496926 83 Linux
因为只有一个区,执行d命令就删除了
Command (m for help): d
Selected partition 1
在查看/deb/sdb1已经没了
Command (m for help): p

Disk /dev/sdb: 599.5 GB, 599550590976 bytes
255 heads, 63 sectors/track, 72891 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

Command (m for help):


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

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

Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 8e
Changed system type of partition 2 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sdb: 599.5 GB, 599550590976 bytes
255 heads, 63 sectors/track, 72891 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 30395 244147806 8e Linux LVM
/dev/sdb2 30396 72891 341349120 8e Linux LVM

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

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


下面查看下结果。
[root@localhost ~]# partprobe
[root@localhost ~]# fdisk /dev/sdb

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

Device Boot Start End Blocks Id System
/dev/sdb1 1 30395 244147806 8e Linux LVM
/dev/sdb2 30396 72891 341349120 8e Linux LVM

Command (m for help):


5、现在开始创建物理卷PV。
[root@localhost ~]# pvscan
PV /dev/sda3 VG wangzi lvm2 [26.65 GB / 14.65 GB free]
Total: 1 [26.65 GB] / in use: 1 [26.65 GB] / in no VG: 0 [0 ]
[root@localhost ~]# pvcreate /dev/sdb
sdb sdb1 sdb2
[root@localhost ~]# pvcreate /dev/sdb{1,2}
Writing physical volume data to disk "/dev/sdb1"
Physical volume "/dev/sdb1" successfully created
Writing physical volume data to disk "/dev/sdb2"
Physical volume "/dev/sdb2" successfully created
[root@localhost ~]# pvscan
PV /dev/sda3 VG wangzi lvm2 [26.65 GB / 14.65 GB free]
PV /dev/sdb1 lvm2 [232.84 GB]
PV /dev/sdb2 lvm2 [325.54 GB]
Total: 3 [585.02 GB] / in use: 1 [26.65 GB] / in no VG: 2 [558.37 GB]
[root@localhost ~]#

[root@localhost ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda3
VG Name wangzi
PV Size 26.65 GB / not usable 3.76 MB
Allocatable yes
PE Size (KByte) 4096
Total PE 6822
Free PE 3750
Allocated PE 3072
PV UUID RhARsu-Isui-6Ztb-hGqY-othy-BWyV-dLBKJZ

"/dev/sdb1" is a new physical volume of "232.84 GB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 232.84 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 22kAOf-E8CT-dgtf-4f2W-KWY8-7q5L-8k3ckF

"/dev/sdb2" is a new physical volume of "325.54 GB"
--- NEW Physical volume ---
PV Name /dev/sdb2
VG Name
PV Size 325.54 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID eiNvoe-i1D9-2IJa-vc69-26xQ-cFT6-JM6GSl



6、移除整个卷组。
[root@localhost ~]# vgremove wangzi
Do you really want to remove volume group "wangzi" containing 1 logical volumes? [y/n]: y
Do you really want to remove active logical volume wangzilv? [y/n]: y
Logical volume "wangzilv" successfully removed
Volume group "wangzi" successfully removed

7、查看目前的pv信息。
[root@localhost ~]# pvscan
PV /dev/sda3 lvm2 [26.65 GB]
PV /dev/sdb1 lvm2 [232.84 GB]
PV /dev/sdb2 lvm2 [325.54 GB]
Total: 3 [585.03 GB] / in use: 0 [0 ] / in no VG: 3 [585.03 GB]
[root@localhost ~]# pvdisplay
"/dev/sda3" is a new physical volume of "26.65 GB"
--- NEW Physical volume ---
PV Name /dev/sda3
VG Name
PV Size 26.65 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID RhARsu-Isui-6Ztb-hGqY-othy-BWyV-dLBKJZ

"/dev/sdb1" is a new physical volume of "232.84 GB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 232.84 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 22kAOf-E8CT-dgtf-4f2W-KWY8-7q5L-8k3ckF

"/dev/sdb2" is a new physical volume of "325.54 GB"
--- NEW Physical volume ---
PV Name /dev/sdb2
VG Name
PV Size 325.54 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID eiNvoe-i1D9-2IJa-vc69-26xQ-cFT6-JM6GSl


8、现在卷组空间为558.37GB。
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name wangzi
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 558.37 GB
PE Size 4.00 MB
Total PE 142943 //这里看到 lvm2已经不再是pe限制最大vg大小了。
Alloc PE / Size 0 / 0
Free PE / Size 142943 / 558.37 GB
VG UUID 9jhAi7-dZMf-bgqw-QxcY-sCuN-F3f8-acfEkD


[root@localhost ~]# lvcreate -l 142943 -n wangzilv wangzi
Logical volume "wangzilv" created
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Name /dev/wangzi/wangzilv
VG Name wangzi
LV UUID LIiDbv-wf7a-gzVP-6sRJ-259s-LtSh-iE82cT
LV Write Access read/write
LV Status available
# open 0
LV Size 558.37 GB
Current LE 142943
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0

9、删除原先创建的卷组,正式开始创建卷组,虽然经过上面实验证明pe size设置为4m也能突破先前256g大小限制,但是为了保险起见我还是设置为了32m。下面演示如何从现有卷组vg中删除单个pv。
[root@localhost ~]# vgremove wangzi
Do you really want to remove volume group "wangzi" containing 1 logical volumes? [y/n]: y
Do you really want to remove active logical volume wangzilv? [y/n]: y
Logical volume "wangzilv" successfully removed
Volume group "wangzi" successfully removed


[root@localhost ~]# vgcreate -s 32m wangzivg /dev/sdb{1,2}
Volume group "wangzivg" successfully created
[root@localhost ~]# vgextend wangzi /dev/sda3
Volume group "wangzi" not found
[root@localhost ~]# vgextend wangzivg /dev/sda3
Volume group "wangzivg" successfully extended
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name wangzivg
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 3
Act PV 3
VG Size 584.97 GB
PE Size 32.00 MB
Total PE 18719
Alloc PE / Size 0 / 0
Free PE / Size 18719 / 584.97 GB
VG UUID YK8q1L-kizD-BaNN-pUtF-2qlu-B68v-285Cf4

[root@localhost ~]# vgreduce wangzivg /dev/sda3
Removed "/dev/sda3" from volume group "wangzivg"
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name wangzivg
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 558.34 GB
PE Size 32.00 MB
Total PE 17867
Alloc PE / Size 0 / 0
Free PE / Size 17867 / 558.34 GB
VG UUID YK8q1L-kizD-BaNN-pUtF-2qlu-B68v-285Cf4


10、现在开始创建逻辑卷,并让逻辑卷wangzilv占用所有卷组wangzivg空间。
[root@localhost ~]# lvcreate -l 17867 -n wangzilv wangzivg
Logical volume "wangzilv" created
[root@localhost ~]# ll /dev/
Display all 195 possibilities? (y or n)
[root@localhost ~]# ll /dev/wangzivg/wangzilv
lrwxrwxrwx 1 root root 29 02-08 17:05 /dev/wangzivg/wangzilv -> /dev/mapper/wangzivg-wangzilv

[root@localhost ~]# df -T
文件系统 类型 1K-块 已用 可用 已用% 挂载点
/dev/sda2 ext3 99190032 4122276 89947764 5% /
/dev/sda1 ext3 99098 12238 81743 14% /boot
tmpfs tmpfs 8196244 0 8196244 0% /dev/shm

11、格式化挂载lv。
[root@localhost ~]# mkfs -t ext3 /dev/wangzivg/wangzilv
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
73187328 inodes, 146366464 blocks
7318323 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
4467 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
102400000

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information:
done

This filesystem will be automatically checked every 33 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

[root@localhost ~]# cp -r /optbak/opt/* /opt
[root@localhost ~]# ll /opt
总计 32
drwxr-xr-x 2 root root 4096 02-08 17:09 disk
drwx------ 2 root root 16384 02-08 17:07 lost+found
drwxr-xr-x 3 root root 4096 02-08 17:09 svn
drwxr-xr-x 3 root root 4096 02-08 17:09 svntongbu
drwxr-xr-x 5 root root 4096 02-08 17:09 wzcs

[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda2 95G 4.0G 86G 5% /
/dev/sda1 97M 12M 80M 14% /boot
tmpfs 7.9G 0 7.9G 0% /dev/shm
/dev/mapper/wangzivg-wangzilv
550G 241M 522G 1% /opt


12、放大LV容量,虽然上步已经将文件恢复到/opt但放大lv容量并不会影响现有文件。
[root@localhost ~]# vgextend wangzivg /dev/sda3
Volume group "wangzivg" successfully extended
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name wangzivg
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 5
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 3
Act PV 3
VG Size 584.97 GB
PE Size 32.00 MB
Total PE 18719
Alloc PE / Size 17867 / 558.34 GB
Free PE / Size 852 / 26.62 GB
VG UUID YK8q1L-kizD-BaNN-pUtF-2qlu-B68v-285Cf4

[root@localhost ~]# lvresize -l +852 /dev/wangzivg/wangzilv
Extending logical volume wangzilv to 584.97 GB
Logical volume wangzilv successfully resized
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Name /dev/wangzivg/wangzilv
VG Name wangzivg
LV UUID 8r63pN-gkeY-28BN-0t2R-I3mb-86fO-H1SZFZ
LV Write Access read/write
LV Status available
# open 1
LV Size 584.97 GB
Current LE 18719
Segments 3
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0

[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda2 95G 4.0G 86G 5% /
/dev/sda1 97M 12M 80M 14% /boot
tmpfs 7.9G 0 7.9G 0% /dev/shm
/dev/mapper/wangzivg-wangzilv
550G 241M 522G 1% /opt
[root@localhost ~]# ll /opt
总计 32
drwxr-xr-x 2 root root 4096 02-08 17:09 disk
drwx------ 2 root root 16384 02-08 17:07 lost+found
drwxr-xr-x 3 root root 4096 02-08 17:09 svn
drwxr-xr-x 3 root root 4096 02-08 17:09 svntongbu
drwxr-xr-x 5 root root 4096 02-08 17:09 wzcs


13、查看pv vg lv状态。

[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name wangzivg
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 6
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 3
Act PV 3
VG Size 584.97 GB
PE Size 32.00 MB
Total PE 18719
Alloc PE / Size 18719 / 584.97 GB
Free PE / Size 0 / 0
VG UUID YK8q1L-kizD-BaNN-pUtF-2qlu-B68v-285Cf4

[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Name /dev/wangzivg/wangzilv
VG Name wangzivg
LV UUID 8r63pN-gkeY-28BN-0t2R-I3mb-86fO-H1SZFZ
LV Write Access read/write
LV Status available
# open 1
LV Size 584.97 GB
Current LE 18719
Segments 3
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0

[root@localhost ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sdb1
VG Name wangzivg
PV Size 232.84 GB / not usable 25.59 MB
Allocatable yes (but full)
PE Size (KByte) 32768
Total PE 7450
Free PE 0
Allocated PE 7450
PV UUID 22kAOf-E8CT-dgtf-4f2W-KWY8-7q5L-8k3ckF

--- Physical volume ---
PV Name /dev/sdb2
VG Name wangzivg
PV Size 325.54 GB / not usable 4.75 MB
Allocatable yes (but full)
PE Size (KByte) 32768
Total PE 10417
Free PE 0
Allocated PE 10417
PV UUID eiNvoe-i1D9-2IJa-vc69-26xQ-cFT6-JM6GSl

--- Physical volume ---
PV Name /dev/sda3
VG Name wangzivg
PV Size 26.65 GB / not usable 27.76 MB
Allocatable yes (but full)
PE Size (KByte) 32768
Total PE 852
Free PE 0
Allocated PE 852
PV UUID RhARsu-Isui-6Ztb-hGqY-othy-BWyV-dLBKJZ


问题1、为何可用空间加已用空间小于实际空间。
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda2 95G 4.0G 86G 5% /
/dev/sda1 97M 12M 80M 14% /boot
tmpfs 7.9G 0 7.9G 0% /dev/shm
/dev/mapper/wangzivg-wangzilv
550G 241M 522G 1% /opt

经过查询得知:ext2/Ext3 缺省保留 5% 硬盘空间供系统管理员工作之用。如果不要保留可执行下面的命令但建议保留。
man tune2fs
-m reserved-blocks-percentage
改成-m 0就好了
问题2、如何实现开机自动挂载磁盘,及如出问题如何解决。
将这句写入mount /dev/wangzivg/wangzilv /opt
/etc/rc.local 或/etc/fstab 推荐写入/etc/rc.local,因为如果fstab如果有误会导致无法开机。
悲剧的是重启后,由于fstab文件忘记修改导致无法开机,后进入单用户模式修改/etc/fstab后恢复,我这里是删除/opt行。
进入单用户模式解决此问题方法:
在(or type control -D to continue):后面输入root密码后回车,执行:init 1,系统将自动重启并进入单用户模式,在进入单用户模式后,由于根分区是只读的,需要通过重新挂载:sh-3.2 # mount -n -o remount,rw /回车,然后再修改/etc/fstab文件。这里我因为我将/opt已经重新挂在了wangzilv上,所以这里删除原先安装系统时的挂载的/opt行后恢复。

葫芦的运维日志

打赏

上一篇 搜索 下一篇
© 冰糖葫芦甜(bthlt.com) 2019 王梓 打赏联系方式 陕ICP备17005322号