惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
D
Docker
月光博客
月光博客
MongoDB | Blog
MongoDB | Blog
Vercel News
Vercel News
美团技术团队
博客园 - 叶小钗
I
InfoQ
Jina AI
Jina AI
博客园 - 司徒正美
雷峰网
雷峰网
B
Blog
Y
Y Combinator Blog
A
About on SuperTechFans
WordPress大学
WordPress大学
酷 壳 – CoolShell
酷 壳 – CoolShell
大猫的无限游戏
大猫的无限游戏
Microsoft Security Blog
Microsoft Security Blog
Stack Overflow Blog
Stack Overflow Blog
腾讯CDC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recent Announcements
Recent Announcements
V
V2EX
N
Netflix TechBlog - Medium

Ittavern.com

Wimage - Hosting Open-Source Image Uploader with Podman and external S3 Storage Switching from Hugo to picopaper Encryption using SSH Keys with age in Linux ETag in nginx - Simple Resource Caching Sending nginx Logs to Loki with Grafana Alloy How to: Cisco ISE backup to SFTP repository with public key authentication Dummy IP & MAC Addresses for Documentation & Sanitization Deploying ISSO Commenting System for Static Content using Docker Generate a Vanity v3 Hidden Service Onion Address with mkp224o ssh-audit Primer - Audit your SSH Server mtr - More Detailed Traceroute - Network Troubleshooting My Personal Backup Strategy - August 2024 iperf3 - User Authentication with Password and RSA Public Keypair Adding a trash can to Linux with trash-cli Bandwidth Measurement using netcat on Linux Getting started with rsync - Comprehensive Guide Cron Jobs on Linux - Comprehensive Guide with Examples SSH Server Hardening Guide v2 Port Knocking with knockd and Linux - Server Hardening Getting started with rclone - Data transmission Getting started with dig - DNS troubleshooting Getting started with Fail2Ban on Linux Getting started with netcat on Linux with examples URL explained - The Fundamentals Troubleshooting Asking The Right Questions Create tmux layouts using bash scripts Getting started with tcpdump - Ittavern.com Curl on Linux - Reference Guide Getting started with nmap scripts My Offsite Backup - March 2023
Linux - unmount a busy target safely
2023-01-03 · via Ittavern.com

Goal - removing target without data loss

Unplugging or unmount -l (lazy unmount) can cause data loss. I want to share a way o avoid data loss.

Side note: unmount -l will let you unmount the device, but as far as I know only 'hides' the mountpoint, and active processes can still write on said device.

The problem #

Error unmounting /dev/sdc1: target is busy

So, there are now different ways to unmount the target safely.

Side note: the most common case is that you are still in a directory of said target. It happened way too often to me.

Preparation #

Those steps are not necessary, but help you troubleshoot.

Finding the mount point #

We are going to use df -h to find the mount point of the busy target. It is often not necessary, but it can be helpful.

kuser@pleasejustwork:~$ df -h
[...]
/dev/sdc1        59G   25G   35G  42% /media/kuser/hdd-target

Check if the device is still actively in use #

Additionally, you can check the activity of said device with iostat.

kuser@pleasejustwork:~$ iostat 1 -p sdc

vg-cpu:  %user   %nice %system %iowait  %steal   %idle
           1,14    0,00    0,63    2,66    0,00   95,56

Device             tps    kB_read/s    kB_wrtn/s    kB_dscd/s    kB_read    kB_wrtn    kB_dscd
sdc              13,00         0,00         7,50         0,00          0          7          0
sdc1             13,00         0,00         7,50         0,00          0          7          0

iostat is powerful, but in this case the most important columns here are kB_read/s kB_wrtn/s. If there is anything but 0,00, the device is in use.

If there is any activity and you unplug or unmount the device forcefully, data loss will most likely occur.

Finding the process #

Using 'fuser' #

More information can be found in the manual of 'fuser'.

kuser@pleasejustwork:~$ fuser -vm /dev/sdc1
                     USER        PID ACCESS COMMAND
/dev/sdc1:           root     kernel mount /media/kuser/hdd-target
                     kuser     43966 F.c.. kate
                     kuser     44842 ..c.. kate

I prefer 'fuser' since it is installed on most OS and does the job too.

Using 'lsof' #

More information can be found in the manual of 'lsof' (list open files).

kuser@pleasejustwork:~$ lsof /dev/sdc1
COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
kate    43966 kuser  cwd    DIR   8,33    32768    1 /media/kuser/hdd-target
kate    43966 kuser   24w   REG   8,33      142 2176 /media/kuser/hdd-target/.busybusy.txt.kate-swp

or

kuser@pleasejustwork:~$ lsof /media/kuser/hdd-target
COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
kate    43966 kuser  cwd    DIR   8,33    32768    1 /media/kuser/hdd-target
kate    43966 kuser   24w   REG   8,33      142 2176 /media/kuser/hdd-target/.busybusy.txt.kate-swp

Kill process / close program #

Kill process by PID:
sudo kill -9 43966

or simply close the program that is using the file in the GUI.

Unmount #

Try to unmount again.

Some other methods #

I have had no problems with those yet, but some notable mentions.

Some other things to look into:
check the swap partition: cat /proc/swaps
stop nfs-kernel-server
stop samba/smb server
check for symbolic links

There are more scenarios in which a target can be busy, but this should cover 95% of cases.