OS Lab 2 - Linux Filesystems

De la WikiLabs
Versiunea din 10 octombrie 2025 13:14, autor: Vserbu (discuție | contribuții) (Pagină nouă: = Learning Objectives = By the end, you will be able to: * Explain the Linux filesystem hierarchy and how paths work (absolute vs. relative). * Distinguish common filesystem node...)
(dif) ← Versiunea anterioară | Versiunea curentă (dif) | Versiunea următoare → (dif)
Jump to navigationJump to search

Learning Objectives

By the end, you will be able to:

  • Explain the Linux filesystem hierarchy and how paths work (absolute vs. relative).
  • Distinguish common filesystem node types: regular files, directories, symbolic links, block/character devices, FIFOs (named pipes), and Unix domain sockets.
  • Discover mounted filesystems and underlying block devices using df and lsblk, and explain the relationship between them.
  • Manipulate filesystem nodes using standard CLI tools (ls, cp, mv, rm, mkdir, ln, chmod, chown, find, file, stat).
  • Create a loopback block device backed by a file and format it with mkfs.ext4, then mount, verify, and clean it up safely.

---

Quick Refresher: Paths & Hierarchy

Linux organizes everything in a single tree with / as the root. Example key directories:

  • /etc (system config), /bin (essential binaries), /usr (user-land programs), /home (user homes), /var (variable data), /mnt (temporary mounts), /proc (procfs), /dev (device nodes).

Absolute path: starts at / (e.g., /home/student/lab/loop.img). Relative path: from current directory (e.g., ../lab/loop.img). Use pwd, cd, and tab-completion to navigate.

Try:

pwd
cd ~
ls -la

---

Filesystem Node Types

Each directory entry points to an inode (metadata: type, permissions, owner, timestamps, block pointers). The file type is shown by the first character in ls -l:

| | | | - | ----------------- | - | ---------------------------------------------------------------------------------------- | | Regular file | | touch file; write with editors or echo/cat | | | | | | Directory | | mkdir dir; remove with rmdir or rm -r | | | | | | Symbolic link | | ln -s target linkname | | | | | | Block device | | Usually in /dev (e.g., /dev/sda, /dev/loop0) | | | | | | Character device | | /dev/null, /dev/tty | | | | | | FIFO (named pipe) | | mkfifo pipe; read/write with cat/echo | | | | | | Unix socket | | Created by programs (e.g., services); list with ls -l or ss -x | | | | | Explore inode numbers:
ls -li
stat file
Hard vs. symbolic links:
mkdir ~/lab-nodes && cd ~/lab-nodes
printf 'hello\n' > a.txt
ln a.txt a.hard        # hard link (same inode)
ln -s a.txt a.sym      # symlink (different inode)
ls -li
rm a.txt && cat a.hard # still works; inode persists via hard link
cat a.sym              # will fail: dangling symlink

---

Part 2 — Discovering Filesystems & Devices (25 min)

Two essential tools:

df — usage of mounted filesystems

  • Shows how full mounted filesystems are and where they are mounted.
  • Helpful flags: -h (human), -T (type), target path to narrow output.
df -hT
# Focus on a path
sudo mkdir -p /mnt && df -hT /mnt

lsblk — block devices and partitions

  • Shows block devices (disks, partitions, loop devices) regardless of mount state.
  • Helpful flags: -f (FSType/Label/UUID), -o to customize columns.
lsblk
lsblk -f
lsblk -o NAME,MAJ:MIN,RM,SIZE,RO,TYPE,FSTYPE,LABEL,UUID,MOUNTPOINTS

Relationship between df and lsblk

  • lsblk lists devices (e.g., /dev/sda2, /dev/loop0) and where they’re mounted (MOUNTPOINTS column).
  • df reports space usage per mounted filesystem.
 Mapping tip: Find the row in lsblk -f whose MOUNTPOINTS equals the df mountpoint; the corresponding NAME (e.g., sda2) is the device backing that filesystem.
Exercise: Identify the device backing your home directory and its filesystem type.
df -hT ~
lsblk -f | grep $(df --output=source ~ | tail -1)

---

Working with Nodes

Core commands (read man pages for details):

  • List & inspect: ls -la, tree (optional), file, stat.
  • Create: touch, mkdir, mkfifo, ln -s, (rarely) mknod for manual device nodes.
  • Copy/Move/Delete: cp (-r for dirs), mv, rm (-r recursive, -i interactive).
  • Permissions & ownership: chmod, chown, chgrp, umask.
  • Search: find, grep.
  • View: cat, less, head, tail, wc.
  • Space usage: du -sh DIR, df -hT.
Mini-lab:
mkdir -p ~/lab-fs/dir1/dir2
cd ~/lab-fs
touch notes.txt
echo "sample" > dir1/data
mkfifo dir1/pipe
ln -s dir1/data link-to-data
ls -lR
file notes.txt dir1/pipe link-to-data
find . -type f -size +0
chmod 640 notes.txt && stat notes.txt
Note: Sockets are typically created by running daemons. You can spot them with find -type s (e.g., in /run/ or /var/run/).

---

Creating & Using a Loopback Filesystem (45–60 min)

Goal: Create a file-backed block device with losetup, format it as ext4, mount it, and verify via df and lsblk.

Create a sparse backing file

mkdir -p ~/lab-loop && cd ~/lab-loop
truncate -s 100M loop.img   # fast; or: dd if=/dev/zero of=loop.img bs=1M count=100
ls -lh loop.img

Attach the file to a free loop device

sudo losetup -fP loop.img               # chooses next free /dev/loopX
losetup -a                               # show all loop mappings
Verify with lsblk:
lsblk -f | grep loopX

Make an ext4 filesystem on the loop device

sudo mkfs.ext4 -L LAB_FS /dev/loopX

What happens: ext4 creates a superblock and inode tables, then initializes data structures.

Mount and validate

sudo mkdir -p /mnt/labfs
sudo mount "$LOOPDEV" /mnt/labfs

df -hT /mnt/labfs
lsblk -f | grep $(basename "$LOOPDEV")

Create some content and examine space usage:

sudo sh -c 'echo "hello ext4" > /mnt/labfs/hello.txt'
sudo ls -la /mnt/labfs
sudo du -sh /mnt/labfs

Unmount and detach (clean up)

sudo umount /mnt/labfs
sudo losetup -d /dev/loopX
rm -f loop.img
Safety tip: Always unmount before detaching the loop device. Use lsof | grep /mnt/labfs if the mount is busy.

---

Mount Tables & Where Linux Stores Them

  • mount without args lists mounts.
  • Modern systems reflect mounts in /proc/self/mounts and /proc/mounts. Many distros keep /etc/mtab as a compatibility symlink.
  • Persistent mounts are configured in /etc/fstab (be careful!).

Explore:

cat /proc/mounts | head
mount | head
cat /etc/fstab

---

Assessment: Lab Submission Tasks

Complete the following tasks and submit a report with your commands and output.

  1. Device to Filesystem Mapping
    • Run df -hT and lsblk -f.
    • Identify the line in each result that corresponds to your / (root) filesystem.
  1. Links and Inodes
    • Create a file original.txt, a hard link hard.link, and a symbolic link symbolic.link to it.
    • Provide the output of ls -li and explain why the inode numbers are the same or different.
  1. Loopback Filesystem Creation
    • Create a 150 MB loop.img, format it with ext4 (label MY_LOOP), and mount it on /mnt/mydata.
    • Create a file proof.txt inside it containing your name.
    • Provide the final output of df -hT /mnt/mydata and lsblk -f showing your mounted device.
  1. find Command Practice
    • Provide the find command that lists all files in your home directory (~) larger than 1 MB and modified in the last 7 days.
    • Provide the find command to list all Unix domain sockets under /run.
  1. Real-World Scenario: The Automated Organizer
    • Imagine your Downloads directory is cluttered. Your task is to write a sequence of commands to automatically clean it up.
    • Setup: Create a ~/cleanup_target directory with at least 10 files, including:
      • Three .log files (e.g., program1.log).
      • Three .tmp files (e.g., data.tmp).
      • One file larger than 10 MB (e.g., truncate -s 15M large_dataset.dat).
      • Some other miscellaneous files.
    • Automation Task:
      • Create subdirectories: logs, temp, and large_files.
      • Move all .log files into logs.
      • Move all .tmp files into temp.
      • Move all files larger than 10 MB into large_files.
      • Create a cleanup_report.txt listing the contents of the new subdirectories.
    • Submission: Provide the exact sequence of commands you used.
    • Bonus: For bonus points, provide a solution that does not use shell expansion (like *.log). Instead, use a more robust tool like find with -exec or xargs.

Submit a short report with command history snippets and brief explanations (≈1–2 pages).

---

Troubleshooting

  • mkfs.ext4: device is busy — Ensure it’s not mounted: mount | grep loop; unmount, then retry.
  • umount: target is busy — Find open files: sudo lsof +f -- /mnt/labfs or fuser -vm /mnt/labfs.
  • No free loop device — Create one: sudo modprobe loop; or manually: sudo losetup /dev/loop10 loop.img.
  • Wrong device formatted — Always confirm with lsblk -f before mkfs.*. In a VM, take a snapshot first.

---

Optional Extensions

  • Create partitions inside the loop device (sudo sfdisk "$LOOPDEV"), then partprobe and format /dev/loopXp1.
  • Compare mkfs.ext2 vs. mkfs.ext4 space usage and features (journaling, extent maps).
  • Mount with options: sudo mount -o noatime,nodiratime "$LOOPDEV" /mnt/labfs and measure stat times.

---

== Quick Command Cheat Sheet ==
# Discover

pwd; ls -la; stat FILE; file FILE; du -sh DIR; df -hT; lsblk -f

# Make nodes

mkdir DIR; touch FILE; ln -s TARGET LINK; mkfifo PIPE

# Links vs inodes

ln SRC DEST   # hard link (same inode)
ln -s SRC DEST # symlink (diff inode)

# Permissions

chmod MODE PATH; chown USER:GROUP PATH

# Loopback + ext4

truncate -s 100M loop.img
sudo losetup -fP loop.img; losetup -a
sudo mkfs.ext4 -L LAB_FS /dev/loopX
sudo mkdir -p /mnt/labfs && sudo mount /dev/loopX /mnt/labfs
df -hT /mnt/labfs; lsblk -f | grep loopX
sudo umount /mnt/labfs; sudo losetup -d /dev/loopX; rm -f loop.img