Block Devices and Partitions#

Concepts#

Block Devices#

A block device is a storage device that reads and writes data in fixed-size blocks. Hard drives, SSDs, USB drives, and virtual disks are all block devices. They appear as files in /dev/:

Device Meaning
/dev/sda First SCSI/SATA disk
/dev/sdb Second disk
/dev/sda1 First partition on first disk
/dev/sda2 Second partition
/dev/nvme0n1 First NVMe SSD
/dev/nvme0n1p1 First partition on NVMe
/dev/vda Virtual disk (KVM/QEMU)

Viewing Block Devices#

# Best overview — tree format
lsblk
# NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
# sda      8:0    0    40G  0 disk
# ├─sda1   8:1    0   512M  0 part /boot/efi
# ├─sda2   8:2    0  38.5G  0 part /
# └─sda3   8:3    0     1G  0 part [SWAP]

# With filesystem info
lsblk -f

# Detailed partition info
sudo fdisk -l /dev/sda

# Block device details
sudo blkid

Partition Tables: MBR vs GPT#

A partition table defines how a disk is divided into partitions.

Feature MBR GPT
Full name Master Boot Record GUID Partition Table
Max disk size 2 TB 9.4 ZB (essentially unlimited)
Max partitions 4 primary (or 3 primary + 1 extended) 128
Boot mode BIOS (legacy) UEFI
Redundancy No backup Backup at end of disk

Modern systems use GPT. MBR is legacy but still encountered.

Partitioning Tools#

fdisk — Interactive Partitioning (MBR and GPT)#

sudo fdisk /dev/sdb
# Commands inside fdisk:
# p  — print partition table
# n  — create new partition
# d  — delete a partition
# t  — change partition type
# w  — write changes and exit
# q  — quit without saving

gdisk — GPT-specific partitioning#

sudo apt install -y gdisk
sudo gdisk /dev/sdb

parted — Scriptable partitioning#

sudo parted /dev/sdb print
sudo parted /dev/sdb mklabel gpt
sudo parted /dev/sdb mkpart primary ext4 1MiB 100%

Practical Partitioning Workflow#

# 1. Identify the disk
lsblk

# 2. Partition it
sudo fdisk /dev/sdb
# n → new partition → accept defaults → w → write

# 3. Create a filesystem
sudo mkfs.ext4 /dev/sdb1

# 4. Mount it
sudo mkdir -p /mnt/data
sudo mount /dev/sdb1 /mnt/data

# 5. Make it permanent (covered in next lesson)

Lab#

Exercise 1: View Your Disks#

lsblk
lsblk -f
sudo fdisk -l 2>/dev/null | head -30
sudo blkid

Exercise 2: Examine Partition Details#

# Detailed view of main disk
sudo fdisk -l /dev/sda 2>/dev/null || sudo fdisk -l /dev/vda 2>/dev/null

# See partition UUIDs
sudo blkid

Exercise 3: Disk Usage#

# Overall disk usage
df -h

# Usage by directory
du -sh /var/* 2>/dev/null | sort -rh | head -10
du -sh /home/* 2>/dev/null

Review#

1. What is the difference between `/dev/sda` and `/dev/sda1`?

/dev/sda is the entire first disk (block device). /dev/sda1 is the first partition on that disk. You create filesystems on partitions, not on whole disks.

2. What is the difference between MBR and GPT?

MBR is legacy: limited to 2TB disks and 4 primary partitions, uses BIOS boot. GPT is modern: supports huge disks, 128 partitions, uses UEFI, and has backup partition tables for redundancy.

3. What command gives the best overview of block devices?

lsblk — shows all block devices in a tree format with size, type, and mount points. Add -f for filesystem details.


Previous: Firewalls | Next: Filesystems and Mounting