I’m planning on a series of articles in order to help some people that either do not yet have the habit to look through man pages or simply are reading my blog in order to find those sudden pieces from information that have the habit to appear in some of the articles you read. I’ll be answering some questions I often see on Linux forums.
- What does “umount” do?
- Why do we need to unmount drives?
- Why “umount”, but not “unmount”?
- How do you unmount drive in Linux if it’s busy?
What does “umount” do? Simple – it u(n)mounts what you have mounted.It could be, for example, flash drive, or, maybe, internal hard disk drive… or network drive, that doesn’t matter. In other words, “umount” is exact opposite of “mount”.
Why we need to “umount” drives before we need to unplug them? Because the way system writes to disks can be compared to the job that people at big warehouses have. Let’s say one of their jobs is to pack big boxes with some materials and then put them into a truck to drive them away to somewhere else. Some people pack the things inside the boxes, some carry those boxes to the truck and put them inside. That should be clear. There’s a warehouse, warehouse workers, boxes with materials and a truck that workers need to fill with boxes. We also know that they don’t transport the boxes carrying them in their bare hands. They probably use some kind of motoric trolley that has the power to transport heavy things. Is there any sense in putting just one small box on the trolley if some more boxes are expected to be ready for transportation in a small amount of time? Sure there isn’t. Workers might as well just put this small box away for a while, and wait for some more – just not to waste time by driving the trolley to the truck and back more times than needed.
Now boxes packed with materials become data, warehouse is your main drive, truck is your removable drive and workers are the Linux system that does all the job. System doesn’t see any sense in moving just one small piece of information when you can wait a little bit and move much more at the time. So it saves that small piece of information, waiting for more to appear, then sends them all at once. This is what we call “buffering”, even though this principle is just one meaning of this word. System “buffers” these little bits of information until it has enough to write them in efficiently, or it is asked to “clear the buffer”, let’s say, because somebody wants to unmount the drive right now.
And then, let’s say, a truck driver, employee of other company than the one that manages the warehouse just drove away the whole truck in the middle of the process, leaving the workers with some boxes still in the warehouse. That sure is a stupid situation, isn’t it? The driver is unaware of the fact that he just lost part of the materials, and he’s screwed when he realizes that. That’s what sometimes happens when you unplug your flash drive without unmounting it – you just lose some of your data not even realizing this, as it was left in the buffers of the system. You’re the driver of the truck, as you can see, and you’re not doing a good job =). Yes, I’ve lost data this way. Just for you to know that this actually happens =( To sum up – ejecting your drive without unmounting is just not wise. And be it Linux or Windows – basic principles of mounting/unmounting drives are the same everywhere. The only thing is that you have no possibility to control how Windows mounts drives – but you are still the only one that can and has to give the signal to unmount the drive! It’s called “Safely remove” or like that – can’t remember now, but you must know what I’m talking about.
Basically, you have to “umount” every drive you mounted with write permissions, and especially if you edited or created some file on it since the time you mounted it. And even though it might be that you haven’t actually edited/created a single thing on it or even mounted it read-only, you better unmount it. Why?
To understand, why, we have to look at what does the “mount /dev/drive /media/partition” actually do.
1) Actually mounts your drive where you said it to mount it. That’s clear, but…
2) It also writes the entry in some system files and other locations. For example, file “/etc/mtab” is one of those files – it contains info about all the partitions currently mounted.
See, there’s one more thing that we usually don’t know about – entry in /etc/mtab. Moreover, some other programs also use /etc/mtab for their own purposes, and part of those programs are system utilities that hold the system alive. Of course, they do need to know about what’s mounted right now – and the only way for many seems to be reading /etc/mtab.
Seems that “umount” should also delete the entry “mount” added – it’s the exact opposite, remember? And, of course, it does. Once the entry is removed, programs are safe to assume partition isn’t mounted anymore, and they’re usually right. But what if the entry stays after partition is physically unmounted? Programs assume otherwise – they think that everything’s alright, but it isn’t. And if they try to somehow work with that partition – well, things happen. Most programs are OK to hold such a situation. Others may not be – those programs crash, potentially making you lose some of your data you that were just editing with this program. System and those system utilities also are oblivious, and even though they’re usually better prepared for any exceptional situations – you wouldn’t want to test them, see, people are people, and bugs are bugs, and some bugs stay hidden waiting exactly for some kind of exceptional situation. I hope that’s clear for you =)
As you can see, “umount” is a very useful program, helping us stay safe. But how does one use it?
Let’s assume we have mounted a partition /dev/sda5 under /media/Partition. Now we want to unmount
it, and we just do…
Simple as that. Moreover, both mentioning mountpoint (/media/Partition) and partition block device path
(/dev/sda5) will do the trick. So,
also works. But – the official man page says that the latter way will fail in case partition is mounted twice at different paths. There will hardly be a reason for you to do that under normal circumstances (I mean, for you to mount one partition twice under two different paths), but keep this in mind.
And there’s more behind this command!
- What if you want “umount” to tell you more? Just type “umount /dev/sd-whatever -v”, adding option “-v” at the end. This turns on so-called verbose mode, which does tell you more in case you are interested – and, given that you’re reading this, you probably are 😉
- And what if you want just to see what happens if the entry in /etc/mtab stays? Well, there’s another option for you, just add “-n” at the end then. I won’t promise lots of fun though =) If you want to delete entry in /etc/mtab later – use the same command as you used previously, but put “–fake” instead of “-n”. See, using “umount something -n” actually unmounts but keeps the entry. Using “umount something –fake” doesn’t actually unmount but deletes the entry – simple as that.
- Feeling angry at your system? Use “umount -a”. This unmounts every single partition mounted, including root. Must be fun watching your system search for files that are not there – and this, unlike “rm -rf /”, does almost no harm =)
- Last option might be actually useful together with “-t filesystem_type” option. For example, “umount -a -t ntfs” will unmount only those partitions that are of NTFS type, yet all of them. Or, if you wish to unmount all the read-only filesystems, use “umount -a -O ro”, where “ro” is an option you earlier gave to “mount”. This applies to all options of course.
- One more “umount” option is truly amazing. Let’s say – you have a filesystem which is currently busy – and you don’t know why! You’d forget about this, as a FS may be busy for a number of reasons, but you need to unmount it, and cannot. You get a message:
umount: /path: device is busy.
- That’s the thing about Windows I hate – if something uses the flash drive, be it Windows Explorer or antivirus program performing a quiet scan when it wasn’t asked to, you’ll just be given a message like “Drive’s busy. Wait, please.” Ooh, it’s OK – I’m so happy to wait when I need my f***ing drive now because the bus will arrive after ten minutes and it won’t wait. User-friendly, they said. It’ll be comfortable, they said.
- … Anyway, your drive is busy – some program is working on it. What do you do on Linux? Use “-l” option. It makes “umount” wait until drive won’t be busy for at least a little while – and then unmount it right on time while that program takes a rest before moving to another file! No more waiting for some unknown program to fully complete its work when you just need your goddamn flash drive, just add “-l” at the end. Amazing, isn’t it?
Or, and one last thing – why “umount”? Why not “unmount”? Well, first versions of Linux kernel were limited to 8-letter filenames, and developers needed to shorten the name so that source code for “umount” filename (with extension .c added – so two more letters) could fit in the limitations. Explanation taken from here 😉
That’s all for today. Hope you enjoyed it and filled in some gaps of your knowledge base. Now, I’ll move on to creating some other article =) Good luck to all of you, Linux beginners!