Diferență între revizuiri ale paginii „OS Lab 2 - Linux Filesystems”

De la WikiLabs
Jump to navigationJump to search
 
(Nu s-au afișat 8 versiuni intermediare efectuate de același utilizator)
Linia 6: Linia 6:
 
* Distinguish common filesystem node types: regular files, directories, symbolic links, block/character devices, FIFOs (named pipes), and Unix domain sockets.
 
* 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 <code>df</code> and <code>lsblk</code>, and explain the relationship between them.
 
* Discover mounted filesystems and underlying block devices using <code>df</code> and <code>lsblk</code>, and explain the relationship between them.
* Manipulate filesystem nodes using standard CLI tools (<code>ls</code>, <code>cp</code>, <code>mv</code>, <code>rm</code>, <code>mkdir</code>, <code>ln</code>, <code>chmod</code>, <code>chown</code>, <code>find</code>, <code>file</code>, <code>stat</code>).
+
* Manipulate filesystem nodes using standard CLI tools (<code>ls</code>, <code>cp</code>, <code>mv</code>, <code>rm</code>, <code>mkdir</code>, <code>ln</code>, <code>find</code>, <code>file</code>, <code>stat</code>).
 
* Create a loopback block device backed by a file and format it with <code>mkfs.ext4</code>, then mount, verify, and clean it up safely.
 
* Create a loopback block device backed by a file and format it with <code>mkfs.ext4</code>, then mount, verify, and clean it up safely.
  
---
+
= Quick Refresher: Paths and Hierarchy =
 
 
= Quick Refresher: Paths & Hierarchy =
 
  
 
Linux organizes everything in a single tree with <code>/</code> as the root. Example key directories:
 
Linux organizes everything in a single tree with <code>/</code> as the root. Example key directories:
Linia 25: Linia 23:
 
cd ~
 
cd ~
 
ls -la </syntaxhighlight>
 
ls -la </syntaxhighlight>
 
---
 
  
 
= Filesystem Node Types =
 
= Filesystem Node Types =
Linia 63: Linia 59:
 
cat a.sym              # will fail: dangling symlink </syntaxhighlight>
 
cat a.sym              # will fail: dangling symlink </syntaxhighlight>
  
---
+
= Discovering Filesystems and Devices =
 
 
= Discovering Filesystems & Devices =
 
  
 
Two essential tools:
 
Two essential tools:
Linia 100: Linia 94:
 
df -hT ~
 
df -hT ~
 
lsblk -f | grep $(df --output=source ~ | tail -1) </syntaxhighlight>
 
lsblk -f | grep $(df --output=source ~ | tail -1) </syntaxhighlight>
 
---
 
  
 
= Working with Nodes =
 
= Working with Nodes =
Linia 107: Linia 99:
 
Core commands (read <code>man</code> pages for details):
 
Core commands (read <code>man</code> pages for details):
  
* '''List & inspect''': <code>ls -la</code>, <code>tree</code> (optional), <code>file</code>, <code>stat</code>.
+
* '''List and inspect''': <code>ls -la</code>, <code>tree</code> (optional), <code>file</code>, <code>stat</code>.
 
* '''Create''': <code>touch</code>, <code>mkdir</code>, <code>mkfifo</code>, <code>ln -s</code>, (rarely) <code>mknod</code> for manual device nodes.
 
* '''Create''': <code>touch</code>, <code>mkdir</code>, <code>mkfifo</code>, <code>ln -s</code>, (rarely) <code>mknod</code> for manual device nodes.
 
* '''Copy/Move/Delete''': <code>cp</code> (<code>-r</code> for dirs), <code>mv</code>, <code>rm</code> (<code>-r</code> recursive, <code>-i</code> interactive).
 
* '''Copy/Move/Delete''': <code>cp</code> (<code>-r</code> for dirs), <code>mv</code>, <code>rm</code> (<code>-r</code> recursive, <code>-i</code> interactive).
* '''Permissions & ownership''': <code>chmod</code>, <code>chown</code>, <code>chgrp</code>, <code>umask</code>.
 
 
* '''Search''': <code>find</code>, <code>grep</code>.
 
* '''Search''': <code>find</code>, <code>grep</code>.
 
* '''View''': <code>cat</code>, <code>less</code>, <code>head</code>, <code>tail</code>, <code>wc</code>.
 
* '''View''': <code>cat</code>, <code>less</code>, <code>head</code>, <code>tail</code>, <code>wc</code>.
 
* '''Space usage''': <code>du -sh DIR</code>, <code>df -hT</code>.
 
* '''Space usage''': <code>du -sh DIR</code>, <code>df -hT</code>.
  
'''Mini-lab:''' <syntaxhighlight lang="bash">
+
'''Mini-lab:'''
 +
 
 +
<syntaxhighlight lang="bash">
 
mkdir -p ~/lab-fs/dir1/dir2
 
mkdir -p ~/lab-fs/dir1/dir2
 
cd ~/lab-fs
 
cd ~/lab-fs
Linia 125: Linia 118:
 
file notes.txt dir1/pipe link-to-data
 
file notes.txt dir1/pipe link-to-data
 
find . -type f -size +0
 
find . -type f -size +0
chmod 640 notes.txt && stat notes.txt </syntaxhighlight>
+
stat notes.txt
 +
</syntaxhighlight>
  
 
: Note: Sockets are typically created by running daemons. You can spot them with <code>find -type s</code> (e.g., in <code>/run/</code> or <code>/var/run/</code>).
 
: Note: Sockets are typically created by running daemons. You can spot them with <code>find -type s</code> (e.g., in <code>/run/</code> or <code>/var/run/</code>).
  
---
+
= Creating and Using a Loopback Filesystem =
 
 
= Creating & Using a Loopback Filesystem =
 
  
 
Goal: Create a '''file-backed block device''' with <code>losetup</code>, format it as '''ext4''', mount it, and verify via <code>df</code> and <code>lsblk</code>.
 
Goal: Create a '''file-backed block device''' with <code>losetup</code>, format it as '''ext4''', mount it, and verify via <code>df</code> and <code>lsblk</code>.
Linia 163: Linia 155:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
sudo mkdir -p /mnt/labfs
 
sudo mkdir -p /mnt/labfs
sudo mount "$LOOPDEV" /mnt/labfs
+
sudo mount /dev/loopX /mnt/labfs
  
 
df -hT /mnt/labfs
 
df -hT /mnt/labfs
lsblk -f | grep $(basename "$LOOPDEV")
+
lsblk -f loopX
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Linia 187: Linia 179:
 
: '''Safety tip:''' Always unmount before detaching the loop device. Use <code>lsof | grep /mnt/labfs</code> if the mount is busy.
 
: '''Safety tip:''' Always unmount before detaching the loop device. Use <code>lsof | grep /mnt/labfs</code> if the mount is busy.
  
---
+
= Mount Tables and Where Linux Stores Them =
 
 
= Mount Tables & Where Linux Stores Them =
 
  
 
* <code>mount</code> without args lists mounts.
 
* <code>mount</code> without args lists mounts.
Linia 202: Linia 192:
 
cat /etc/fstab
 
cat /etc/fstab
 
</syntaxhighlight>
 
</syntaxhighlight>
 
---
 
  
 
= Assessment: Lab Submission Tasks =
 
= Assessment: Lab Submission Tasks =
Linia 209: Linia 197:
 
Complete the following tasks and submit a report with your commands and output.
 
Complete the following tasks and submit a report with your commands and output.
  
# '''Device to Filesystem Mapping'''
+
=== Device to Filesystem Mapping ===
 +
* Run <code>df -hT</code> and <code>lsblk -f</code>.
 +
* Identify the line in each result that corresponds to your <code>/</code> (root) filesystem.
  
#* Run <code>df -hT</code> and <code>lsblk -f</code>.
+
=== Links and Inodes ===
#* Identify the line in each result that corresponds to your <code>/</code> (root) filesystem.
+
* Create a file <code>original.txt</code>, a hard link <code>hard.link</code>, and a symbolic link <code>symbolic.link</code> to it.
 +
* Provide the output of <code>ls -li</code> and explain why the inode numbers are the same or different.
  
# '''Links and Inodes'''
+
=== Loopback Filesystem Creation ===
 +
* Create a 150&nbsp;MB <code>loop.img</code>, format it with ext4 (label <code>MY_LOOP</code>), and mount it on <code>/mnt/mydata</code>.
 +
* Create a file <code>proof.txt</code> inside it containing your name.
 +
* Provide the final output of <code>df -hT /mnt/mydata</code> and <code>lsblk -f</code> showing your mounted device.
  
#* Create a file <code>original.txt</code>, a hard link <code>hard.link</code>, and a symbolic link <code>symbolic.link</code> to it.
+
=== <code>find</code> Command Practice ===
#* Provide the output of <code>ls -li</code> and explain why the inode numbers are the same or different.
+
* Provide the <code>find</code> command that lists all files in your home directory (<code>~</code>) larger than 1&nbsp;MB and modified in the last 7 days.
 +
* Provide the <code>find</code> command to list all Unix domain sockets under <code>/run</code>.
  
# '''Loopback Filesystem Creation'''
+
=== Real-World Scenario: The Automated Organizer ===
 
+
# ''Imagine your <code>Downloads</code> directory is cluttered. Your task is to write a sequence of commands to automatically clean it up.''
#* Create a 150 MB <code>loop.img</code>, format it with ext4 (label <code>MY_LOOP</code>), and mount it on <code>/mnt/mydata</code>.
+
# '''Setup:''' Create a <code>~/cleanup_target</code> directory with at least 10 files, including:
#* Create a file <code>proof.txt</code> inside it containing your name.
+
#* Three <code>.log</code> files (e.g., <code>program1.log</code>).
#* Provide the final output of <code>df -hT /mnt/mydata</code> and <code>lsblk -f</code> showing your mounted device.
+
#* Three <code>.tmp</code> files (e.g., <code>data.tmp</code>).
 
+
#* One file larger than 10&nbsp;MB (e.g., <code>truncate -s 15M large_dataset.dat</code>).
# '''<code>find</code> Command Practice'''
+
#* Some other miscellaneous files.
 
+
# '''Automation Task:'''
#* Provide the <code>find</code> command that lists all files in your home directory (<code>~</code>) larger than 1 MB and modified in the last 7 days.
+
#* Create subdirectories: <code>logs</code>, <code>temp</code>, and <code>large_files</code>.
#* Provide the <code>find</code> command to list all Unix domain sockets under <code>/run</code>.
+
#* Move all <code>.log</code> files into <code>logs</code>.
 
+
#* Move all <code>.tmp</code> files into <code>temp</code>.
# '''Real-World Scenario: The Automated Organizer'''
+
#* Move all files larger than 10&nbsp;MB into <code>large_files</code>.
 
+
#* Create a <code>cleanup_report.txt</code> listing the contents of the new subdirectories.
#* ''Imagine your <code>Downloads</code> directory is cluttered. Your task is to write a sequence of commands to automatically clean it up.''
+
# '''Submission:''' Provide the exact sequence of commands you used.
#* '''Setup:''' Create a <code>~/cleanup_target</code> directory with at least 10 files, including:
+
# '''Bonus:''' Provide a solution that does '''not''' use shell expansion (like <code>*.log</code>). Instead, use a more robust tool like <code>find</code> with <code>-exec</code> or <code>xargs</code>.
#** Three <code>.log</code> files (e.g., <code>program1.log</code>).
 
#** Three <code>.tmp</code> files (e.g., <code>data.tmp</code>).
 
#** One file larger than 10 MB (e.g., <code>truncate -s 15M large_dataset.dat</code>).
 
#** Some other miscellaneous files.
 
#* '''Automation Task:'''
 
#** Create subdirectories: <code>logs</code>, <code>temp</code>, and <code>large_files</code>.
 
#** Move all <code>.log</code> files into <code>logs</code>.
 
#** Move all <code>.tmp</code> files into <code>temp</code>.
 
#** Move all files larger than 10 MB into <code>large_files</code>.
 
#** Create a <code>cleanup_report.txt</code> 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 <code>*.log</code>). Instead, use a more robust tool like <code>find</code> with <code>-exec</code> or <code>xargs</code>.
 
  
 +
=== Submission ===
 
Submit a short report with command history snippets and brief explanations (≈1–2 pages).
 
Submit a short report with command history snippets and brief explanations (≈1–2 pages).
 
---
 
  
 
= Troubleshooting =
 
= Troubleshooting =
Linia 257: Linia 239:
 
* '''No free loop device''' — Create one: <code>sudo modprobe loop</code>; or manually: <code>sudo losetup /dev/loop10 loop.img</code>.
 
* '''No free loop device''' — Create one: <code>sudo modprobe loop</code>; or manually: <code>sudo losetup /dev/loop10 loop.img</code>.
 
* '''Wrong device formatted''' — Always confirm with <code>lsblk -f</code> before <code>mkfs.*</code>. In a VM, take a snapshot first.
 
* '''Wrong device formatted''' — Always confirm with <code>lsblk -f</code> before <code>mkfs.*</code>. In a VM, take a snapshot first.
 
---
 
  
 
= Optional Extensions =
 
= Optional Extensions =
  
* Create partitions '''inside''' the loop device (<code>sudo sfdisk "$LOOPDEV"</code>), then <code>partprobe</code> and format <code>/dev/loopXp1</code>.
+
* Create partitions '''inside''' the loop device (<code>sudo sfdisk /dev/loopX</code>), then <code>partprobe</code> and format <code>/dev/loopXp1</code>.
 
* Compare <code>mkfs.ext2</code> vs. <code>mkfs.ext4</code> space usage and features (journaling, extent maps).
 
* Compare <code>mkfs.ext2</code> vs. <code>mkfs.ext4</code> space usage and features (journaling, extent maps).
* Mount with options: <code>sudo mount -o noatime,nodiratime "$LOOPDEV" /mnt/labfs</code> and measure <code>stat</code> times.
+
* Mount with options: <code>sudo mount -o noatime,nodiratime /dev/loopX /mnt/labfs</code> and measure <code>stat</code> times.
  
---
+
== Quick Command Cheat Sheet ==
  
== Quick Command Cheat Sheet == <syntaxhighlight lang="bash">
+
<syntaxhighlight lang="bash">
  
 
# Discover
 
# Discover

Versiunea curentă din 10 octombrie 2025 15:26

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, 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 and 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:

Char Type How to see/create/observe
- Regular file touch file; write with editors or echo/cat
d Directory mkdir dir; remove with rmdir or rm -r
l Symbolic link ln -s target linkname
b Block device Usually in /dev (e.g., /dev/sda, /dev/loop0)
c Character device /dev/null, /dev/tty
p FIFO (named pipe) mkfifo pipe; read/write with cat/echo
s 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

Discovering Filesystems and Devices

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 and 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).
  • 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
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 and Using a Loopback Filesystem

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 /dev/loopX /mnt/labfs

df -hT /mnt/labfs
lsblk -f loopX

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 and 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.

Device to Filesystem Mapping

  • Run df -hT and lsblk -f.
  • Identify the line in each result that corresponds to your / (root) filesystem.

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.

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.

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.

Real-World Scenario: The Automated Organizer

  1. Imagine your Downloads directory is cluttered. Your task is to write a sequence of commands to automatically clean it up.
  2. 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.
  3. 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.
  4. Submission: Provide the exact sequence of commands you used.
  5. Bonus: Provide a solution that does not use shell expansion (like *.log). Instead, use a more robust tool like find with -exec or xargs.

Submission

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 /dev/loopX), 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 /dev/loopX /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)

# 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