Skip to content

Instantly share code, notes, and snippets.

@khirbat
Last active December 8, 2025 18:42
Show Gist options
  • Select an option

  • Save khirbat/35fc069e355e71db8e9e6d82fd87585a to your computer and use it in GitHub Desktop.

Select an option

Save khirbat/35fc069e355e71db8e9e6d82fd87585a to your computer and use it in GitHub Desktop.
Look inside .qcow2 files
pi@4a:~ $ ./vm-explore.sh nocloud_alpine-3.20.3-aarch64-uefi-cloudinit-r0.qcow2 -M alpine
Mounted /dev/nbd0p2 at /media/root/_
Spawning container alpine on /media/root/_.
Press ^] three times within 1s to kill container.
alpine:~#
pi@4a:~ $ ./vm-explore.sh openwrt-23.05.2-armsr-armv8-generic-ext4-combined.qcow2
Mounted /dev/nbd0p2 at /media/root/rootfs
Spawning container rootfs on /media/root/rootfs.
Press ^] three times within 1s to kill container.
_______ ________ __
| |.-----.-----.-----.| | | |.----.| |_
| - || _ | -__| || | | || _|| _|
|_______|| __|_____|__|__||________||__| |____|
|__| W I R E L E S S F R E E D O M
-----------------------------------------------------
OpenWrt 23.05.2, r23630-842932a63d
-----------------------------------------------------
=== WARNING! =====================================
There is no root password defined on this device!
Use the "passwd" command to set up a new password
in order to prevent unauthorized SSH logins.
--------------------------------------------------
#!/usr/bin/env -S sudo bash
set -x
img=$1
part=$2
if [[ "$1" == *.qcow2 || "$1" == *.img ]]; then
img="$1"
else
img=/var/lib/libvirt/images/$1.qcow2
fi
NBD=/dev/nbd0
function cleanup () {
udisksctl unmount -b "/dev/$part"
qemu-nbd -d "$NBD"
}
function find-part () {
lsblk -bOJ "$NBD" | jq -r '.blockdevices[].children | map(select(.fstype != "vfat")) | max_by(.size).name'
}
trap cleanup EXIT
if ! test -f "$img"; then
echo "$img" does not exist
exit 1
fi
modprobe nbd
qemu-nbd -d "$NBD" || exit 1
qemu-nbd -c "$NBD" "$img"
sleep 0.5
fdisk -l "$NBD"
if [[ "$part" == "" ]]; then
part=$(find-part)
fi
echo PART: "$part"
sleep 0.5
udisksctl mount -b "/dev/$part"
mnt="$(udisksctl info -b "/dev/$part" | grep -Po '^ *MountPoints: *\K.*')"
#bash -l
hostname=$(basename $img)
hostname=${hostname%.*}
if ! systemd-nspawn -M $hostname -D "$mnt" "${@:2}"; then # Fedora needs "$mnt/root"
bash --rcfile <(echo "cd $mnt")
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment