Categories
Linux Tutorial Series

Linux Tutorial Series – 146 – Unmounting a device

Here is the video version, if you prefer it:

To unmount a device, type in the following command: (Ward, 2014)⁠

umount mountpoint

where mountpoint is the mount point of the device. Mind the spelling of the command.

Hope you learned something useful!

References

Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Page 76

Categories
Linux Tutorial Series

Linux Tutorial Series – 145 – Mounting a device

Here is the video version, if you prefer it:

In today’s Linux distributions, mounting is usually done automatically. I never had to mount anything manually on my personal computer. However, sometimes you will have to mount something (or a tutorial will ask you to do so), so here is how it is done:

mount -t type device mountpoint

As we can see, it is done with the mount where type is the filesystem type (ex4, FAT, NTFS, …), device is the device file (remember the /dev folder discussion) and the mountpoint is the point in our Linux directory structure where we want to place the device (the device’s storage). (Ward, 2014)⁠

Now you know how to mount a device. Again, you will rarely ever do this, but when you do, you know what you’re doing and what each of those arguments to the mount command mean. Sounds like you had a great Linux teacher!

Thank you for reading!

References

Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Page 76

Categories
Linux Tutorial Series

Linux Tutorial Series – 144 – Mounting and unmounting

Here is the video version, if you prefer it:

Today we will talk about something more conceptual yet again. I know that you might be thinking: “Why this conceptual stuff again? It’s been a lot of that lately and I’m tired of that…”. I get it. But be patient – this knowledge will pay off. I am trimming the fat – the things you don’t need to know – but it pays to know these particular concepts. Especially mounting and unmounting because even though you’ll probably never do those manually, there will be some situations where you will have to mount or unmount (an example – “burning” an operating system on an USB) and you have to know what is going on.

Still with me? Good. So what is mounting? Mounting is the process of attaching your device(your device’s storage)in the Linux directory structure. As we know, everything in Linux starts from the root folder (/). Every other directory is accessible from the root folder by navigating from the root folder to the other folders hierarchically below it. When you insert a new device (say, a USB drive) you have to place it in a directory so that it is accessible from the root directory. The directory in which the new device resides is called its mount point. That process (when you assign a directory accessible from the root folder to a new device) is called mounting. Unmounting is the reverse process from mounting – removing the association between a directory in the directory hierarchy and the device (device’s storage).

So basically, when you mount, you make your device’s storage accessible to your Linux computer and when you unmount, you make your device’s storage inaccessible. Not all devices that are mounted need to have storage, but that doesn’t matter for our practical applications. (“What is meant by mounting a device in Linux?,” n.d.)⁠

I modeled my explanation after (“understanding ‘mount’ as a concept in the OS [duplicate],” n.d.)⁠ , which you can read as well for a second perspective.

Thank you for reading!

References

understanding “mount” as a concept in the OS [duplicate]. (n.d.). Retrieved February 12, 2020, from https://unix.stackexchange.com/questions/3247/understanding-mount-as-a-concept-in-the-os

What is meant by mounting a device in Linux? (n.d.). Retrieved February 12, 2020, from https://unix.stackexchange.com/questions/3192/what-is-meant-by-mounting-a-device-in-linux

Categories
Linux Tutorial Series

Linux Tutorial Series – 143 – Device files

Here is the video version, if you prefer it:

Device files are files that are actually device I/O (input/output) interfaces. (Ward, 2014)⁠ If you take the trip back down the memory lane, when I was explaining what each Linux directory is for, I said that /dev contains files which represent devices. They are located in the /dev folder. They can also be called device nodes.

Let’s see what I get with ls -l when I go to /dev:

mislav@mislavovo-racunalo:/dev$ ls -l

total 0

crw-r--r-- 1 root root 10, 235 Jan 2 18:11 autofs

drwxr-xr-x 2 root root 280 Feb 12 07:48 block

drwxr-xr-x 2 root root 60 Feb 12 07:48 bsg

crw-rw---- 1 root disk 10, 234 Feb 11 16:19 btrfs-control

drwxr-xr-x 3 root root 60 Nov 9 17:37 bus

drwxr-xr-x 2 root root 3520 Feb 12 07:48 char

crw------- 1 root root 5, 1 Jan 2 18:11 console

A bunch of files representing devices.

There is a special “device” called /dev/null, which doesn’t represent any device, but rather, it represents “void”. If you send any data to it, as in:

mislav@mislavovo-racunalo:/dev$ ls -l > /dev/null

nothing happens. More specifically, the kernel ignores the information sent to /dev/null.

One important thing to note is that not all devices are represented with device files.

Hope you learned something new!

References

Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Page 46

Categories
Linux Tutorial Series

Linux Tutorial Series – 142 – What file systems are out there?

Here is the video version, if you prefer it:

We talked about filesystems in a previous article. We said that, approximately, they are comprised of a database which keeps track of files and of data pool, where files are kept.

Here are some of the more popular filesystems: (“List of file systems,” n.d.)⁠

  • ext4 (fourth extended filesystem), used in Linux
  • FAT (File Allocation Table), used for USBs
  • NTFS (New Technology File System), used in Windows

References

List of file systems. (n.d.). Retrieved February 12, 2020, from https://en.wikipedia.org/wiki/List_of_file_systems

Categories
Linux Tutorial Series

Linux Tutorial Series – 141 – Symbolic and hard links – explaining the difference with ln

Here is the video version, if you prefer it:

We talked about permanent storage devices, partitions, file systems and inodes. Today we are going to put all that knowledge to use and talk about the difference between symbolic and hard links.

Before we go into this, let me say that this is important to understand. If you ever encounter broken links, you have to know what is going on conceptually. So pay attention.

As we already know, a file is represented as an inode. More accurately, an inode tells you where the file is in the data pool (among other things), but in essence a file is represented by an inode.

We also already know what links are – links are pointers to files. There are two kinds of links – symbolic (also called soft links) and hard links. Let’s explain the difference.

Let’s use an example to illustrate this. This example is modeled after (“What is the difference between a hard link and a symbolic link?,” n.d.)

I already have two files to play with. Here are their contents:

mislav@mislavovo-racunalo:~/Linux_folder$ cat ab.txt

AB

Mustard is how we transfer wealth

Ab

aB

ab

mislav@mislavovo-racunalo:~/Linux_folder$ cat a.txt

A

a

aa

AA

Aa

aA

Some line

Now let’s create some links (both hard and symbolic):

mislav@mislavovo-racunalo:~/Linux_folder$ ln ab.txt ab-hard

mislav@mislavovo-racunalo:~/Linux_folder$ ln -s a.txt a-symbolic⁠

The first command creates a hard link and the second command (with the -s option) creates a symbolic link.

We can now access the files with the links to the files:

mislav@mislavovo-racunalo:~/Linux_folder$ cat ab-hard

AB

Mustard is how we transfer wealth

Ab

aB

ab

mislav@mislavovo-racunalo:~/Linux_folder$ cat a-symbolic

A

a

aa

AA

Aa

aA

Some line

Let’s look at ls -l output:

mislav@mislavovo-racunalo:~/Linux_folder$ ls -l

total 40

...

-rw-r--r-- 2 mislav mislav 46 Feb 9 16:59 ab-hard

-rw-r--r-- 2 mislav mislav 46 Feb 9 16:59 ab.txt

...

lrwxrwxrwx 1 mislav mislav 5 Feb 12 07:33 a-symbolic -> a.txt

-rw-r--r-- 1 mislav mislav 26 Jan 13 05:18 a.txt

...

Look at the second column. It now says that link count for ab.txt is 2, while link count for a.txt is 1. How can this be? Shouldn’t link count for both ab.txt and a.txt be 2?

No, because hard links point to the underlying data and symbolic links are like shortcuts (shortcuts you may have used in Windows). So hard links point to the underlying data (which is an inode), while symbolic links point to the file.

I think that the difference is best explained with this figure, modeled after (“What is the difference between a hard link and a symbolic link?,” n.d.)⁠.

Here’s what happens when we rename ab.txt (the one with the hard link):

mislav@mislavovo-racunalo:~/Linux_folder$ mv ab.txt ab-new.txt

mislav@mislavovo-racunalo:~/Linux_folder$ cat ab-hard

AB

Mustard is how we transfer wealth

Ab

aB

ab

Since ab-hard points to the underlying data, it still works, even though the original filename changed.

Here’s what happens if we rename a.txt (the one with the symbolic link):

mislav@mislavovo-racunalo:~/Linux_folder$ mv a.txt a-new.txt

mislav@mislavovo-racunalo:~/Linux_folder$ cat a-symbolic

cat: a-symbolic: No such file or directory

Since symbolic links just point to the filename, our a-symbolic link is not working anymore. We now call it a broken link.

That’s it for today. Hope you learned something useful! Maybe you don’t see it yet, but when you will be dealing with broken links, it will pay greatly that you understand what is going on under the hood.

References:

What is the difference between a hard link and a symbolic link? (n.d.). Retrieved February 12, 2020, from https://askubuntu.com/questions/108771/what-is-the-difference-between-a-hard-link-and-a-symbolic-link

Categories
Linux Tutorial Series

Linux Tutorial Series – 140 – Links – what are those?

Here is the video version, if you prefer it:

Let’s talk about links – what are those anyway?

Link is a pointer to a file or a directory. If you are not a programmer, a pointer is exactly what its name says it is – it is used for pointing to something. Let’s look at the following situation:

mislav@mislavovo-racunalo:~/Linux_folder$ ls -l

total 36

-rw-r--r-- 1 mislav mislav 8 Feb 5 23:09 1264.txt

-rw-r--r-- 1 mislav mislav 8 Feb 5 23:06 1-4.txt~

-rw-r--r-- 1 mislav mislav 8 Feb 5 23:06 2345.txt

The number in the second column (after file permissions) is the link count. Why is this link count number equal to 1 for these files? Because the only link that exists at this time is the link which is pointing from Linux_folder to the file.

You can increase the number of links to a file and there are two types of links – only one type of link increases the link count. But that’s a topic for another article.

Thank you for reading and hope you learned something useful!

Categories
Linux Tutorial Series

Linux Tutorial Series – 139 – How files and directories are represented internally – inodes

Here is the video version, if you prefer it:

Let’s clear out one more conceptual thing before we jump into other things – how are files and directories represented internally.

See, a usual filesystem you will find on Linux consists of a pool of data blocks where you can store data and a database system that manages the data pool. The database contains entries which describe the file type, file permissions and where the file is stored in the data pool. (Ward, 2014)⁠ These database entries are called inodes. Interestingly, the inodes don’t store the file name. (“Where are filenames stored on a filesystem?,” n.d.)⁠

Files and directories are represented as inodes. This is imporant to keep in mind as we progress, although in my explanations I try to review some key concepts before explaining new ones.

Hope you learned something new!

References

Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Page 87

Where are filenames stored on a filesystem? (n.d.). Retrieved February 29, 2020, from https://unix.stackexchange.com/questions/117325/where-are-filenames-stored-on-a-filesystem

Categories
Linux Tutorial Series

Linux Tutorial Series – 138 – Disks, partitions and filesystems – how do they relate to each other?

Here is the video version, if you prefer it:

Today let’s talk about the thing that bothered me for some time. What is a partition exactly? What is a filesystem, exactly? How do they relate to each other?

Let’s go top-down, from the biggest to the smallest. First, the hardware – the permanent storage devices. There are many permanent storage devices, but let’s focus on the ones most people use in their computer for permanent storage. They can be either hard drives or solid state drives. Permanent storage device is the physical device your data is stored on. Again, the term also includes USB drives, CDs and DVDs, but let’s focus on hard drives and solid state drives. Let’s call them disks.

Partitions are subdivisions of the whole disk. A disk can, thus, be divided into multiple partitions. Partitions are defined on a small area of the disk called a partition table. (Ward, 2014)⁠

Every partition has a filesystem. A filesystem is a hierarchy of files and directories you are accustomed to interacting with. A filesystem resembles a database. A great illustration of this is in the figure below, modeled after (Ward, 2014), page 66.

If we want to access a file, we first consult the partition table to see which partition we need to access. Then after we access that partition, we consult the filesystem database to find the data we are looking for.

I think this is important to understand conceptually, as there is a lot of confusion between what is a partition and what is a filesystem. I hope this cleared it up.

Thank you for reading!

References

Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Pages 66-67

Categories
Linux Tutorial Series

Linux Tutorial Series – 137 – Checkpoint

Here is the video version, if you prefer it:

Next up, we will talk about devices at a low-level. More specifically, we will talk about how are devices represented in your Linux system, what are links, what is mounting, what is unmounting and some other things related to devices which are not as important, but warrant a quick read.

This is not really important for your everyday use, but you will almost guaranteed encounter some device-related things (especially if you are a software developer), so it pays to know these things in those moments. And yes, even if you are a regular user, it pays to know these things. So read on. Don’t lose your enthusiasm – you are still learning the fundamental things.

Talk soon!