Skip to content

标题: Linux Magic System Request Key Hacks

https://scz.617.cn/unix/201611291822.txt

参看内核源码中的"Documentation/sysrq.txt"

Q:

什么是"magic SysRq key"

A:

是一系列魔法按键,按下它们时,不管内核正在干什么都会立即响应,进行指定操作, 除非内核已经彻底失去响应。

echo "number" > /proc/sys/kernel/sysrq


0 disable sysrq completely 1 enable all functions of sysrq (default)

1 bitmask of allowed sysrq functions 2 0x2 enable control of console logging level 4 0x4 enable control of keyboard (SAK, unraw) 8 0x8 enable debugging dumps of processes etc. 16 0x10 enable sync command 32 0x20 enable remount read-only 64 0x40 enable signalling of processes (term, kill, oom-kill) 128 0x80 allow reboot/poweroff 256 0x100 allow nicing of all RT tasks


"/proc/sys/kernel/sysrq"只影响通过键盘引发"Magic System Request"。无论如何 总是允许通过"/proc/sysrq-trigger"引发"Magic System Request"。

Q:

如何按下魔法按键

A:

x86

Alt-SysRq-<command key>
Alt-PrScrn-<command key>

serial console

You send a BREAK, then within 5 seconds a command key.

Q:

都有哪些"Magic System Request"

A:

echo "..." > /proc/sysrq-trigger


b immediately reboot the system without syncing or unmounting your disks c perform a system crash by a NULL pointer dereference. A crashdump will be taken if configured d shows all locks that are held e send a SIGTERM to all processes, except for init f call oom_kill to kill a memory hog process g used by kgdb (kernel debugger) h display help (actually any other key than those listed here will display help) i send a SIGKILL to all processes, except for init j forcibly "Just thaw it" - filesystems frozen by the FIFREEZE ioctl k Secure Access Key (SAK) Kills all programs on the current virtual console l shows a stack backtrace for all active CPUs m dump current memory info to your console n used to make RT tasks nice-able o shut your system off (if configured and supported) p dump the current registers and flags to your console q dump per CPU lists of all armed hrtimers (but NOT regular timer_list timers) and detailed information about all clockevent devices r turns off keyboard raw mode and sets it to XLATE s attempt to sync all mounted filesystems t dump a list of current tasks and their information to your console u attempt to remount all mounted filesystems read-only v forcefully restores framebuffer console

causes ETM buffer dump [ARM-specific]

w dumps tasks that are in uninterruptable (blocked) state x used by xmon interface on ppc/powerpc platforms

show global PMU Registers on sparc64

y show global CPU Registers [SPARC-64 specific] z dump the ftrace buffer 0-9 sets the console_loglevel. for example 0 would make it so that only emergency messages like PANICs or OOPSes would make it to your console.


echo "h" > /proc/sysrq-trigger

dmesg | tail -1

[100383.935040] SysRq : HELP : ...

原来是单行输出,整理一下:

loglevel(0-9) reboot(b) crash(c) terminate-all-tasks(e) memory-full-oom-kill(f) kill-all-tasks(i) thaw-filesystems(j) sak(k) show-backtrace-all-active-cpus(l) show-memory-usage(m) nice-all-RT-tasks(n) poweroff(o) show-registers(p) show-all-timers(q) unraw(r) sync(s) show-task-states(t) unmount(u) show-blocked-tasks(w) dump-ftrace-buffer(z)

在我这个测试环境中,不支持"d"。