OPS Notes By 枯木

本地fsck修复qcow2

| Comments

因为一些原因导致若干kvm虚拟机出现启动不了的情况,如下图:

这应该是文件系统破坏的问题,一般情况下还可以在虚拟机中fsck修复文件系统,但是这种情况在虚拟机中根本就没有机会fsck,所以想到通过外界方法对虚拟磁盘进行fsck。创建虚拟机使用的虚拟磁盘的类型是qcow2,开机无法fsck的话,可以通过如下两种方式:

  • 通过启动其它虚拟机的时候指定损坏虚拟磁盘启动再修复
  • 通过qemu-nbd工具本地宿主机上修复qcow2

这里介绍第二种方式,直接在宿主机上修复损坏qcow2磁盘,利用qemu-nbd把qcow2映射为网络设备Network block device

加载nbd

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# modprobe nbd max_part=8 # max_part表示每个设备的分区,根据实际情况修改,默认为0
# modinfo nbd   # 查看nbd相关信息
filename:       /lib/modules/3.5.0-23-generic/kernel/drivers/block/nbd.ko
license:        GPL
description:    Network Block Device
srcversion:     B540FE0119F7C28B9D15C21
depends:
intree:         Y
vermagic:       3.5.0-23-generic SMP mod_unload modversions
parm:           nbds_max:number of network block devices to initialize (default: 16) (int)
parm:           max_part:number of partitions per device (default: 0) (int)
parm:           debugflags:flags for controlling debug output (int)
# ls /dev/nbd* # 默认有16个nbd设备文件
/dev/nbd0  /dev/nbd1  /dev/nbd10  /dev/nbd11  /dev/nbd12  /dev/nbd13
/dev/nbd14  /dev/nbd15  /dev/nbd2  /dev/nbd3  /dev/nbd4  /dev/nbd5
/dev/nbd6 /dev/nbd7  /dev/nbd8  /dev/nbd9

映射损坏qcow2磁盘为nbd

1
2
3
4
# qemu-nbd --connect=/dev/nbd0 /data/0/41/disk.qcow2 
# 映射disk.qcow2为本地的nbd0设备,qcow2需要为绝对路径
# ls /dev/nbd0* # 映射虚拟磁盘有两个分区
/dev/nbd0  /dev/nbd0p1  /dev/nbd0p2

本来准备修复了,但是发现发现之前的虚拟机分区是LVM设备,如果是正常分区直接修复就可以了,而LVM需要先LVM设备激活。

1
2
3
4
5
6
7
8
9
10
11
12
# fdisk -l /dev/nbd0

Disk /dev/nbd0: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders, total 62914560 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
Disk identifier: 0x00065194

     Device Boot      Start         End      Blocks   Id  System
/dev/nbd0p1   *          63      208844      104391   83  Linux
/dev/nbd0p2          208845    62910539    31350847+  8e  Linux LVM

激活LVM

安装lvm

1
2
3
# mount /dev/nbd0p2 /mnt/ # 说明没有安装LVM
mount: unknown filesystem type 'LVM2_member'
# apt-get install lvm2 -y

激活LVM

默认状态可能LVM没有激活,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# lvdisplay 
  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol00
  VG Name                VolGroup00
  LV UUID                VJkt2b-zSeD-DB23-XcfZ-1OHn-s0Ju-u7sXAN
  LV Write Access        read/write
  LV Status              NOT available
  LV Size                21.91 GiB
  Current LE             701
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto

  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol01
  VG Name                VolGroup00
  LV UUID                kIOZsr-0uYd-FFud-yEnk-x6x7-1GxC-Q1yEOJ
  LV Write Access        read/write
  LV Status              NOT available
  LV Size                7.97 GiB
  Current LE             255
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto

激活方式如下:

1
2
3
4
# vgchange -ay /dev/VolGroup00
  2 logical volume(s) in volume group "VolGroup00" now active
# ls /dev/VolGroup00/*
/dev/VolGroup00/LogVol00  /dev/VolGroup00/LogVol01

修复指定分区

1
2
3
4
5
6
7
8
# fsck -y /dev/VolGroup00/LogVol0
... ...
Free blocks count wrong (5069553, counted=5070543).
Fix? yes


/dev/mapper/VolGroup00-LogVol00: ***** FILE SYSTEM WAS MODIFIED *****
/dev/mapper/VolGroup00-LogVol00: 79036/5744640 files (0.7% non-contiguous), 672049/5742592 blocks

如上结果,最终修复磁盘成功。

取消映射

修复完成之后取消nbd映射,开启虚拟机即可,取消映射方法如下命令。

1
2
# qemu-nbd --disconnect /dev/nbd0
/dev/nbd0 disconnected

修复之后,顺利启动相关虚拟机,推荐开机之后再shutdown -F now强制修复一下,以防万一。

参考

–EOF–

Comments