感谢支持
我们一直在努力

Linux逻辑卷管理(LVM)

前言:LVM基本术语
 物理存储介质(The physical media)
 这里指系统的存储设备:硬盘,如:/dev/hda、/dev/sda等等,是存储系统最低层的存储单元。
 物理卷(physical volume)
 物理卷就是指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。
 卷组(Volume Group)
 LVM卷组类似于非LVM系统中的物理硬盘,其由物理卷组成。可以在卷组上创建一个或多个“LVM分区”(逻辑卷),LVM卷组由一个或多个物理卷组成。
 逻辑卷(logical volume)
 LVM的逻辑卷类似于非LVM系统中的硬盘分区,在逻辑卷之上可以建立文件系统(比如/home或者/usr等)。
 PE(physical extent)
 每一个物理卷被划分为称为PE(Physical Extents)的基本单元,具有唯一编号的PE是可以被LVM寻址的最小单元。PE的大小是可配置的,默认为4MB。
 LE(logical extent)
 逻辑卷也被划分为被称为LE(Logical Extents) 的可被寻址的基本单位。在同一个卷组中,LE的大小和PE是相同的,并且一一对应。
 
一、创建逻辑卷的步骤
 1)通过pvcreate命令将linux分区处理成物理卷(PV);
 2)通过vgcreate命令将创建好的物理卷处理成卷组(Vg);
 3)通过lvcreate命令将卷组分成若干个逻辑卷(Lv);
 之后我们可以对逻辑卷进行格式化,挂载,删除等操作,我们可以动态的调整逻辑卷的大小,并且该操作不会影响我们在逻辑卷(Lv)上的数据。
 
二、物理卷创建管理操作
 1、物理分区信息
 [root@node1 ~]# fdisk /dev/sdd
 
The number of cylinders for this disk is set to 2610.
 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/sdd: 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/sdd1 1 200 1606468+ 83 Linux
 /dev/sdd2 201 400 1606500 83 Linux
 /dev/sdd3 401 600 1606500 83 Linux
 /dev/sdd4 601 2610 16145325 5 Extended
 /dev/sdd5 601 800 1606468+ 83 Linux
 /dev/sdd6 801 1000 1606468+ 83 Linux
 /dev/sdd7 1001 1200 1606468+ 83 Linux
 /dev/sdd8 1201 1400 1606468+ 83 Linux
 /dev/sdd9 1600 1800 1614532+ 83 Linux
 /dev/sdd10 1401 1599 1598404+ 83 Linux
 /dev/sdd11 1801 2000 1606468+ 83 Linux
 /dev/sdd12 2001 2200 1606468+ 83 Linux
 /dev/sdd13 2201 2400 1606468+ 83 Linux
 /dev/sdd14 2401 2610 1686793+ 83 Linux
 
Partition table entries are not in disk order
 
2、创建物理卷
 [root@node1 ~]# pvcreate /dev/sdd*
 Device /dev/sdd not found (or ignored by filtering).
 Physical volume “/dev/sdd1″ successfully created
 Physical volume “/dev/sdd10″ successfully created
 Physical volume “/dev/sdd11″ successfully created
 Physical volume “/dev/sdd12″ successfully created
 Physical volume “/dev/sdd13″ successfully created
 Physical volume “/dev/sdd14″ successfully created
 Physical volume “/dev/sdd2″ successfully created
 Physical volume “/dev/sdd3″ successfully created
 Device /dev/sdd4 not found (or ignored by filtering).
 Physical volume “/dev/sdd5″ successfully created
 Physical volume “/dev/sdd6″ successfully created
 Physical volume “/dev/sdd7″ successfully created
 Physical volume “/dev/sdd8″ successfully created
 Physical volume “/dev/sdd9″ successfully created
 
3、删除物理卷
 [root@node1 ~]# pvremove /dev/sdd1
 Labels on physical volume “/dev/sdd1″ successfully wiped
 
4、添加物理卷
 [root@node1 ~]# pvcreate /dev/sdd1
 Physical volume “/dev/sdd1″ successfully created
 
5、查看物理卷信息
 [root@node1 ~]# pvscan
 PV /dev/sdd1 lvm2 [1.53 GB]
 PV /dev/sdd2 lvm2 [1.53 GB]
 PV /dev/sdd3 lvm2 [1.53 GB]
 PV /dev/sdd5 lvm2 [1.53 GB]
 PV /dev/sdd6 lvm2 [1.53 GB]
 PV /dev/sdd7 lvm2 [1.53 GB]
 PV /dev/sdd8 lvm2 [1.53 GB]
 PV /dev/sdd9 lvm2 [1.54 GB]
 PV /dev/sdd10 lvm2 [1.52 GB]
 PV /dev/sdd11 lvm2 [1.53 GB]
 PV /dev/sdd12 lvm2 [1.53 GB]
 PV /dev/sdd13 lvm2 [1.53 GB]
 PV /dev/sdd14 lvm2 [1.61 GB]
 Total: 13 [19.99 GB] / in use: 0 [0 ] / in no VG: 13 [19.99 GB]
 
6、查看物理卷详细参数
 root@node1 ~]# pvdisplay /dev/sdd5
“/dev/sdd5″ is a new physical volume of “1.53 GB”
 — NEW Physical volume —
PV Name /dev/sdd5
 VG Name
 PV Size 1.53 GB
 Allocatable NO
 PE Size (KByte) 0
 Total PE 0
 Free PE 0
 Allocated PE 0
 PV UUID 7Ms1jT-wxUI-2d7l-h24c-t17F-J9vL-fz2zHM二、卷组的创建管理
 1、创建卷组
 [root@node1 ~]# vgcreate -s 8M xifenfei /dev/sdd{1,2,3,5,6,7,8,9,10,11,12,13,14}
 Volume group “xifenfei” successfully created
 格式:vgcreate [-s <8M|16M|…>] xifenfei /dev/sdb{1,2,3…}
 -s:指定扩展块(PE)的大小,默认为4MB;(相当与磁盘上的簇,扩展逻辑卷的基本单位。 后面的值可以是8M 16M 32M 64M …..最多65532个扩展块。
 xifenfei:新创建的卷组的名字
 
2、查看卷组信息
 [root@node1 ~]# vgscan
 Reading all physical volumes. This may take a while…
Found volume group “xifenfei” using metadata type lvm2
 
[root@node1 ~]# pvscan
 PV /dev/sdd1 VG xifenfei lvm2 [1.53 GB / 1.53 GB free]
 PV /dev/sdd2 VG xifenfei lvm2 [1.53 GB / 1.53 GB free]
 PV /dev/sdd3 VG xifenfei lvm2 [1.53 GB / 1.53 GB free]
 PV /dev/sdd5 VG xifenfei lvm2 [1.53 GB / 1.53 GB free]
 PV /dev/sdd6 VG xifenfei lvm2 [1.53 GB / 1.53 GB free]
 PV /dev/sdd7 VG xifenfei lvm2 [1.53 GB / 1.53 GB free]
 PV /dev/sdd8 VG xifenfei lvm2 [1.53 GB / 1.53 GB free]
 PV /dev/sdd9 VG xifenfei lvm2 [1.54 GB / 1.54 GB free]
 PV /dev/sdd10 VG xifenfei lvm2 [1.52 GB / 1.52 GB free]
 PV /dev/sdd11 VG xifenfei lvm2 [1.53 GB / 1.53 GB free]
 PV /dev/sdd12 VG xifenfei lvm2 [1.53 GB / 1.53 GB free]
 PV /dev/sdd13 VG xifenfei lvm2 [1.53 GB / 1.53 GB free]
 PV /dev/sdd14 VG xifenfei lvm2 [1.60 GB / 1.60 GB free]
 Total: 13 [19.98 GB] / in use: 13 [19.98 GB] / in no VG: 0 [0 ]
 
3、查看卷组的详细参数
 [root@node1 ~]# vgdisplay /dev/xifenfei
— Volume group —
VG Name xifenfei
 System ID
 Format lvm2
 Metadata Areas 13
 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 13
 Act PV 13
 VG Size 19.98 GB
 PE Size 8.00 MB
 Total PE 2557
 Alloc PE / Size 0 / 0
 Free PE / Size 2557 / 19.98 GB
 VG UUID dGa5e5-Jjef-GhYN-jpP0-EHMj-pAK1-Y9jJG2
 
4、删除卷组中物理卷
 [root@node1 ~]# vgreduce xifenfei /dev/sdd3
 Removed “/dev/sdd3″ from volume group “xifenfei”
 
5、添加物理卷到卷组中
 [root@node1 ~]# vgextend xifenfei /dev/sdd3
 Volume group “xifenfei” successfully extended
 
6、删除卷组
 [root@node1 ~]# vgremove /dev/xifenfei
 Volume group “xifenfei” successfully removed四、逻辑卷的创建及管理
 1、创建逻辑卷
 1.1)
 [root@node1 ~]# lvcreate -L 2G -n data xifenfei
 Logical volume “data” created
 -L 2G:设置逻辑卷的大小为512M。
 -n data:设置逻辑卷的名字为data ;表示法:/dev/卷组名/data
 xifenfei :设置是有那个卷组生成的逻辑卷
 1.2)
 [root@node1 ~]# lvcreate -l 128 -n xff xifenfei
 Logical volume “xff” created
 -l 128:生成的逻辑卷使用128个扩展块,逻辑卷大小=32*PE(默认4M,本处为8M)则为1024M
 -n xff:逻辑卷的名字;
 xifenfei :设置是有那个卷组生成的逻辑卷
 
2、删除逻辑卷
 [root@node1 ~]# lvremove /dev/xifenfei/xff
 Do you really want to remove active logical volume “xff”? [y/n]: y
 Logical volume “xff” successfully removed
 
3、查看逻辑卷信息
 [root@node1 ~]# lvscan
 ACTIVE ‘/dev/xifenfei/data’ [2.00 GB] inherit
 ACTIVE ‘/dev/xifenfei/xff’ [1.00 GB] inherit
 
4、逻辑卷详细信息
 [root@node1 ~]# lvdisplay /dev/xifenfei/data
— Logical volume —
LV Name /dev/xifenfei/data
 VG Name xifenfei
 LV UUID 84pp2v-GnfP-X3cL-a3fj-q3Cs-FDjh-i2d02u
 LV Write Access read/write
 LV Status available
 # open 0
 LV Size 2.00 GB
 Current LE 256
 Segments 2
 Allocation inherit
 Read ahead sectors auto
– currently set to 256
 Block device 253:0
 
5、格式化逻辑卷
 [root@node1 ~]# mkfs.ext3 /dev/xifenfei/data
 mke2fs 1.35 (28-Feb-2004)
 Filesystem label=
 OS type: Linux
 Block size=4096 (log=2)
 Fragment size=4096 (log=2)
 262144 inodes, 524288 blocks
 26214 blocks (5.00%) reserved for the super user
 First data block=0
 Maximum filesystem blocks=536870912
 16 block groups
 32768 blocks per group, 32768 fragments per group
 16384 inodes per group
 Superblock backups stored on blocks:
 32768, 98304, 163840, 229376, 294912
 
Writing inode tables: done
 Creating journal (8192 blocks): done
 Writing superblocks and filesystem accounting information: done
 
This filesystem will be automatically checked every 29 mounts or
 180 days, whichever comes first. Use tune2fs -c or -i to override.
 
6、挂载逻辑卷
 [root@node1 ~]# mkdir /data
 [root@node1 ~]# mount /dev/xifenfei/data /data
 [root@node1 ~]# df -h
 Filesystem 容量 已用 可用 已用% 挂载点
 /dev/sda2 18G 9.3G 7.5G 56% /
 /dev/sda1 99M 13M 82M 14% /boot
 none 395M 0 395M 0% /dev/shm
 /dev/drbd0 1.6G 656M 810M 45% /opt/mysql
 /dev/mapper/xifenfei-data
 2.0G 36M 1.9G 2% /data
 
7、增大逻辑卷大小(因为减小实际情况中很少用,所以不做实验lvreduce)
 [root@node1 ~]# lvextend -L 5.5G /dev/xifenfei/data
 Extending logical volume data to 5.50 GB
 Logical volume data successfully resized
 [root@node1 ~]# resize2fs /dev/xifenfei/data
 resize2fs 1.35 (28-Feb-2004)
 /dev/xifenfei/data is mounted; can’t resize a mounted filesystem!
 
[root@node1 ~]# umount /data
 [root@node1 ~]# resize2fs /dev/xifenfei/data
 resize2fs 1.35 (28-Feb-2004)
 Please run ‘e2fsck -f /dev/xifenfei/data’ first.
 
[root@node1 ~]# e2fsck -f /dev/xifenfei/data
 e2fsck 1.35 (28-Feb-2004)
 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/xifenfei/data: 11/655360 files (9.1% non-contiguous), 29800/1310720 blocks
 [root@node1 ~]# resize2fs /dev/xifenfei/data
 resize2fs 1.35 (28-Feb-2004)
 Resizing the filesystem on /dev/xifenfei/data to 1441792 (4k) blocks.
 The filesystem on /dev/xifenfei/data is now 1441792 blocks long.
 
[root@node1 ~]# mount /dev/xifenfei/data /data
 [root@node1 ~]# df -h
 Filesystem 容量 已用 可用 已用% 挂载点
 /dev/sda2 18G 9.3G 7.5G 56% /
 /dev/sda1 99M 13M 82M 14% /boot
 none 395M 0 395M 0% /dev/shm
 /dev/drbd0 1.6G 656M 810M 45% /opt/mysql
 /dev/mapper/xifenfei-data
 5.5G 37M 5.3G 1% /data
 
8、删除逻辑卷
 [root@node1 ~]# lvremove /dev/xifenfei/xff
 Do you really want to remove active logical volume “xff”? [y/n]: y
 Logical volume “xff” successfully removed

赞(0) 打赏
转载请注明出处:服务器评测 » Linux逻辑卷管理(LVM)
分享到: 更多 (0)

听说打赏我的人,都进福布斯排行榜啦!

支付宝扫一扫打赏

微信扫一扫打赏