Diferență între revizuiri ale paginii „OS Lab 2 - Linux Filesystems”
| Linia 9: | Linia 9: | ||
* 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 | + | = Quick Refresher: Paths and 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 59: | Linia 59: | ||
cat a.sym # will fail: dangling symlink </syntaxhighlight> | cat a.sym # will fail: dangling symlink </syntaxhighlight> | ||
| − | = Discovering Filesystems | + | = Discovering Filesystems and Devices = |
Two essential tools: | Two essential tools: | ||
| Linia 99: | Linia 99: | ||
Core commands (read <code>man</code> pages for details): | Core commands (read <code>man</code> pages for details): | ||
| − | * '''List | + | * '''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). | ||
| − | |||
* '''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>. | ||
| Linia 121: | Linia 120: | ||
: 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 | + | = Creating and 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 177: | Linia 176: | ||
: '''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 | + | = Mount Tables and Where Linux Stores Them = |
* <code>mount</code> without args lists mounts. | * <code>mount</code> without args lists mounts. | ||
Versiunea de la data 10 octombrie 2025 13:44
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
dfandlsblk, 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 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),-oto customize columns.
lsblk
lsblk -f
lsblk -o NAME,MAJ:MIN,RM,SIZE,RO,TYPE,FSTYPE,LABEL,UUID,MOUNTPOINTS
Relationship between df and lsblk
lsblklists devices (e.g.,/dev/sda2,/dev/loop0) and where they’re mounted (MOUNTPOINTS column).dfreports space usage per mounted filesystem.
Mapping tip: Find the row inlsblk -fwhoseMOUNTPOINTSequals thedfmountpoint; the correspondingNAME(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)mknodfor manual device nodes. - Copy/Move/Delete:
cp(-rfor dirs),mv,rm(-rrecursive,-iinteractive). - 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 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 "$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/labfsif the mount is busy.
Mount Tables and Where Linux Stores Them
mountwithout args lists mounts.- Modern systems reflect mounts in
/proc/self/mountsand/proc/mounts. Many distros keep/etc/mtabas 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 -hTandlsblk -f. - Identify the line in each result that corresponds to your
/(root) filesystem.
Links and Inodes
- Create a file
original.txt, a hard linkhard.link, and a symbolic linksymbolic.linkto it. - Provide the output of
ls -liand explain why the inode numbers are the same or different.
Loopback Filesystem Creation
- Create a 150 MB
loop.img, format it with ext4 (labelMY_LOOP), and mount it on/mnt/mydata. - Create a file
proof.txtinside it containing your name. - Provide the final output of
df -hT /mnt/mydataandlsblk -fshowing your mounted device.
find Command Practice
- Provide the
findcommand that lists all files in your home directory (~) larger than 1 MB and modified in the last 7 days. - Provide the
findcommand to list all Unix domain sockets under/run.
Real-World Scenario: The Automated Organizer
- Imagine your
Downloadsdirectory is cluttered. Your task is to write a sequence of commands to automatically clean it up. - Setup: Create a
~/cleanup_targetdirectory with at least 10 files, including:- Three
.logfiles (e.g.,program1.log). - Three
.tmpfiles (e.g.,data.tmp). - One file larger than 10 MB (e.g.,
truncate -s 15M large_dataset.dat). - Some other miscellaneous files.
- Three
- Automation Task:
- Create subdirectories:
logs,temp, andlarge_files. - Move all
.logfiles intologs. - Move all
.tmpfiles intotemp. - Move all files larger than 10 MB into
large_files. - Create a
cleanup_report.txtlisting the contents of the new subdirectories.
- Create subdirectories:
- Submission: Provide the exact sequence of commands you used.
- Bonus: Provide a solution that does not use shell expansion (like
*.log). Instead, use a more robust tool likefindwith-execorxargs.
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/labfsorfuser -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 -fbeforemkfs.*. In a VM, take a snapshot first.
Optional Extensions
- Create partitions inside the loop device (
sudo sfdisk "$LOOPDEV"), thenpartprobeand format/dev/loopXp1. - Compare
mkfs.ext2vs.mkfs.ext4space usage and features (journaling, extent maps). - Mount with options:
sudo mount -o noatime,nodiratime "$LOOPDEV" /mnt/labfsand measurestattimes.
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