SysRq使用摘录


Creative Commons LicenseCreative Commons LicenseCreative Commons License

1 什么是SysRq

SysRq 经常被称为 Magic System Request,它被定义为一系列按键组合。之所以说它神奇,是因为它在
系统挂起,大多数服务已无法响应的情况下,还能通过按键组合来完成一系列预先定义的系统操作。通过它,
不但可以在保证磁盘数据安全的情况下重启一台挂起的服务器,避免数据丢失和重启后长时间的文件系统检查,
还可以收集包括系统内存使用,CPU 任务处理,进程运行状态等系统运行信息,甚至还可能在无需重启的情况
下挽回一台已经停止响应的服务器。

[IBM-Developerwork:l-cn-sysRq]中对SysRq在PC上的使用描写已经比较全面。可以先阅读这篇文章。

2 SysRq的procfs接口[sysrq.txt]

嵌入式设备通常不支持按键组合的方式来使用SysRq,在Linux内核中提供的procfs接口来控制和访问SysRq,相关的接口:

1
2
/proc/sys/kernel/sysrq  
/proc/sysrq-trigger

打开上述接口需要开启内核配置项CONFIG_MAGIC_SYSRQ

2.1 /proc/sys/kernel/sysrq接口说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
主要是用来控制使能哪些功能被SysRq Key使用
可用的值有:
0 - 完全禁用sysrq
1 - 打开sysrq的所有功能
各个功能使能的位掩码值如下:
2 = 0x2 - enable control of console logging level
4 = 0x4 - ennable 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
在串口控制台上可以使用echo命令来设置sysrq的值:
echo "number" >/proc/sys/kernel/sysrq
number的值可以是10进制也可以是16进制的格式(带0x前缀)

2.2 /proc/sysrq-trigger接口说明

主要是代替按键来使用SysRq Key.使用它只需要写相应的字符到这个procfs文件接口就可以:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
echo t > /proc/sysrq-trigger  //其中的t值可以是以下这些值
'b' - Will immediately reboot the system without syncing or unmounting
your disks.
'c' - Will 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' - Will call oom_kill to kill a memory hog process.
'g' - Used by kgdb (kernel debugger)
'h' - Will display help (actually any other key than those listed
here will display help. but 'h' is easy to remember :-)
'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. NOTE: See important comments below in SAK section.
'l' - Shows a stack backtrace for all active CPUs.
'm' - Will dump current memory info to your console.
'n' - Used to make RT tasks nice-able
'o' - Will shut your system off (if configured and supported).
'p' - Will dump the current registers and flags to your console.
'q' - Will 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' - Will attempt to sync all mounted filesystems.
't' - Will dump a list of current tasks and their information to your
console.
'u' - Will attempt to remount all mounted filesystems read-only.
'v' - Forcefully restores framebuffer console
'v' - 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 log level, controlling which kernel messages
will be printed to your console. ('0', for example would make
it so that only emergency messages like PANICs or OOPSes would
make it to your console.)

-------------本文结束感谢您的阅读-------------
如果文章对您有帮助,也可以打赏支持喔!