Module 7 · The system, disks and the environment
Block devices: blkid, fdisk -l, /dev
What a block device is, how to list the ones you have, how to read their labels and UUIDs, and how to turn an ordinary file into a disk with a loop device.
What you will learn
- Tell block devices from character devices in /dev.
- Inspect disks with fdisk -l, /proc/partitions and blkid.
- Attach a disk image to a loop device, format it and mount it.
Everything in /dev is a file, but two kinds of file. Character devices hand bytes through one at a time: terminals, the serial port, /dev/null, /dev/zero. Block devices are addressable in fixed-size blocks you can seek around in: disks, partitions, CD-ROMs, and anything you can put a filesystem on. ls -l shows the difference in the very first column, c or b.
~% ls -l /dev/sr0 /dev/loop0 /dev/null /dev/ttyS0
brw------- 1 root root 7, 0 Oct 4 18:51 /dev/loop0
crw-rw-rw- 1 root root 1, 3 Oct 4 18:51 /dev/null
brw------- 1 root root 11, 0 Oct 4 18:51 /dev/sr0
crw------- 1 root root 4, 64 Oct 4 18:51 /dev/ttyS0
Where a regular file shows a size, a device shows two numbers: the major picks the driver (11 is SCSI CD-ROM, 7 is loop, 8 would be SCSI/SATA disks like sda) and the minor picks the instance. Names follow conventions: sda, sdb for whole disks, sda1, sda2 for their partitions, sr0 for the first optical drive, loop0–loop7 for loop devices.
Which disks do I have?
Three views, from rawest to friendliest. /proc/partitions is the kernel's list of block devices with their size in 1 KB blocks. fdisk -l prints every disk's geometry and its partition table; fdisk -l DEVICE limits it to one. blkid reads the first bytes of each device looking for a filesystem signature, and reports the UUID (a unique id assigned at format time), the LABEL if one was set, and the TYPE. UUIDs are how modern /etc/fstab files name disks, because /dev/sdb can become /dev/sdc after a reboot but the UUID never moves.
~% cat /proc/partitions
major minor #blocks name
11 0 7550 sr0
~% fdisk -l /dev/sr0
Note: sector size is 2048 (not 512)
Disk /dev/sr0: 7 MB, 7731200 bytes, 15100 sectors
0 cylinders, 255 heads, 63 sectors/track
Units: cylinders of 16065 * 2048 = 32901120 bytes
Disk /dev/sr0 doesn't contain a valid partition table
The VM has a single block device, the boot CD, and no hard disk, so fdisk -l alone prints nothing and blkid alone is silent too. To have something to practise on, we will make a disk.
A disk made from a file: loop devices
A loop device makes a regular file behave as a block device. Create an empty 4 MB file with dd, attach it with losetup /dev/loop0 FILE, and /dev/loop0 now *is* that file as far as the kernel is concerned. mke2fs /dev/loop0 writes an ext2 filesystem onto it (-L NAME sets a label), after which blkid sees it, and mount /dev/loop0 DIR mounts it like any disk. losetup -a lists attachments, losetup -d /dev/loop0 detaches.
~% dd if=/dev/zero of=/root/lab/l66/disk.img bs=1M count=4
4+0 records in
4+0 records out
~% losetup /dev/loop0 /root/lab/l66/disk.img
~% mke2fs -L labdisk /dev/loop0 > /dev/null
~% blkid
/dev/loop0: LABEL="labdisk" UUID="487f006e-cba1-490a-9403-3d4513275e9f"
~% mount /dev/loop0 /root/lab/l66/mnt
~% df -h /root/lab/l66/mnt
Filesystem Size Used Available Use% Mounted on
/dev/loop0 3.9M 13.0K 3.7M 0% /root/lab/l66/mnt
~% ls /root/lab/l66/mnt
lost+found
| Tool | Tells you |
|---|---|
ls -l /dev/X | b or c, major and minor numbers |
cat /proc/partitions | Every block device the kernel knows, with size |
fdisk -l [DEV] | Disk size, geometry and partition table |
blkid [DEV] | Filesystem UUID, LABEL and TYPE |
losetup -a | Which files are attached to which loop devices |
Commands in this lesson
| Command | What it does |
|---|---|
ls -l /dev/sr0 | Show the device type (b/c) and major/minor numbers. |
cat /proc/partitions | Kernel list of block devices. |
fdisk -l /dev/sr0 | Size and partition table of one disk. |
blkid | UUID, label and type of every filesystem found. |
losetup /dev/loop0 FILE | Make FILE appear as the block device /dev/loop0. |
mke2fs -L NAME /dev/loop0 | Format with ext2 and a label (destroys contents!). |
losetup -d /dev/loop0 | Detach the loop device. |
Quiz
`ls -l` shows `brw------- ... 11, 0 /dev/sr0`. What does the leading b mean?
- Backup
- Block device
- Binary file
- Bootable
Why do modern fstab files refer to disks by UUID instead of /dev/sdb1?
- UUIDs are shorter
- Device names can change between boots; the UUID stays with the filesystem
- /dev names need root
- UUIDs are faster to mount
What does `losetup /dev/loop0 disk.img` do?
- Formats disk.img
- Mounts disk.img on /dev/loop0
- Makes the file disk.img accessible as the block device /dev/loop0
- Copies disk.img to the first disk
Before running mke2fs on a device you should…
- Mount it first
- Check with blkid or fdisk -l that it is the device you think, since formatting destroys its contents
- Run fsck
- Nothing; mke2fs asks for confirmation
Practice
Save fdisk's report about the CD-ROM device `/dev/sr0` (only that device) into `/root/lab/l66/sr0.txt`.
An empty 4 MB image exists at `/root/lab/l66/disk.img`. Attach it to `/dev/loop0` and format that loop device with an ext2 filesystem, so that blkid reports a UUID for it.
`/dev/loop0` is attached to a formatted image. Mount it on the existing directory `/root/lab/l66/mnt`.
Open this lesson in the app to do the tasks in a real Linux machine and have them checked.