在 Windows 的磁盘管理器里,在列出的一块硬盘上右键,可以找到一个菜单项,使得所选硬盘离线(刚刚看了一眼,中文写的是“脱机”)。这个操作的后果就是,让这块盘在整个系统中消失,但又不同于物理层面的拔掉连接线,因为还可以再把它“联机”回来。在 Linux 有没有类似的功能呢?查了一下资料,找到多年前就有人写就的一段脚本。
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
#!/bin/bash # AUTHOR: Tommy Butler # # DESCRIPTION: # Run this script to offline and delete a disk from your Linux system. # It should work for most people, but if you've got an old kernel it may not. # Unless you know what you're doing, DO NOT USE THIS SCRIPT! # # LICENSE: Perl Artistic License - http://dev.perl.org/licenses/artistic.html # # DISCLAIMER AND LIMITATION OF WARRANTY: # This software is distributed in the hope that it will be useful, but without # any warranty; without even the implied warranty of merchantability or fitness # for a particular purpose. USE AT YOUR OWN RISK. I ASSUME NO LIABILITY. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin DISK=$1; if [[ `id -u` -ne 0 ]]; then echo Run this script with root or sudo exit 1 fi while true; do [[ "$DISK" != "" ]] && break; read -p 'Enter the name of the disk you want to offline and delete: ' DISK done if [[ "$( expr substr $DISK 1 4 )" == '/dev' ]]; then DISK=$( expr substr $DISK 6 10 ) fi if [[ ! -e /sys/block/$DISK ]]; then echo No entry for /dev/$DISK was not found in /sys/block/ - Cannot continue exit 1 fi echo Are you sure you want to offline and delete /dev/${DISK}? select yn in "Yes" "No"; do case $yn in Yes ) break;; No ) exit;; esac done echo offline > /sys/block/$DISK/device/state echo 1 > /sys/block/$DISK/device/delete echo DONE exit; |
其下有人追问,那如何才能再让它“联机”回来。另一个人做了一个简单回答。
(脚本中的)offline 和 1 是不同的(作用),前者使得磁盘在系统的块设备子系统中不可用,而后者会在物理上通知驱动器断电。重新扫描的动作不会使得系统将离线的磁盘加回来的原因在于,内核记忆了它的离线状态。要想在线回来的话,有个吊诡的地方是,需要使用的状态词不是直觉的 online,而是 running。但在 offline 和 running 之间切换的前提是不能 delete。