神奇的系统请求键SysRq

什么是SysRq

It is a 'magical' key combo you can hit which the kernel will respond to
regardless of whatever else it is doing, unless it is completely locked up.

如何使能SysRq?

内核配置SysRq

若使用SysRq,需要在内核配置时设置Magic SysRq key(CONFIG_MAGIC_SYSRQ)为Y,内核配置信息如下:

  CONFIG_MAGIC_SYSRQ:                                                 
                                                                      
  If you say Y here, you will have some control over the system even  
  if the system crashes for example during kernel debugging (e.g., you
  will be able to flush the buffer cache to disk, reboot the system   
  immediately or dump some status information). This is accomplished  
  by pressing various keys while holding SysRq (Alt+PrintScreen). It  
  also works on a serial console (on PC hardware at least), if you    
  send a BREAK and then within 5 seconds a command keypress. The      
  keys are documented in <file:Documentation/sysrq.txt>. Don't say Y  
  unless you really know what this hack does.                         
                                                                      
  Symbol: MAGIC_SYSRQ [=y]                                            
  Type  : boolean                                                     
  Prompt: Magic SysRq key                                             
    Location:                                                         
      -> Kernel hacking                                               
    Defined at lib/Kconfig.debug:352                                  
    Depends on: !UML                                                  
    Selected by: KGDB_SERIAL_CONSOLE [=y] && KGDB [=y] && TTY [=y] 

启用/禁用SysRq功能

/proc/sys/kernel/sysrq是内核提供给用户态可以控制SysRq启用与否的接口,如果/proc/sys/kernel/sysrq内容为0, 则SysRq被禁用; 如果/proc/sys/kernel/sysrq内容为1, 则SysRq被启用. 关于/proc/sys/kernel/sysrq的更多描述, 请参考path/to/kernel/Documentation/sysrq.txt

启用SysRq

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

禁用SysRq

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

如果需要永久启用或者禁用SysRq功能,则可在/etc/sysctl.conf中设置kernel.sysrq = 1 (启用SsyRq)或kernel.sysrq = 0 (禁用SysRq)

# sysctl kernel.sysrq
kernel.sysrq = 0

# sysctl -w kernel.sysrq=1
kernel.sysrq = 1

# vi /etc/sysctl.conf
...
# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 1
...

如何使用SysRq?

SysRq的命令键有

'r'     - Turns off keyboard raw mode and sets it to XLATE.

'k'     - Secure Access Key (SAK) Kills all programs on the current virtual
          console. NOTE: See important comments below in SAK section.

'b'     - Will immediately reboot the system without syncing or unmounting
          your disks.

'o'     - Will shut your system off (if configured and supported).

's'     - Will attempt to sync all mounted filesystems.

'u'     - Will attempt to remount all mounted filesystems read-only.

'p'     - Will dump the current registers and flags to your console.

't'     - Will dump a list of current tasks and their information to your
          console.

'm'     - Will dump current memory info to your console.

'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.)

'e'     - Send a SIGTERM to all processes, except for init.

'i'     - Send a SIGKILL to all processes, except for init.

'l'     - Send a SIGKILL to all processes, INCLUDING init. (Your system
          will be non-functional after this.)

'h'     - Will display help ( actually any other key than those listed
          above will display help. but 'h' is easy to remember :-)

在终端上同时按Alt, SysRq和命令键则会执行SysRq命令, SysRq键就是"Prent Screen"健. 比如Alt+SysRq+b则重启机器. 

如果使用telnet 或ssh连接到服务器则可以使用echo '<SysRq命令键>' > /proc/sysrq-trigger的方式执行SysRq命令, 比如echo 'b' > /proc/sysrq-trigger重启系统.

参考资料

# 最权威的文档, 最好参考当前运行的kernel附带的文档

<Kernel Source>/Documentation/sysrq.txt: sysrq.txt

# 使用SysRq键安全重启挂起的Linux

http://www.vpsee.com/2013/10/safe-reboot-linux-with-the-magic-sysrq-key/

你可能感兴趣的:(linux,sysrq)