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)
上一篇 2017-03-16 19:54
下一篇 2017-03-16 20:01

相关推荐

  • 自制linux系统

    本文主要通过裁剪现有Linux系统,打造一个属于自己的Linux小系统,让其能够装载网卡驱动,并配置IP地址,实现网络功能。 自制linux系统 步骤概述: 1、新建一个硬盘2、在该新硬盘上新建两个分区,一个当boot分区,一个当/分区3、格式化并且挂载两个分区4、安装grub至目标磁盘5、为grub提供配置文件6、复制内核文件和initrd文件7、创建目标…

    Linux干货 2016-09-13
  • find命令、归档压缩工具、xargs、exec、tar、xz、cpio

    find命令、归档压缩工具、xargs、exec、tar、xz、cpio find命令 Linux中的文件查找工具常见的有locate和find以及whereis.他们适用于不同的场合,如whereis只能用于搜索程序的二进制文件、源代码文件和man手册等相关文件,find和local能够查找磁盘上的所有文件(不包括proc和sys目录下的虚拟文件)。fin…

    Linux干货 2016-08-18
  • path

    path简单应用

    Linux干货 2017-10-30
  • N25期—第四周作业

    1、 复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 cp –rf /etc/skel /home/tuser1 chmod -R go= /home/tuser1 2、 编辑/etc/group文件,添加组hado…

    Linux干货 2016-12-26
  • 路径操作&StringIO/BytesIO

    Edit 路径操作&StringIO/BytesIO 路径操作 路径操作模块: 3.4版本以前os.path模块 In [1]: from os import path In [2]: p = path.join(‘/etc’,’sysconfig’,’network’)#将字符…

    Linux干货 2017-10-30
  • python文件操作

    文件操作使用的函数open 打开read 读write 写close 关其他非常用的 seek 设置读取指针 tell 读取读取指针位置 windows中def encode(self, encoding=’utf-8′, errors=’strict’)open 和它的参数open(file,mode=&#8…

    Linux干货 2017-10-31