I recently took a painful and convoluted path to understanding management of disks in Linux. I wanted to post that here for my own reference, and maybe you will find it useful as well. Note that these commands should generally not be directly copy-pasted, and should be used advisedly after careful planning.
Let’s take a plunge into the ocean, shall we?
You’ve definitely seen this. This is the surface level, the very highest layer of abstraction. If you’re not a sysadmin, there’s a good chance this is the only layer you care about (on your phone, it’s likely you don’t even care about this one!).
The filesystem is where files are kept and managed. There are tools to mount either the underlying device (/dev/mapper or /dev/vgname) or the filesystem itself to mount points – for example, over NFS. You can also use the filesystem on a logical volume (see below) as the disk for a virtual machine.
This is where ext2, ext3, ext4, xfs, and more come in. This is not a post about filesystems (I don’t know enough about filesystems to credibly write that post) but they each have features and associated utilities. Most of our systems are ext4 but we have some older systems with ext2 and some systems with xfs.
Commands (vary by filesystem)
df -h can show you if your filesystem mount is crowded for storage
e4fsck, and similar are wrappers around
resize2fs /dev/lv/home 512G # resize a filesystem to be 512G, might accompany
xfsrestore for XFS filesystems
mkfs /dev/lvmdata/device # make a filesystem on a device
fdisk -l isn’t technically a filesystem tool, but it operates at a high level of abstraction and you should be aware of it
Filesystems are made on top of underlying volumes in LVM, or “logical volume manager” – Linux’s partitioning system. (Actually manipulating LVM’s rather that passively using simple defaults is technically optional, but it’s widely used.)
LVM has three layers of abstraction within itself that each have utilities associated with them. This closely follows the abstraction patterns we’ve already seen in the layers below this one.
LVM logical volumes
A volume group can then be organized into logical volumes. The commands here are incredibly powerful and give you the ability to manage disk space with ease (we’re grading “easy” on a curve).
If you want to resize a filesystem, there’s a good chance you’ll want to follow up by resizing the volume underneath it.
lvcreate -L 20G -n mylv myvg # create a 20GB LVM called mylv in group myvg
lvresize -L 520G /dev/lv/home # make the LVM on /dev/lv/home 520GB in size
LVM volume groups
A logical volume is created from devices/space within a volume group. It’s a collection of one or more LVM “physical” volumes (see below).
pvmove /dev/mydevice # to get stuff off of a PV and move it to available free space elsewhere in the VG
LVM physical volumes
At the lowest LVM layer there are “physical” volumes. These might actually correspond to physical volumes (if you have no hardware RAID), or they might be other /dev objects in the OS (/dev/md127 would be a physical volume in this model).
These are the LVM analog to disk partitions.
Software RAID (optional)
RAID is a system for data management on disk. There are both “hardware” and “software” implementations of RAID, and software is at a higher level of abstraction. It’s convenient for a (super-)user to manage. Our machines (like many) use mdadm, but there are other tools.
mdadm --detail --scan
mdadm -D /dev/mdXYZ # details
mdadm -Q /dev/mdXYZ # short, human-readable
Devices in the OS
“In UNIX, everything is a file.” In Linux that’s mostly true as well.
The /dev directory contains the files that correspond to each particular device detected by the OS. I found these useful mostly for reference, because everything refers to them in some way.
If you look closely, things like /dev/mapper/devicename are often symlinks (pointers) to other devices.
All the other layers provide you better abstractions and more powerful tools for working with devices. For that reason, you probably won’t do much with these directly.
(The astute will observe that /dev is a directory so we’ve leapt up the layers of abstraction here. True! However, it’s the best lens you as a user have on the things the OS detects in the lower layers.)
dmesg. It will help you.
Hardware RAID (optional)
If you use software RAID for convenience, you use hardware RAID for performance and information-hiding.
Hardware RAID presents the underlying drives to the OS at boot time by way of a RAID controller on the motherboard. At boot, you can access a tiny bit of software (with a GUI that’s probably older than me) to create and modify hardware RAID volumes. In other words, the RAID volume(s), not the physical drives, appear to you as a user.
At least some, and I presume most, RAID controllers have software that you can install on the operating system that will let you get a look at the physical disks that compose the logical volumes.
Relevant software at this level:
MegaCLI # we have a MegaRAID controller on the server in question
smartctl -a -d megaraid,15 /dev/bus/6 # substitute the identifying numbers from the scan command above
- not much else – managing hardware RAID carefully requires a reboot; for this reason we tend to keep ours simple
We have reached the seafloor, where you have some drives – SSD’s, spinning disks, etc. Those drives are the very lowest level of abstraction: they are literal, physical machines. Because of this, we don’t tend to work with them directly except at installation and removal.
Summary and context
From highest to lowest layers of abstraction:
- LVM [lv > vg > pv]
- software RAID
- devices in the OS
- hardware RAID
The origin story of this blog post (also a wiki page, if you’re an Earlham CS sysadmin student!): necessity’s the mother of invention.
I supervise a sysadmin team. It’s a team of students who work part-time, so in practice I’m a player-coach.
In February, we experienced a disk failure that triggered protracted downtime on an important server. It was a topic I was unfamiliar with, so I did a lot of on-the-job training and research. I read probably dozens of blog posts about filesystems, but none used language that made sense to me in a coherent, unified, and specific way. I hope I’ve done so here, so that others can learn from my mistakes!