在Ubuntu 16.04上使用LVM的快照、恢复、分区拓展等操作

0.基础知识

LVM的系统结构,从物理磁盘还是分别分为PV(物理分区)、VG(卷组)、LV(逻辑卷)

img

最终,操作系统所使用的一切文件都是在逻辑卷上使用的,相当于在最普遍的物理分区使用方法上加入了VG和LV两个抽象层,从而实现快捷地扩缩容量、重建“分区”的目的

1.安装

Ubuntu 16.04 server安装的时候可以选择自动在硬盘上建立LVM,会自动名为“ubuntu-vg”的vg卷组,并在此卷组上建立两个LV分区,分别为ext4文件系统的名为“root”主分区和名为“swap”的swap分区

注:在此时请注意,默认会把硬盘所有可用容量分配给主分区,建议在安装系统时询问分配给主分区多少容量的时候自行减少想要用来做快照的容量

完成安装后可以使用vgs和lvs以及pvdisplay、vgdisplay、vgdisplay命令观察VG和LV(途中goSS为我已经建立的快照逻辑卷,还剩下15.93g的剩余空间,我会用剩下的空间再建立一个快照)

1
2
3
4
5
6
7
8
supermaxine@ubuntu:~$ sudo vgs
VG #PV #LV #SN Attr VSize VFree
ubuntu-vg 1 3 1 wz--n- 222.85g 15.93g
supermaxine@ubuntu:~$ sudo lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
goSS ubuntu-vg swi-a-s--- 12.00g root 10.73
root ubuntu-vg owi-aos--- 193.97g
swap_1 ubuntu-vg -wi-ao---- 976.00m
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
supermaxine@ubuntu:~$ sudo pvdisplay
--- Physical volume ---
PV Name /dev/sdb5
VG Name ubuntu-vg
PV Size 222.85 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 57050
Free PE 4078
Allocated PE 52972
PV UUID e6XiZv-irc2-xQRY-S64I-Is4F-WnCf-bnlyCg

supermaxine@ubuntu:~$ sudo vgdisplay
--- Volume group ---
VG Name ubuntu-vg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 43
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 222.85 GiB
PE Size 4.00 MiB
Total PE 57050
Alloc PE / Size 52972 / 206.92 GiB
Free PE / Size 4078 / 15.93 GiB
VG UUID 30o6yr-eYmt-P7wp-01wD-Ph6P-KwdH-5sByDp

supermaxine@ubuntu:~$ sudo lvdisplay
--- Logical volume ---
LV Path /dev/ubuntu-vg/root
LV Name root
VG Name ubuntu-vg
LV UUID qPNN2m-ietz-xIgm-PAzd-GFUe-eA0B-kmc4bo
LV Write Access read/write
LV Creation host, time ubuntu, 2020-02-23 14:39:25 +0800
LV snapshot status source of
goSS [active]
LV Status available
# open 1
LV Size 193.97 GiB
Current LE 49656
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:2

--- Logical volume ---
LV Path /dev/ubuntu-vg/swap_1
LV Name swap_1
VG Name ubuntu-vg
LV UUID u5bdZA-VTlx-A2e2-1YrY-tgWA-h7XD-7YhMUP
LV Write Access read/write
LV Creation host, time ubuntu, 2020-02-23 14:39:25 +0800
LV Status available
# open 2
LV Size 976.00 MiB
Current LE 244
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:6

--- Logical volume ---
LV Path /dev/ubuntu-vg/goSS
LV Name goSS
VG Name ubuntu-vg
LV UUID drvnYV-h504-x8ml-VcqI-EDvd-gXzc-6rtae6
LV Write Access read/write
LV Creation host, time ubuntu, 2020-02-23 22:38:10 +0800
LV snapshot status active destination for root
LV Status available
# open 0
LV Size 193.97 GiB
Current LE 49656
COW-table size 12.00 GiB
COW-table LE 3072
Allocated to snapshot 10.73%
Snapshot chunk size 4.00 KiB
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:5

2.建立快照

创建快照时,将创建一个新的逻辑卷,作为原逻辑卷的克隆。 快照卷最初不使用任何空间,但是当对原始卷进行更改时,更改的块在更改之前被复制到快照卷,以便保存它们。 这意味着对原点的更改越多,快照需要的空间就越多。 如果快照卷使用了分配给它的所有空间,那么快照就会中断,不能再使用,只剩下修改过的原点。 Lvs 命令将告诉您在逻辑卷快照中使用了多少空间。

1
2
3
supermaxine@ubuntu:~$ sudo lvcreate -s -n go -L 15g ubuntu-vg/root
[sudo] password for supermaxine:
Logical volume "go" created.

如果它开始满了,您可能需要使用 lvextend 命令扩展它。 要创建root逻辑卷的快照并命名为 go,请运行:

1
2
3
4
supermaxine@ubuntu:~$ sudo lvextend -L +0.9g ubuntu-vg/go
Rounding size to boundary between physical extents: 924.00 MiB
Size of logical volume ubuntu-vg/go changed from 15.00 GiB (3840 extents) to 15.90 GiB (4071 extents).
Logical volume go successfully resized.

3.合并快照

当您获得快照时,如果您愿意,您可以挂载它,并且可以看到原始文件系统,就像您创建快照时看到的那样。 在下面的示例中,您将挂载 /dev /ubuntu-vg/go 逻辑卷。 您可以在不影响原始分区的情况下修改快照,也可以再不影响快照的情况下使用修改原始分区。

1
2
3
4
5
6
7
8
9
supermaxine@ubuntu:~$ mkdir bak
supermaxine@ubuntu:~$ ls
bak
supermaxine@ubuntu:~$ sudo mount /dev/ubuntu-vg/go ~/bak
supermaxine@ubuntu:~$ cd bak/
supermaxine@ubuntu:~/bak$ ls
bin dev home lib lost+found mnt proc run snap sys usr vmlinuz
boot etc initrd.img lib64 media opt root sbin srv tmp var
supermaxine@ubuntu:~$ sudo umount ~/bak

如果您获取根逻辑卷的快照,然后升级一些包,或者升级到下一个整个发行版,然后认为它不能正常工作,那么您可以将快照合并回原始卷,有效地恢复到创建快照时的状态。 要做到这一点,你只需运行:

1
sudo lvconvert --merge ubuntu-vg/go

如果正在使用 ubuntu-vg/root 的原始卷,它将通知您在下次激活卷时将进行合并。 如果这是根卷,那么您需要重新引导才能实现。 在下一次启动时,卷将被激活,合并将在后台开始,因此您的系统将启动,就好像自从创建快照以来您从未做过更改一样,并且实际的数据移动将在您工作的时候在后台发生。

4.删除分区

如果您已经确定不需要旧的数据卷或者想要腾出空间建立新的数据卷,您可以使用以下命令删除数据卷

1
sudo lvremove ubuntu-vg/go

5.备份快照(待完成)

把快照中的数据导出

挂载数据卷

这一步与前文相同

导出
1
2
supermaxine@ubuntu:~$ cd bak/
supermaxine@ubuntu:~/bak$ sudo tar -cf ~/go.tar.gz *
还原

待实验