VirtualBox & SysRq

Sometimes I need to send sysrq keys to a Linux virtual machine and I always forget how to do this, so here it is:

VBoxManage controlvm <VM> keyboardputscancode 1d 38 54 <PRESS> <RELEASE> d4 b8 9d
The PRESS and RELEASE values are derived from the scancodes: The PRESS value is the bare scancode, the RELEASE value is the PRESS value plus 0x80.

So, to send a "s" (to Sync the filesystems), the scancode would be 0x1F. And 0x1F + 0x80 equals 9F, this would be the scancode for releasing the key. Putting this all together, sending sysrq-s to the virtual machine goes like this:
VBoxManage controlvm <VM> keyboardputscancode 1D 38 54 1F 9F D4 B8 9D
Note: Be sure to set kernel.sysrq = 1 in your Linux guest machine, so that sysrq-keycodes are actually honored by the guest.

This can also be used to switch to a different terminal (if you have a VirtualBox console window open):
VBoxManage controlvm <VM> keyboardputscancode 1d 38 3b
This is the equivalent to Ctrl-Alt-F1 and would switch to the first terminal. Iterate 0x3b up to 0x42 to switch up to Ctrl-Alt-F8.

Update: I finally released the wrapper script to send sysrq keycodes to a VirtualBox VM.