• Linux中的LVM


    逻辑卷管理器,通过将另外一个硬盘上的分区加到已有文件系统,来动态地向已有文件系统添加空间的方法。

    逻辑卷管理的核心是处理安装在系统上的硬盘分区。在逻辑卷管理的世界里,硬盘称作物理卷(Physical Volume,PV).每个物理卷都会映射到硬盘上创建的某一个物理分区。

    多个物理卷元素集中在一起可以组成一个卷组(Volume Group,VG)。逻辑卷管理系统会把卷组当做物理硬盘一样对待,但事实上卷组可能是由分布多个物理硬盘上的多个物理分区组成的。卷组提供了一个创建逻辑分区的平台,而这些逻辑分区事实上包含了文件系统。

    整个结构中的最后一层是逻辑卷(Logic Volume,LV)。逻辑卷为Linux提供了创建文件系统的分区环境,作用类似于到目前为止我们一直在探讨的Linux中的物理硬盘。Linux系统将逻辑卷当做物理分区对待。可以使用任意一种标准Linux文件系统来格式化逻辑卷,然后再将它在某个挂载点添加进入Linux虚拟目录中。

    一.创建LINUX LVM

    1、定义物理卷

    [root@TP-CW-TS-DB-01 ~]# 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): t
    Partition number (1-6): 8e
    Value out of range.
    Partition number (1-6): quit
    Partition number (1-6): ^C
    [root@TP-CW-TS-DB-01 ~]# 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): t
    Partition number (1-6): 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-6): 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-6): 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-6): 4
    Hex code (type L to list codes): 8e
    You cannot change a partition into an extended one or vice versa
    Delete it first.
    Command (m for help): t
    Partition number (1-6): 5
    Hex code (type L to list codes): 8e
    Changed system type of partition 5 to 8e (Linux LVM)
    Command (m for help): t
    Partition number (1-6): 6
    Hex code (type L to list codes): 8e
    Changed system type of partition 6 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@TP-CW-TS-DB-01 ~]# 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: 300.0 GB, 300000000000 bytes
    255 heads, 63 sectors/track, 36472 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: 0xc82dffc2
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1               1          30      240943+  8e  Linux LVM
    /dev/sdb2              31        2642    20980890   8e  Linux LVM
    /dev/sdb3            2643        3948    10490445   8e  Linux LVM
    /dev/sdb4            3949        6560    20980890    5  Extended
    /dev/sdb5            3949        5254    10490413+  8e  Linux LVM
    /dev/sdb6            5255        6560    10490413+  8e  Linux LVM

    用来sdb的6个分区转换成Linux LVM分区,8e分区类型表示这个分区将会被用做Linux LVM系统的一部分,而不是一个直接的文件系统,

    2、创建PV

    [root@TP-CW-TS-DB-01 ~]# pvcreate /dev/sdb5
      Physical volume "/dev/sdb5" successfully created
    [root@TP-CW-TS-DB-01 ~]# pvcreate /dev/sdb6
      Physical volume "/dev/sdb6" successfully created
    [root@TP-CW-TS-DB-01 ~]# pvs
      /dev/drbd0: open failed: Wrong medium type
      PV         VG       Fmt  Attr PSize  PFree
      /dev/sda2  VolGroup lvm2 a-   67.88g     0
      /dev/sdb5           lvm2 a-   10.00g 10.00g
      /dev/sdb6           lvm2 a-   10.00g 10.00g

    3.创建VGS

    [root@TP-CW-TS-DB-01 ~]# vgcreate onlinevg /dev/sdb5
      Volume group "onlinevg" successfully created
    [root@TP-CW-TS-DB-01 ~]# vgcreate onlinevg /dev/sdb6
      A volume group called onlinevg already exists.
    [root@TP-CW-TS-DB-01 ~]# vgcreate onlinevg /dev/sdb{5,6}
      Volume group "onlinevg" successfully created
    [root@TP-CW-TS-DB-01 ~]# vgs
      VG       #PV #LV #SN Attr   VSize  VFree
      VolGroup   1   3   0 wz--n- 67.88g     0
      onlinevg   2   0   0 wz--n- 20.00g 20.00g

    4、创建LV

    [root@TP-CW-TS-DB-01 ~]# lvcreate -L 5G -n mylv onlinevg
      Logical volume "mylv" created
    [root@TP-CW-TS-DB-01 ~]# lvs
      LV      VG       Attr   LSize  Origin Snap%  Move Log Copy%  Convert
      lv_home VolGroup -wi-ao 17.81g                                 
      lv_root VolGroup -wi-ao 32.36g                                 
      lv_swap VolGroup -wi-ao 17.70g                                 
      mylv    onlinevg -wi-a-  5.00g

    逻辑卷的设备文件:/dev/VG_NAME/LV_NAME

    [root@TP-CW-TS-DB-01 ~]# ls /dev/onlinevg/mylv
    /dev/onlinevg/mylv
    [root@TP-CW-TS-DB-01 ~]# ls -l /dev/onlinevg/mylv
    lrwxrwxrwx. 1 root root 7 Oct 12 16:21 /dev/onlinevg/mylv -> ../dm-3

    5、格式化LV

    mke2fs -t ext4 /dev/onlinevg/mylv
    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 32 mounts or
    180 days, whichever comes first.  Use tune2fs -c or -i to override.

    6、挂载分区

    [root@TP-CW-TS-DB-01 ~]# mount /dev/onlinevg/mylv /mnt/
    [root@TP-CW-TS-DB-01 ~]# df -h
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-lv_root
                           32G  3.7G   27G  13% /
    tmpfs                 7.8G     0  7.8G   0% /dev/shm
    /dev/sda1             485M   49M  411M  11% /boot
    /dev/mapper/VolGroup-lv_home
                           18G  172M   17G   2% /home
    /dev/mapper/onlinevg-mylv
                          5.0G  138M  4.6G   3% /mnt

    二、扩展VG

    1、首先在创建一个PV

    [root@TP-CW-TS-DB-01 mnt]# pvcreate /dev/sdb3
      Physical volume "/dev/sdb3" successfully created

    2、通过vgcreatend命令扩展VG

    [root@TP-CW-TS-DB-01 mnt]# vgs
      VG       #PV #LV #SN Attr   VSize  VFree
      VolGroup   1   3   0 wz--n- 67.88g     0
      onlinevg   2   1   0 wz--n- 20.00g 15.00g
    [root@TP-CW-TS-DB-01 mnt]# vgextend onlinevg /dev/sdb
    sdb   sdb1  sdb2  sdb3  sdb4  sdb5  sdb6
    [root@TP-CW-TS-DB-01 mnt]# vgextend onlinevg /dev/sdb3
      Volume group "onlinevg" successfully extended
    [root@TP-CW-TS-DB-01 mnt]# vgs
      VG       #PV #LV #SN Attr   VSize  VFree
      VolGroup   1   3   0 wz--n- 67.88g     0
      onlinevg   3   1   0 wz--n- 30.00g 25.00g

    通过命令查看onlinevg扩展到了30G

    [root@TP-CW-TS-DB-01 mnt]# pvs
      PV         VG       Fmt  Attr PSize  PFree
      /dev/sda2  VolGroup lvm2 a-   67.88g     0
      /dev/sdb3  onlinevg lvm2 a-   10.00g 10.00g
      /dev/sdb5  onlinevg lvm2 a-   10.00g  5.00g
      /dev/sdb6  onlinevg lvm2 a-   10.00g 10.00g

    三、缩减VG

    1、确定要移除的PV(/dev/sdb5)

    2、利用pvmove将/dev/sdb5中的数据移到物理卷上

    pvmove /dev/sdb5
    /dev/sdb5:Mover:100.0%

    3、从卷组中将缩减vg

    [root@TP-CW-TS-DB-01 ~]# vgreduce onlinevg /dev/sdb5
      Removed "/dev/sdb5" from volume group "onlinevg"
    [root@TP-CW-TS-DB-01 ~]# vgs
      VG       #PV #LV #SN Attr   VSize  VFree
      VolGroup   1   3   0 wz--n- 67.88g     0
      onlinevg   2   1   0 wz--n- 20.00g 15.00g

    4、删除/dev/sdb5

    [root@TP-CW-TS-DB-01 ~]# pvremove /dev/sdb5
      Labels on physical volume "/dev/sdb5" successfully wiped

    四、扩展逻辑卷

    1、扩展物理边界

    [root@TP-CW-TS-DB-01 ~]# lvextend -L +3G /dev/onlinevg/mylv
      Extending logical volume mylv to 8.00 GiB
      Logical volume mylv successfully resized

    注意:这个是候文件系统挂载的/mnt大小不会有任何变化

    2、扩展逻辑边界

    [root@TP-CW-TS-DB-01 ~]# resize2fs /dev/onlinevg/mylv
    resize2fs 1.41.12 (17-May-2010)
    Filesystem at /dev/onlinevg/mylv is mounted on /mnt; on-line resizing required
    old desc_blocks = 1, new_desc_blocks = 1
    Performing an on-line resize of /dev/onlinevg/mylv to 2097152 (4k) blocks.
    The filesystem on /dev/onlinevg/mylv is now 2097152 blocks long.
    [root@TP-CW-TS-DB-01 ~]# df -h
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-lv_root
                           32G  3.7G   27G  13% /
    tmpfs                 7.8G     0  7.8G   0% /dev/shm
    /dev/sda1             485M   49M  411M  11% /boot
    /dev/mapper/VolGroup-lv_home
                           18G  172M   17G   2% /home
    /dev/mapper/onlinevg-mylv
                          7.9G  140M  7.4G   2% /mnt

    五、缩减逻辑卷

    1、卸载并强行检测文件系统:

    umount /mnt

    [root@TP-CW-TS-DB-01 ~]# e2fsck -f /dev/onlinevg/mylv
    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/onlinevg/mylv: 12/524288 files (0.0% non-contiguous), 68564/2097152 blocks

    2、缩减逻辑边界

    [root@TP-CW-TS-DB-01 ~]# resize2fs /dev/onlinevg/mylv 3G
    resize2fs 1.41.12 (17-May-2010)
    Resizing the filesystem on /dev/onlinevg/mylv to 786432 (4k) blocks.
    The filesystem on /dev/onlinevg/mylv is now 786432 blocks long.

    3、缩减物理边界

    [root@TP-CW-TS-DB-01 ~]# lvreduce -L 3G /dev/onlinevg/mylv
      WARNING: Reducing active logical volume to 3.00 GiB
      THIS MAY DESTROY YOUR DATA (filesystem etc.)
    Do you really want to reduce mylv? [y/n]: y
      Reducing logical volume mylv to 3.00 GiB
      Logical volume mylv successfully resized
    [root@TP-CW-TS-DB-01 ~]# mount /dev/onlinevg/mylv /mnt
    [root@TP-CW-TS-DB-01 ~]# df -h
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-lv_root
                           32G  3.7G   27G  13% /
    tmpfs                 7.8G     0  7.8G   0% /dev/shm
    /dev/sda1             485M   49M  411M  11% /boot
    /dev/mapper/VolGroup-lv_home
                           18G  172M   17G   2% /home
    /dev/mapper/onlinevg-mylv
                          3.0G  136M  2.7G   5% /mnt

    六、创建快照卷

    lvcreate

    -s:快照卷:

    -p r:只读

    -n:定义快照

    [root@TP-CW-TS-DB-01 dev]# lvcreate -L 100M -s -p r -n mylv-snap /dev/onlinevg/mylv
     [root@TP-CW-TS-DB-01 dev]# mount -o ro /dev/onlinevg/mylv-snap /media
    [root@TP-CW-TS-DB-01 dev]# df -h
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-lv_root
                           32G  3.7G   27G  13% /
    tmpfs                 7.8G     0  7.8G   0% /dev/shm
    /dev/sda1             485M   49M  411M  11% /boot
    /dev/mapper/VolGroup-lv_home
                           18G  172M   17G   2% /home
    /dev/mapper/onlinevg-mylv
                          3.0G  136M  2.7G   5% /mnt
    /dev/mapper/onlinevg-mylv--snap
                          3.0G  136M  2.7G   5% /media
     
  • 相关阅读:
    Pycharm在线/手动离线安装第三方库-以scapy为例(本地离线添加已经安装的第三方库通过添加Path实现)
    python+splinter实现12306网站刷票并自动购票流程
    利用RELK进行日志收集
    web安全之文件上传漏洞攻击与防范方法
    C# 使用 CancellationTokenSource 终止线程
    ASP.NET MVC 下拉框的传值的两种方式
    SQL Server(解决问题)已成功与服务器建立连接,但是在登录过程中发生错误。(provider: Shared Memory Provider, error:0
    C# .net中json字符串和对象之间的转化方法
    asp.net 未能加载文件或程序集“WebApi”或它的某一个依赖项。试图加载格式不正确的程序。
    将博客搬至CSDN
  • 原文地址:https://www.cnblogs.com/net2012/p/3365904.html
Copyright © 2020-2023  润新知