Monday, January 02, 2012

Quickly Mapping Linux VM SCSI Devices to SCSI Device IDs, maybe on VMware ESX4i Hosts

Long Title, but it's good to be precise.

I had an issue where, partway into a huge multi-day data- and drive-migration, I wasn't 100.0% sure I knew which drive I needed to pull on an ESXi VM after I'd moved off it on the OS side.  The process was simple, FWIW:
  1. add new VMware HDD
  2. echo "- - -" > /sys/class/scsi_host/host0/scan
    on the Linux (RHEL5) host.
  3. pvcreate
  4. vgextend
  5. pvmove   off the old one
  6. pvdisplay -C
    to check
  7. vgreduce vgname /dev/sdOldDiskPart
  8. pvremove /dev/sdOldDiskPart
  9. echo 1 > /sys/block/sdd/device/delete to remove the old disk
  10. remove old VMware HDD
When you come to that last step, though, you want to be clear as to which one you're pulling out, lest you replicate what we at the sweatshop call the Annihilated Barbarians incident and end up with an active drive yanked instead of the unused old drive you intended.  It's no fun.

So after some futzing around - it's a word; shut up - I fell across this:
# ls -ld /sys/block/sd?/device
lrwxrwxrwx 1 root root 0 Jan  2 16:49 /sys/block/sda/device -> ../../devices/pci0000:00/0000:00:10.0/host0/target0:0:0/0:0:0:0
lrwxrwxrwx 1 root root 0 Jan  2 16:49 /sys/block/sdb/device -> ../../devices/pci0000:00/0000:00:10.0/host0/target0:0:1/0:0:1:0
lrwxrwxrwx 1 root root 0 Jan  2 16:49 /sys/block/sdc/device -> ../../devices/pci0000:00/0000:00:10.0/host0/target0:0:2/0:0:2:0
lrwxrwxrwx 1 root root 0 Jan  2 16:49 /sys/block/sde/device -> ../../devices/pci0000:00/0000:00:10.0/host0/target0:0:6/0:0:6:0
lrwxrwxrwx 1 root root 0 Jan  2 16:49 /sys/block/sdf/device -> ../../devices/pci0000:00/0000:00:10.0/host0/target0:0:8/0:0:8:0
lrwxrwxrwx 1 root root 0 Jan  2 16:49 /sys/block/sdg/device -> ../../devices/pci0000:00/0000:00:10.0/host0/target0:0:9/0:0:9:0
lrwxrwxrwx 1 root root 0 Jan  2 16:49 /sys/block/sdh/device -> ../../devices/pci0000:00/0000:00:10.0/host0/target0:0:10/0:0:10:0
lrwxrwxrwx 1 root root 0 Jan  2 16:49 /sys/block/sdi/device -> ../../devices/pci0000:00/0000:00:10.0/host0/target0:0:11/0:0:11:0
lrwxrwxrwx 1 root root 0 Jan  2 16:49 /sys/block/sdj/device -> ../../devices/pci0000:00/0000:00:10.0/host0/target0:0:3/0:0:3:0
Trimming with some awk hackery:
# ls -ld /sys/block/sd?/device | awk -F/ '{print $4" "$11}'
sda target0:0:0
sdb target0:0:1
sdc target0:0:2
sde target0:0:6
sdf target0:0:8
sdg target0:0:9
sdh target0:0:10
sdi target0:0:11
sdj target0:0:3
I'm not thinking that's the best way to do things, but I'm hoping it's useful in the future -- at least to prevent the uncertainty while one's running on a (loose) clock.

Got a better way?  Do you know the canonical way?  Please let me know and I'll edit this thing to suit.