Skip to main content
  1. 2022/
  2. February/

Recovering a VM Disk Image with fsck

·2 mins

Recovering a qcow2 image using fsck #

This was found as a gist here. I’m adapting it for proxmox nuances.

PreReqs #

Load network block device module if necessary #

the nbd kernel module is needed for this… do we have it enabled?

if [[ $(/usr/sbin/lsmod|grep -c nbd) == 1 ]]; then 
  echo 'Kernel module already loaded';
else
  echo "The nbd kernel module is not loaded. We need to load it."
  sudo /usr/sbin/modprobe nbd max_part=8
fi

Poweroff machine (if necessary) #

virsh destroy virtual-machine

Connect disk image: #

qemu-nbd --connect=/dev/nbd0 /var/lib/libvirt/images/virtual-machine.qcow2

Check disk: #

fdisk -l /dev/nbd0
Disk /dev/nbd0: 488.28 GiB, 524288000000 bytes, 1024000000 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: gpt
Disk identifier: 3E5BE263-297E-4E22-AA5D-CBBD412D5898

Device           Start        End   Sectors   Size Type
/dev/nbd0p1  470056960  486834175  16777216     8G Microsoft basic data
/dev/nbd0p2 1005123584 1021900799  16777216     8G Microsoft basic data
/dev/nbd0p3       2048    2099199   2097152     1G BIOS boot
/dev/nbd0p4 1021900800 1023997951   2097152     1G Microsoft basic data
/dev/nbd0p5    2099200  470056959 467957760 223.1G Microsoft basic data
/dev/nbd0p6  537165824 1005123583 467957760 223.1G Microsoft basic data
/dev/nbd0p7  486834176  537165823  50331648    24G Linux swap
iterate thru the partitions with filesystems #
for fs in 1 2 4 5 6; do 
  date; 
  echo "/dev/nbd0p${fs}"
  fsck -y /dev/nbd0p${fs}
done
fsck from util-linux 2.25.2
e2fsck 1.42.12 (29-Aug-2014)
/dev/nbd0p1: recovering journal
/dev/nbd0p1 contains a file system with errors, check forced.
Pass 1: Checking inodes, blocks, and sizes
Inodes that were part of a corrupted orphan linked list found.  Fix<y>? yes
Inode 274 was part of the orphaned inode list.  FIXED.
Inode 132276 was part of the orphaned inode list.  FIXED.
Deleted inode 142248 has zero dtime.  Fix<y>? yes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
Block bitmap differences:  -603674 -623174 +(689342--689343)
Fix<y>? yes
Free blocks count wrong for group #18 (15076, counted=15077).
Fix<y>? yes
Free blocks count wrong for group #19 (11674, counted=11675).
Fix<y>? yes
Free blocks count wrong (632938, counted=670871).
Fix<y>? yes
Inode bitmap differences:  -274 -132276 -142248
Fix<y>? yes
Free inodes count wrong for group #0 (52, counted=53).
Fix<y>? yes
Free inodes count wrong for group #16 (99, counted=100).
Fix<y>? yes
Free inodes count wrong for group #17 (519, counted=520).
Fix<y>? yes
Free inodes count wrong (204392, counted=204599).
Fix<y>? yes

/dev/nbd0p1: ***** FILE SYSTEM WAS MODIFIED *****
/dev/nbd0p1: 101833/306432 files (0.2% non-contiguous), 553321/1224192 blocks
Disconnect device: #
# qemu-nbd --disconnect /dev/nbd0
/dev/nbd0 disconnected
Start machine: #
# virsh start virtual-machine