首页 > 代码库 > LVM分区实例
LVM分区实例
大致思路:
1.创建物理卷,对磁盘分区进行初始化。
2.生成卷组。
3.在已有的卷组中创建逻辑卷。
4.格式化并挂载逻辑卷。
LVM(Logical Volume Manager)是基于内核的一种逻辑卷管理器。适合管理大存储设备,并允许用户动态调整文件系统的大小。快照功能可以帮助我们快速备份数据。
几个概念:
物理卷(PV):是LVM的逻辑存储块。
卷组(VG):是LVM逻辑概念上的磁盘设备。由单个或多个物理卷组和而成。
物理长度(PE):逻辑意义上磁盘的最小存储单元。LVM默认PE大小为4MB。
逻辑卷(LV):LVM逻辑意义上的分区。
示意图:
创建LVM分区实例:
[root@localhost xyz]# fdisk -cul Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders, total 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 Disk /dev/sda: 42.9 GB, 42949672960 bytes 255 heads, 63 sectors/track, 5221 cylinders, total 83886080 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000a5864 Device Boot Start End Blocks Id System /dev/sda1 * 2048 616447 307200 83 Linux /dev/sda2 616448 8939519 4161536 82 Linux swap / Solaris /dev/sda3 8939520 83886079 37473280 83 Linux [root@localhost xyz]# fdisk -cu /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0xe40f87d9. 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) Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First sector (2048-41943039, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +5G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First sector (10487808-41943039, default 10487808): Using default value 10487808 Last sector, +sectors or +size{K,M,G} (10487808-41943039, default 41943039): +5G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 3 First sector (20973568-41943039, default 20973568): Using default value 20973568 Last sector, +sectors or +size{K,M,G} (20973568-41943039, default 41943039): +5G Command (m for help): n Command action e extended p primary partition (1-4) e Selected partition 4 First sector (31459328-41943039, default 31459328): Using default value 31459328 Last sector, +sectors or +size{K,M,G} (31459328-41943039, default 41943039): Using default value 41943039 Command (m for help): n First sector (31461376-41943039, default 31461376): Using default value 31461376 Last sector, +sectors or +size{K,M,G} (31461376-41943039, default 41943039): Using default value 41943039 Command (m for help): t Partition number (1-5): 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-5): 2 Hex code (type L to list codes): 8e Changed system type of partition 2 to 8e (Linux LVM) Command (m for help): t Partition number (1-5): 3 Hex code (type L to list codes): 8e Changed system type of partition 3 to 8e (Linux LVM) Command (m for help): t Partition number (1-5): 5 Hex code (type L to list codes): 8e Changed system type of partition 5 to 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 xyz]# partprobe /dev/sdb
创建物理卷并查看结果:
[root@localhost xyz]# pvcreate /dev/sdb{1,2,3,5} Physical volume "/dev/sdb1" successfully created Physical volume "/dev/sdb2" successfully created Physical volume "/dev/sdb3" successfully created Physical volume "/dev/sdb5" successfully created [root@localhost xyz]# pvdisplay "/dev/sdb1" is a new physical volume of "5.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb1 VG Name PV Size 5.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID Z3Egil-bBKr-2Ays-663y-7saq-B1y5-WCV3uV "/dev/sdb2" is a new physical volume of "5.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb2 VG Name PV Size 5.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID vO8b86-bnyL-3OMU-HAXf-iP7b-tqYr-cHJg4E "/dev/sdb3" is a new physical volume of "5.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb3 VG Name PV Size 5.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID d5E3GE-DAdR-LYPO-Lyfh-z77H-6KJ1-HO09B5 "/dev/sdb5" is a new physical volume of "5.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb5 VG Name PV Size 5.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID DsdACT-L0x9-QeI1-PBQT-hG80-0Bav-MZZ0dt
创建卷组并查看结果:
[root@localhost xyz]# vgcreate test_vg /dev/sdb{1,2,3,5} Volume group "test_vg" successfully created
创建逻辑卷并查看结果:
[root@localhost xyz]# lvcreate -n test_web -L 10G test_vg Logical volume "test_web" created [root@localhost xyz]# lvcreate -n test_data -l 1024 test_vg Logical volume "test_data" created [root@localhost xyz]# lvdisplay --- Logical volume --- LV Path /dev/test_vg/test_web LV Name test_web VG Name test_vg LV UUID wtWJXi-b2Vh-GZFh-tVgW-b6iI-lsyM-hdmfyp LV Write Access read/write LV Creation host, time localhost.localdomain, 2014-07-30 20:14:35 -0700 LV Status available # open 0 LV Size 10.00 GiB Current LE 2560 Segments 3 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 --- Logical volume --- LV Path /dev/test_vg/test_data LV Name test_data VG Name test_vg LV UUID cEQ6Pw-Ffwt-JQkr-l1ZH-irH7-gL1z-XgVeYX LV Write Access read/write LV Creation host, time localhost.localdomain, 2014-07-30 20:15:09 -0700 LV Status available # open 0 LV Size 4.00 GiB Current LE 1024 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:1
格式化并挂载磁盘:
[root@localhost xyz]# mkfs.ext4 /dev/test_vg/test_web mke2fs 1.41.12 (17-May-2010) 文件系统标签= 操作系统:Linux 块大小=4096 (log=2) 分块大小=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 655360 inodes, 2621440 blocks 131072 blocks (5.00%) reserved for the super user 第一个数据块=0 Maximum filesystem blocks=2684354560 80 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632 正在写入inode表: 完成 Creating journal (32768 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 This filesystem will be automatically checked every 38 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@localhost xyz]# mkfs.ext4 /dev/test_vg/test_data mke2fs 1.41.12 (17-May-2010) 文件系统标签= 操作系统:Linux 块大小=4096 (log=2) 分块大小=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 262144 inodes, 1048576 blocks 52428 blocks (5.00%) reserved for the super user 第一个数据块=0 Maximum filesystem blocks=1073741824 32 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 正在写入inode表: 完成 Creating journal (32768 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 This filesystem will be automatically checked every 37 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@localhost xyz]# mkdir -p /test/{web,data} [root@localhost xyz]# cat >> /etc/fstab << EOF > /dev/test_vg/test_data /test/data ext4 defaults 0 0 > /dev/test_vg/test_web /test/web ext4 defaults 00 > EOF [root@localhost xyz]# mount -a [root@localhost xyz]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 36G 5.0G 29G 15% / tmpfs 947M 232K 947M 1% /dev/shm /dev/sda1 291M 62M 215M 23% /boot /dev/mapper/test_vg-test_web 9.9G 151M 9.2G 2% /test/web /dev/mapper/test_vg-test_data 4.0G 136M 3.7G 4% /test/data
修改LVM分区容量:
[root@localhost xyz]# lvextend -L +1G /dev/test_vg/test_data Extending logical volume test_data to 5.00 GiB Logical volume test_data successfully resized [root@localhost xyz]# resize2fs /dev/test_vg/test_data resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/test_vg/test_data is mounted on /test/data; on-line resizing required old desc_blocks = 1, new_desc_blocks = 1 Performing an on-line resize of /dev/test_vg/test_data to 1310720 (4k) blocks. The filesystem on /dev/test_vg/test_data is now 1310720 blocks long. [root@localhost xyz]# df-h bash: df-h: command not found [root@localhost xyz]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 36G 5.0G 29G 15% / tmpfs 947M 232K 947M 1% /dev/shm /dev/sda1 291M 62M 215M 23% /boot /dev/mapper/test_vg-test_web 9.9G 151M 9.2G 2% /test/web /dev/mapper/test_vg-test_data 5.0G 136M 4.6G 3% /test/data
删除LVM分区:
[root@localhost xyz]# umount /dev/test_vg/test_data [root@localhost xyz]# umount /dev/test_vg/test_web [root@localhost xyz]# lvremove /dev/test_vg/test_data Do you really want to remove active logical volume test_data? [y/n]: y Logical volume "test_data" successfully removed [root@localhost xyz]# lvremove /dev/test_vg/test_web Do you really want to remove active logical volume test_web? [y/n]: y Logical volume "test_web" successfully removed [root@localhost xyz]# vgremove test_vg Volume group "test_vg" successfully removed [root@localhost xyz]# pvremove /dev/sdb{1,2,3,5} Labels on physical volume "/dev/sdb1" successfully wiped Labels on physical volume "/dev/sdb2" successfully wiped Labels on physical volume "/dev/sdb3" successfully wiped Labels on physical volume "/dev/sdb5" successfully wiped
本文出自 “noogel” 博客,请务必保留此出处http://noogel.blog.51cto.com/9222825/1533378
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。