磁盘管理(三)

lVM

  逻辑卷管理器(LVM)

  允许对卷进行方便操作的抽象层,包括重新设定文件系统的大小
  允许在多个物理设备间重新组织文件系统
    将设备指定为物理卷
    用一个或者多个物理卷来创建一个卷组
    物理卷是用固定大小的物理区域(Physical Extent,PE)来定义的
    在物理卷上创建的逻辑卷是由物理区域(PE)组成
    可以在逻辑卷上创建文件系统

  LVM

  LVM: Logical Volume Manager, Version: 2
  dm: device mapper:将一个或多个底层块设备组织成一个逻辑设备的模块
  设备名:/dev/dm-#
  软链接:
   /dev/mapper/VG_NAME-LV_NAME
   /dev/mapper/vol0-root
   /dev/VG_NAME/LV_NAME
   /dev/vol0/root

  LVM更改文件系统的容量
  LVM可以弹性的更改LVM的容量通过交换PE来进行资料的转换,将原来LV内的PE转移到其他的设备中以降低
LV的容量,或将其他设备中的PE加到LV中以加大容量

  pv管理工具

  显示pv信息
   pvs:简要pv信息显示
   pvdisplay
  创建pv
   pvcreate /dev/DEVICE
  
  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
   lvcreate -l 60%VG -n mylv testvg
   lvcreate -l 100%FREE -n yourlv testvg
  删除逻辑卷
   lvremove /dev/VG_NAME/LV_NAME
  重设文件系统大小
   fsadm [options] resize device [new_size[BKMGTEP]]
   resize2fs [-f] [-F] [-M] [-P] [-p] device [new_size]
  扩展和缩减逻辑卷
   扩展逻辑卷:
    lvextend -L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME
    resize2fs /dev/VG_NAME/LV_NAME
    lvresize -r -l +100%FREE /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
  跨主机迁移卷组
   源计算机上
    1 在旧系统中,umount所有卷组上的逻辑卷
    2 vgchange –a n vg0
      lvdisplay
    3 vgexport vg0 
      pvscan
      vgdisplay
  拆下旧硬盘
   在目标计算机上
    4 在新系统中安装旧硬盘,并vgimport vg0.
    5 vgchange –ay vg0
    6 mount所有卷组上的逻辑卷

  创建逻辑卷示例

  创建物理卷
   pvcreate /dev/sda3
  为卷组分配物理卷
   vgcreate vg0 /dev/sda3
  从卷组创建逻辑卷
   lvcreate -L 256M -n data vg0
   mke2fs -j /dev/vg0/data
   mount /dev/vg0/data /mnt/data

  逻辑卷管理器快照

  快照是特殊的逻辑卷,它是在生成快照时存在的逻辑卷的准确拷贝
  对于需要备份或者复制的现有数据临时拷贝以及其它操作来说,快照是最合适的选择快照只有在它们和原
来的逻辑卷不同时才会消耗空间在生成快照时会分配给它一定的空间,但只有在原来的逻辑卷或者快照有所
改变才会使用这些空间,当原来的逻辑卷中有所改变时,会将旧的数据复制到快照中。
  快照中只含有原来的逻辑卷中更改的数据或者自生成快照后的快照中更改的数据
  建立快照的卷大小只需要原始逻辑卷的15%~20%就够了,也可以使用lvextend放大快照

  逻辑卷管理器快照

  快照就是将当时的系统信息记录下来,就好像照相一般,若将来有任何数据改动了,则原始数据会被移动
到快照区,没有改动的区域则由快照区和文件系统共享由于快照区与原本的LV共用很多PE的区块,因此快照
与被快照的LV必须在同一个VG中.系统恢复的时候的文件数量不能高于快照区的实际容量

  使用LVM快照

  为现有逻辑卷创建快照
   lvcreate -l 64 -s -n data-snapshot -p r /dev/vg0/data
  挂载快照
   mkdir -p /mnt/snap
   mount -o ro /dev/vg0/data-snapshot /mnt/snap
  恢复快照
   umount /dev/vg0/data-snapshot
   umount /dev/vg0/data
   lvconvert --merge /dev/vg0/data-snapshot 
  删除快照
   umount /mnt/databackup
   lvremove /dev/vg0/databackup

练习

   1、创建一个至少有两个PV组成的大小为20G的名为testvg的VG;要求PE大小为16MB, 而后在卷组中创建
大小为5G的逻辑卷testlv;挂载至/users目录
   2、新建用户archlinux,要求其家目录为/users/archlinux,而后su切换至archlinux用户,复制
/etc/pam.d目录至自己的家目录
   3、扩展testlv至7G,要求archlinux用户的文件不能丢失
   4、收缩testlv至3G,要求archlinux用户的文件不能丢失
   5、对testlv创建快照,并尝试基于快照备份数据,验证快照的功能

centos6中
    
---->首先准备好两块或者两块以上硬盘或分区,(分区格式调为8e),总大小为20G
┌─[root@centos6]─[~]
└──╼ #lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 3.7G 0 rom /mnt/cdrom
sdb 8:16 0 20G 0 disk 
└─sdb1 8:17 0 10G 0 part 
sdc 8:32 0 20G 0 disk 
└─sdc1 8:33 0 10G 0 part 
sda 8:0 0 200G 0 disk 
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 48.8G 0 part /
├─sda3 8:3 0 29.3G 0 part /data
├─sda4 8:4 0 1K 0 part 
├─sda5 8:5 0 2G 0 part [SWAP]
└─sda6 8:6 0 3G 0 part 

---->创建pv
┌─[root@centos6]─[~]
└──╼ #pvcreate /dev/sd{b1,c1}
 Physical volume "/dev/sdb1" successfully created
 Physical volume "/dev/sdc1" successfully created

---->创建vg
┌─[root@centos6]─[~]
└──╼ #vgcreate -s 16M testvg /dev/sd{b1,c1}
 Volume group "testvg" successfully created

---->创建lv
┌─[root@centos6]─[~]
└──╼ #lvcreate -L 5G -n testlv testvg
 Logical volume "testlv" created.

---->创建文件系统
┌──[root@centos6]─[~]
└──╼ #mkfs -t ext4 -L "users new home" /dev/testvg/testlv 
mke2fs 1.41.12 (17-May-2010)
Filesystem label=users new home
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.

---->创建挂载点目录
┌─[root@centos6]─[~]
└──╼ #mkdir /users

---->挂载
┌─[root@centos6]─[~]
└──╼ #mount /dev/testvg/testlv /users

---->创建新用户,指定新加目录
┌─[root@centos6]─[~]
└──╼ #useradd -m -d /users/archlinux archlinux

---->扩展lv到7G
┌─[root@centos6]─[~]
└──╼ #lvextend -L 7G /dev/testvg/testlv -r
 Size of logical volume testvg/testlv changed from 5.00 GiB (320 extents) to 7.00 GiB (448 extents).
 Logical volume testlv successfully resized.
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/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/mapper/testvg-testlv to 1835008 (4k) blocks.
The filesystem on /dev/mapper/testvg-testlv is now 1835008 blocks long.

---->缩小lv到3G
┌─[root@centos6]─[~]
└──╼ #umount /dev/testvg/testlv 

┌─[root@centos6]─[~]
└──╼ #fsck -t ext4 -f /dev/testvg/testlv 
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
users_new_home: 19/458752 files (0.0% non-contiguous), 64455/1835008 blocks

┌──[root@centos6]─[~]
└──╼ #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@centos6]─[~]
└──╼ #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 testvg/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@centos6]─[~]
└──╼ #lvcreate -L 1G -s -n testlv-snapshot -p r /dev/testvg/testlv 
 Logical volume "testlv-snapshot" created.

---->挂载
┌─[root@centos6]─[~]
└──╼ #mount /dev/testvg/testlv-snapshot /snap
mount: block device /dev/mapper/testvg-testlv--snapshot is write-protected, mounting read-only

---->删除pam.d下以k开头的文件
[archlinux@centos6 pam.d]$ rm -f k*
[archlinux@centos6 pam.d]$ ls
abrt-cli-root gdm reboot sudo-i
abrt-gui-root gdm-autologin remote su-l
atd gdm-fingerprint run_init system-auth
authconfig gdm-password runuser system-auth-ac
authconfig-gtk gnome-screensaver runuser-l system-auth.rpmnew
authconfig-tui halt setup system-config-authentication
chfn login smartcard-auth system-config-date
chsh newrole smartcard-auth-ac system-config-kdump
config-util other smartcard-auth.rpmnew system-config-keyboard
crond passwd smtp system-config-language
cups password-auth smtp.postfix system-config-network
cvs password-auth-ac smtp.sendmail system-config-network-cmd
eject password-auth.rpmnew sshd system-config-users
fingerprint-auth polkit-1 ssh-keycat xdm
fingerprint-auth-ac poweroff su xserver
fingerprint-auth.rpmnew ppp sudo

---->恢复快照
┌─[root@centos6]─[~]
└──╼ #umount /snap
┌─[root@centos6]─[~]
└──╼ #umount /users
┌─[root@centos6]─[~]
└──╼ #lvconvert --merge /dev/testvg/testlv-snapshot 
 Merging of volume testlv-snapshot started.
 testlv: Merged: 100.0%
 testlv: Merged: 100.0%
 Merge of snapshot into logical volume testlv has finished.
 Logical volume "testlv-snapshot" successfully removed

---->查看快照效果
┌──[root@centos6]─[~]
└──╼ #su - archlinux
[archlinux@centos6 ~]$ cd pam.d/
[archlinux@centos6 pam.d]$ ls
abrt-cli-root gdm-fingerprint poweroff sudo-i
abrt-gui-root gdm-password ppp su-l
atd gnome-screensaver reboot system-auth
authconfig halt remote system-auth-ac
authconfig-gtk kcheckpass run_init system-auth.rpmnew
authconfig-tui kdm runuser system-config-authentication
chfn kdm-np runuser-l system-config-date
chsh kppp setup system-config-kdump
config-util kscreensaver smartcard-auth system-config-keyboard
crond ksu smartcard-auth-ac system-config-language
cups login smartcard-auth.rpmnew system-config-network
cvs newrole smtp system-config-network-cmd
eject other smtp.postfix system-config-users
fingerprint-auth passwd smtp.sendmail xdm
fingerprint-auth-ac password-auth sshd xserver
fingerprint-auth.rpmnew password-auth-ac ssh-keycat
gdm password-auth.rpmnew su
gdm-autologin polkit-1 sudo

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/97089

(0)
上一篇 2018-04-26 11:12
下一篇 2018-04-26 17:12

相关推荐

  • 第四周

      sed的高级用法 安装包路径/misc/cd/Packages/ rpm 可以安装 卸载 软件包 rpm -i 跟软件包名称 (安装) -e 卸载软件包 (只有配置文件会自己备份 备份文件以.rpmsave结尾的文件)-v显示详细进度 -h显示百分条 –test 测试安装,但不是真的装–nodeps 忽略依赖性 (但安装…

    Linux笔记 2018-04-22
  • MySQL之主从复制相关实验

    本节索引 实验一:搭建主从复制 实验二:为已存放大量数据的在用服务器搭建从服务器 实验三:级联复制 实验四:提升一个从服务器为主服务器 实验五:主主复制 实验六:半同步复制 实验七:实现主从SSL加密复制     实验一:搭建主从复制 前期准备: 两台虚拟机 主服务器:     CentOS7.4  192.168.30.10      …

    2018-06-18
  • 华为交换机配置

    示例设备华为s5700S-28P-LI-AC 用户视图<HUAWEI>:默认登录 系统视图[HUAWEI]:system-view 接口视图[HUAWEI-GigabitEthernetX/Y/Z]:interface gigabitethernet X/Y/Z X/Y/Z为需要配置的接口的编号,分别对应“槽位号/子卡号/接口序号” 路由协议视图…

    Linux笔记 2018-06-07
  • 马哥教育 第一周培训4.1

    常用命令 1.ifconfig 查ip地址 2.cal 日历 3.who查看用户名 (whoami,who a mi 也可以) 4.init 0 关机(poweroff) 5.init 3 字符界面 6.init5  图形界面  CERL+C 关闭图形界面 7.free 查看内存大小 8.free -m 以M为单位看内存大小 9.clock 查看时间10.h…

    Linux笔记 2018-04-01
  • linux 学习5

    第五周(7.16-7.30)

    2018-07-30
  • linux ACL访问控制列表

    1.ACL介绍 Linux 下用户对文件的操作权限有 r-读, w-写, x-可执行三种,而对linux 下的文件而言,用户身份分为:所有者, 所属组, 其它人, 且文件的所有者,所属组都只能是一个,所以在对文件分配用户的使用权限时,只能对这三种身份进行分配rwx 权限.Linux 主要作为服务器系统使用,用户众多.所以在实际使用场景中,这三种身份并不能很好…

    Linux笔记 2018-04-08