lvm逻辑卷 练习题

创建一个至少有两个PV组成的大小为20G的名为testvg的VG;要求PE大小为16MB, 而后在卷组中创建大小为5G的逻辑卷testlv;挂载至/users目录;在/users下创建一个test.txt文件。扩展testlv至7G,要求archlinux用户的文件不能丢失;收缩testlv至3G,要求archlinux用户的文件不能丢失;对testlv创建快照,并尝试基于快照备份数据,验正快照的功能。

  任务1,创建逻辑卷testlv

  首先,先创建2个分区   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
[root@localhost~]# 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
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (7859-13081, default 7859): 
Using default value 7859
Last cylinder, +cylinders or +size{K,M,G} (7859-13081, default 13081): +10G
 
Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (9165-13081, default 9165):    
Using default value 9165
Last cylinder, +cylinders or +size{K,M,G} (9165-13081, default 13081): +10G
 
Command (m for help): p
 
Disk /dev/sda: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 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: 0x0002b73a
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        7859    62914560   8e  Linux LVM
/dev/sda3            7859       13081    41952748+   5  Extended
/dev/sda5            7859        9164    10489414+  83  Linux
/dev/sda6            9165       10470    10490413+  83  Linux
 
Command (m for help): t   #修改分区ID
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): p
 
Disk /dev/sda: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 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: 0x0002b73a
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        7859    62914560   8e  Linux LVM
/dev/sda3            7859       13081    41952748+   5  Extended
/dev/sda5            7859        9164    10489414+  8e  Linux LVM
/dev/sda6            9165       10470    10490413+  8e  Linux LVM
 
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: Device or resource busy.
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@localhost ~]# partx -a /dev/sda
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
[root@localhost ~]# partx -a /dev/sda
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
BLKPG: Device or resource busy
error adding partition 5
BLKPG: Device or resource busy
error adding partition 6
[root@localhost ~]# cat /proc/partitions #查看分区信息
major minor  #blocks  name
 
   8        0  125829120 sda
   8        1     204800 sda1
   8        2   62914560 sda2
   8        3          1 sda3
   8        5   10489414 sda5
   8        6   10490413 sda6
 253        0   20971520 dm-0
 253        1    2097152 dm-1
 253        2   10485760 dm-2
 253        3   20971520 dm-3

   创建pv

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[root@localhost ~]# pvcreate /dev/sda5
  Physical volume “/dev/sda5” successfully created
[root@localhost ~]# pvcreate /dev/sda6
  Physical volume “/dev/sda6” successfully created
[root@localhost ~]# pvdisplay  
  “/dev/sda5” is a new physical volume of “10.00 GiB”
  — NEW Physical volume —
  PV Name               /dev/sda5
  VG Name               
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               aWkcdi-T8zI-ZQ7h-xcPE-KhDI-HAmz-TEaAph
    
  “/dev/sda6” is a new physical volume of “10.00 GiB”
  — NEW Physical volume —
  PV Name               /dev/sda6
  VG Name               
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               Ib9szy-eyhG-zWDZ-sm6t-MUXI-sCUu-S2XtuG

  创建vg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[root@localhost~]# vgcreate -v -s 16MB testvg /dev/sda5
    DEGRADED MODE. Incomplete RAID LVs will be processed.
    Wiping cache of LVM-capable devices
    Wiping cache of LVM-capable devices
    Adding physical volume ‘/dev/sda5’ to volume group ‘testvg’
    Archiving volume group “testvg” metadata (seqno 0).
    Creating volume group backup “/etc/lvm/backup/testvg” (seqno 1).
  Volume group “testvg” successfully created
[root@localhost~]# vgextend -v testvg /dev/sda6
    DEGRADED MODE. Incomplete RAID LVs will be processed.
    Checking for volume group “testvg”
    Archiving volume group “testvg” metadata (seqno 1).
    Wiping cache of LVM-capable devices
    Adding physical volume ‘/dev/sda6’ to volume group ‘testvg’
    Volume group “testvg” will be extended by 1 new physical volumes
    Creating volume group backup “/etc/lvm/backup/testvg” (seqno 2).
  Volume group “testvg” successfully extended
[root@localhost ~]# vgdisplay 
  — Volume group —
  VG Name               testvg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  2
  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   #PE 大小16MB
  Total PE              1280
  Alloc PE / Size       0 / 0   
  Free  PE / Size       1280 / 20.00 GiB
  VG UUID               i06ZbB-QWLX-zWmW-x2hS-L8Uo-zAbN-g4MhEu

  创建逻辑卷testlv

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@localhost ~]# lvcreate -L 5G -n testlv /dev/testvg
  Logical volume “testlv” created
[root@localhost ~]# lvdisplay 
  — Logical volume —
  LV Path                /dev/testvg/testlv
  LV Name                testlv
  VG Name                testvg
  LV UUID                tNyjOF-7Sb4-RVbW-W7Yk-zi6N-jXYG-LK4sOI
  LV Write Access        read/write
  LV Creation host, time laopan, 2015-08-27 18:37:40 +0800
  LV Status              available
  # open                 0
  LV Size                5.00 GiB  #逻辑卷5G
  Current LE             320
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  – currently set to     256
  Block device           253:4

   在/dev/testvg/testlv创建ext4文件系统   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@localhost ~]# mkfs -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 20 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

   创建挂载点 mkdir /users

   把/dev/testvg/testlv 挂载至/users,在其目录下创建一个test.txt文件

1
2
3
4
5
6
7
[root@localhost ~]# mount /dev/testvg/testlv /users
[root@localhost ~]# cd /users
[root@localhost users]# echo `date` > test.txt
[root@localhost users]# ls
lost+found  test.txt
[root@laopan users]# cat test.txt 
Thu Aug 27 18:45:46 CST 2015

   任务2,在线把testlv扩展至7G,保证test.txt是否丢失

   先对逻辑卷扩容

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost users]# lvextend -L 7G /dev/testvg/testlv 
  Size of logical volume testvg/testlv changed from 5.00 GiB (320 extents) to 7.00 GiB (448 extents). #已经扩展到7G了,但是文件系统识别的还是5G,所以需要对文件系统扩容
  Logical volume testlv successfully resized
[root@localhost users]# df
Filesystem           1K-blocks    Used Available Use% Mounted on
/dev/mapper/vg0-root  20511356  293400  19169380   2% /
tmpfs                  1954364       0   1954364   0% /dev/shm
/dev/sda1               194241   33069    150932  18% /boot
/dev/mapper/vg0-usr   10190136 1953200   7712648  21% /usr
/dev/mapper/vg0-var   20511356  199612  19263168   2% /var
/dev/mapper/testvg-testlv
                       5029504   10236   4757124   1% /users

   再对testlv的文件系统扩容,命令resize2fs 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@localhost users]# resize2fs -f /dev/testvg/testlv 
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@localhost users]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg0-root   20G  287M   19G   2% /
tmpfs                 1.9G     0  1.9G   0% /dev/shm
/dev/sda1             190M   33M  148M  18% /boot
/dev/mapper/vg0-usr   9.8G  1.9G  7.4G  21% /usr
/dev/mapper/vg0-var    20G  195M   19G   2% /var
/dev/mapper/testvg-testlv
                      6.8G   12M  6.5G   1% /users

   回头再确认一下/users下的test是否还在

1
2
[root@localhost users]# cat test.txt 
Thu Aug 27 18:45:46 CST 2015

   经验证test.txt没有损坏,任务完成

   任务3,下面进行逻辑卷缩减实验

    首先需要卸载/dev/testvg/testlv

1
2
[root@localhost users]# cd ..
[root@localhost /]# umount /dev/testvg/testlv

    进行文件检查

1
2
3
4
5
6
7
8
[root@localhost /]# 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/458752 files (0.0% non-contiguous), 64448/1835008 blocks

   检查OK,进入一步,文件系统缩减

1
2
3
4
[root@localhost /]# 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.

    最后缩减逻辑卷testlv

1
2
3
4
5
6
[root@localhost /]# 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 7.00 GiB (448 extents) to 3.00 GiB (192 extents).
  Logical volume testlv successfully resized

   把缩减后的逻辑卷testlv挂载至/users下面,检查

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost /]# mount /dev/testvg/testlv /users/
[root@localhost /]# df -h #容量检查
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg0-root   20G  287M   19G   2% /
tmpfs                 1.9G     0  1.9G   0% /dev/shm
/dev/sda1             190M   33M  148M  18% /boot
/dev/mapper/vg0-usr   9.8G  1.9G  7.4G  21% /usr
/dev/mapper/vg0-var    20G  195M   19G   2% /var
/dev/mapper/testvg-testlv
                      2.9G  7.6M  2.7G   1% /users
[root@localhost /]# cat /users/test.txt #文件检查
Thu Aug 27 18:45:46 CST 2015

   testlv变成2.9G在一定范围可接受。文件test.txt没有损坏。

   任务4,对testlv做快照

1
2
[root@localhost users]# lvcreate -s -L 1G -p r -n test_snap_shot /dev/testvg/testlv 
  Logical volume “test_snap_shot” created

   检查快照

1
2
3
4
[root@localhost users]# mount /dev/testvg/test_snap_shot /mnt
mount: block device /dev/mapper/testvg-test_snap_shot is write-protected, mounting read-only
[root@localhost users]# cat /mnt/test.txt 
Thu Aug 27 18:45:46 CST 2015

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

(0)
islandisland
上一篇 2017-03-16 19:54
下一篇 2017-03-16 20:01

相关推荐

  • 磁盘管理的相关知识

    1.设备文件:关联至一个设备驱动程序,进而与之相对应设备文件通信 2.设备号码:     主设备号:major number 标识文件类型     次设别号:minor number  同一类型下不同设备 3磁盘组成结构: .    &nb…

    Linux干货 2016-08-30
  • 马哥教育网络班22期第五周博客作业

    正则表达式与grep 文本编辑工具     grep:是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来;     sed :是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用;     awk…

    Linux干货 2016-10-19
  • linux及其文件系统

       计算机是由CPU、存储设备(内存、硬盘)、I/O设备(键盘、鼠标、显示器)组成。CPU负责进行运算和控制;硬盘和内存负责永久和临时存储数据;Input设备负责输入指令或者数据,output设备负责输出处理过的数据。          Linux的发行版主要有Debian,S…

    Linux干货 2016-09-21
  • 马哥教育网络班21期+第14周课程练习

    iptables关键点 表:filter(过滤,防火墙);nat(网络地址转换);mangle(拆解报文,做出修改,封装报文);raw(关闭nat表启用的链接追踪机制);上述是根据功能来区分的,写规则要明白要实现的功能 链:PREROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING 数据流向:流入PREROUTING–…

    Linux干货 2016-08-26
  • Linux系统启动流程简介

    centos系统启动流程 本篇仅仅讲解centos5和6 centos7并不适用 Linux系统的组成部分:内核+根文件系统 内核功能: 进程管理 内存管理 网络管理 驱动程序 文件系统 安全功能 有以下目录结构的文件系统可以被识别为根文件系统,但根文件系统本身不存在 rootfs:/bin/ /sbin /etc/ /sys/…

    Linux干货 2016-09-10
  • 2016/10/26作业:用户和组的相关配置文件

    linux系统是通过文件来保存配置的,其中关于用户和组的配置文件包括以下几个: /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/login.defs /etc/default/useradd /etc/passwd 存放用户信息的配置文件,其基本格式如下 root:x:0:0:root:/root:/…

    Linux干货 2016-10-26