Linux - How To Safely Remove External Drives

December 11, 2012 | By
| Reply More

An external drive might be corrupted if it is not properly removed. When the data is being transferred to or from the drive, it must not be removed. You might lose data or it may also cause some bad sectors on the drive. The external drive must always be unmounted before removing. But does unmounting itself is sufficient for the health of drive? Even after unmounting, powering off the drive may cause unnecessary wear and to the drive. This is because the head of the drive will perform the emergency retract.

How to safely remove the external drive

So what is the solution?

First of all, all the partitions of the drive must be unmounted. Next, the drive must be spin down before removing the power. This can be performed with the sdparm utility provided in Linux. For the example of this article, UBUNTU machine is used. So first of all, we need to install this utility with apt-get. As root, running the following command will install the sdparm utility.

$ apt-get install sdparm
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
sdparm
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 0 B/103 kB of archives.
After this operation, 303 kB of additional disk space will be used.
Selecting previously deselected package sdparm.
(Reading database ... 164558 files and directories currently installed.)
Unpacking sdparm (from .../sdparm_1.06-2_i386.deb) ...
Processing triggers for man-db ...
Setting up sdparm (1.06-2) ..

.

$ sdparm -V
version: 1.06 20101031 [svn: r166]

Now, sdparm is installed. According to the manual page of sdparm utility,

"This utility fetches and potentially changes SCSI device (e.g. disk) mode pages. Inquiry data including Vital Product Data (VPD) pages can also be displayed. Commands associated with starting and stopping the medium; loading and unloading the medium; and other housekeeping function may also be issued by this utility."

"By default this utility shows mode pages that are common to all transport protocols. These are termed as "generic" mode pages."

So, running this utility on the hard drive will output the following:

$ sdparm /dev/sdb
/dev/sdb: ATA WDC WD3200BEVT-7 11.0
Read write error recovery mode page:
AWRE 1
ARRE 0
PER 0
Caching (SBC) mode page:
WCE 1
RCD 0
Control mode page:
SWP 0

But we are not concerned with the modes here. This was just for example purpose to show what this command is about. We are concerned with spinning down the hard disk. Issuing the stop command will spin down the medium. With -C option, commands can be given to sdparm utility. Running the following command will spin down /dev/sdb.

$ sdparm -C stop /dev/sdb
/dev/sdb: ATA WDC WD3200BEVT-7 11.0

Another utility, hdparm, used for getting or setting the hard disk parameters, can be used to spin down the disk. The -y option will perform the required function. According to the manual page of hdparm,

"Force an IDE drive to immediately enter the low power consumption standby mode, usually causing it to spin down. The current power mode status can be checked using the -C flag."

So, first of all, we check the current power status mode with -C option.

$ hdparm -C /dev/sdb

/dev/sdb:
drive state is: active/idle

Now, we issue the "hdparm -y" command on "/dev/sdb"

$ hdparm -y /dev/sda

/dev/sda:
issuing standby command

$ echo $?
0

The exit status 0 shows that the command is successfully executed.

Filed Under : HOWTOS, LINUX COMMANDS, LINUX HOWTO

Tagged With :

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.