Ubuntu – How to figure out which pty’s are from which qemu

lsofqemuUbuntu

I'm running qemu's like this:

$ sudo qemu -boot d -m 1024 \
-netdev tap,id=tap0 \
-device virtio-net-pci,netdev=tap0,id=vth0 \
-drive file=ubuntu.iso,media=cdrom,cache=none,if=ide \
-monitor pty \
-serial pty \
-parallel none \
-nographic

When I check /dev/pts/:

$ sudo lsof +d /dev/pts/

Qemu pty's do not show up, although they do work using for example:

$ sudo screen /dev/pts/8

How can I figure out which pty's are from which qemu?

Best Answer

You can do it this way using virsh along with some scripting:

$ for i in `virsh list | awk '{print $2}' | egrep -v "^$|Name"`; do
     printf "%-14s:%s\n" $i $(virsh ttyconsole $i | grep -v "^$");
  done

cobbler       :/dev/pts/1
xwiki         :/dev/pts/3
fan           :/dev/pts/4
mercury       :/dev/pts/5
mungr         :/dev/pts/0
win2008R2-01  :/dev/pts/7

Incidentally those same VMs through an lsof command:

$ lsof|grep qemu|grep ptmx
qemu-kvm   3796      root   14u      CHR                5,2         0t0        993 /dev/ptmx
qemu-kvm   3895      root   14u      CHR                5,2         0t0        993 /dev/ptmx
qemu-kvm   3972      root   14u      CHR                5,2         0t0        993 /dev/ptmx
qemu-kvm   4294      root   15u      CHR                5,2         0t0        993 /dev/ptmx
qemu-kvm  11897      root   14u      CHR                5,2         0t0        993 /dev/ptmx
qemu-kvm  16250      root   15u      CHR                5,2         0t0        993 /dev/ptmx

It doesn't look like lsof shows which pty they're using, just that they're using the ptmx. See the ptmx man page for more info.

References

The left side are the names of the VMs and the right side is the pts.

Related Question