Monday, 31 March 2008

Magic SysRq Key

Nothing is perfect in this world, and even Linux sometimes crashes. But even then there is a connection with kernel - Magic SysRq Key.

What is this?

If you look to the keyboard then you see a strange key PrtSc / SysRq. Most people thinks that such key is for taking screenshots, but SysRq appeared on keyboards many years before Redmond behemoth.

Using SysRq key it is possible to force a Linux system to sync disks, remount disks into "read-only" mode, and then reboot computer safely. The key combination is intercepted directly by kernel, hence it is guaranty that Alt+SysRq is workable almost in any situations.

How we can use it?
First, we need to ensure that Magic SysRq key is enabled in kernel config: CONFIG_MAGIC_SYSRQ value must be Y (in most distributions it is so). To use such key, press and hold Alt, then press SysRq, release it and press another key - meanings of most useful keys are listed below

Simultaneously pressed Alt + SysRq +

'H'elp - prints to output all key combinations;

loglevel'0'-'8' - can change verbosity of the output from 0 (only critical messages) to 8 (most verbose mode);

re'B'oot - immediately reboot, like RESET button (no sync, no unmount filesystems);

'C'rashdump - runs kexec to reboot for obtaining errors info;

hol'D's - shows all blockers that blocks devices and files;

power'O'ff - correctly shuts down the system (if tweaked);

'S'ync - tries to sync all mounted filesystems and remount them in ReadOnly mode; you must see "Emergency Remount R/O" in console, and "Emergency Remount Complete" when it is over;

'U'nmount - tries to remount all mounted filesystems to ReadOnly mode;

show'T'asks - shows all tasks that are running now;

sho'W'-blocked-tasks - shows all uninterruptable tasks that are blocked or waiting I/O;

'F'ull - running oom_kill (out-of-memory kill) to kill applications that are eating all of memory;

secure access 'K'ey - to kill all tasks on the current console (don't do it on a console where X server running; in this case you can not see anything);

un'R'aw - takes away mouse and keyboard from X server; it it useful if X is trapped so you can change console and kill unwanted tasks;

t'E'rm - send SIGTERM signal to all processes except init.

K'I'LL - send SIGKILL signal (immediate termination) to all processes except init.

Thus for emergency sync you should press ALT+SysRq + s then ALT+SysRq + u and after that you can reset you computer or press ALT+SysRq+b

Except kernel documentation you may be interested in reading this and this links.