Skip to content

Instantly share code, notes, and snippets.

@khirbat
Last active November 23, 2024 17:25
Show Gist options
  • Save khirbat/35fc069e355e71db8e9e6d82fd87585a to your computer and use it in GitHub Desktop.
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
img=$1
if [[ "$1" == *.qcow2 ]]; then
img="$1"
else
img=/var/lib/libvirt/images/$1.qcow2
fi
if ! test -f "$img"; then
echo $img does not exist
exit 1
fi
modprobe nbd
qemu-nbd -c /dev/nbd0 "$img"
sleep 0.5
p=$(lsblk -bJ /dev/nbd0 | jq -r '.blockdevices[].children | max_by(.size).name')
udisksctl mount -b /dev/$p
systemd-nspawn -D $(udisksctl info -b /dev/$p | grep -Po '^ *MountPoints: *\K.*') "${@:2}"
udisksctl unmount -b /dev/$p
qemu-nbd -d /dev/nbd0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment