Everything is a text file, or… How to tell a drive is connected in Linux.

Working with Linux can be a daunting experience at first, but once you get the basics down you will find that things are usually pretty simple as long as you know the right commands.

One thing to keep in mind is something that someone, somewhere once told me; “Everything in Linux is a text file”.

In other operating systems (I’m looking at you Windows!) everything is hidden from the user. OS’s like Windows store everything in binary blobs that are only directly readable by a computer. The user must use a utility (like Microsoft Management Console) to ask the computer to translate the binary into a user readable format. With Windows, even the log files are stored as binary blobs (Event Viewer anyone?).

But with Linux, everything is a text file. Not only logs but also system information is essentially a text file. For starters, let’s look at Linux directory named ‘/proc’. This is a pseudo-file-system mount-point where the system stores information used by the ‘kernel’. For the laymen, this is information about the inner workings of your Linux operating system. Everything from memory usage to process information can be pulled from the many files located in this directory.

In this article I will focus on how to retrieve disk information for use in connecting a new disk-drive to Linux. We will assume that the drive is fresh out of the box and has never been used before.

After plugging in the drive and powering on the machine the first thing we will want to do is check to make sure that the drive is being seen by the OS. On a Windows machine you would go into the ‘Computer Management’ MMC and view the Disk Management information. On Linux you can essentially think of the ‘/proc’ directory as your ‘Computer Management console. To view the most basic disk information on your Linux box you will want to view a file named ‘diskstats’ located in the ‘/proc’ directory. We can view this file with the following command:

cat /proc/diskstats

Below you can view the output of the command on my Linux box.

cat proc diskstats

In Linux hard drives are named in the format: connection type (sd=SCSI); drive letter (a,b,c…); parition (1,2,3…). So as you can see from the output I have a SCSI attached drive (Its a Serial ATA drive) that has been assigned the letter ‘a’ and it has a couple of partitions in it (sda1,sda2, sda5). This is my system drive and the partition structure of 1,2,5 is normal for a system disk (boot, system, and swap). If I enter in the new disk and check the file again we will hopefully see a ‘sdb’ show up in the list. At this point the disk will not have any partitions on it so we should just see the simple ‘sdb’ without any partition numbers.

Now that we have verified that the disk has been detected by the system kernel we can go ahead and start to get that disk ready for use. Preparing a disk consists of two task: creating one or more partitions and formatting the partitions. In tomorrows post I will talk about how to use the ‘fdisk’ utility to partition your drive and how to go about formatting the partition once it has been created.

Here we have a perfect example of how easy Linux can be if you just keep in mind that, “Everything is a text file”. What the system sees is essentially what the user sees and even though most of the files contained in the ‘/proc’ directory are read-only, you can obtain quite a bit of information from them. If you are new to Linux, one of the first things I would recommend is that the user uses the ‘cat’ command to view the files located in the ‘/proc’ directory. You cant hurt anything by just looking and you will get a better idea of what the system sees by exploring. That is part of the fun of Linux; you, as the user, has complete freedom to explore the system!

Advertisements

One thought on “Everything is a text file, or… How to tell a drive is connected in Linux.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s