Linux上LVM创建逻辑卷和LVM快照习题


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

先查看磁盘大小数据

[root@centos ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   50G  0 disk 
├─sda1            8:1    0  500M  0 part /boot
└─sda2            8:2    0 49.5G  0 part 
  ├─centos-root 253:0    0 45.6G  0 lvm  /
  └─centos-swap 253:1    0  3.9G  0 lvm  [SWAP]
sdb               8:16   0   10G  0 disk 
sdc               8:32   0   10G  0 disk 
sdd               8:48   0   10G  0 disk 
sr0              11:0    1    4G  0 rom  

为磁盘创建分区,并且分区大小设置为最大,并将分区类型改为8e(linux LVM)

[root@centos ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
Partition number (1-4, default 1): 
First sector (2048-20971519, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): 
Using default value 20971519
Partition 1 of type Linux and of size 10 GiB is set

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@centos ~]# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
Partition number (1-4, default 1): 
First sector (2048-20971519, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): 
Using default value 20971519
Partition 1 of type Linux and of size 10 GiB is set

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

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

Calling ioctl() to re-read partition table.
Syncing disks.

现在再查看分区情况

[root@centos ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   50G  0 disk 
├─sda1            8:1    0  500M  0 part /boot
└─sda2            8:2    0 49.5G  0 part 
  ├─centos-root 253:0    0 45.6G  0 lvm  /
  └─centos-swap 253:1    0  3.9G  0 lvm  [SWAP]
sdb               8:16   0   10G  0 disk 
└─sdb1            8:17   0   10G  0 part 
sdc               8:32   0   10G  0 disk 
└─sdc1            8:33   0   10G  0 part 
sdd               8:48   0   10G  0 disk 
sr0              11:0    1    4G  0 rom  

创建pv

[root@centos ~]# pvcreate /dev/sdb1 /dev/sdc1
  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdc1" successfully created
[root@centos ~]# pvs
  PV         VG     Fmt  Attr PSize  PFree 
  /dev/sda2  centos lvm2 a--  49.51g 44.00m
  /dev/sdb1         lvm2 ---  10.00g 10.00g
  /dev/sdc1         lvm2 ---  10.00g 10.00g

创建卷组vg并设置pe大小为16M

[root@centos ~]# vgcreate -s 16M /dev/testvg /dev/sdb1 /dev/sdc1 
Volume group "testvg" successfully created
[root@centos ~]# vgs
VG     #PV #LV #SN Attr   VSize  VFree 
centos   1   2   0 wz--n- 49.51g 44.00m
testvg   2   0   0 wz--n- 19.97g 19.97g

创建逻辑卷lv

[root@centos ~]# lvcreate -L 5G -n testlv testvg 
Logical volume "testlv" created.

对逻辑卷进行格式化文件系统

[root@centos ~]# mkfs.ext4 /dev/testvg/testlv 
mke2fs 1.42.9 (28-Dec-2013)
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

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done 

在根下创建目录并且完成挂载

[root@centos ~]# mkdir /users
[root@centos ~]# mount /dev/testvg/testlv /users/
[root@centos ~]# lsblk
NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                 8:0    0   50G  0 disk 
├─sda1              8:1    0  500M  0 part /boot
└─sda2              8:2    0 49.5G  0 part 
  ├─centos-root   253:0    0 45.6G  0 lvm  /
  └─centos-swap   253:1    0  3.9G  0 lvm  [SWAP]
sdb                 8:16   0   10G  0 disk 
└─sdb1              8:17   0   10G  0 part 
  └─testvg-testlv 253:2    0    5G  0 lvm  /users
sdc                 8:32   0   10G  0 disk 
└─sdc1              8:33   0   10G  0 part 
sdd                 8:48   0   10G  0 disk 
sr0                11:0    1    4G  0 rom  
2、新建用户archlinux,要求其家目录为/users/archlinux,而后su切换至archlinux用户,复制/etc/pam.d目录至自己的家目录

创建用户

[root@centos ~]# useradd archlinux -d /users/archlinux

我们查看一下新用户的主目录发现正确

[root@centos ~]# cat /etc/passwd|tail -n 1
archlinux:x:1001:1001::/users/archlinux:/bin/bash

登录到新用户上进行复制

[root@centos ~]# su - archlinux 
[archlinux@centos ~]$ ls
[archlinux@centos ~]$ cp -a /etc/pam.d/ ./
[archlinux@centos ~]$ ls
pam.d
3、扩展testlv至7G,要求archlinux用户的文件不能丢失

先将逻辑卷进行扩大空间

[root@centos ~]# lvextend -L +2G /dev/testvg/testlv 
  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@centos ~]# resize2fs /dev/testvg/testlv 
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/testvg/testlv is mounted on /users; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/testvg/testlv is now 1835008 blocks long.

查看逻辑卷的大小

[root@centos ~]# df -h
Filesystem                 Size  Used Avail Use% Mounted on
/dev/mapper/centos-root     46G  4.5G   42G  10% /
devtmpfs                   893M     0  893M   0% /dev
tmpfs                      910M     0  910M   0% /dev/shm
tmpfs                      910M  9.1M  901M   1% /run
tmpfs                      910M     0  910M   0% /sys/fs/cgroup
/dev/sda1                  497M  182M  315M  37% /boot
tmpfs                      182M     0  182M   0% /run/user/0
/dev/mapper/testvg-testlv  6.8G   23M  6.4G   1% /users
4、收缩testlv至3G,要求archlinux用户的文件不能丢失

先将逻辑卷取消挂载并检查文件系统

[root@centos ~]# umount /dev/testvg/testlv 
[root@centos ~]# e2fsck -f /dev/testvg/testlv 
e2fsck 1.42.9 (28-Dec-2013)
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: 72/458752 files (0.0% non-contiguous), 67382/1835008 blocks

我们调整文件系统的大小为3G

[root@centos ~]# resize2fs /dev/testvg/testlv 3G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/testvg/testlv to 786432 (4k) blocks.
The filesystem on /dev/testvg/testlv is now 786432 blocks long.

我们可以暂时将逻辑卷进行挂载查看一下文件系统的大小

[root@centos ~]# mount /dev/testvg/testlv /users/
[root@centos ~]# df -h
Filesystem                 Size  Used Avail Use% Mounted on
/dev/mapper/centos-root     46G  4.5G   42G  10% /
devtmpfs                   893M     0  893M   0% /dev
tmpfs                      910M     0  910M   0% /dev/shm
tmpfs                      910M  9.1M  901M   1% /run
tmpfs                      910M     0  910M   0% /sys/fs/cgroup
/dev/sda1                  497M  182M  315M  37% /boot
tmpfs                      182M     0  182M   0% /run/user/0
/dev/mapper/testvg-testlv  2.9G   16M  2.7G   1% /users

我们取消挂载,并将逻辑卷的大小减去4G

[root@centos ~]# umount /users/
[root@centos ~]# lvreduce -L -4G /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 7.00 GiB (448 extents) to 3.00 GiB (192 extents).
  Logical volume testlv successfully resized.

将逻辑卷挂载上之后我们查看文件系统和逻辑卷的大小均为3G

[root@centos ~]# mount /dev/testvg/testlv /users/
[root@centos ~]# df -h
Filesystem                 Size  Used Avail Use% Mounted on
/dev/mapper/centos-root     46G  4.5G   42G  10% /
devtmpfs                   893M     0  893M   0% /dev
tmpfs                      910M     0  910M   0% /dev/shm
tmpfs                      910M  9.1M  901M   1% /run
tmpfs                      910M     0  910M   0% /sys/fs/cgroup
/dev/sda1                  497M  182M  315M  37% /boot
tmpfs                      182M     0  182M   0% /run/user/0
/dev/mapper/testvg-testlv  2.9G   16M  2.7G   1% /users
[root@centos ~]# lsblk
NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                 8:0    0   50G  0 disk 
├─sda1              8:1    0  500M  0 part /boot
└─sda2              8:2    0 49.5G  0 part 
├─centos-root   253:0    0 45.6G  0 lvm  /
└─centos-swap   253:1    0  3.9G  0 lvm  [SWAP]
sdb                 8:16   0   10G  0 disk 
└─sdb1              8:17   0   10G  0 part 
└─testvg-testlv 253:2    0    3G  0 lvm  /users
sdc                 8:32   0   10G  0 disk 
└─sdc1              8:33   0   10G  0 part 
sdd                 8:48   0   10G  0 disk 
sr0                11:0    1    4G  0 rom  
[root@centos ~]# lvs
LV     VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
root   centos -wi-ao---- 45.59g                                                    
swap   centos -wi-ao----  3.88g                                                    
testlv testvg -wi-ao----  3.00g                                                    
5、对testlv创建快照,并尝试基于快照备份数据,验正快照的功能

先是在/users/archlinux/目录下创建一个文件1,内容为“abcd”

[root@centos ~]# cd /users/archlinux/
[root@centos archlinux]# ls
pam.d
[root@centos archlinux]# echo "abcd" >1
[root@centos archlinux]# cat 1
abcd

为testlv逻辑卷创建一个快照,名称为snap-data,快照的权限为只读,大小为1G

[root@centos archlinux]# cd
[root@centos ~]# lvcreate -L 1G -s -n snap-data -p r /dev/testvg/testlv
  Logical volume "snap-data" created.

创建目录/mnt/snap并且将快照挂载在此目录下

[root@centos ~]# mkdir -p /mnt/snap
[root@centos ~]# mount -o ro /dev/testvg/snap-data /mnt/snap
[root@centos ~]# lsblk
NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                         8:0    0   50G  0 disk 
├─sda1                      8:1    0  500M  0 part /boot
└─sda2                      8:2    0 49.5G  0 part 
  ├─centos-root           253:0    0 45.6G  0 lvm  /
  └─centos-swap           253:1    0  3.9G  0 lvm  [SWAP]
sdb                         8:16   0   10G  0 disk 
└─sdb1                      8:17   0   10G  0 part 
  ├─testvg-testlv-real    253:3    0    3G  0 lvm  
  │ ├─testvg-testlv       253:2    0    3G  0 lvm  /users
  │ └─testvg-snap--data   253:5    0    3G  1 lvm  /mnt/snap
  └─testvg-snap--data-cow 253:4    0    1G  1 lvm  
    └─testvg-snap--data   253:5    0    3G  1 lvm  /mnt/snap
sdc                         8:32   0   10G  0 disk 
└─sdc1                      8:33   0   10G  0 part 
sdd                         8:48   0   10G  0 disk 
sr0                        11:0    1    4G  0 rom  

使用lvs查看快照,发现大小为1G,源为testlv

[root@centos ~]# lvs
  LV        VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root      centos -wi-ao---- 45.59g                                                    
  swap      centos -wi-ao----  3.88g                                                    
  snap-data testvg sri-aos---  1.00g      testlv 0.01                                   
  testlv    testvg owi-aos---  3.00g     

进入到/users/archlinux/目录下修改文件1的内容为“aabbcc”

[root@centos ~]# cd /users/archlinux/
[root@centos archlinux]# echo "aabbcc" > 1
[root@centos archlinux]# cat 1
aabbcc

我们恢复快照

[root@centos archlinux]# cd
[root@centos ~]# umount /dev/testvg/snap-data 
[root@centos ~]# umount /dev/testvg/testlv 
[root@centos ~]# lvconvert --merge /dev/testvg/snap-data 
  Merging of volume snap-data started.
  testlv: Merged: 100.0%
[root@centos ~]# mount /dev/testvg/testlv /users/
[root@centos ~]# lsblk
NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                 8:0    0   50G  0 disk 
├─sda1              8:1    0  500M  0 part /boot
└─sda2              8:2    0 49.5G  0 part 
  ├─centos-root   253:0    0 45.6G  0 lvm  /
  └─centos-swap   253:1    0  3.9G  0 lvm  [SWAP]
sdb                 8:16   0   10G  0 disk 
└─sdb1              8:17   0   10G  0 part 
  └─testvg-testlv 253:2    0    3G  0 lvm  /users
sdc                 8:32   0   10G  0 disk 
└─sdc1              8:33   0   10G  0 part 
sdd                 8:48   0   10G  0 disk 
sr0                11:0    1    4G  0 rom  

我们查看我们之前改过的文件1,发现内容现在为“abcd”,证明是快照前的内容

[root@centos ~]# cd /users/archlinux/
[root@centos archlinux]# ls
1  pam.d
[root@centos archlinux]# cat 1
abcd
  • 6
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值