简介:
LVM ( Logical Volume Manager ) 逻辑卷管理
一、创建 LV
1、首先在你的虚拟机上添加一块新的硬盘用来做实验。
2、安装 lvm : yum -y install lvm2
3、查看新添加的磁盘
shell > fdisk -l Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000
## 其中,/dev/sdb 就是我新添加的磁盘了
4、创建物理分区
shell > fdisk /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x62beb6a4. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) WARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u'). 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 # 打印分区类型对应的 ID 号 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 # 更改分区 ID u change display/entry units v verify the partition table w write table to disk and exit # 保存退出 x extra functionality (experts only)
## 执行的命令是 fdisk /dev/sdb .
Command (m for help): n # 执行添加分区指令 Command action e extended p primary partition (1-4) p # 添加一个主分区 Partition number (1-4): 1 # 分区号为 1 First cylinder (1-2610, default 1): # 分区起始位置(直接回车) Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +1024M # 分区结束位置(+1024M 表示 1G 大小) Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (133-2610, default 133): Using default value 133 Last cylinder, +cylinders or +size{K,M,G} (133-2610, default 2610): +1024M Command (m for help): p # 执行打印分区表 Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x62beb6a4 Device Boot Start End Blocks Id System /dev/sdb1 1 132 1060258+ 83 Linux /dev/sdb2 133 264 1060290 83 Linux
## 可以看到分了两个区 /dev/sdb1 /dev/sdb2
Command (m for help): t # 执行更改分区 ID (83 表示Linux 普通分区、82 表示swap、8e 表示LVM) 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: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x62beb6a4 Device Boot Start End Blocks Id System /dev/sdb1 1 132 1060258+ 8e Linux LVM /dev/sdb2 133 264 1060290 8e Linux LVM
## 已经看到分区 ID 变为 8e ; 文件系统为 Linux LVM
Command (m for help): w # 执行保存退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. shell > fdisk -l /dev/sdb Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x62beb6a4 Device Boot Start End Blocks Id System /dev/sdb1 1 132 1060258+ 8e Linux LVM /dev/sdb2 133 264 1060290 8e Linux LVM
## 可以看到磁盘 /dev/sdb 有两个分区 /dev/sdb1 /dev/sdb2
5、创建 PV (物理卷)
shell > pvcreate /dev/sdb1 /dev/sdb2 Physical volume "/dev/sdb1" successfully created Physical volume "/dev/sdb2" successfully created
## 使用 pvcreate 创建 PV 后面跟想要创建的分区
shell > pvscan PV /dev/sda3 VG vg_study lvm2 [18.80 GiB / 0 free] PV /dev/sdb1 lvm2 [1.01 GiB] PV /dev/sdb2 lvm2 [1.01 GiB] Total: 3 [20.82 GiB] / in use: 1 [18.80 GiB] / in no VG: 2 [2.02 GiB]
## 使用 pvscan 、pvdisplay 、pvs 都可以查看 PV
## PV /dev/sdb1 pv /dev/sdb2 为我新创建的 PV 大小为 1G(PV /dev/sda3 为之前创建的属 VG vg_study )
6、创建 VG (卷组)
shell > vgcreate my_vg /dev/sdb1 /dev/sdb2 Volume group "my_vg" successfully created
## 使用 vgcreate 创建卷组,后面接 vg 名,接 pv
shell > vgdisplay --- Volume group --- VG Name my_vg 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 2.02 GiB PE Size 4.00 MiB Total PE 516 Alloc PE / Size 0 / 0 Free PE / Size 516 / 2.02 GiB VG UUID ynCJG8-ZIvI-9gBF-dO5K-aQws-N9WW-r26hch
## 同样可以使用 vgscan 、vgs 、vgdisplay 来查看 VG
## 可以看到 VG名为 my_vg 、VG 大小为 2G 、PE 大小 4M 等
7、创建 LV (逻辑卷)
shell > lvcreate -L 2G -n my_lv my_vg Logical volume "my_lv" created
## 使用 lvcreate 创建 LV ,-L 指定 LV大小,-n 指定 LV名称,后面接 VG名
shell > lvdisplay --- Logical volume --- LV Path /dev/my_vg/my_lv LV Name my_lv VG Name my_vg LV UUID j9Bduh-3TB9-Owhz-X3PX-fVeQ-bvJe-OAFBVp LV Write Access read/write LV Creation host, time study.localhost.localdomain, 2014-11-26 11:14:28 +0800 LV Status available # open 0 LV Size 2.00 GiB Current LE 512 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:1
## 同样可以使用 lvscan 、lvs 、lvdisplay 来查看 LV
## 可以看到 LV路径 /dev/my_vg/my_lv 、LV名 my_lv 、VG名 my_vg 、LV大小 2G 等
8、创建文件系统 (格式化)
shell > mkfs.ext4 /dev/my_vg/my_lv mke2fs 1.41.12 (17-May-2010) 文件系统标签= 操作系统:Linux 块大小=4096 (log=2) 分块大小=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 131072 inodes, 524288 blocks 26214 blocks (5.00%) reserved for the super user 第一个数据块=0 Maximum filesystem blocks=536870912 16 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912 正在写入inode表: 完成 Creating journal (16384 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 This filesystem will be automatically checked every 32 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
## 使用 mkfs.ext4 格式化为 ext4 文件系统
9、挂载使用
shell > mkdir /data shell > mount /dev/my_vg/my_lv /data/ shell > df -HT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vg_study-LogVol00 ext4 20G 1.4G 18G 8% / tmpfs tmpfs 521M 0 521M 0% /dev/shm /dev/sda1 ext4 204M 30M 164M 16% /boot /dev/mapper/my_vg-my_lv ext4 2.2G 71M 2.0G 4% /data
## 创建 /data 目录,将 /dev/my_vg/my_lv 挂载到 /data
shell > umount /data/ shell > blkid /dev/my_vg/my_lv /dev/my_vg/my_lv: UUID="a8cb322e-1872-4ea8-bfba-f21b678bc849" TYPE="ext4" shell > echo "UUID="a8cb322e-1872-4ea8-bfba-f21b678bc849" /data ext4 defaults 0 0" >> /etc/fstab shell > mount -a shell > df -HT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vg_study-LogVol00 ext4 20G 1.5G 18G 8% / tmpfs tmpfs 521M 0 521M 0% /dev/shm /dev/sda1 ext4 204M 30M 164M 16% /boot /dev/mapper/my_vg-my_lv ext4 2.2G 71M 2.0G 4% /data
## 也可以使用 blkid 生成 UUID 然后写入到 /etc/fstab 文件中,实现开机自动挂载
二、扩容 LV
1、首先创建物理分区(重点是这次创建后要格式化)
2、创建 PV
3、扩容 VG
shell > vgextend my_vg /dev/sdb3 Volume group "my_vg" successfully extended
## 使用 vgextend 指令扩展 VG ,后面接要扩展的 VG名 ,后面接将哪个 PV 扩展进去
shell > vgdisplay my_vg --- Volume group --- VG Name my_vg System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 3 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 3.02 GiB PE Size 4.00 MiB Total PE 774 Alloc PE / Size 512 / 2.00 GiB Free PE / Size 262 / 1.02 GiB VG UUID ynCJG8-ZIvI-9gBF-dO5K-aQws-N9WW-r26hch
## 可以使用 vgdisplay my_vg 来指定查看哪个 VG
## 可以看到 VG 总大小为 3G 、已使用 2G 、空闲 1G(因为我们扩进去 1G 的 PV)
4、扩容 LV
shell > lvextend -L +1G /dev/my_vg/my_lv Size of logical volume my_vg/my_lv changed from 2.00 GiB (512 extents) to 3.00 GiB (768 extents). Logical volume my_lv successfully resized
## 使用 lvextend 扩容 LV ,-L 指定扩多大 ,后面接 LV 实际路径 /dev/my_vg/my_lv
shell > lvdisplay /dev/my_vg/my_lv --- Logical volume --- LV Path /dev/my_vg/my_lv LV Name my_lv VG Name my_vg LV UUID j9Bduh-3TB9-Owhz-X3PX-fVeQ-bvJe-OAFBVp LV Write Access read/write LV Creation host, time study.localhost.localdomain, 2014-11-26 11:14:28 +0800 LV Status available # open 1 LV Size 3.00 GiB Current LE 768 Segments 3 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:1
## 可以使用 lvdisplay /dev/my_vg/my_lv 来查看 LV 大小
## LV 大小变为了 3G (原 2G)
5、投入使用
shell > df -HT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vg_study-LogVol00 ext4 20G 1.4G 18G 8% / tmpfs tmpfs 521M 0 521M 0% /dev/shm /dev/sda1 ext4 204M 30M 164M 16% /boot /dev/mapper/my_vg-my_lv ext4 2.2G 71M 2.0G 4% /data
## 我们发现分区 /data 大小还是 2G
shell > resize2fs /dev/my_vg/my_lv resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/my_vg/my_lv is mounted on /data; on-line resizing required old desc_blocks = 1, new_desc_blocks = 1 Performing an on-line resize of /dev/my_vg/my_lv to 786432 (4k) blocks. The filesystem on /dev/my_vg/my_lv is now 786432 blocks long. shell > df -HT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vg_study-LogVol00 ext4 20G 1.4G 18G 8% / tmpfs tmpfs 521M 0 521M 0% /dev/shm /dev/sda1 ext4 204M 30M 164M 16% /boot /dev/mapper/my_vg-my_lv ext4 3.2G 71M 3.0G 3% /data
## 使用 resize2fs /dev/my_vg/my_lv 重新计算分区大小后 发现扩容到了 3G
三、缩减 LV
1、模拟数据写入
shell > dd if=/dev/zero of=/data/block.db bs=1024M count=1 记录了1+0 的读入 记录了1+0 的写出 1073741824字节(1.1 GB)已复制,81.6381 秒,13.2 MB/秒
## 我们使用 dd 命令写了 1G 的数据到 /data
shell > cp /etc/passwd /data/ ## 复制这个文件的目的是验证数据是否丢失 shell > df -HT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vg_study-LogVol00 ext4 20G 1.4G 18G 8% / tmpfs tmpfs 521M 0 521M 0% /dev/shm /dev/sda1 ext4 204M 30M 164M 16% /boot /dev/mapper/my_vg-my_lv ext4 3.2G 1.2G 1.9G 39% /data
## 现在这个剩余空间 我们想把他撤出来 1G
2、首先检查一下文件系统
shell > umount /data/ ## 一定要先 umount shell > e2fsck -f /dev/my_vg/my_lv e2fsck 1.41.12 (17-May-2010) 第一步: 检查inode,块,和大小 第二步: 检查目录结构 第3步: 检查目录连接性 Pass 4: Checking reference counts 第5步: 检查簇概要信息 /dev/my_vg/my_lv: 13/196608 files (0.0% non-contiguous), 291646/786432 blocks
3、重新计算大小
shell > resize2fs /dev/my_vg/my_lv 2G resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/my_vg/my_lv to 524288 (4k) blocks. The filesystem on /dev/my_vg/my_lv is now 524288 blocks long.
## 重新定义大小为 2G
4、缩小 LV
shell > lvreduce -L -1G /dev/my_vg/my_lv WARNING: Reducing active logical volume to 2.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce my_lv? [y/n]: y Size of logical volume my_vg/my_lv changed from 3.00 GiB (768 extents) to 2.00 GiB (512 extents). Logical volume my_lv successfully resized
## 使用 lvreduce 来缩小 LV ,-L -1G 缩小 1G
shell > mount -a shell > df -HT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vg_study-LogVol00 ext4 20G 1.4G 18G 8% / tmpfs tmpfs 521M 0 521M 0% /dev/shm /dev/sda1 ext4 204M 30M 164M 16% /boot /dev/mapper/my_vg-my_lv ext4 2.2G 1.2G 863M 58% /data shell > ls /data/ block.db lost+found passwd
## 重新挂载分区,发现数据没有问题 (仅是测试,没有发现问题)
5、VG 中移除 PV
shell > vgdisplay my_vg --- Volume group --- VG Name my_vg 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 3.02 GiB PE Size 4.00 MiB Total PE 774 Alloc PE / Size 637 / 2.49 GiB Free PE / Size 137 / 548.00 MiB VG UUID ynCJG8-ZIvI-9gBF-dO5K-aQws-N9WW-r26hch
## 首先我们 vgdisplay my_vg ,发现 VG 空闲 548M
shell > pvdisplay --- Physical volume --- PV Name /dev/sdb1 VG Name my_vg PV Size 1.01 GiB / not usable 3.41 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 258 Free PE 0 Allocated PE 258 PV UUID JB8X8v-jBPU-eOiN-aEOU-TOig-tv01-SuHytd --- Physical volume --- PV Name /dev/sdb2 VG Name my_vg PV Size 1.01 GiB / not usable 3.44 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 258 Free PE 0 Allocated PE 258 PV UUID YXBMJJ-RyG3-z1xa-tlw6-piaI-cTgj-SIBIlz --- Physical volume --- PV Name /dev/sdb3 VG Name my_vg PV Size 1.01 GiB / not usable 3.44 MiB Allocatable yes PE Size 4.00 MiB Total PE 258 Free PE 137 Allocated PE 121 PV UUID jSpEdJ-fxZh-LP27-rFIM-0gnO-mL2x-PpBJpK
## 其次使用 pvdisplay 查看 PV 的使用情况发现:
/dev/sdb1 PE 空闲 0
/dev/sdb2 PE 空闲 0
/dev/sdb3 PE 空闲 137 (上图显示每个 PE 为 4M ,那么 /dev/sdb3 总空闲容量=137*4 = 548M)
## 正好等于 VG 的空闲大小,而 PV /dev/sdb3 大小为 1G ,也就是说有部分空间被占用,所以无法移除该 PV
shell > vgreduce my_vg /dev/sdb3 Physical volume "/dev/sdb3" still in use shell > umount /data/ shell > vgreduce my_vg /dev/sdb3 Physical volume "/dev/sdb3" still in use
## 提示正在被占用,无法移除( umount 分区也不行 )
shell > umount /data/ shell > e2fsck -f /dev/my_vg/my_lv e2fsck 1.41.12 (17-May-2010) 第一步: 检查inode,块,和大小 第二步: 检查目录结构 第3步: 检查目录连接性 Pass 4: Checking reference counts 第5步: 检查簇概要信息 /dev/my_vg/my_lv: 14/163840 files (0.0% non-contiguous), 289591/652288 blocks shell > resize2fs /dev/my_vg/my_lv 2G resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/my_vg/my_lv to 524288 (4k) blocks. The filesystem on /dev/my_vg/my_lv is now 524288 blocks long. shell > lvreduce -L -500M /dev/my_vg/my_lv WARNING: Reducing active logical volume to 2.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce my_lv? [y/n]: y Size of logical volume my_vg/my_lv changed from 2.49 GiB (637 extents) to 2.00 GiB (512 extents). Logical volume my_lv successfully resized
## 我们把 LV 缩小了 500M
shell > vgdisplay my_vg --- Volume group --- VG Name my_vg System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 9 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 0 Max PV 0 Cur PV 3 Act PV 3 VG Size 3.02 GiB PE Size 4.00 MiB Total PE 774 Alloc PE / Size 512 / 2.00 GiB Free PE / Size 262 / 1.02 GiB VG UUID ynCJG8-ZIvI-9gBF-dO5K-aQws-N9WW-r26hch
## 现在再看 VG 大小 空闲 1G
shell > pvdisplay --- Physical volume --- PV Name /dev/sdb3 VG Name my_vg PV Size 1.01 GiB / not usable 3.44 MiB Allocatable yes PE Size 4.00 MiB Total PE 258 Free PE 258 Allocated PE 0 PV UUID jSpEdJ-fxZh-LP27-rFIM-0gnO-mL2x-PpBJpK
## pvdisplay 也发现 /dev/sdb3 正好全部空闲
shell > vgreduce my_vg /dev/sdb3 Removed "/dev/sdb3" from volume group "my_vg"
## 已经成功将 PV /dev/sdb3 从 VG my_vg 中移除
shell > vgs VG #PV #LV #SN Attr VSize VFree my_vg 2 1 0 wz--n- 2.02g 16.00m
## 使用 vgs 可以看到 vg my_vg 中包含 PV 数为 2
6、删除 PV
shell > pvremove /dev/sdb3 Labels on physical volume "/dev/sdb3" successfully wiped
## 使用 pvremove 删除 PV /dev/sdb3
shell > pvs PV VG Fmt Attr PSize PFree /dev/sdb1 my_vg lvm2 a-- 1.01g 0 /dev/sdb2 my_vg lvm2 a-- 1.01g 16.00m
## 使用 pvs 看到只有两个 PV 了
## 然后 fdisk /dev/sdb 将 sdb3 的 ID 改为 83 ,保存
mkfs.ext4 /dev/sdb3 ,重新挂载到目录,至此 /dev/sdb3 成为普通分区
四、删除整个 lv vg pv
shell > umount /data/ shell > lvremove /dev/my_vg/my_lv Do you really want to remove active logical volume my_lv? [y/n]: y Logical volume "my_lv" successfully removed shell > vgremove my_vg Volume group "my_vg" successfully removed shell > pvremove /dev/sdb1 Labels on physical volume "/dev/sdb1" successfully wiped shell > pvremove /dev/sdb2 Labels on physical volume "/dev/sdb2" successfully wiped
## 删除比创建容易多了 ...