Everything Is A File (Live Demo)

Simple is beautiful

Important

One sometimes has to think more to reach simplicity.

This pays off a thousand times.

Ok: a File is a File

  • A file is a file. That’s simple.

  • There are tools explicitly made to read and write files, everybody can use these.

Write to a File
$ echo Hello > /tmp/a-file
Read from a File
$ cat /tmp/a-file
Hello

Is a Serial Interface (UART) a File?

Why not? Data go out and come in!

Write into the Cable
$ echo Hello > /dev/ttyUSB0
Read off the Cable
$ cat /dev/ttyUSB1
Hello

Pseudo Terminals

Terminals

  • History: login via a hardware terminal, connected through a serial line

  • Terminal (TTY) layer (in the kernel) implements session management

  • Pseudo Terminal: software instead of cable

Consequentially, output to a pseudo terminal is like writing to a cable, err, file.

Write to a Pseudo Terminal
$ echo Hello > /dev/pts/0

Disks and Partitions

USB Stick Backup
# cat /proc/partitions
major minor  #blocks  name

   8       32    2006854 sdc
   8       33    2006823 sdc1
# cp /dev/sdc1 /Backups/USB-Stick
# mount -o loop /Backups/USB-Stick /mnt

/proc and /sys

  • Kernel has variables in memory that configure certain aspects of its operation

  • Most of these variables are exposed as files

  • Virtual file systems: sys and proc

Processes as directories
$ cd /proc/$$       # ... deserves a bit of explanation though :-)
Suspend to Disk
# echo disk > /sys/power/state

Pseudo Devices

  • /dev/null: trash can, swallowing everything you write to it)x

    $ echo hallo hallo $ echo hallo > /dev/null

  • /dev/zero: insane amount of NULL-bytes

    Burn CPU copying NULL-bytes back and forth
    $ ls -l /dev/zero
    crw-rw-rw-. 1 root root 1, 5 Mar  7 12:10 /dev/zero
    $ cat /dev/zero > /dev/null
    
    Create a 1MiB sized file containing NULL-bytes, and format it as FAT
    $ dd if=/dev/zero of=/tmp/1MibFS.fat count=1024 bs=1024
    $ ls -lh /tmp/1MibFS.fat
    -rw-rw-r--. 1 jfasch jfasch 1.0M Mar  7 17:32 /tmp/1MibFS.fat
    $ mkfs.vfat /tmp/1MibFS.fat
    
  • /dev/random: kernel, respectively drivers, collect entropy from certain kinds of interrupts.

    $ ls -l /dev/random
    crw-rw-rw-. 1 root root 1, 8 Mar  7 12:10 /dev/random
    $ cat /dev/random