在Linux的逻辑卷管理,下面的步骤包括新建一个逻辑卷以及怎样增加逻辑卷的大小。
1、新挂载一个硬盘设备
使用fdisk -l查看设备名称
在这里,我们使用的是 /dev/sdc
2、使用fdisk创建LVM分区
[root@localhost etc]# fdisk /dev/sdc
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
p
Partition number (1-4): 1
Partition 1 is already defined. Delete it before re-adding it.
Command (m for help): d
Selected partition 1
Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Command (m for help): d /dev/sdc1 --因为这里原来就使用了sdc设备,所以需要先删除
No partition is defined yet!
Command (m for help): p
Disk /dev/sdc: 1073 MB, 1073741824 bytes
64 heads, 32 sectors/track, 1024 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Device Boot Start End Blocks Id System
Command (m for help): n
Command action
e extended
p primary partition (1-4) --在这里选择p,选择扩展将无法转换lvm格式
p
Partition number (1-4): 1
First cylinder (1-1024, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-1024, default 1024):
Using default value 1024
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e --8e即表示为lvm
Changed system type of partition 1 to 8e (Linux LVM)
Command (m for help): p
Disk /dev/sdc: 1073 MB, 1073741824 bytes
64 heads, 32 sectors/track, 1024 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Device Boot Start End Blocks Id System
/dev/sdc1 1 1024 1048560 8e Linux LVM
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
3、创建物理卷pv
[root@localhost etc]# fdisk -l
Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 509 4088511 83 Linux
/dev/sda2 637 2610 15856155 83 Linux
/dev/sda3 510 636 1020127+ 82 Linux swap
Partition table entries are not in disk order
Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 1 391 3140676 8e Linux LVM
Disk /dev/sdc: 1073 MB, 1073741824 bytes
64 heads, 32 sectors/track, 1024 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Device Boot Start End Blocks Id System
/dev/sdc1 1 1024 1048560 8e Linux LVM
[root@localhost etc]# pvcreate /dev/sdc1
pvcreate -- ERROR: "/etc/lvmtab" doesn't exist; please run vgscan
[root@localhost etc]# vgscan --需要先执行vgscan,创建/etc/lvmtab和/etc/lvmtab.d
vgscan -- reading all physical volumes (this may take a while...)
vgscan -- "/etc/lvmtab" and "/etc/lvmtab.d" successfully created
vgscan -- WARNING: This program does not do a VGDA backup of your volume group
[root@localhost etc]# pvcreate /dev/sdc1
pvcreate -- physical volume "/dev/sdc1" successfully created
[root@localhost etc]# vgcreate testfg /dev/sdc1
vgcreate -- INFO: using default physical extent size 4 MB
vgcreate -- INFO: maximum logical volume size is 255.99 Gigabyte
vgcreate -- doing automatic backup of volume group "testfg"
vgcreate -- volume group "testfg" successfully created and activated
4、创建卷组
[root@localhost etc]# vgcreate testfg /dev/sdc1
vgcreate -- INFO: using default physical extent size 4 MB
vgcreate -- INFO: maximum logical volume size is 255.99 Gigabyte
vgcreate -- doing automatic backup of volume group "testfg"
vgcreate -- volume group "testfg" successfully created and activated
--这里默认的PE大小为4M,PE的大小也决定逻辑卷的最大容量。这里是255.99 Gigabyte,如果希望获得更大的逻辑卷容量,可以更改PE的大小。可以使用-s参数指定PE的大小,PE的取值范围为8 KB 到 512 MB。
5、激活卷组
[root@localhost etc]# vgchange -a y testvg
vgchange -- volume group "testvg" does not exist
[root@localhost etc]# vgchange -a y testfg
vgchange -- volume group "testfg" already active
6、创建逻辑卷
[root@localhost etc]# vgdisplay testfg
--- Volume group ---
VG Name testfg
VG Access read/write
VG Status available/resizable
VG # 0
MAX LV 256
Cur LV 0
Open LV 0
MAX LV Size 255.99 GB
Max PV 256
Cur PV 1
Act PV 1
VG Size 1016 MB
PE Size 4 MB
Total PE 254
Alloc PE / Size 0 / 0
Free PE / Size 254 / 1016 MB
VG UUID EYvilo-4nl3-CC5r-NkEh-7AGJ-oQn4-wfZeC0
[root@localhost etc]# lvcreate -L254 -nlvm1 testfg --L参数指定的是PE的数量
lvcreate -- rounding size up to physical extent boundary
lvcreate -- doing automatic backup of "testfg"
lvcreate -- logical volume "/dev/testfg/lvm1" successfully created
7、创建文件系统
[root@localhost etc]# mkfs -t ext2 /dev/testfg/lvm1
mke2fs 1.32 (09-Nov-2002)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
65536 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
First data block=1
32 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 34 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
8、挂载文件系统
[root@localhost home]# mkdir ibm
[root@localhost home]# mount /dev/testfg/lvm1 /home/ibm
[root@localhost home]# df -k
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/sda1 4024188 2293400 1526364 61% /
/dev/sda2 15607296 3299848 11514644 23% /home
none 115352 0 115352 0% /dev/shm
/dev/testfg/lvm1 253871 13 240751 1% /home/ibm
9、添加一个新的磁盘
比如我们在这里新添加了一个磁盘/dev/sdb。需要将该磁盘增加到卷组/dev/testfg/lvm1中。
按照前面的步骤,将该磁盘创建称谓一个物理卷 /dev/sdb1,然后将该物理卷添加到卷组中:
[root@localhost home]# fdisk /dev/sdb --格式化为lvm设备
[root@localhost home]# pvcreate /dev/sdb1
pvcreate -- physical volume "/dev/sdb1" successfully created
[root@localhost home]# vgextend testfg /dev/sdb1
vgextend -- INFO: maximum logical volume size is 255.99 Gigabyte
vgextend -- doing automatic backup of volume group "testfg"
vgextend -- volume group "testfg" successfully extended
10、删除一个物理卷
[root@localhost home]# pvdisplay /dev/sdb1
--- Physical volume ---
PV Name /dev/sdb1
VG Name testfg
PV Size 3 GB [6281352 secs] / NOT usable 4.19 MB [LVM: 130 KB]
PV# 2
PV Status NOT available
Allocatable yes
Cur LV 0
PE Size (KByte) 4096
Total PE 765
Free PE 765
Allocated PE 0
PV UUID LsTOGX-bnhO-uWqj-ivsc-fY98-3CrN-0i7vbK
--在这里,需要确认该pv没有正在被使用
[root@localhost home]# vgreduce /dev/testfg/lvm1 /dev/sdb1
vgreduce -- ERROR: invalid volume group name "testfg/lvm1"
[root@localhost home]# vgreduce testfg /dev/sdb1
vgreduce -- doing automatic backup of volume group "testfg"
vgreduce -- volume group "testfg" successfully reduced by physical volume:
vgreduce -- /dev/sdb1
在pv /dev/sdb1被删除之前,我们看到testfg的情况是:
[root@localhost home]# vgdisplay testfg
--- Volume group ---
VG Name testfg
VG Access read/write
VG Status available/resizable
VG # 0
MAX LV 256
Cur LV 1
Open LV 1
MAX LV Size 255.99 GB
Max PV 256
Cur PV 2
Act PV 2
VG Size 3.98 GB
PE Size 4 MB
Total PE 1019
Alloc PE / Size 64 / 256 MB
Free PE / Size 955 / 3.73 GB
VG UUID EYvilo-4nl3-CC5r-NkEh-7AGJ-oQn4-wfZeC0
在pv /dev/sdb1被删除之后,我们看到testfg的容量变化
[root@localhost home]# vgdisplay testfg
--- Volume group ---
VG Name testfg
VG Access read/write
VG Status available/resizable
VG # 0
MAX LV 256
Cur LV 1
Open LV 1
MAX LV Size 255.99 GB
Max PV 256
Cur PV 1
Act PV 1
VG Size 1016 MB
PE Size 4 MB
Total PE 254
Alloc PE / Size 64 / 256 MB
Free PE / Size 190 / 760 MB
VG UUID EYvilo-4nl3-CC5r-NkEh-7AGJ-oQn4-wfZeC0:
11、扩展逻辑卷的大小
在10中,我们看到是通过新增一个pv或者删除一个pv来增加或这删除lv的大小。现在我们使用lvextend和lvreduce来增加和减小lv的大小。
[root@localhost home]# lvextend -L+4m /dev/testfg/lvm1
lvextend -- extending logical volume "/dev/testfg/lvm1" to 260 MB
lvextend -- doing automatic backup of volume group "testfg"
lvextend -- logical volume "/dev/testfg/lvm1" successfully extended
--如果在这里你增加的不是PE的倍数,系统会自动round到一个PE的倍数大小。
[root@localhost home]# lvreduce -L-4m /dev/testfg/lvm1
lvreduce -- WARNING: reducing active and open logical volume to 256 MB
lvreduce -- THIS MAY DESTROY YOUR DATA (filesystem etc.)
lvreduce -- do you really want to reduce "/dev/testfg/lvm1"? [y/n]: y
lvreduce -- doing automatic backup of volume group "testfg"
lvreduce -- logical volume "/dev/testfg/lvm1" successfully reduced
在这里,我们扩展或者删除了逻辑卷的容量,但是在文件系统上还是还是没有识别出这部分空间的变化。可以使用resize2fs命令是更改被操作系统所识别.
如下所示,在lvextend增加了40m的逻辑卷空间。但是在操作系统上没有识别出来。
[root@localhost root]# df -k
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/sda1 4024188 2294624 1525140 61% /
/dev/sda2 15607296 3299848 11514644 23% /home
none 115352 0 115352 0% /dev/shm
/dev/testfg/lvm1 253871 13 240751 1% /home/bak
[root@localhost root]# lvextend -L+40m /dev/testfg/lvm1
lvextend -- extending logical volume "/dev/testfg/lvm1" to 340 MB--增加到340m空间了。
lvextend -- doing automatic backup of volume group "testfg"
lvextend -- logical volume "/dev/testfg/lvm1" successfully extended
[root@localhost root]# vgdisplay testfg
--- Volume group ---
VG Name testfg
VG Access read/write
VG Status available/resizable
VG # 0
MAX LV 256
Cur LV 1
Open LV 1
MAX LV Size 255.99 GB
Max PV 256
Cur PV 1
Act PV 1
VG Size 1016 MB
PE Size 4 MB
Total PE 254
Alloc PE / Size 85 / 340 MB --从vg上也可以看到分配了340m的空间。
Free PE / Size 169 / 676 MB
VG UUID EYvilo-4nl3-CC5r-NkEh-7AGJ-oQn4-wfZeC0
[root@localhost root]# df -k
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/sda1 4024188 2294624 1525140 61% /
/dev/sda2 15607296 3299848 11514644 23% /home
none 115352 0 115352 0% /dev/shm
/dev/testfg/lvm1 253871 13 240751 1% /home/bak --但在操作系统上没有识别出空间已经增长。
[root@localhost root]# resize2fs /dev/testfg/lvm1
resize2fs 1.32 (09-Nov-2002)
/dev/testfg/lvm1 is mounted; can't resize a mounted filesystem!
[root@localhost root]# umount /dev/testfg/lvm1
[root@localhost root]# resize2fs /dev/testfg/lvm1
resize2fs 1.32 (09-Nov-2002)
Please run 'e2fsck -f /dev/testfg/lvm1' first.
[root@localhost root]# e2fsck -f /dev/testfg/lvm1
e2fsck 1.32 (09-Nov-2002)
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
/dev/testfg/lvm1: 11/65536 files (0.0% non-contiguous), 8286/262144 blocks
[root@localhost root]# df -k
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/sda1 4024188 2294624 1525140 61% /
/dev/sda2 15607296 3299848 11514644 23% /home
none 115352 0 115352 0% /dev/shm
[root@localhost root]# resize2fs /dev/testfg/lvm1
resize2fs 1.32 (09-Nov-2002)
The filesystem on /dev/testfg/lvm1 is now 348160 blocks long.
[root@localhost root]# mount /dev/testfg/lvm1 /home/bak
[root@localhost root]# df -k
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/sda1 4024188 2294624 1525140 61% /
/dev/sda2 15607296 3299848 11514644 23% /home
none 115352 0 115352 0% /dev/shm
/dev/testfg/lvm1 337041 61 323054 1% /home/bak --已经识别出增长后的空间了。
12、扫尾工作
在我们添加了新的文件系统之后,一般会需要在开机之后自动挂载。因此我们可以把挂载的文件系统以及挂载目录添加到/etc/fstab中。
在/etc/fstab增加一个条目
/dev/testfg/lvm1 /home/ibm ext2 default 0 0
再补充一下关于fstab的格式说明:
etc/fstab 使用一种特殊语法格式。每行都包含六个字段。这些字段之间由空白键(空格键,tab键,或者两者混合使用)分隔。每个字段都有自己的含意:
* 第一个字段是对分区的描述,也就是设备文件的路径
* 第二个字段是分区挂载点,也就是分区应该挂载到的地方
* 第三个字段给出分区所用的文件系统
* 第四个字段给出的是挂载分区时mount命令所用的挂载选项。由于每个 文件系统都有自己的挂载选项,我们建议你阅读mount手册(man mount)以获得 所有挂载选项的列表。多个挂载选项之间是用逗号分隔的。
* 第五个字段是给dump使用的,用以决定这个分区是否需要dump。 一般情况下,你可以把该字段设为0(零)。
* 第六个字段是给fsck使用的,用以决定系统非正常关机之后文件系统的检查 顺序。根文件系统应该为1,而其它的应该为2(如果不需要 文件系统自检的话可以设为0)。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10867315/viewspace-194833/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/10867315/viewspace-194833/