操作系统

$ cat /etc/os-release 
PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
NAME="Debian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

LVM

$ sudo lvm version
  LVM version:     2.02.168(2) (2016-11-30)
  Library version: 1.02.137 (2016-11-30)
  Driver version:  4.35.0

硬盘情况

其中 debianA 和 NewVirtualDisk1 用于操作系统安装,后两块用作本例 LVM 配置。

2018-05-05-11-05-43----

磁盘分区

似乎将磁盘分区用作 LVM 更符合习惯,但其优势和好处有待考察。创建分区使用 fdisk 和 parted 皆可。

$ sudo fdisk /dev/sdc                                  
Welcome to fdisk (util-linux 2.29.2).

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-20971519, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): 

Created a new partition 1 of type 'Linux' and of size 10 GiB.

Command (m for help): p
Disk /dev/sdc: 10 GiB, 10737418240 bytes, 20971520 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x7db971e8

Device     Boot Start      End  Sectors Size Id Type
/dev/sdc1        2048 20971519 20969472  10G 83 Linux

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

查看可用于 LVM 的磁盘

如下,/dev/sdc1/dev/sdd1 是本例中要用作 LVM 的磁盘分区。

$ sudo lvmdiskscan 
  /dev/debian-vg/swap [       1.91 GiB] 
  /dev/sda1           [      10.00 GiB] LVM physical volume
  /dev/debian-vg/root [      18.09 GiB] 
  /dev/sdb1           [      10.00 GiB] LVM physical volume
  /dev/sdc1           [      10.00 GiB] 
  /dev/sdd1           [      10.00 GiB] 
  2 disks
  2 partitions
  0 LVM physical volume whole disks
  2 LVM physical volumes

创建 PV 物理卷

创建 VG 卷组

创建 data-vg 卷组

$ sudo vgcreate data-vg /dev/sdc1 /dev/sdd1
  Physical volume "/dev/sdc1" successfully created.
  Physical volume "/dev/sdd1" successfully created.
  Volume group "data-vg" successfully created

查看卷组

$ sudo vgs
  VG        #PV #LV #SN Attr   VSize  VFree 
  data-vg     2   0   0 wz--n- 19.99g 19.99g
  debian-vg   2   2   0 wz--n- 19.99g     0 

创建 LV 逻辑卷

创建名为 storage 的逻辑卷

$ sudo lvcreate data-vg --name storage -l 100%VG
  Logical volume "storage" created.

查看 LV

$ sudo lvs
  LV      VG        Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  storage data-vg   -wi-a----- 19.99g                                                    
  root    debian-vg -wi-ao---- 18.09g                                                    
  swap    debian-vg -wi-ao----  1.91g                                                    

格式化 LV

把逻辑卷 storage 格式化为 ext4 类型

$ sudo mkfs.ext4 /dev/data-vg/storage

注意:暂时不要格式化为 xfs 格式,坏盘恢复时暂时没有成功恢复文件系统的经验。

查看 LV 分区类型

LV 和普通磁盘分区一样,正常用 mount 挂载 LV。可以用 df -Th 查看分区类型。

$ sudo df -Th
文件系统                     类型      容量  已用  可用 已用% 挂载点
udev                         devtmpfs  487M     0  487M    0% /dev
tmpfs                        tmpfs     100M  2.9M   97M    3% /run
/dev/mapper/debian--vg-root  xfs        19G 1003M   18G    6% /
tmpfs                        tmpfs     499M     0  499M    0% /dev/shm
tmpfs                        tmpfs     5.0M     0  5.0M    0% /run/lock
tmpfs                        tmpfs     499M     0  499M    0% /sys/fs/cgroup
tmpfs                        tmpfs     100M     0  100M    0% /run/user/1000
/dev/mapper/data--vg-storage xfs        20G   53M   20G    1% /var/data

查看磁盘 UUID

$ sudo blkid
/dev/sda1: UUID="5jlaFz-9eaG-Sab5-jgTQ-DQ2q-ItfU-B6271m" TYPE="LVM2_member" PARTUUID="821d50e9-01"
/dev/sdb1: UUID="9Yj3oi-n5cE-zZOC-NYNH-Axpa-YmP7-fs4yPY" TYPE="LVM2_member" PARTUUID="9c9eba88-01"
/dev/sdc1: UUID="mne7Wa-j4Xw-MNVN-35v2-d1cA-pmfY-3ffnKE" TYPE="LVM2_member" PARTUUID="7db971e8-01"
/dev/sdd1: UUID="A6JfWT-zm6Y-Xefk-a35I-lwQH-93Qi-SDOENd" TYPE="LVM2_member" PARTUUID="6c875878-01"
/dev/mapper/debian--vg-swap: UUID="5e60dfda-85b2-4020-8ca2-4c0568cd73e5" TYPE="swap"
/dev/mapper/debian--vg-root: UUID="ce86d2f8-16b2-4050-b118-36632dd941fc" TYPE="xfs"
/dev/mapper/data--vg-storage: UUID="0a7307b5-6f16-4cc4-9b17-de20099500ad" TYPE="xfs"

生成测试文件

使用 dd 命令可以快速在当前目录生成特定数量和大小的文件

生成大小为 1GB 的文件

dd if=/dev/zero of=test bs=1M count=1000

生成 100 个大小为 100MB 的占位文件

/var/data$ seq 100 | xargs -i dd if=/dev/zero of={}.dat bs=99999999 count=1

LVM 替换坏盘

VG 中的硬盘发生损坏时,故障恢复思路为,移除损坏的磁盘,换上新的硬盘。使用新硬盘创建 PV 将其 UUID 设置成损坏硬盘的 UUID。然后恢复 VG 元数据,激活 VG。最后 e2fs 检查并修复恢复后的 LV 文件系统。

模拟硬盘损坏

关掉虚拟机,移除 data-vg 中的一块硬盘 /dev/sdd。创建并添加新的虚拟硬盘。

$ sudo pvs
  WARNING: Device for PV A6JfWT-zm6Y-Xefk-a35I-lwQH-93Qi-SDOENd not found or rejected by a filter.
  PV         VG        Fmt  Attr PSize  PFree
  /dev/sda1  debian-vg lvm2 a--  10.00g    0 
  /dev/sdb1  debian-vg lvm2 a--  10.00g    0 
  /dev/sdc1  data-vg   lvm2 a--  10.00g    0 
  [unknown]  data-vg   lvm2 a-m  10.00g    0 
$ sudo pvscan 
  WARNING: Device for PV A6JfWT-zm6Y-Xefk-a35I-lwQH-93Qi-SDOENd not found or rejected by a filter.
  PV /dev/sdc1   VG data-vg         lvm2 [10.00 GiB / 0    free]
  PV [unknown]   VG data-vg         lvm2 [10.00 GiB / 0    free]
  PV /dev/sda1   VG debian-vg       lvm2 [10.00 GiB / 0    free]
  PV /dev/sdb1   VG debian-vg       lvm2 [10.00 GiB / 0    free]
  Total: 4 [39.98 GiB] / in use: 4 [39.98 GiB] / in no VG: 0 [0   ]                          

使用损毁硬盘的 UUID 创建新 PV

/dev/sdd 是新换上的硬盘

$ sudo lvmdiskscan 
  /dev/debian-vg/swap [       1.91 GiB] 
  /dev/sda1           [      10.00 GiB] LVM physical volume
  /dev/debian-vg/root [      18.09 GiB] 
  /dev/sdb1           [      10.00 GiB] LVM physical volume
  /dev/sdc            [      10.00 GiB] LVM physical volume
  /dev/sdd            [      10.00 GiB] 
  3 disks
  0 partitions
  1 LVM physical volume whole disk
  2 LVM physical volumes

下方命令第一行警告信息中 62ev4t-A0gU-F8K4-iQPX-q1P5-CuKA-gHgbUE 即为损坏 PV 的 UUID。

$ sudo pvs
  WARNING: Device for PV 62ev4t-A0gU-F8K4-iQPX-q1P5-CuKA-gHgbUE not found or rejected by a filter.
  PV         VG        Fmt  Attr PSize  PFree
  /dev/sda1  debian-vg lvm2 a--  10.00g    0 
  /dev/sdb1  debian-vg lvm2 a--  10.00g    0 
  /dev/sdc   data-vg   lvm2 a--  10.00g    0 
  [unknown]  data-vg   lvm2 a-m  10.00g    0 

查看 VG 的配置文件:

# ls -lah /etc/lvm/archive/
总用量 8.0K
drwx------ 2 root root   74 5月   6 12:38 .
drwxr-xr-x 4 root root   51 5月   4 17:36 ..
-rw------- 1 root root 1.1K 5月   6 12:37 data-vg_00000-745144895.vg
-rw------- 1 root root 1.1K 5月   6 12:38 data-vg_00001-178499820.vg

查看最新的一个配置文件中的信息:

# cat /etc/lvm/archive/data-vg_00001-178499820.vg 
# Generated by LVM2 version 2.02.168(2) (2016-11-30): Sun May  6 12:38:27 2018

contents = "Text Format Volume Group"
version = 1

description = "Created *before* executing 'lvcreate data-vg -n home -l 100%VG'"

creation_host = "vboxdebian"	# Linux vboxdebian 4.9.0-6-amd64 #1 SMP Debian 4.9.88-1 (2018-04-29) x86_64
creation_time = 1525581507	# Sun May  6 12:38:27 2018

data-vg {
	id = "lDcIRO-FGuT-zPeT-je3x-lKGT-Gf9W-7G0ykw"
	seqno = 1
	format = "lvm2"			# informational
	status = ["RESIZEABLE", "READ", "WRITE"]
	flags = []
	extent_size = 8192		# 4 Megabytes
	max_lv = 0
	max_pv = 0
	metadata_copies = 0

	physical_volumes {

		pv0 {
			id = "R2lV9p-Evh1-rVuH-G6iX-CRIH-mWvJ-tQeOB0"
			device = "/dev/sdc"	# Hint only

			status = ["ALLOCATABLE"]
			flags = []
			dev_size = 20971520	# 10 Gigabytes
			pe_start = 2048
			pe_count = 2559	# 9.99609 Gigabytes
		}

		pv1 {
			id = "62ev4t-A0gU-F8K4-iQPX-q1P5-CuKA-gHgbUE"
			device = "/dev/sdd"	# Hint only

			status = ["ALLOCATABLE"]
			flags = []
			dev_size = 20971520	# 10 Gigabytes
			pe_start = 2048
			pe_count = 2559	# 9.99609 Gigabytes
		}
	}


}

创建 PV,这里切换成了 root 用户身份,普通用户无法访问 /etc/lvm/archive/ 目录中的卷组信息备份归档文件。

# pvcreate --uuid "62ev4t-A0gU-F8K4-iQPX-q1P5-CuKA-gHgbUE" --restorefile /etc/lvm/archive/data-vg_00001-178499820.vg /dev/sdd
  Couldn't find device with uuid 62ev4t-A0gU-F8K4-iQPX-q1P5-CuKA-gHgbUE.
  WARNING: Device for PV 62ev4t-A0gU-F8K4-iQPX-q1P5-CuKA-gHgbUE not found or rejected by a filter.
  Physical volume "/dev/sdd" successfully created.

恢复 VG 元数据

# vgcfgrestore data-vg
  Restored volume group data-vg

重新激活 VG

# vgchange -a y data-vg
  1 logical volume(s) in volume group "data-vg" now active

修复 LV 上的文件系统

文件系统类型 ext2/ext3/ext4

e2fsck /dev/data-vg/home

参考资料