Linux Storage Devices, Partitions, and Mount Points Explained

Earlier today I was talking with a guy in the Ubuntu IRC channel (shout out to codemagician!) who was asking how to format a USB stick via the command line. Through explaining how it worked, I realized that to someone who isn't already very familiar with Linux, the way Linux handles drives can be very confusing, especially since you can control almost every step of the mounting process, unlike with Windows, which is why Windows is so easy (albeit less functional however).

What do you say I do a post on how Linux handles storage devices? Yes? No? Great!

=The Quick Overview= When you plug in a USB stick (for example) to your linux machine, it is assigned a device location (/dev/sd ). From there, that new device is assigned a mount point (assuming we are using Ubuntu here many Linux distros won't auto mount a storage device, even if it is internal). This mount point can be located anywhere, but typically is located in /media/. From the folder created in /media (or wherever the mountpoint is located), you can indirectly read and write data.

=The /dev/ Directory= The /dev/ directory is an interesting one to explain. I probably won't do it right, but I'll give it a shot either way. Dev is short for devices. If you run ls from within /dev/ you will likely see things like  sda, sdb, hda, and more and more devices.

What do these mean? Basically, each of the files listed in /dev/ is a direct pointer to either a physical or a virtual device. This part is actually super cool I think. Basically, when you transfer say, a picture, to your usb stick, the operating system literally writes the instructions for writing the file (in binary) to the device location/file (/dev/sdb for instance), which in turn writes it to the USB stick. You may say that's not that neat, but consider your audio device. When your music player (amarok, rhythmbox, etc) plays music, it literally streams the music file's uncompressed binary audio to the audio device file and that is in turn translated by the hardware driver and converted into speaker vibrations.

You can actually try this by running a quickie command in the command line. The audio device is typically located at /dev/dsp. Pick a file on your hard drive that you want to "listen" to (it is likely going to sound like static), and run the following command. For this example, I'm going to use a jpeg image. cat /home/username/Desktop/background.jpeg > /dev/dsp

What we just did there was to redirect the file contents of background.jpeg into the device pointed to by /dev/dsp. Mine sounds like static for some time (It's a really high resolution jpeg).

If THAT isn't cool, I don't know what is.

=Mount Points= Once your storage device is assigned a device location (IE: /dev/sdb), it then needs a mount point that interfaces with the device location. In a less complicated fashion, you need a folder that represents the drive. For instance, say you plug in your usb stick named Maverick (that's one I formatted last night). Ubuntu creates a temporary folder located at /media/Maverick/. That became the mount point for my usb stick. All a mount point is, generally speaking (I'll get into the technicalities of it in the next paragraph), is simply a folder that points to a device location. Ubuntu, Mint, as well as Debian all default to creating folders in /media/.

=So what do mount points and device locations have anything to do with each other?= Here's where it gets pretty technical (so much so that I don't fully know how this works). Succinctly, a mount point provides an interface that your operating system uses to convert data into binary for writing directly to the device location. That means that when you copy a picture file to your usb stick (IE: /media/Maverick), your operating system converts it to binary and streams said binary to the device location associated (IE: /dev/sdb1) with that mount point.

=Why sda,b,c,d,e,f...?= The sd part of that stands for storage drive. The a, b, c, etc. is simply an incrementing value assigned to your drive. If you plug in a usb drive, it will be assigned sdb. If you plug in a second, it will be assigned sdc. If you plug in a third, it will be assigned sdd, and so on.

=How do you explain the number at the end of my device locations (IE: /dev/sdb1)?= That number represents the partition. For instance, your local hard drive is device sda (presumably it was the first drive to be plugged in since your computer is running off of it). Your hard drive has partitions (these are like virtual sections in your hard drive with them you can divide your hard drive into one or more pieces mine is divided into 8 actually). Typically usb sticks only have one partition.

That's all for now. I think I covered just about everything. If i missed anything, please let me know in the comments section and I'll add it on as soon as I get the chance.

Now if you will all excuse me, I was at work at 2:00 this morning and desperately need sleep. Don't break too many things on your computer by redirecting file output to random devices now. I'm watching you...really. So is Santa.