Linux : How to put a disk or block device in offline mode and safely remove it

By | July 5, 2023

To put a disk or block device in offline mode and delete it from the system, follow the following steps.

This example will assume that we want to remove the 5th disk on the system (connected on controller 0, id 5 and seen as “sdf” and mounted as /mnt/disk5).

REMARK: If you were using this disk with LVM, make sure to delete everything first (lvremove, vgremove, pvremove).

1. Let’s first list all the disk seen by the system by using the “lsscsi” command – example :

[0:0:0:0]    disk    VMware   Virtual disk     2.0   /dev/sda
[0:0:1:0]    disk    VMware   Virtual disk     2.0   /dev/sdb
[0:0:2:0]    disk    VMware   Virtual disk     2.0   /dev/sdc
[0:0:3:0]    disk    VMware   Virtual disk     2.0   /dev/sdd
[0:0:4:0]    disk    VMware   Virtual disk     2.0   /dev/sde
[0:0:5:0]    disk    VMware   Virtual disk     2.0   /dev/sdf
[0:0:6:0]    disk    VMware   Virtual disk     2.0   /dev/sdg
[1:0:0:0]    cd/dvd  NECVMWar VMware IDE CDR00 1.00  /dev/sr0

2. Before going any further, let’s see if anything is accessing the disk you want to remove :

lsof /mnt/disk5

3. Unmount the drive :

umount /mnt/disk5

4. Comment the corresponding entry in fstab :

vi /etc/fstab

5. Issue the offline command :

echo offline > /sys/block/sdf/device/state

6. Remove the device :

echo 1 > /sys/block/sdf/device/delete

7. Verify again the list of disk seen by the system :

# lsscsi
[0:0:0:0]    disk    VMware   Virtual disk     2.0   /dev/sda
[0:0:1:0]    disk    VMware   Virtual disk     2.0   /dev/sdb
[0:0:2:0]    disk    VMware   Virtual disk     2.0   /dev/sdc
[0:0:3:0]    disk    VMware   Virtual disk     2.0   /dev/sdd
[0:0:4:0]    disk    VMware   Virtual disk     2.0   /dev/sde
[0:0:6:0]    disk    VMware   Virtual disk     2.0   /dev/sdg
[1:0:0:0]    cd/dvd  NECVMWar VMware IDE CDR00 1.00  /dev/sr0

The disk is no longer seen by the system and may be physically (or virtually!) removed.