Disks and Block Devices

Storage is a block device

A hard disk or SSD is a block device: a long row of fixed-size blocks the kernel can read and write in any order. Block devices live in /dev. On a normal server the first disk is /dev/sda (or /dev/vda on a virtual machine), the second /dev/sdb, and their partitions get a number: /dev/sda1, /dev/sda2.

lsblk (list block devices) draws them as a tree, and df -h shows which ones are mounted and how full they are. This machine's root filesystem is served to your browser over the network, so df shows it as host9p and lsblk has almost nothing to list yet:

lsblk
df -h

In the terminal

student@vulntech:~$ lsblk
NAME MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sr0   11:0    1 1024M  1 rom
student@vulntech:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            224M     0  224M   0% /dev
tmpfs            48M  824K   47M   2% /run
host9p          256G  421M  256G   1% /
tmpfs           238M     0  238M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock

Making a practice disk

There is no spare physical disk here to experiment on, so we make one. A disk is really just a lot of blocks, and a file full of zeros is exactly that. dd creates the file, and losetup attaches it to a loop device — a block device backed by a file. From then on the kernel treats it like a real disk:

cd ~
dd if=/dev/zero of=disk.img bs=1M count=64
ls -lh disk.img
sudo losetup -fP --show disk.img
lsblk

In the terminal

student@vulntech:~$ cd ~
student@vulntech:~$ dd if=/dev/zero of=disk.img bs=1M count=64
64+0 records in
64+0 records out
67108864 bytes (67 MB, 64 MiB) copied, 2.17399 s, 30.9 MB/s
student@vulntech:~$ ls -lh disk.img
-rw-r--r-- 1 student student 64M Oct 18 05:05 disk.img
student@vulntech:~$ sudo losetup -fP --show disk.img
[sudo] password for student:
/dev/loop0
student@vulntech:~$ lsblk
NAME  MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
loop0   7:0    0   64M  0 loop
sr0    11:0    1 1024M  1 rom

losetup -f picks the first free loop device (here /dev/loop0), --show prints the name it chose, and -P tells the kernel to look for partitions on it. Now lsblk lists loop0: a 64 MiB disk, ready to use.

Think of it like this: A loop device is a real disk as far as every command is concerned. Everything in this module — mkfs, mount, fdisk, parted — is exactly what you would type against /dev/sdb on a real server. Only the name differs.

Tip: losetup -a lists every loop device in use and the file behind it. sudo losetup -d /dev/loop0 detaches one when you're done with it.

Remember: A disk is a block device in /dev (/dev/sda, /dev/sdb). lsblk lists block devices; df -h shows the mounted ones. Here we practise on an image file attached with sudo losetup -fP --show FILE.

HOME LEARN COMMUNITY DASHBOARD
Practice terminal Starting…