How can I safely remove a SATA disk from a running system?












61















I sometimes need to plug a disk into a disk bay. At other times, I have the very weird setup of connecting a SSD using a SATA-eSATA cable on my laptop while pulling power from a desktop.



How can I safely remove the SATA disk from the system? This Phoronix forum thread has some suggestions:




justsumdood wrote:




An(noymous)droid wrote:

What then do you do on the software side before unplugging? Is it a simple "umount /dev/sd"[drive letter]?
after unmounting the device, to "power off" (or sleep) the unit:




hdparm -Y /dev/sdX


(where X represents the device you wish to power off. for example: /dev/sdb)



this will power the drive down allowing for it's removal w/o risk of voltage surge.




Does this mean that the disk caches are properly flushed and powered off thereafter?



Another suggestion from the same thread:




chithanh wrote:

All SATA and eSATA hardware is physically able to be hotplugged (ie. not damaged if you insert/pull the plug).



How the chipset and driver handles this is another question. Some driver/chipset combinations do not properly handle hotplugging and need a warmplug command such as the following one:



echo 0 - 0 > /sys/class/scsi_host/hostX/scan


Replace X with the appropriate number for your SATA/eSATA port.




I doubt whether is the correct way to do so, but I cannot find some proof against it either.



So, what is the correct way to remove an attached disk from a system? Assume that I have already unmounted every partition on the disk and ran sync. Please point to some official documentation if possible, I could not find anything in the Linux documentation tree, nor the Linux ATA wiki.










share|improve this question



























    61















    I sometimes need to plug a disk into a disk bay. At other times, I have the very weird setup of connecting a SSD using a SATA-eSATA cable on my laptop while pulling power from a desktop.



    How can I safely remove the SATA disk from the system? This Phoronix forum thread has some suggestions:




    justsumdood wrote:




    An(noymous)droid wrote:

    What then do you do on the software side before unplugging? Is it a simple "umount /dev/sd"[drive letter]?
    after unmounting the device, to "power off" (or sleep) the unit:




    hdparm -Y /dev/sdX


    (where X represents the device you wish to power off. for example: /dev/sdb)



    this will power the drive down allowing for it's removal w/o risk of voltage surge.




    Does this mean that the disk caches are properly flushed and powered off thereafter?



    Another suggestion from the same thread:




    chithanh wrote:

    All SATA and eSATA hardware is physically able to be hotplugged (ie. not damaged if you insert/pull the plug).



    How the chipset and driver handles this is another question. Some driver/chipset combinations do not properly handle hotplugging and need a warmplug command such as the following one:



    echo 0 - 0 > /sys/class/scsi_host/hostX/scan


    Replace X with the appropriate number for your SATA/eSATA port.




    I doubt whether is the correct way to do so, but I cannot find some proof against it either.



    So, what is the correct way to remove an attached disk from a system? Assume that I have already unmounted every partition on the disk and ran sync. Please point to some official documentation if possible, I could not find anything in the Linux documentation tree, nor the Linux ATA wiki.










    share|improve this question

























      61












      61








      61


      34






      I sometimes need to plug a disk into a disk bay. At other times, I have the very weird setup of connecting a SSD using a SATA-eSATA cable on my laptop while pulling power from a desktop.



      How can I safely remove the SATA disk from the system? This Phoronix forum thread has some suggestions:




      justsumdood wrote:




      An(noymous)droid wrote:

      What then do you do on the software side before unplugging? Is it a simple "umount /dev/sd"[drive letter]?
      after unmounting the device, to "power off" (or sleep) the unit:




      hdparm -Y /dev/sdX


      (where X represents the device you wish to power off. for example: /dev/sdb)



      this will power the drive down allowing for it's removal w/o risk of voltage surge.




      Does this mean that the disk caches are properly flushed and powered off thereafter?



      Another suggestion from the same thread:




      chithanh wrote:

      All SATA and eSATA hardware is physically able to be hotplugged (ie. not damaged if you insert/pull the plug).



      How the chipset and driver handles this is another question. Some driver/chipset combinations do not properly handle hotplugging and need a warmplug command such as the following one:



      echo 0 - 0 > /sys/class/scsi_host/hostX/scan


      Replace X with the appropriate number for your SATA/eSATA port.




      I doubt whether is the correct way to do so, but I cannot find some proof against it either.



      So, what is the correct way to remove an attached disk from a system? Assume that I have already unmounted every partition on the disk and ran sync. Please point to some official documentation if possible, I could not find anything in the Linux documentation tree, nor the Linux ATA wiki.










      share|improve this question














      I sometimes need to plug a disk into a disk bay. At other times, I have the very weird setup of connecting a SSD using a SATA-eSATA cable on my laptop while pulling power from a desktop.



      How can I safely remove the SATA disk from the system? This Phoronix forum thread has some suggestions:




      justsumdood wrote:




      An(noymous)droid wrote:

      What then do you do on the software side before unplugging? Is it a simple "umount /dev/sd"[drive letter]?
      after unmounting the device, to "power off" (or sleep) the unit:




      hdparm -Y /dev/sdX


      (where X represents the device you wish to power off. for example: /dev/sdb)



      this will power the drive down allowing for it's removal w/o risk of voltage surge.




      Does this mean that the disk caches are properly flushed and powered off thereafter?



      Another suggestion from the same thread:




      chithanh wrote:

      All SATA and eSATA hardware is physically able to be hotplugged (ie. not damaged if you insert/pull the plug).



      How the chipset and driver handles this is another question. Some driver/chipset combinations do not properly handle hotplugging and need a warmplug command such as the following one:



      echo 0 - 0 > /sys/class/scsi_host/hostX/scan


      Replace X with the appropriate number for your SATA/eSATA port.




      I doubt whether is the correct way to do so, but I cannot find some proof against it either.



      So, what is the correct way to remove an attached disk from a system? Assume that I have already unmounted every partition on the disk and ran sync. Please point to some official documentation if possible, I could not find anything in the Linux documentation tree, nor the Linux ATA wiki.







      ssd sata hot-plug disk






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jul 18 '12 at 20:04









      LekensteynLekensteyn

      9,943115288




      9,943115288






















          5 Answers
          5






          active

          oldest

          votes


















          75















          1. Unmount any filesystems on the disk. (umount ...)

          2. Deactivate any LVM groups. (vgchange -an)

          3. Make sure nothing is using the disk for anything.

          4. Once you've done that, it should be safe to unplug.


          If you want to be extra cautious, do echo 1 > /sys/block/(whatever)/device/delete first. That'll unregister the device from the kernel, so you know nothing's using it when you unplug it. When I do that with a drive in an eSATA enclosure, I can hear the drive's heads park themselves, so the kernel apparently tells the drive to prepare for power-down.



          If you're using an AHCI controller, it should cope with devices being unplugged. If you're using some other sort of SATA controller, the driver might be confused by hotplugging.



          In my experience, SATA hotplugging (with AHCI) works pretty well in Linux. I've unplugged an optical drive, plugged in a hard drive, scanned it for errors, made a filesystem and copied data to it, unmounted and unplugged it, plugged in a differerent DVD drive, and burned a disc, all with the machine up and running.






          share|improve this answer


























          • I had a need to unplug a HDD which I wanted to erase completely. After pulling the disk out of the bay, /dev/sdXY still showed up. Writing 1 to delete made it disappear and I could hear the disk spinning down. Just hdparm -Y was not enough because the /dev/ entries would still exist. Thanks!

            – Lekensteyn
            Aug 9 '12 at 9:25








          • 12





            I strongly advice to always issue the echo 1 > /sys/block/(whatever)/device/delete command because the drive will park the heads, fully stop the disk and disable power on the bus. If a non-parked head touches a spinning plate the drive can be permanently destroyed.

            – drumfire
            Feb 12 '14 at 21:21






          • 2





            Also - if smartd happens to be running, it's a good idea to issue a SIGHUP to the process so that it reloads the drive info. Especially important if you're replacing drives because smartd will reload the info for that drive and all others.

            – drumfire
            Feb 12 '14 at 21:23






          • 1





            Just one note worth mentioning, echo 1 > /sys/block/(whatever)/device/delete will not work under sudo, the "Permission denied" error will be thrown. You'll need to become a real root, so use plain old su instead.

            – TranslucentCloud
            Mar 29 '15 at 10:59






          • 10





            @TranslucentCloud You can pipe it through sudo tee to emulate a redirect as root: echo 1 | sudo tee /sys/block/(whatever)/device/delete

            – Oli
            Apr 15 '15 at 20:54



















          5














          Those two sections are for different things.



          The first is for unplugging. The second is for plugging.



          For unplugging, the OS will sync the data during the unmount operation. Thus, if the disk is unmounted (assuming you in fact do have full hardware support) you can power off the disk then unplug it without risk of data loss or corruption.



          For plugging, the device should be automatically recognized. If not, you can execute that command to trigger a bus scan. Once the device is recognized you can mount it.



          And let me caveat this by saying I've only ever done this sort of thing with USB drives.






          share|improve this answer
























          • Thanks for your answer, but I am still not convinced what the correct action would be. SSDs have a "Unsafe Shutdown Count" S.M.A.R.T. field, just unplugging it without doing anything does not sound safe to me.

            – Lekensteyn
            Jul 18 '12 at 21:58






          • 3





            If it helps, I routinely hot-plug and hot-unplug SATA hard drives as part of my job, with no more than making sure it's unmounted first, and I've never run into a problem. That's anecdotal, so don't take it as gospel, but it's at least some evidence that it's likely safe. In any case, I'd consider it a kernel bug if the OS doesn't ensure that the data is fully written at the end of an unmount operation, especially in a hotplug world.

            – Jander
            Jul 19 '12 at 0:24






          • 1





            @Lekensteyn, the hdparm -Y will take care of that. That is basically what the system does every time you suspend or shutdown.

            – psusi
            Jul 19 '12 at 13:16



















          5














          what about eject /dev/sdX? On my setup, this commands umounts, syncs and powers down the drive.






          share|improve this answer





















          • 4





            I tried this with a disk connected over eSATA but the command failed with "not hotpluggable" or something.

            – Lekensteyn
            Aug 30 '13 at 9:29






          • 1





            Afaik eject plugs out the pluggable disk and not the device. It depends on that the block device driver supports the ioctl() operation used by the eject tool. Esata hard disk drives don't support it, but optical devices, floppies and probably flash drives, yes.

            – peterh
            Jul 31 '15 at 4:11



















          2














          I have a pair of scripts that build upon the answer by Wyzard. The first, scsi-drop, is to safely detach a single disk:



          #!/bin/sh

          if test -h "$1"
          then
          disk=$(chase "$1")
          else
          disk="$1"
          fi

          if test -b "$disk"
          then
          echo 1 >/sys/block/$(basename "$disk")/device/delete
          else
          echo "$0: not a block device: $1" >&2
          exit 1
          fi


          Its main benefit is that you can pass it a symlink, such as found in /dev/disk/by-id/ and it will resolve that to the real device. It does require chase to be installed; you might be able to get the same result using readlink -e.



          The second script, scsi-rescan, is used after hotplugging a new device:



          #!/bin/bash

          exec tee /sys/class/scsi_host/host*/scan <<<'- - -' >/dev/null


          This makes all adapters re-scan for devices. It was the only way I managed to get the new capacity and partition table to be read.






          share|improve this answer































            0














            actually sata drives automatically parks the head when power is lost from what i know so pulling the drive while still spinning should not result in issues however spinning disks are prone to dings while spinning. you can hear this when you have flaky power or solar flares :P



            the most damage is usually from non flushed cache and uncommitted writes in buffers etc ... this is why you MUST unmount a drive before removing it . the scsi command is just good measures to the same end.





            share








            New contributor




            spongebob is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.




















              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "106"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f43413%2fhow-can-i-safely-remove-a-sata-disk-from-a-running-system%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              5 Answers
              5






              active

              oldest

              votes








              5 Answers
              5






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              75















              1. Unmount any filesystems on the disk. (umount ...)

              2. Deactivate any LVM groups. (vgchange -an)

              3. Make sure nothing is using the disk for anything.

              4. Once you've done that, it should be safe to unplug.


              If you want to be extra cautious, do echo 1 > /sys/block/(whatever)/device/delete first. That'll unregister the device from the kernel, so you know nothing's using it when you unplug it. When I do that with a drive in an eSATA enclosure, I can hear the drive's heads park themselves, so the kernel apparently tells the drive to prepare for power-down.



              If you're using an AHCI controller, it should cope with devices being unplugged. If you're using some other sort of SATA controller, the driver might be confused by hotplugging.



              In my experience, SATA hotplugging (with AHCI) works pretty well in Linux. I've unplugged an optical drive, plugged in a hard drive, scanned it for errors, made a filesystem and copied data to it, unmounted and unplugged it, plugged in a differerent DVD drive, and burned a disc, all with the machine up and running.






              share|improve this answer


























              • I had a need to unplug a HDD which I wanted to erase completely. After pulling the disk out of the bay, /dev/sdXY still showed up. Writing 1 to delete made it disappear and I could hear the disk spinning down. Just hdparm -Y was not enough because the /dev/ entries would still exist. Thanks!

                – Lekensteyn
                Aug 9 '12 at 9:25








              • 12





                I strongly advice to always issue the echo 1 > /sys/block/(whatever)/device/delete command because the drive will park the heads, fully stop the disk and disable power on the bus. If a non-parked head touches a spinning plate the drive can be permanently destroyed.

                – drumfire
                Feb 12 '14 at 21:21






              • 2





                Also - if smartd happens to be running, it's a good idea to issue a SIGHUP to the process so that it reloads the drive info. Especially important if you're replacing drives because smartd will reload the info for that drive and all others.

                – drumfire
                Feb 12 '14 at 21:23






              • 1





                Just one note worth mentioning, echo 1 > /sys/block/(whatever)/device/delete will not work under sudo, the "Permission denied" error will be thrown. You'll need to become a real root, so use plain old su instead.

                – TranslucentCloud
                Mar 29 '15 at 10:59






              • 10





                @TranslucentCloud You can pipe it through sudo tee to emulate a redirect as root: echo 1 | sudo tee /sys/block/(whatever)/device/delete

                – Oli
                Apr 15 '15 at 20:54
















              75















              1. Unmount any filesystems on the disk. (umount ...)

              2. Deactivate any LVM groups. (vgchange -an)

              3. Make sure nothing is using the disk for anything.

              4. Once you've done that, it should be safe to unplug.


              If you want to be extra cautious, do echo 1 > /sys/block/(whatever)/device/delete first. That'll unregister the device from the kernel, so you know nothing's using it when you unplug it. When I do that with a drive in an eSATA enclosure, I can hear the drive's heads park themselves, so the kernel apparently tells the drive to prepare for power-down.



              If you're using an AHCI controller, it should cope with devices being unplugged. If you're using some other sort of SATA controller, the driver might be confused by hotplugging.



              In my experience, SATA hotplugging (with AHCI) works pretty well in Linux. I've unplugged an optical drive, plugged in a hard drive, scanned it for errors, made a filesystem and copied data to it, unmounted and unplugged it, plugged in a differerent DVD drive, and burned a disc, all with the machine up and running.






              share|improve this answer


























              • I had a need to unplug a HDD which I wanted to erase completely. After pulling the disk out of the bay, /dev/sdXY still showed up. Writing 1 to delete made it disappear and I could hear the disk spinning down. Just hdparm -Y was not enough because the /dev/ entries would still exist. Thanks!

                – Lekensteyn
                Aug 9 '12 at 9:25








              • 12





                I strongly advice to always issue the echo 1 > /sys/block/(whatever)/device/delete command because the drive will park the heads, fully stop the disk and disable power on the bus. If a non-parked head touches a spinning plate the drive can be permanently destroyed.

                – drumfire
                Feb 12 '14 at 21:21






              • 2





                Also - if smartd happens to be running, it's a good idea to issue a SIGHUP to the process so that it reloads the drive info. Especially important if you're replacing drives because smartd will reload the info for that drive and all others.

                – drumfire
                Feb 12 '14 at 21:23






              • 1





                Just one note worth mentioning, echo 1 > /sys/block/(whatever)/device/delete will not work under sudo, the "Permission denied" error will be thrown. You'll need to become a real root, so use plain old su instead.

                – TranslucentCloud
                Mar 29 '15 at 10:59






              • 10





                @TranslucentCloud You can pipe it through sudo tee to emulate a redirect as root: echo 1 | sudo tee /sys/block/(whatever)/device/delete

                – Oli
                Apr 15 '15 at 20:54














              75












              75








              75








              1. Unmount any filesystems on the disk. (umount ...)

              2. Deactivate any LVM groups. (vgchange -an)

              3. Make sure nothing is using the disk for anything.

              4. Once you've done that, it should be safe to unplug.


              If you want to be extra cautious, do echo 1 > /sys/block/(whatever)/device/delete first. That'll unregister the device from the kernel, so you know nothing's using it when you unplug it. When I do that with a drive in an eSATA enclosure, I can hear the drive's heads park themselves, so the kernel apparently tells the drive to prepare for power-down.



              If you're using an AHCI controller, it should cope with devices being unplugged. If you're using some other sort of SATA controller, the driver might be confused by hotplugging.



              In my experience, SATA hotplugging (with AHCI) works pretty well in Linux. I've unplugged an optical drive, plugged in a hard drive, scanned it for errors, made a filesystem and copied data to it, unmounted and unplugged it, plugged in a differerent DVD drive, and burned a disc, all with the machine up and running.






              share|improve this answer
















              1. Unmount any filesystems on the disk. (umount ...)

              2. Deactivate any LVM groups. (vgchange -an)

              3. Make sure nothing is using the disk for anything.

              4. Once you've done that, it should be safe to unplug.


              If you want to be extra cautious, do echo 1 > /sys/block/(whatever)/device/delete first. That'll unregister the device from the kernel, so you know nothing's using it when you unplug it. When I do that with a drive in an eSATA enclosure, I can hear the drive's heads park themselves, so the kernel apparently tells the drive to prepare for power-down.



              If you're using an AHCI controller, it should cope with devices being unplugged. If you're using some other sort of SATA controller, the driver might be confused by hotplugging.



              In my experience, SATA hotplugging (with AHCI) works pretty well in Linux. I've unplugged an optical drive, plugged in a hard drive, scanned it for errors, made a filesystem and copied data to it, unmounted and unplugged it, plugged in a differerent DVD drive, and burned a disc, all with the machine up and running.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jul 30 '18 at 14:48









              Pro Backup

              2,07363157




              2,07363157










              answered Jul 19 '12 at 3:25









              WyzardWyzard

              1,7611214




              1,7611214













              • I had a need to unplug a HDD which I wanted to erase completely. After pulling the disk out of the bay, /dev/sdXY still showed up. Writing 1 to delete made it disappear and I could hear the disk spinning down. Just hdparm -Y was not enough because the /dev/ entries would still exist. Thanks!

                – Lekensteyn
                Aug 9 '12 at 9:25








              • 12





                I strongly advice to always issue the echo 1 > /sys/block/(whatever)/device/delete command because the drive will park the heads, fully stop the disk and disable power on the bus. If a non-parked head touches a spinning plate the drive can be permanently destroyed.

                – drumfire
                Feb 12 '14 at 21:21






              • 2





                Also - if smartd happens to be running, it's a good idea to issue a SIGHUP to the process so that it reloads the drive info. Especially important if you're replacing drives because smartd will reload the info for that drive and all others.

                – drumfire
                Feb 12 '14 at 21:23






              • 1





                Just one note worth mentioning, echo 1 > /sys/block/(whatever)/device/delete will not work under sudo, the "Permission denied" error will be thrown. You'll need to become a real root, so use plain old su instead.

                – TranslucentCloud
                Mar 29 '15 at 10:59






              • 10





                @TranslucentCloud You can pipe it through sudo tee to emulate a redirect as root: echo 1 | sudo tee /sys/block/(whatever)/device/delete

                – Oli
                Apr 15 '15 at 20:54



















              • I had a need to unplug a HDD which I wanted to erase completely. After pulling the disk out of the bay, /dev/sdXY still showed up. Writing 1 to delete made it disappear and I could hear the disk spinning down. Just hdparm -Y was not enough because the /dev/ entries would still exist. Thanks!

                – Lekensteyn
                Aug 9 '12 at 9:25








              • 12





                I strongly advice to always issue the echo 1 > /sys/block/(whatever)/device/delete command because the drive will park the heads, fully stop the disk and disable power on the bus. If a non-parked head touches a spinning plate the drive can be permanently destroyed.

                – drumfire
                Feb 12 '14 at 21:21






              • 2





                Also - if smartd happens to be running, it's a good idea to issue a SIGHUP to the process so that it reloads the drive info. Especially important if you're replacing drives because smartd will reload the info for that drive and all others.

                – drumfire
                Feb 12 '14 at 21:23






              • 1





                Just one note worth mentioning, echo 1 > /sys/block/(whatever)/device/delete will not work under sudo, the "Permission denied" error will be thrown. You'll need to become a real root, so use plain old su instead.

                – TranslucentCloud
                Mar 29 '15 at 10:59






              • 10





                @TranslucentCloud You can pipe it through sudo tee to emulate a redirect as root: echo 1 | sudo tee /sys/block/(whatever)/device/delete

                – Oli
                Apr 15 '15 at 20:54

















              I had a need to unplug a HDD which I wanted to erase completely. After pulling the disk out of the bay, /dev/sdXY still showed up. Writing 1 to delete made it disappear and I could hear the disk spinning down. Just hdparm -Y was not enough because the /dev/ entries would still exist. Thanks!

              – Lekensteyn
              Aug 9 '12 at 9:25







              I had a need to unplug a HDD which I wanted to erase completely. After pulling the disk out of the bay, /dev/sdXY still showed up. Writing 1 to delete made it disappear and I could hear the disk spinning down. Just hdparm -Y was not enough because the /dev/ entries would still exist. Thanks!

              – Lekensteyn
              Aug 9 '12 at 9:25






              12




              12





              I strongly advice to always issue the echo 1 > /sys/block/(whatever)/device/delete command because the drive will park the heads, fully stop the disk and disable power on the bus. If a non-parked head touches a spinning plate the drive can be permanently destroyed.

              – drumfire
              Feb 12 '14 at 21:21





              I strongly advice to always issue the echo 1 > /sys/block/(whatever)/device/delete command because the drive will park the heads, fully stop the disk and disable power on the bus. If a non-parked head touches a spinning plate the drive can be permanently destroyed.

              – drumfire
              Feb 12 '14 at 21:21




              2




              2





              Also - if smartd happens to be running, it's a good idea to issue a SIGHUP to the process so that it reloads the drive info. Especially important if you're replacing drives because smartd will reload the info for that drive and all others.

              – drumfire
              Feb 12 '14 at 21:23





              Also - if smartd happens to be running, it's a good idea to issue a SIGHUP to the process so that it reloads the drive info. Especially important if you're replacing drives because smartd will reload the info for that drive and all others.

              – drumfire
              Feb 12 '14 at 21:23




              1




              1





              Just one note worth mentioning, echo 1 > /sys/block/(whatever)/device/delete will not work under sudo, the "Permission denied" error will be thrown. You'll need to become a real root, so use plain old su instead.

              – TranslucentCloud
              Mar 29 '15 at 10:59





              Just one note worth mentioning, echo 1 > /sys/block/(whatever)/device/delete will not work under sudo, the "Permission denied" error will be thrown. You'll need to become a real root, so use plain old su instead.

              – TranslucentCloud
              Mar 29 '15 at 10:59




              10




              10





              @TranslucentCloud You can pipe it through sudo tee to emulate a redirect as root: echo 1 | sudo tee /sys/block/(whatever)/device/delete

              – Oli
              Apr 15 '15 at 20:54





              @TranslucentCloud You can pipe it through sudo tee to emulate a redirect as root: echo 1 | sudo tee /sys/block/(whatever)/device/delete

              – Oli
              Apr 15 '15 at 20:54













              5














              Those two sections are for different things.



              The first is for unplugging. The second is for plugging.



              For unplugging, the OS will sync the data during the unmount operation. Thus, if the disk is unmounted (assuming you in fact do have full hardware support) you can power off the disk then unplug it without risk of data loss or corruption.



              For plugging, the device should be automatically recognized. If not, you can execute that command to trigger a bus scan. Once the device is recognized you can mount it.



              And let me caveat this by saying I've only ever done this sort of thing with USB drives.






              share|improve this answer
























              • Thanks for your answer, but I am still not convinced what the correct action would be. SSDs have a "Unsafe Shutdown Count" S.M.A.R.T. field, just unplugging it without doing anything does not sound safe to me.

                – Lekensteyn
                Jul 18 '12 at 21:58






              • 3





                If it helps, I routinely hot-plug and hot-unplug SATA hard drives as part of my job, with no more than making sure it's unmounted first, and I've never run into a problem. That's anecdotal, so don't take it as gospel, but it's at least some evidence that it's likely safe. In any case, I'd consider it a kernel bug if the OS doesn't ensure that the data is fully written at the end of an unmount operation, especially in a hotplug world.

                – Jander
                Jul 19 '12 at 0:24






              • 1





                @Lekensteyn, the hdparm -Y will take care of that. That is basically what the system does every time you suspend or shutdown.

                – psusi
                Jul 19 '12 at 13:16
















              5














              Those two sections are for different things.



              The first is for unplugging. The second is for plugging.



              For unplugging, the OS will sync the data during the unmount operation. Thus, if the disk is unmounted (assuming you in fact do have full hardware support) you can power off the disk then unplug it without risk of data loss or corruption.



              For plugging, the device should be automatically recognized. If not, you can execute that command to trigger a bus scan. Once the device is recognized you can mount it.



              And let me caveat this by saying I've only ever done this sort of thing with USB drives.






              share|improve this answer
























              • Thanks for your answer, but I am still not convinced what the correct action would be. SSDs have a "Unsafe Shutdown Count" S.M.A.R.T. field, just unplugging it without doing anything does not sound safe to me.

                – Lekensteyn
                Jul 18 '12 at 21:58






              • 3





                If it helps, I routinely hot-plug and hot-unplug SATA hard drives as part of my job, with no more than making sure it's unmounted first, and I've never run into a problem. That's anecdotal, so don't take it as gospel, but it's at least some evidence that it's likely safe. In any case, I'd consider it a kernel bug if the OS doesn't ensure that the data is fully written at the end of an unmount operation, especially in a hotplug world.

                – Jander
                Jul 19 '12 at 0:24






              • 1





                @Lekensteyn, the hdparm -Y will take care of that. That is basically what the system does every time you suspend or shutdown.

                – psusi
                Jul 19 '12 at 13:16














              5












              5








              5







              Those two sections are for different things.



              The first is for unplugging. The second is for plugging.



              For unplugging, the OS will sync the data during the unmount operation. Thus, if the disk is unmounted (assuming you in fact do have full hardware support) you can power off the disk then unplug it without risk of data loss or corruption.



              For plugging, the device should be automatically recognized. If not, you can execute that command to trigger a bus scan. Once the device is recognized you can mount it.



              And let me caveat this by saying I've only ever done this sort of thing with USB drives.






              share|improve this answer













              Those two sections are for different things.



              The first is for unplugging. The second is for plugging.



              For unplugging, the OS will sync the data during the unmount operation. Thus, if the disk is unmounted (assuming you in fact do have full hardware support) you can power off the disk then unplug it without risk of data loss or corruption.



              For plugging, the device should be automatically recognized. If not, you can execute that command to trigger a bus scan. Once the device is recognized you can mount it.



              And let me caveat this by saying I've only ever done this sort of thing with USB drives.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jul 18 '12 at 21:37









              bahamatbahamat

              24.5k15090




              24.5k15090













              • Thanks for your answer, but I am still not convinced what the correct action would be. SSDs have a "Unsafe Shutdown Count" S.M.A.R.T. field, just unplugging it without doing anything does not sound safe to me.

                – Lekensteyn
                Jul 18 '12 at 21:58






              • 3





                If it helps, I routinely hot-plug and hot-unplug SATA hard drives as part of my job, with no more than making sure it's unmounted first, and I've never run into a problem. That's anecdotal, so don't take it as gospel, but it's at least some evidence that it's likely safe. In any case, I'd consider it a kernel bug if the OS doesn't ensure that the data is fully written at the end of an unmount operation, especially in a hotplug world.

                – Jander
                Jul 19 '12 at 0:24






              • 1





                @Lekensteyn, the hdparm -Y will take care of that. That is basically what the system does every time you suspend or shutdown.

                – psusi
                Jul 19 '12 at 13:16



















              • Thanks for your answer, but I am still not convinced what the correct action would be. SSDs have a "Unsafe Shutdown Count" S.M.A.R.T. field, just unplugging it without doing anything does not sound safe to me.

                – Lekensteyn
                Jul 18 '12 at 21:58






              • 3





                If it helps, I routinely hot-plug and hot-unplug SATA hard drives as part of my job, with no more than making sure it's unmounted first, and I've never run into a problem. That's anecdotal, so don't take it as gospel, but it's at least some evidence that it's likely safe. In any case, I'd consider it a kernel bug if the OS doesn't ensure that the data is fully written at the end of an unmount operation, especially in a hotplug world.

                – Jander
                Jul 19 '12 at 0:24






              • 1





                @Lekensteyn, the hdparm -Y will take care of that. That is basically what the system does every time you suspend or shutdown.

                – psusi
                Jul 19 '12 at 13:16

















              Thanks for your answer, but I am still not convinced what the correct action would be. SSDs have a "Unsafe Shutdown Count" S.M.A.R.T. field, just unplugging it without doing anything does not sound safe to me.

              – Lekensteyn
              Jul 18 '12 at 21:58





              Thanks for your answer, but I am still not convinced what the correct action would be. SSDs have a "Unsafe Shutdown Count" S.M.A.R.T. field, just unplugging it without doing anything does not sound safe to me.

              – Lekensteyn
              Jul 18 '12 at 21:58




              3




              3





              If it helps, I routinely hot-plug and hot-unplug SATA hard drives as part of my job, with no more than making sure it's unmounted first, and I've never run into a problem. That's anecdotal, so don't take it as gospel, but it's at least some evidence that it's likely safe. In any case, I'd consider it a kernel bug if the OS doesn't ensure that the data is fully written at the end of an unmount operation, especially in a hotplug world.

              – Jander
              Jul 19 '12 at 0:24





              If it helps, I routinely hot-plug and hot-unplug SATA hard drives as part of my job, with no more than making sure it's unmounted first, and I've never run into a problem. That's anecdotal, so don't take it as gospel, but it's at least some evidence that it's likely safe. In any case, I'd consider it a kernel bug if the OS doesn't ensure that the data is fully written at the end of an unmount operation, especially in a hotplug world.

              – Jander
              Jul 19 '12 at 0:24




              1




              1





              @Lekensteyn, the hdparm -Y will take care of that. That is basically what the system does every time you suspend or shutdown.

              – psusi
              Jul 19 '12 at 13:16





              @Lekensteyn, the hdparm -Y will take care of that. That is basically what the system does every time you suspend or shutdown.

              – psusi
              Jul 19 '12 at 13:16











              5














              what about eject /dev/sdX? On my setup, this commands umounts, syncs and powers down the drive.






              share|improve this answer





















              • 4





                I tried this with a disk connected over eSATA but the command failed with "not hotpluggable" or something.

                – Lekensteyn
                Aug 30 '13 at 9:29






              • 1





                Afaik eject plugs out the pluggable disk and not the device. It depends on that the block device driver supports the ioctl() operation used by the eject tool. Esata hard disk drives don't support it, but optical devices, floppies and probably flash drives, yes.

                – peterh
                Jul 31 '15 at 4:11
















              5














              what about eject /dev/sdX? On my setup, this commands umounts, syncs and powers down the drive.






              share|improve this answer





















              • 4





                I tried this with a disk connected over eSATA but the command failed with "not hotpluggable" or something.

                – Lekensteyn
                Aug 30 '13 at 9:29






              • 1





                Afaik eject plugs out the pluggable disk and not the device. It depends on that the block device driver supports the ioctl() operation used by the eject tool. Esata hard disk drives don't support it, but optical devices, floppies and probably flash drives, yes.

                – peterh
                Jul 31 '15 at 4:11














              5












              5








              5







              what about eject /dev/sdX? On my setup, this commands umounts, syncs and powers down the drive.






              share|improve this answer















              what about eject /dev/sdX? On my setup, this commands umounts, syncs and powers down the drive.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Aug 27 '13 at 22:19









              Anthon

              60.8k17103166




              60.8k17103166










              answered Aug 27 '13 at 21:58









              bitvitezbitvitez

              5111




              5111








              • 4





                I tried this with a disk connected over eSATA but the command failed with "not hotpluggable" or something.

                – Lekensteyn
                Aug 30 '13 at 9:29






              • 1





                Afaik eject plugs out the pluggable disk and not the device. It depends on that the block device driver supports the ioctl() operation used by the eject tool. Esata hard disk drives don't support it, but optical devices, floppies and probably flash drives, yes.

                – peterh
                Jul 31 '15 at 4:11














              • 4





                I tried this with a disk connected over eSATA but the command failed with "not hotpluggable" or something.

                – Lekensteyn
                Aug 30 '13 at 9:29






              • 1





                Afaik eject plugs out the pluggable disk and not the device. It depends on that the block device driver supports the ioctl() operation used by the eject tool. Esata hard disk drives don't support it, but optical devices, floppies and probably flash drives, yes.

                – peterh
                Jul 31 '15 at 4:11








              4




              4





              I tried this with a disk connected over eSATA but the command failed with "not hotpluggable" or something.

              – Lekensteyn
              Aug 30 '13 at 9:29





              I tried this with a disk connected over eSATA but the command failed with "not hotpluggable" or something.

              – Lekensteyn
              Aug 30 '13 at 9:29




              1




              1





              Afaik eject plugs out the pluggable disk and not the device. It depends on that the block device driver supports the ioctl() operation used by the eject tool. Esata hard disk drives don't support it, but optical devices, floppies and probably flash drives, yes.

              – peterh
              Jul 31 '15 at 4:11





              Afaik eject plugs out the pluggable disk and not the device. It depends on that the block device driver supports the ioctl() operation used by the eject tool. Esata hard disk drives don't support it, but optical devices, floppies and probably flash drives, yes.

              – peterh
              Jul 31 '15 at 4:11











              2














              I have a pair of scripts that build upon the answer by Wyzard. The first, scsi-drop, is to safely detach a single disk:



              #!/bin/sh

              if test -h "$1"
              then
              disk=$(chase "$1")
              else
              disk="$1"
              fi

              if test -b "$disk"
              then
              echo 1 >/sys/block/$(basename "$disk")/device/delete
              else
              echo "$0: not a block device: $1" >&2
              exit 1
              fi


              Its main benefit is that you can pass it a symlink, such as found in /dev/disk/by-id/ and it will resolve that to the real device. It does require chase to be installed; you might be able to get the same result using readlink -e.



              The second script, scsi-rescan, is used after hotplugging a new device:



              #!/bin/bash

              exec tee /sys/class/scsi_host/host*/scan <<<'- - -' >/dev/null


              This makes all adapters re-scan for devices. It was the only way I managed to get the new capacity and partition table to be read.






              share|improve this answer




























                2














                I have a pair of scripts that build upon the answer by Wyzard. The first, scsi-drop, is to safely detach a single disk:



                #!/bin/sh

                if test -h "$1"
                then
                disk=$(chase "$1")
                else
                disk="$1"
                fi

                if test -b "$disk"
                then
                echo 1 >/sys/block/$(basename "$disk")/device/delete
                else
                echo "$0: not a block device: $1" >&2
                exit 1
                fi


                Its main benefit is that you can pass it a symlink, such as found in /dev/disk/by-id/ and it will resolve that to the real device. It does require chase to be installed; you might be able to get the same result using readlink -e.



                The second script, scsi-rescan, is used after hotplugging a new device:



                #!/bin/bash

                exec tee /sys/class/scsi_host/host*/scan <<<'- - -' >/dev/null


                This makes all adapters re-scan for devices. It was the only way I managed to get the new capacity and partition table to be read.






                share|improve this answer


























                  2












                  2








                  2







                  I have a pair of scripts that build upon the answer by Wyzard. The first, scsi-drop, is to safely detach a single disk:



                  #!/bin/sh

                  if test -h "$1"
                  then
                  disk=$(chase "$1")
                  else
                  disk="$1"
                  fi

                  if test -b "$disk"
                  then
                  echo 1 >/sys/block/$(basename "$disk")/device/delete
                  else
                  echo "$0: not a block device: $1" >&2
                  exit 1
                  fi


                  Its main benefit is that you can pass it a symlink, such as found in /dev/disk/by-id/ and it will resolve that to the real device. It does require chase to be installed; you might be able to get the same result using readlink -e.



                  The second script, scsi-rescan, is used after hotplugging a new device:



                  #!/bin/bash

                  exec tee /sys/class/scsi_host/host*/scan <<<'- - -' >/dev/null


                  This makes all adapters re-scan for devices. It was the only way I managed to get the new capacity and partition table to be read.






                  share|improve this answer













                  I have a pair of scripts that build upon the answer by Wyzard. The first, scsi-drop, is to safely detach a single disk:



                  #!/bin/sh

                  if test -h "$1"
                  then
                  disk=$(chase "$1")
                  else
                  disk="$1"
                  fi

                  if test -b "$disk"
                  then
                  echo 1 >/sys/block/$(basename "$disk")/device/delete
                  else
                  echo "$0: not a block device: $1" >&2
                  exit 1
                  fi


                  Its main benefit is that you can pass it a symlink, such as found in /dev/disk/by-id/ and it will resolve that to the real device. It does require chase to be installed; you might be able to get the same result using readlink -e.



                  The second script, scsi-rescan, is used after hotplugging a new device:



                  #!/bin/bash

                  exec tee /sys/class/scsi_host/host*/scan <<<'- - -' >/dev/null


                  This makes all adapters re-scan for devices. It was the only way I managed to get the new capacity and partition table to be read.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Apr 21 '16 at 11:01









                  Toby SpeightToby Speight

                  5,31111031




                  5,31111031























                      0














                      actually sata drives automatically parks the head when power is lost from what i know so pulling the drive while still spinning should not result in issues however spinning disks are prone to dings while spinning. you can hear this when you have flaky power or solar flares :P



                      the most damage is usually from non flushed cache and uncommitted writes in buffers etc ... this is why you MUST unmount a drive before removing it . the scsi command is just good measures to the same end.





                      share








                      New contributor




                      spongebob is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.

























                        0














                        actually sata drives automatically parks the head when power is lost from what i know so pulling the drive while still spinning should not result in issues however spinning disks are prone to dings while spinning. you can hear this when you have flaky power or solar flares :P



                        the most damage is usually from non flushed cache and uncommitted writes in buffers etc ... this is why you MUST unmount a drive before removing it . the scsi command is just good measures to the same end.





                        share








                        New contributor




                        spongebob is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.























                          0












                          0








                          0







                          actually sata drives automatically parks the head when power is lost from what i know so pulling the drive while still spinning should not result in issues however spinning disks are prone to dings while spinning. you can hear this when you have flaky power or solar flares :P



                          the most damage is usually from non flushed cache and uncommitted writes in buffers etc ... this is why you MUST unmount a drive before removing it . the scsi command is just good measures to the same end.





                          share








                          New contributor




                          spongebob is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.










                          actually sata drives automatically parks the head when power is lost from what i know so pulling the drive while still spinning should not result in issues however spinning disks are prone to dings while spinning. you can hear this when you have flaky power or solar flares :P



                          the most damage is usually from non flushed cache and uncommitted writes in buffers etc ... this is why you MUST unmount a drive before removing it . the scsi command is just good measures to the same end.






                          share








                          New contributor




                          spongebob is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.








                          share


                          share






                          New contributor




                          spongebob is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          answered 15 mins ago









                          spongebobspongebob

                          1




                          1




                          New contributor




                          spongebob is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.





                          New contributor





                          spongebob is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






                          spongebob is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Unix & Linux Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f43413%2fhow-can-i-safely-remove-a-sata-disk-from-a-running-system%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              CARDNET

                              Boot-repair Failure: Unable to locate package grub-common:i386

                              濃尾地震