• Linux LVM 总结


    LVM全称是Logical Volume Manager. 它主要是实现硬盘容量的动态扩展. 一般用于存储数据量无法预估的场景, 例如Linux的根目录用lvm存储.

    LVM创建过程一般是这样的

    1. 硬盘先分区: 一般是一个硬盘一个分区, 或者是多个分区在一个硬盘

        命令: fdisk

    2. PV(Physcial Volumes)划分, 可以把几个硬盘分区化成PV

        命令: pvcreate, pvs, pvdisplay, pvremove, pvmove, pvscan

    3. VG(Volume Group), 把几个或所有PV划分成可用空间, 有就是一定大小的块, 块的大小可以自己定义, 默认是4M

        命令:vgcreate, vgs, vgdisplay, vgreduce, vgextend, vgscan

    4. LV(Logical Volumes), 逻辑卷划分, 将vg划分成大小不超过vg总量的lv

        命令: lvcreate, lvs, lvdisplay, lvremove, lvreduce, lvextend, lvscan

    5. 格式化磁盘

        命令: mke2fs

    6. 挂载

        命令: mount

    技术分享

    实例分别为2G, 3G, 4G的硬盘, 全部划分成pv, 其中的5G划分成lv, 并格式化成ext4.

    1. 三块硬盘分别分成3个区

    [root@centos ~]# fdisk /dev/sdb
    
    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): p
    
    Disk /dev/sdb: 53.7 GB, 53687091200 bytes
    255 heads, 63 sectors/track, 6527 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: 0x289452a0
    
       Device Boot      Start         End      Blocks   Id  System
    
    Command (m for help): n
    Command action
       e   extended
       p   primary partition (1-4)
    p
    Partition number (1-4): 1
    First cylinder (1-6527, default 1): 
    Using default value 1
    Last cylinder, +cylinders or +size{K,M,G} (1-6527, default 6527): +2G
    
    Command (m for help): n
    Command action
       e   extended
       p   primary partition (1-4)
    p
    Partition number (1-4): 2
    First cylinder (263-6527, default 263): 
    Using default value 263
    Last cylinder, +cylinders or +size{K,M,G} (263-6527, default 6527): +3G
    
    Command (m for help): n
    Command action
       e   extended
       p   primary partition (1-4)
    p
    Partition number (1-4): 3
    First cylinder (656-6527, default 656): 
    Using default value 656
    Last cylinder, +cylinders or +size{K,M,G} (656-6527, default 6527): +4G
    
    Command (m for help): w
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    Syncing disks.
    [root@centos ~]# fdisk -l /dev/sdb
    
    Disk /dev/sdb: 53.7 GB, 53687091200 bytes
    255 heads, 63 sectors/track, 6527 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: 0x289452a0
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1               1         262     2104483+  83  Linux
    /dev/sdb2             263         655     3156772+  83  Linux
    /dev/sdb3             656        1178     4200997+  83  Linux

    2. 三个分区全部转换成pv

    [root@centos ~]# pvcreate /dev/sdb{1,2,3}
      Physical volume "/dev/sdb1" successfully created
      Physical volume "/dev/sdb2" successfully created
      Physical volume "/dev/sdb3" successfully created
    [root@centos ~]# pvs
      PV         VG        Fmt  Attr PSize  PFree
      /dev/sda2  vg_centos lvm2 a--  49.51g    0 
      /dev/sdb1            lvm2 ---   2.01g 2.01g
      /dev/sdb2            lvm2 ---   3.01g 3.01g
      /dev/sdb3            lvm2 ---   4.01g 4.01g

    3. pv转换成vg并且取名testvg

    [root@centos ~]# vg testvg /dev/sdb{1,2,3}
    -bash: vg: command not found
    [root@centos ~]# vgcreate testvg /dev/sdb{1,2,3}
      Volume group "testvg" successfully created
    [root@centos ~]# vgs
      VG        #PV #LV #SN Attr   VSize  VFree
      testvg      3   0   0 wz--n-  9.02g 9.02g
      vg_centos   1   2   0 wz--n- 49.51g    0

    4. 划分成5G lv并且取名testlv

    [root@centos ~]# lvcreate testvg -L 5G -n testlv
      Logical volume "testlv" created
    [root@centos ~]# lvs
      LV      VG        Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
      testlv  testvg    -wi-a-----  5.00g                                                    
      lv_root vg_centos -wi-ao---- 47.57g                                                    
      lv_swap vg_centos -wi-ao----  1.94g

    5. 格式化新建的testlv为ext4

    [root@centos ~]# mke2fs -t ext4 /dev/testvg/testlv
    mke2fs 1.41.12 (17-May-2010)
    Filesystem label=
    OS type: Linux
    Block size=4096 (log=2)
    Fragment size=4096 (log=2)
    Stride=0 blocks, Stripe width=0 blocks
    327680 inodes, 1310720 blocks
    65536 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=1342177280
    40 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
    
    Writing inode tables: done                            
    Creating journal (32768 blocks): done
    Writing superblocks and filesystem accounting information: done
    
    This filesystem will be automatically checked every 28 mounts or
    180 days, whichever comes first.  Use tune2fs -c or -i to override.

    6. 加载新建的lv并确定4G

    [root@centos ~]# mount /dev/testvg/testlv /data
    [root@centos ~]# df -lh
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/vg_centos-lv_root
                           47G  4.7G   40G  11% /
    tmpfs                 495M   72K  495M   1% /dev/shm
    /dev/sda1             477M   29M  424M   7% /boot
    /dev/mapper/testvg-testlv
                          4.8G   10M  4.6G   1% /data

    需要几点

    1. 扩展物理边界可以直接加硬盘,pvcreate, vgextend, lvextend

    [root@centos ~]# lvextend -L 6G testlv
      Path required for Logical Volume "testlv"
      Please provide a volume group name
      Run `lvextend --help‘ for more information.
    [root@centos ~]# lvextend -L 6G /dev/testvg/testlv
      Size of logical volume testvg/testlv changed from 5.00 GiB (1280 extents) to 6.00 GiB (1536 extents).
      Logical volume testlv successfully resized

    2.缩减硬盘, 要先离线, 强制检测, 缩减逻辑空间, vgreduce

    [root@centos ~]# e2fsck -f /dev/testvg/testlv
    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/testvg/testlv: 12/327680 files (0.0% non-contiguous), 55950/1310720 blocks
    [root@centos ~]# resize2fs /dev/testvg/testlv 3G
    resize2fs 1.41.12 (17-May-2010)
    Resizing the filesystem on /dev/testvg/testlv to 786432 (4k) blocks.
    The filesystem on /dev/testvg/testlv is now 786432 blocks long.
    
    [root@centos ~]# lvreduce testlv -L 3G
      Path required for Logical Volume "testlv"
      Please provide a volume group name
      Run `lvreduce --help‘ for more information.
    [root@centos ~]# lvreduce -L 3G /dev/testvg/testlv
      WARNING: Reducing active logical volume to 3.00 GiB
      THIS MAY DESTROY YOUR DATA (filesystem etc.)
    Do you really want to reduce testlv? [y/n]: y  
      Size of logical volume testvg/testlv changed from 6.00 GiB (1536 extents) to 3.00 GiB (768 extents).
      Logical volume testlv successfully resized
    [root@centos ~]# df -lh
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/vg_centos-lv_root
                           47G  4.7G   40G  11% /
    tmpfs                 495M   72K  495M   1% /dev/shm
    /dev/sda1             477M   29M  424M   7% /boot
    
    #用pedisplay查出内个硬盘的快是free的
    [root@centos ~]# pvdisplay
      --- Physical volume ---
      PV Name               /dev/sdb1
      VG Name               testvg
      PV Size               2.01 GiB / not usable 3.16 MiB
      Allocatable           yes 
      PE Size               4.00 MiB
      Total PE              513
      Free PE               513
      Allocated PE          0
      PV UUID               QsN7uK-DTsA-IVdF-2TFY-jjcs-dN4E-UPa9XP
       
      --- Physical volume ---
      PV Name               /dev/sdb3
      VG Name               testvg
      PV Size               4.01 GiB / not usable 2.54 MiB
      Allocatable           yes 
      PE Size               4.00 MiB
      Total PE              1025
      Free PE               257
      Allocated PE          768
      PV UUID               2XqZ73-EBnb-yJF9-mIQQ-8VRg-46h3-T1fwLW
       
      --- Physical volume ---
      PV Name               /dev/sdb2
      VG Name               testvg
      PV Size               3.01 GiB / not usable 2.79 MiB
      Allocatable           yes 
      PE Size               4.00 MiB
      Total PE              770
      Free PE               770
      Allocated PE          0
      PV UUID               BRUkrH-rSgS-a3VD-2KRC-yxHV-Cd76-eI6MX4
       
      --- Physical volume ---
      PV Name               /dev/sda2
      VG Name               vg_centos
      PV Size               49.51 GiB / not usable 3.00 MiB
      Allocatable           yes (but full)
      PE Size               4.00 MiB
      Total PE              12674
      Free PE               0
      Allocated PE          12674
      PV UUID               CbCr1K-jw10-L3fC-Bsxm-Owql-zxQd-60cNab
      
      #从vg移除硬盘, 不能直接reduce vg先用用pe移除硬盘块数据
      [root@centos ~]# vgreduce testvg /dev/sdb1
        Physical volume "/dev/sdb3" still in use
      [root@centos ~]# pvmove /dev/sdb1
      No data to move for testvg
      [root@centos ~]# pvmove /dev/sdb2
      /dev/sdb2: Moved: 0.5%
      /dev/sdb2: Moved: 25.5%
      /dev/sdb2: Moved: 51.0%
      /dev/sdb2: Moved: 77.0%
      /dev/sdb2: Moved: 100.0%
        
      再reduce
      [root@centos ~]# vgreduce testvg /dev/sdb1
      Removed "/dev/sdb1" from volume group "testvg"
    [root@centos ~]# vgreduce testvg /dev/sdb2
      Removed "/dev/sdb2" from volume group "testvg"
    [root@centos ~]# vgreduce testvg /dev/sdb3

    3. lv的正式设备文件是在/dev/dm-*, 有两个符号连接文件指向这个文件/dev/vg名字/lv名字 和/dev/mapper/vg名字-lv名字

    [root@centos ~]# ll /dev/testvg/testlv
    lrwxrwxrwx. 1 root root 7 Sep 23 11:12 /dev/testvg/testlv -> ../dm-2
    [root@centos ~]# ll /dev/mapper/testvg-testlv
    lrwxrwxrwx. 1 root root 7 Sep 23 11:12 /dev/mapper/testvg-testlv -> ../dm-2

    补充:snapshot

    snapshot就是快照的意思, 它是实现来相同逻辑卷上的"备份"功能, 需要注意的是, snapshot不能取代一般意义上的备份. 

    快照的创建方式和创建lv一样, 只是要添加-s来指定快照卷.

    接着上面的立即来创建个128M快照卷, 并且挂载到/snap

    [root@centos ~]# lvcreate -L 128M -s -n lvsnap /dev/testvg/testlv
      Logical volume "lvsnap" created
    [root@centos ~]# mount /dev/testvg/lvsnap /snap

    注意: 创在快照卷的时候, 最好停止在lv上的所有操作, 以免数据混乱

  • 相关阅读:
    20145202 《信息安全系统设计基础》第7周学习总结(2)
    20145202 《信息安全系统设计基础》第7周学习总结(1)
    20145202 《信息安全系统设计基础》第6周学习总结(2)
    Y86模拟器的安装
    20145202 《信息安全系统设计基础》第6周学习总结(1)
    20145202 《信息安全系统设计基础》第5周学习总结(2)
    20145202 《信息安全系统设计基础》第5周学习总结(1)
    20145202 《信息安全系统设计基础》git安装
    20145202 《信息安全系统设计基础》第3周学习总结
    20145202 《信息安全系统设计基础》第2周学习总结
  • 原文地址:https://www.cnblogs.com/zknublx/p/9188341.html
Copyright © 2020-2023  润新知