使用逻辑卷管理灵活存储

使用逻辑卷管理灵活存储

作者:LAMP小白  点击:1645  发布日期:2014-06-02 01:54:49  返回列表

查看LVM定义

物理分区或磁盘是LVM的第一构建块。这些可以是分区、完整磁盘、raid集或SAN磁盘。

物理卷是LVM所使用的基础“物理”存储。这通常是块设备、例如分区或完整磁盘。设备必须初始化为LVM物理卷,才能与LVM结合使用。

卷组是存储池,由一个或多个卷组成。

物理区块是物理卷中存储的小型数据区块,用作LVM存储的后端。

逻辑区块映射到物理区块,构成LVM存储的前端。默认情况下,每个逻辑区块映射到一个物理区块,启用某些选项将更改此映射。例如:镜像会导致每个逻辑区块映射到两个物理区块。

逻辑卷是逻辑区块组。逻辑卷可以像硬盘驱动器分区一样使用。

使用命令行工具实施LVM存储


准备物理卷


[root@MIO-02 ~]# fdisk -cu /dev/vda

Command (m for help): p

Disk /dev/vda: 8589 MB, 8589934592 bytes
16 heads, 63 sectors/track, 16644 cylinders, total 16777216 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: 0x0005b3b1

  Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048      411647      204800   83  Linux
/dev/vda2          411648     8800255     4194304   8e  Linux LVM
/dev/vda3         8800256    10897407     1048576   82  Linux swap / Solaris
/dev/vda4        10897408    16777215     2939904    5  Extended
/dev/vda5        10899456    12996607     1048576   83  Linux
/dev/vda6        12998656    14022655      512000   83  Linux

Command (m for help): n
First sector (14024704-16777215, default 14024704):
Using default value 14024704
Last sector, +sectors or +size{K,M,G} (14024704-16777215, default 16777215): +500M

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@MIO-02 ~]# partx /dev/vda
[root@MIO-02 ~]# pvcreate /dev/vda7
 Physical volume "/dev/vda7" successfully created

pvcreate用于初始化分区(或其他物理设备),使其作为物理卷与LVM结合使用。直接在物理卷中创建用于存储LVM配置数据的表头。


创建卷组


创建和使用新逻辑卷

[root@MIO-02 ~]# vgcreate new01 /dev/vda7
 Volume group "new01" successfully created

创建和使用新逻辑卷
[root@MIO-02 ~]# lvcreate -n newname -L 480M new01
 Logical volume "newname" created
[root@MIO-02 ~]# mkfs.ext4 /dev/new01/newname
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
122880 inodes, 491520 blocks
24576 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
60 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, 401409

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

This filesystem will be automatically checked every 32 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@MIO-02 ~]# mkdir /newname_mount
[root@MIO-02 ~]# mount /dev/new01/newname /newname_mount/


查看LVM状态信息

[root@MIO-02 ~]# pvdisplay
 --- Physical volume ---
 PV Name               /dev/vda7
 VG Name               new01
 PV Size               500.00 MiB / not usable 4.00 MiB
 Allocatable           yes
 PE Size               4.00 MiB
 Total PE              124
 Free PE               4
 Allocated PE          120
 PV UUID               akffb5-5ODW-L75m-dkoh-U6Tq-w3Bd-6Jkvtu

 --- Physical volume ---
 PV Name               /dev/vda2
 VG Name               vg_mio02
 PV Size               4.00 GiB / not usable 4.00 MiB
 Allocatable           yes (but full)
 PE Size               4.00 MiB
 Total PE              1023
 Free PE               0
 Allocated PE          1023
 PV UUID               pwEb4L-7MOu-fSos-2JPF-nEPM-kjKo-wOGDnM

 --- Physical volume ---
 PV Name               /dev/vda5
 VG Name               vg_mio02
 PV Size               1.00 GiB / not usable 4.00 MiB
 Allocatable           yes (but full)
 PE Size               4.00 MiB
 Total PE              255
 Free PE               0
 Allocated PE          255
 PV UUID               HhGzm6-TP8A-udjb-533a-auDC-jI0f-7LbnP1

[root@MIO-02 ~]# vgdisplay
 --- Volume group ---
 VG Name               new01
 System ID            
 Format                lvm2
 Metadata Areas        1
 Metadata Sequence No  2
 VG Access             read/write
 VG Status             resizable
 MAX LV                0
 Cur LV                1
 Open LV               1
 Max PV                0
 Cur PV                1
 Act PV                1
 VG Size               496.00 MiB
 PE Size               4.00 MiB
 Total PE              124
 Alloc PE / Size       120 / 480.00 MiB
 Free  PE / Size       4 / 16.00 MiB
 VG UUID               gYCRi5-TzBB-ZVtv-yABc-jruQ-zSLM-LkvBp0

 --- Volume group ---
 VG Name               vg_mio02
 System ID            
 Format                lvm2
 Metadata Areas        2
 Metadata Sequence No  4
 VG Access             read/write
 VG Status             resizable
 MAX LV                0
 Cur LV                1
 Open LV               1
 Max PV                0
 Cur PV                2
 Act PV                2
 VG Size               4.99 GiB
 PE Size               4.00 MiB
 Total PE              1278
 Alloc PE / Size       1278 / 4.99 GiB
 Free  PE / Size       0 / 0  
 VG UUID               ZDh2Ol-B4th-z6JS-Ngg7-RIdM-DTEP-fDc0Nk

[root@MIO-02 ~]# lvdisplay
 --- Logical volume ---
 LV Path                /dev/new01/newname
 LV Name                newname
 VG Name                new01
 LV UUID                c321R4-NtsL-flM4-daTT-iD1q-lxTE-5CCi7i
 LV Write Access        read/write
 LV Creation host, time MIO-02, 2014-06-01 23:14:56 +0800
 LV Status              available
 # open                 1
 LV Size                480.00 MiB
 Current LE             120
 Segments               1
 Allocation             inherit
 Read ahead sectors     auto
 - currently set to     256
 Block device           253:1

 --- Logical volume ---
 LV Path                /dev/vg_mio02/LogVol00
 LV Name                LogVol00
 VG Name                vg_mio02
 LV UUID                wO3M2x-hYEH-cdIt-GmO6-M683-1dCd-AReKb4
 LV Write Access        read/write
 LV Creation host, time MIO-02, 2014-05-14 22:47:19 +0800
 LV Status              available
 # open                 1
 LV Size                4.99 GiB
 Current LE             1278
 Segments               2
 Allocation             inherit
 Read ahead sectors     auto
 - currently set to     256
 Block device           253:0


扩展逻辑卷和ext4文件系统


[root@MIO-02 ~]# lvextend -l 124 /dev/new01/newname
 Extending logical volume newname to 496.00 MiB
 Logical volume newname successfully resized
[root@MIO-02 ~]# resize2fs -p /dev/new01/newname
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/new01/newname is mounted on /newname_mount; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 2
Performing an on-line resize of /dev/new01/newname to 507904 (1k) blocks.
The filesystem on /dev/new01/newname is now 507904 blocks long.


减小文件系统和逻辑卷


[root@MIO-02 ~]# umount /newname_mount/
[root@MIO-02 ~]# fsck -f /dev/new01/newname
fsck from util-linux-ng 2.17.2
e2fsck 1.41.12 (17-May-2010)
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/mapper/new01-newname: 11/126976 files (0.0% non-contiguous), 26534/507904 blocks
[root@MIO-02 ~]# resize2fs -p /dev/new01/newname 200M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/new01/newname to 204800 (1k) blocks.
Begin pass 3 (max = 62)
Scanning inode table          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/new01/newname is now 204800 blocks long.

[root@MIO-02 ~]# lvreduce -L 200M /dev/new01/newname
 WARNING: Reducing active logical volume to 200.00 MiB
 THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce newname? [y/n]: y
 Reducing logical volume newname to 200.00 MiB
 Logical volume newname successfully resized


扩展和缩减卷组


扩展卷组
[root@MIO-02 ~]# pvcreate /dev/vda8
 Physical volume "/dev/vda8" successfully created
[root@MIO-02 ~]# vgextend new01 /dev/vda8
 Volume group "new01" successfully extended
[root@MIO-02 ~]# vgdisplay


缩减卷组
[root@MIO-02 /]# pvmove /dev/vda8
 No data to move for new01
[root@MIO-02 /]# vgdisplay
 --- Volume group ---
 VG Name               new01
 System ID            
 Format                lvm2
 Metadata Areas        2
 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                2
 Act PV                2
 VG Size               592.00 MiB
 PE Size               4.00 MiB
 Total PE              148
 Alloc PE / Size       25 / 100.00 MiB
 Free  PE / Size       123 / 492.00 MiB
 VG UUID               x1Xz0H-DICG-Zjzc-g7rY-aHzO-xdfR-ex1H2U

 --- Volume group ---
 VG Name               vg_mio02
 System ID            
 Format                lvm2
 Metadata Areas        2
 Metadata Sequence No  4
 VG Access             read/write
 VG Status             resizable
 MAX LV                0
 Cur LV                1
 Open LV               1
 Max PV                0
 Cur PV                2
 Act PV                2
 VG Size               4.99 GiB
 PE Size               4.00 MiB
 Total PE              1278
 Alloc PE / Size       1278 / 4.99 GiB
 Free  PE / Size       0 / 0  
 VG UUID               ZDh2Ol-B4th-z6JS-Ngg7-RIdM-DTEP-fDc0Nk

[root@MIO-02 /]# vgreduce new01 /dev/vda8
 Removed "/dev/vda8" from volume group "new01"
[root@MIO-02 /]# vgdisplay new01
 --- Volume group ---
 VG Name               new01
 System ID            
 Format                lvm2
 Metadata Areas        1
 Metadata Sequence No  4
 VG Access             read/write
 VG Status             resizable
 MAX LV                0
 Cur LV                1
 Open LV               1
 Max PV                0
 Cur PV                1
 Act PV                1
 VG Size               496.00 MiB
 PE Size               4.00 MiB
 Total PE              124
 Alloc PE / Size       25 / 100.00 MiB
 Free  PE / Size       99 / 396.00 MiB
 VG UUID               x1Xz0H-DICG-Zjzc-g7rY-aHzO-xdfR-ex1H2U


创建快照便于数据备份

[root@MIO-02 /]# lvcreate -s -n snaplv -L 20M /dev/new01/lv01
 Logical volume "snaplv" created
[root@MIO-02 /]# mkdir /snapdir
[root@MIO-02 /]# mount -o ro /dev/new01/snaplv /snapdir/
[root@MIO-02 /]# cd /snapdir/
[root@MIO-02 snapdir]# ll
total 13
-rw-r--r--. 1 root root     0 Jun  1 23:46 123
drwx------. 2 root root 12288 Jun  1 23:46 lost+found
[root@MIO-02 snapdir]# lvs /dev/new01/snaplv
 LV     VG    Attr      LSize  Pool Origin Data%  Move Log Cpy%Sync Convert
 snaplv new01 swi-aos-- 20.00m      lv01     0.06                          
[root@MIO-02 snapdir]# umount /snapdir/
umount: /snapdir: device is busy.
       (In some cases useful info about processes that use
        the device is found by lsof(8) or fuser(1))
[root@MIO-02 snapdir]# cd /
[root@MIO-02 /]# umount /snapdir/
[root@MIO-02 /]# lvremove /dev/new01/snaplv
Do you really want to remove active logical volume snaplv? [y/n]: y
 Logical volume "snaplv" successfully removed
[root@MIO-02 /]#







上一篇:SETFACL文件目录授权和继承 下一篇:快递查询API
0