lvm基本应用

前言

一种技术要知其然,还要知其所以然

lvm简介

LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制。普通的磁盘分区管理方式在逻辑分区划分好之后就无法改变其大小,当一个逻辑分区存放不下某个文件时,这个文件因为受上层文件系统的限制,也不能跨越多个分区来存放,所以也不能同时放到别的磁盘上。而遇到出现某个分区空间耗尽时,解决的方法通常是使用符号链接,或者使用调整分区大小的工具,但这只是暂时解决办法,没有从根本上解决问题。随着Linux的逻辑卷管理功能的出现,这些问题都迎刃而解,用户在无需停机的情况下可以方便地调整各个分区大小。

lvm的基本术语

*物理存储介质(PhysicalStorageMedia) 指系统的物理存储设备:磁盘,如:/dev/hda、/dev/sda等,是存储系统最底层的存储单元。

*物理卷(Physical Volume,PV) 指磁盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。

*卷组(Volume Group,VG) 类似于非LVM系统中的物理磁盘,其由一个或多个物理卷PV组成。可以在卷组上创建一个或多个LV(逻辑卷)。

*逻辑卷(Logical Volume,LV) 类似于非LVM系统中的磁盘分区,逻辑卷建立在卷组VG之上。在逻辑卷LV之上可以建立文件系统(比如/home或者/usr等)。

*物理块(Physical Extent,PE) 每一个物理卷PV被划分为称为PE(Physical Extents)的基本单元,具有唯一编号的PE是可以被LVM寻址的最小单元。PE的大小是可配置的,默认为4MB。所以物理卷(PV)由大小等同的基本单元PE组成。

*逻辑块(Logical Extent,LE) 逻辑卷LV也被划分为可被寻址的基本单位,称为LE。在同一个卷组中,LE的大小和PE是相同的,并且一一对应。

LVM抽象模型

lvm基本应用

图所示LVM抽象模型,展示了PV、VG、LV三者之间关系:

和非LVM系统将包含分区信息的元数据保存在位于分区的起始位置的分区表中一样,逻辑卷以及卷组相关的元数据也是保存在位于物理卷起始处的VGDA(卷组描述符区域)中。

VGDA包括以下内容:PV描述符、VG描述符、LV描述符、和一些PE描述符。

系统启动LVM时激活VG,并将VGDA加载至内存,来识别LV的实际物理存储位置。当系统进行I/O操作时,就会根据VGDA建立的映射机制来访问实际的物理位置。

lvm的基本使用方法

pv管理工具:
    pvs:简要pv信息显示
    pvdisplay:显示pv的详细信息

    pvcreate /dev/DEVICE: 创建pv

vg管理工具:
    vgs:
    vgdisplay

    vgcreate  [-s #[kKmMgGtTpPeE]] VolumeGroupName  PhysicalDevicePath [PhysicalDevicePath...]
    vgextend  VolumeGroupName  PhysicalDevicePath [PhysicalDevicePath...]
    vgreduce  VolumeGroupName  PhysicalDevicePath [PhysicalDevicePath...]
        先做pvmove

    vgremove

lv管理工具:
    lvs
    lvdisplay

    lvcreate -L #[mMgGtT] -n NAME VolumeGroup

    lvremove /dev/VG_NAME/LV_NAME

扩展逻辑卷:
    # lvextend -L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME
    # resize2fs /dev/VG_NAME/LV_NAME

缩减逻辑卷:
    # umount /dev/VG_NAME/LV_NAME
    # e2fsck -f /dev/VG_NAME/LV_NAME
    # resize2fs /dev/VG_NAME/LV_NAME #[mMgGtT]
    # lvreduce -L [-]#[mMgGtT] /dev/VG_NAME/LV_NAME
    # mount

快照:snapshot
    lvcreate -L #[mMgGtT] -p r -s -n snapshot_lv_name original_lv_name

实验环境及步骤

采用centos6.7的环境。 lvm的工具包安装 yum install -y lvm

1:创建一个至少有两个PV组成的大小为20G的名为testvg的VG;要求PE大小为16MB, 而后在卷组中创建大小为5G的逻辑卷testlv;挂载至/users目录;

      先创建2个分区大小都为10g的pv,
     [root@king-liu tmp]# fdisk /dev/sda   

     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): n       #新建一个分区
     First cylinder (6602-10443, default 6602): 
     Using default value 6602
     Last cylinder, +cylinders or +size{K,M,G} (6602-10443, default 10443): +10G       #增加大小为10G

     Command (m for help): n    
     First cylinder (7908-10443, default 7908):     
     Using default value 7908
     Last cylinder, +cylinders or +size{K,M,G} (7908-10443, default 10443): +10G       

      Command (m for help): T   #分区类型要为8e linux LVM
      Partition number (1-12): 11
      Hex code (type L to list codes): 8e    
      Changed system type of partition 11 to 8e (Linux LVM)

      Command (m for help): t
      Partition number (1-12): 12   
      Hex code (type L to list codes): 8e
      Changed system type of partition 12 to 8e (Linux LVM)

      Command (m for help): p   查看分区

     Disk /dev/sda: 85.9 GB, 85899345920 bytes
     255 heads, 63 sectors/track, 10443 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: 0x0009fc3c

     Device Boot      Start         End      Blocks   Id  System
     /dev/sda1   *           1          64      512000   83  Linux
     Partition 1 does not end on cylinder boundary.
     /dev/sda2              64        2614    20480000   83  Linux
     /dev/sda3            2614        2678      512000   82  Linux swap / Solaris
     /dev/sda4            2678       10443    62378373+   5  Extended
     /dev/sda5            4640        5293     5253223+  fd  Linux raid autodetect
     /dev/sda6            5294        5947     5253223+  fd  Linux raid autodetect
     /dev/sda7            5948        6601     5253223+  8e  Linux LVM
     /dev/sda8            2678        3983    10488392   8e  Linux LVM
     /dev/sda9            3984        4637     5253223+  8e  Linux LVM
     /dev/sda10           4638        4639       16033+  83  Linux
     /dev/sda11           6602        7907    10490413+  8e  Linux LVM
     /dev/sda12           7908        9213    10490413+  8e  Linux LVM

     Partition table entries are not in disk order

     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: 设备或资源忙.
     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@king-liu tmp]# partx -a /dev/sda #内核重新读取一下硬盘
    [root@king-liu tmp]# partx -a /dev/sda #内核重新读取一下硬盘
    [root@king-liu tmp]# pvcreate /dev/sda{11,12}  #将分区创建为物理卷 pv
    Physical volume "/dev/sda11" successfully created
    Physical volume "/dev/sda12" successfully created
    [root@king-liu tmp]# pvs  #查看创建的物理卷 pvdisplay查看详细的物理卷
     PV         VG   Fmt  Attr PSize  PFree 
     /dev/sda11      lvm2 ---  10.00g 10.00g
     /dev/sda12      lvm2 ---  10.00g 10.00g
     [root@king-liu tmp]# vgcreate -s 16M testvg /dev/sda{11,12}  
          #将物理卷创建为卷组vg pe大小为16M 卷组名为testvg 
       Volume group "testvg" successfully created
     [root@king-liu tmp]# vgdisplay   查看详细的卷组
       --- Volume group ---
       VG Name               testvg
       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               20.00 GiB
     PE Size               16.00 MiB
     Total PE              1280
     Alloc PE / Size       0 / 0   
     Free  PE / Size       1280 / 20.00 GiB
     VG UUID               SgtewP-UMwa-RLY4-a8oq-XrF0-dzx7-F2Reyd
    [root@king-liu tmp]# lvcreate -L 5G -n testlv testvg
     #将卷组testvg创建为逻辑卷lv 大小为5G 名字为testlv
     Logical volume "testlv" created.
    [root@king-liu tmp]# mke2fs -j /dev/testvg/testlv 
    #将testlv格式化
    mke2fs 1.41.12 (17-May-2010)
    文件系统标签=
    操作系统:Linux
    块大小=4096 (log=2)
    分块大小=4096 (log=2)
    Stride=0 blocks, Stripe width=0 blocks
    327680 inodes, 1310720 blocks
    65536 blocks (5.00%) reserved for the super user
    第一个数据块=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

    正在写入inode表: 完成                            
    Creating journal (32768 blocks): 完成
    Writing superblocks and filesystem accounting information: 完成

    This filesystem will be automatically checked every 27 mounts or
    180 days, whichever comes first.  Use tune2fs -c or -i to override.
    [root@king-liu ~]# mkdir /users  #创建users文件夹
   [root@king-liu ~]# mount /dev/testvg/testlv /users/   
       #将/dev/testvg/testlv挂载到 /users/
   [root@king-liu ~]# mount #查看是否已挂载
   /dev/sda2 on / type ext4 (rw)
   proc on /proc type proc (rw)
   sysfs on /sys type sysfs (rw)
   devpts on /dev/pts type devpts (rw,gid=5,mode=620)
   tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
   /dev/sda1 on /boot type ext4 (rw)
   none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
   /dev/mapper/testvg-testlv on /users type ext3 (rw)
  [root@king-liu ~]# ls /users/  #查看
  lost+found
  [root@king-liu ~]# df -hT    #查看硬盘
  Filesystem           Type   Size  Used Avail Use% Mounted on
  /dev/sda2            ext4    20G  964M   18G   6% /
  tmpfs                tmpfs  306M     0  306M   0% /dev/shm
  /dev/sda1            ext4   477M   26M  426M   6% /boot
  /dev/mapper/testvg-testlv
                       ext3   5.0G  139M  4.6G   3% /users

2: 新建用户archlinux,要求其家目录为/users/archlinux,而后su切换至archlinux用户,复制/etc/pam.d目录至自己的家目录;

[root@king-liu ~]# useradd -d /users/archlinux archlinux 
  #创建用户archlinux 指定家目录为/users/archlinux
[root@king-liu ~]# echo "kingliu" | passwd --stdin archlinux
  #将kingliu作为archlinux的密码
更改用户 archlinux 的密码 。
passwd: 所有的身份验证令牌已经成功更新。
[root@king-liu ~]# su archlinux #  切换到archlinux
[archlinux@king-liu ~]$ cp -r /etc/pam.d /users/archlinux/ 
  # 复制/etc/pam.d 到 archlinux用户的家目录中

3:扩展testlv至7G,要求archlinux用户的文件不能丢失;

  [archlinux@king-liu ~]$ su root  #切换到root管理员
  密码:
  [root@king-liu archlinux]# pvs    #查看物理卷是否能扩展
  PV         VG     Fmt  Attr PSize  PFree 
  /dev/sda11 testvg lvm2 a--  10.00g  5.00g
  /dev/sda12 testvg lvm2 a--  10.00g 10.00g
  [root@king-liu archlinux]# vgs    #查看卷组是否能扩展
   VG     #PV #LV #SN Attr   VSize  VFree 
   testvg   2   1   0 wz--n- 20.00g 15.00g
  [root@king-liu archlinux]# lvextend  -L +2G /dev/testvg/testlv 
  #之前已经添加了5g为lv,现在只需再加上2G
  Size of logical volume testvg/testlv changed from 5.00 GiB (320 extents) to 7.00 GiB (448 extents).
  Logical volume testlv successfully resized
 [root@king-liu archlinux]# df -h   #发现硬盘并没有增大
 Filesystem            Size  Used Avail Use% Mounted on
 /dev/sda2              20G  964M   18G   6% /
 tmpfs                 306M     0  306M   0% /dev/shm
 /dev/sda1             477M   26M  426M   6% /boot
 /dev/mapper/testvg-testlv
                  5.0G  139M  4.6G   3% /users
 [root@king-liu pam.d]# resize2fs  -p /dev/testvg/testlv   
    #通过resize2fs工具来把文件系统确实添加
 resize2fs 1.41.12 (17-May-2010)
 Filesystem at /dev/testvg/testlv is mounted on /users; on-line resizing required
 old desc_blocks = 1, new_desc_blocks = 1
 Performing an on-line resize of /dev/testvg/testlv to 1835008 (4k) blocks.
 The filesystem on /dev/testvg/testlv is now 1835008 blocks long.

 [root@king-liu pam.d]# df -h   #查看硬盘
 Filesystem            Size  Used Avail Use% Mounted on
 /dev/sda2              20G  964M   18G   6% /
 tmpfs                 306M     0  306M   0% /dev/shm
 /dev/sda1             477M   26M  426M   6% /boot
 /dev/mapper/testvg-testlv
                       6.9G  140M  6.5G   3% /users  #加了2G 不一定会真正达到7G的%10以内是可以接受的
 [root@king-liu pam.d]# cd /users/archlinux/pam.d/  #查看数据是否丢失
 [root@king-liu pam.d]# ls
 chfn                 login             polkit-1        smartcard-auth-ac  sudo
 chsh                 newrole           remote          smtp               sudo-i
 config-util          other             run_init        smtp.postfix       su-l
 crond                passwd            runuser         sshd               system-auth
 fingerprint-auth     password-auth     runuser-l       ssh-keycat         system-auth-ac
 fingerprint-auth-ac  password-auth-ac  smartcard-auth  su

4:收缩testlv至3G,要求archlinux用户的文件不能丢失;

[root@king-liu /]# umount /dev/testvg/testlv  #卸载分区
[root@king-liu /]# resize2fs /dev/testvg/testlv 3G  #缩减逻辑大小  
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/testvg/testlv is mounted on /users; on-line resizing required
On-line shrinking from 1835008 to 786432 not supported.

[root@king-liu /]# lvreduce -L 3G /dev/testvg/testlv  #缩减物理边界大小
WARNING: Reducing active and open 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 7.00 GiB (448 extents) to 3.00 GiB (192 extents).
Logical volume testlv successfully resized
[root@king-liu /]# lvs    #查看逻辑卷
LV     VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
testlv testvg -wi-ao---- 3.00g   
[root@king-liu /]# mount /dev/testvg/testlv /users/archlinux/ #挂载
[root@king-liu archlinux]#cd /users/archlinux
[root@king-liu archlinux]# ls
 pam.d
[root@king-liu archlinux]# cd pam.d/
[root@king-liu pam.d]# ls
 chfn                 login             polkit-1        smartcard-auth-ac  sudo
chsh                 newrole           remote          smtp               sudo-i
config-util          other             run_init        smtp.postfix       su-l
crond                passwd            runuser         sshd               system-auth
fingerprint-auth     password-auth     runuser-l       ssh-keycat         system-auth-ac
 fingerprint-auth-ac  password-auth-ac  smartcard-auth  su

5:对testlv创建快照,并尝试基于快照备份数据,验正快照的功能;

[root@king-liu pam.d]# lvcreate -L 30M -n backup -s -p r /dev/testvg/testlv 
 #创建快照 -L 指定快照大小 -n 指定名字  -s 创建快照 -p 只读  
 Rounding up size to full physical extent 32.00 MiB
 Logical volume "backup" created.
 mount: block device /dev/mapper/testvg-backup is write-protected, mounting read-only
 # 可以通过快照来访问数据
 [root@king-liu tmp]# ls /tmp/backup/
  archlinux  lost+found
 [root@king-liu tmp]# ls /tmp/backup/archlinux/  
  pam.d

总结

文章那里出错了 请留言QQ1505222150 我会及时改正的。或者还需要增加什么也请留言。 坚信开源才能会越来越好。 请大家多多评论

原创文章,作者:N19_king,如若转载,请注明出处:http://www.178linux.com/16476

(0)
N19_kingN19_king
上一篇 2016-05-20 15:23
下一篇 2016-05-22 14:27

相关推荐

  • Ansible初识

    Ansible概念 ansible是自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。说…

    2017-07-08
  • 正则表达式

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其他用户都没有任何访问权限 [root@localhost ~]#  mkdir /home/tuser1======>创建/home/tuser1目录 [root@localhost ~]#   cp  -a  /etc/skel/   /hom…

    2017-10-10
  • 马哥教育网络班22期第二周课程练习1-未闻花名

    1、Linux管理文件和目录的命令 命令 功能 命令 功能 pwd 显示当前目录 ls 查看目录下的内容 cd 改变所在目录 cat 显示文件的内容 grep 在文件中查找某字符 cp 复制文件 touch 创建文件 mv 移动文件 rm 删除文件 rmdir 删除目录 1.1 pwd命令 该命令的英文解释为print working directory(打…

    Linux干货 2016-08-29
  • shell脚本编写规范

    1 脚本名以.sh结尾,名称尽量见名之意,比如ClearLog.sh Clear_Log.sh clearlog.sh SerRestart.sh Ser_Restart.sh;2 尽量使用UTF-8编码,注释及输出尽量使用英文;3 一般给到执行权限,但一些关于变量的配置文件不用加执行权限;4 执行的时候可以使用bash 执行,或者使用bash -x执行,可…

    Linux干货 2017-04-16
  • 修改Linux命令终端提示符

    作业1. 设置自己的终端提示符,要求字符终端登录时:     a> 需要带颜色     b> 需要显示当前执行到了第几条命令     c> 显示当前登录终端,主机名和当前时间     这可以通…

    Linux干货 2016-10-17
  • lvs–各类型的原理及实现—(原理篇)

    前言:    LVS即是linux虚拟服务器,是一个虚拟的服务器集群系统,目的是使用集群技术和linux操作系统实现一个高性能、高可用的服务器。采用IP负载均衡技术和基于内容请求分发技术,将请求负载均衡地转移到不同的服务器上执行,从而将一组服务器构成一个高性能、高可用的虚拟服务器。此篇的内容只包含两种较为常用的LVS技术,分别是LVS-N…

    Linux干货 2016-10-27