The filesystem and archives

INPUT · Slides

Now, in the shape of a file (`/proc`)

01 / 06

Files that are not on the disk

Chapter 9 ends in a slightly strange place.

~ $ cat /proc/uptime2.36 1.43

Out came how many seconds since it started. But this /proc/uptime is not on the disk.

look at /proc with df -h and the size is 0ls -l /proc/uptime says 0 as welland still cat gives you contents

What is happening is that the kernel makes it at the moment you try to read, and hands it over.

cat /proc/uptime  | "I want to read"the kernel works out the numbers as of now  |it hands over the characters "2.36 1.43"

So read it twice and you get different numbers. A counter dressed up as a file.

In Linux this arrangement is called procfs (the process file system). Remember the proc line in df last lesson? That was this.

02 / 06

Why put it in the shape of a file

You may have thought "why the disguise, when they could just write a command for it". A good question.

The answer is this.

> Put it in the shape of a file and every tool you already have works on it

cat /proc/meminfo                     look at itgrep MemFree /proc/meminfo            search itawk '{print $2}' /proc/loadavg        pull out a fieldwatch cat /proc/uptime                repeat it

You can look into the state of the system without learning one new tool.

This is Linux design thinking itself.

"everything is a file"

And you have met the idea several times already.

What you sawWhat it is
/dev/nullthe counter that throws away what you write
/dev/zerothe counter that gives out zeros for ever
/dev/urandomthe counter that gives out random values
/proc/uptimethe counter that gives out the current uptime

None of them is a "file", and you could read and write them all as files. Line up the ways in and out and the tools can be reused — that is the idea.

03 / 06

The numbered directories are processes

Peer into /proc and there are a lot of numeric names.

~ $ ls /proc1  10  11  12  ...  78  cpuinfo  meminfo  uptime  ...

Those numbers are process numbers (PIDs), the ones you saw with ps in chapter 8.

Go into one and it looks like this.

/proc/78/|- cmdline     the command it started from|- status      name, parent, state, memory in use|- stat        the same information as bare numbers|- cwd         the directory it is in (a link)|- exe         the program itself (a link)\- fd/         the files it has open

The ps of chapter 8 was only reading here and laying it out in a table. The trick was in here all along.

To look at your own shell, use $$.

~ $ grep Name /proc/$$/statusName:   sh~ $ grep PPid /proc/$$/statusPPid:   1

The name is sh and the parent is 1 (init). The family tree you drew in chapter 8 can be read as files too.

Note that cwd, exe and fd/ are not readable here, on permissions: the owner of the process is root. The permissions from chapters 6 and 7 apply to /proc just the same.

04 / 06

The counters you read often

Here are the ones worth knowing. All of them read with cat.

WhereWhat it tells you
/proc/uptimeseconds since it started
/proc/loadavghow busy it is (1 / 5 / 15 minutes)
/proc/meminfothe breakdown of memory
/proc/cpuinfothe kind of CPU
/proc/versionthe version of the kernel
/proc/mountswhat is attached where, right now
/proc/filesystemsthe kinds of storage it can handle
/proc/sys/kernel/hostnamethe name of this machine

The top and free of chapter 8 are tools that read here and lay it out in a table.

/proc/loadavg has five fields.

0.00 0.00 0.00 1/35 84  ^    ^    ^    ^   ^ 1m   5m   15m  run/all  last PID

/proc/mounts is where df gets its material.

~ $ grep 9p /proc/mountshost9p /home/learner 9p rw,relatime,... 0 0

It says your home is attached by a method called 9p. The host9p that df mentioned last lesson came from here.

05 / 06

Some counters you can write to

Parts of /proc change how the system behaves when you write to them.

below /proc/sys/ are the settings counters

The name of the machine, for instance, changes when you write here.

# echo newname > /proc/sys/kernel/hostname

It starts with # because only root can write it, as in chapter 7.

What is nice about the arrangement is that no special command is needed to change a setting.

read with cat   ->  you see the current settingwrite with echo ->  the setting changes

It is a strong power, of course, so do not write where you do not understand. There are counters that change how networking and memory behave, and things can stop working on the spot.

And do not forget.

writes to /proc go away when it restarts

Because it is a counter in memory, not on the disk. A setting you want to keep goes in a file below /etc. /etc is lasting, /proc is now — that is the division. The arrangement you saw on the map turns out to mean this.

06 / 06

Now have a go

Here is what you read in this lesson.

cat /proc/uptime                    seconds since it startedcat /proc/loadavg                   how busy it isgrep MemTotal /proc/meminfo         the total memorycat /proc/version                   the kernel versioncat /proc/sys/kernel/hostname       the name of the machinegrep Name /proc/$$/status           the name of your shellgrep PPid /proc/$$/status           its parentgrep 9p /proc/mounts                how it is attached

Not one new command, is there. Only cat, grep and awk. That being enough is the worth of /proc.

One more amusing thing to try at the end.

ls -l /proc/uptime   ->  size 0wc -c < /proc/uptime ->  12 bytes

Size 0, and yet 12 bytes when you read it. Something that could never happen with an ordinary file.

The kernel does not know in advance how many bytes it is about to hand over, so it answers 0 for the size. It makes them at the moment they are read.

That one thing says what /proc is better than anything, I think. So let us read.