Skip to content

Latest commit

 

History

History
237 lines (205 loc) · 7.93 KB

02-fs.md

File metadata and controls

237 lines (205 loc) · 7.93 KB

Understanding a Linux based OS: S1E2 - Filesystem

When you open a terminal, usually, you land up in your home /home/<your name>

Navigating through the filesystem

  • pwd - Print Working directory

  • cd - Change directory

  • ls - List the contents of directory

Option Meaning
-a Shows hidden files (The ones whose name begins with .)
-i Show inode number
-l Long List
-h Human Readable file sizes
  • file - Identifies the type of file

  • cp and mv - Copy and move, respectively

Special Directories

  • . - The current directory
  • .. - The previous directory
  • / - Root
  • ~ - Your home

Tree structure of the file system

/
├── bin
├── boot
├── dev
├── etc
├── home
├── lib
├── proc
├── sys
├── tmp
.
.
Directory Purpose
/bin Binary files to be used by all users
/boot Bootloader files
/dev Device files
/etc Configuration files
/home home of users
/lib Static and dynamic libraries
/proc procfs- Process file system
/sys sysfs - The filesystem exported by the kernel to control hardware
/tmp tempfs - Temporary files go here

The procfs

  • Virtual file system
  • On-demand file access (so all file sizes are 0)
  • Can be used to interact with the kernel (you can use system calls also)
  • Provides information related to the system as a whole, and of individual processes
  • Following is a list of some files providing information related to system
File Description
/proc/uptime System uptime in seconds.
/proc/cpuinfo CPU Information (Can also get using lscpu)
/proc/meminfo Memory information
/proc/vmstat Virtual memory information
/proc/mounts Information on mounted file systems (Can also get it using mount command)
/proc/filesystems Supported file systems
/proc/self/fd/0 stdin - The standard input stream
/proc/self/fd/1 stdout - The standard output stream
/proc/self/fd/2 stderr - The standard error stream
  • Following is a list of some files provinding information related to each process. They reside in /proc/<pid>
File Description
cmdline Command line used to start the process
comm The process name in the command line
cwd Link to the directory in which the process has started
exe Link to the program executable
environ Environment variables inside the process
limits Resource limits for the current process
maps Memory map of the process
status Status of the process

The sysfs

  • Also a virtual file system
  • kobject hierarchy exported by the kernel
  • Used to activate/deactivate kernel features (without coding)
  • If a file has write flag enabled, it can be used to do configuration
  • We can play with /sys/class/leds/<device>/brightness value to turn LEDs of the device on and off by just using echo.

/dev - The Devices

  • The real and pseudo devices that are attached to the system
  • 2 types - block (b) and character (c)
  • Out of pseudo devices, terminal devices /dev/tty* and /dev/pts/* can be used to communicate with other real terminals and terminal emulator sessions, respectively
  • /dev/zero, /dev/null act as an infinite supply of zeros, and, infinite sink, respectively
  • /dev/random is the random number generator provided bt the kernel
  • stdin, stdout and stderr provide the streams to process to read and write the data.
  • Other files corresponding to real devices have drivers implemented for them. According to the driver specifications, we can communicate with them

/tmp - The tempfs

  • Temporary filesystem, as the name implies!
  • Files on a volatile memory
  • Fast access, but not too much files
  • Storing intermediate data

/etc - Home of configuration files

  • Many configuration files are stored here, organized by application
  • The configuration is a plain text that is read by the application
  • Some important files are listed below
File Description
passwd List of users
shadow Hashed passwords!
fstab Default mounts file
hosts Host definition for local system
hostname Current hostname
resolv.conf DNS server configuration

/home - Your home

  • ~
  • Your files are stored here, under your ownership
  • Per-user configurations and data are stored in ~/.local
  • But, home of the root is /home/root, but /root

Links

Many times, we become lazy to walk the file system tree, so shortcuts are needed. They are called links in Linux.

There are 2 types of links in Linux.

  1. Soft links
  2. Hard links

Soft Links

----------        ---------
|  file1 |<---    | link  |
----------   |    ---------
    |        |       |
    V        |       V
----------   |     ---------
| Inode1 |   ---- |Inode2 |
---------         ---------

ln -s file1 link

Hard Links

                         ---------
----------        -------| file1 |
| Inode1 |<-------|      ---------
--------          |      ---------
                  -------| link  |
                         ---------

ln file1 link
  • Impossible to hard link directories
  • rm is not deleting the files, but unlinking them.
  • Due to the above reason, if we delete file1, we can still access its content using link.

Attaching External Filesystems

Linux allows you to attach an external filesystem to a point in the existing file system tree. This process is called mounting

The point at which external filesystem gets attached is called mount point

Use mount to list currently mounted filesystems.

mount <disk path> <mountpoint> to mount an external file systems

umount <dispath or mountpoint> is used to unmount the file system

        Before mount
=============================
Host System
--------------   
/
├── ...
├── ...
├── volume1
├── ...
├── ...
.
.

Extenral Filesystem at /dev/disk1s2
-----------------------------------
/
├── dir1
├── dir2
├── dir3
├── ...
.
.

After `mount /dev/disk1s2 volume1`
===================================
/
├── ...
├── ...
├── volume1
│   ├── dir1
│   ├── dir2
│   ├── dir3
│   ├── ...
|   .
|   .
├── ...
.
.

Switching root - chroot

  • If the mounted directory is a Linux root, you can get the shell of mounted Linux using chroot <mountpoint>.
  • But you need to link /dev and /proc of the host (either using ln -s or mount -o bind) to that of the inside of the mount point.
  • If we want network access inside the shell, we also have to copy /etc/resolv.conf to the inside of the mount point.
  • This process is typically done from live-boot media to gain access to OS on a hard disk to repair it.

Unsupported Filesystems

  • mount supports filesystems implemented in the kernel
  • To add new filesystem, we have to implement a kernel module
  • But, FUSE (File system in USEr space) allows us to implement file system in userspace
  • Just have to implement a bunch of functions!
  • Your classmates are working on a project on FUSE 😄
  • exfat-fuse, apfs-fuse, ...

Filesystem Utilities

Linux has several utilities to work with filesystem. They are listed below

Utility Uses
mount and umount Mounting and unmounting a filesystem, respectively
df List amount of disk space available on file system
du Disk usage of each file in particular directory. -h to human readble sizes
fdisk Partition table manipulation

Stay ~, Stay Safe!