Managing Files and Disk Space
This course - Linux partitions and filesystems - concludes the first half of our Linux certification series and, not coincidentally, covers the last topics you'll need to know for the LPIC-1 101 exam. The final six courses will get you up to speed on the 102 exam.
Besides getting to know the Linux Filesystem Hierarchy Standard, we'll learn how to:
- Create and maintain secure and reliable partitions and filesystems
- Mount and unmount filesystems
- Limit access to only authorized users.
- Create and manage hard and symbolic linked files.
- Control the disk space allocation.
The previous course covered the Linux command line.
If you have thoughts or suggestions for this course, please contact Cloud Academy at firstname.lastname@example.org.
If you're going to install a brand new block device, a hard disk or solid state drive, or if you're repurposing an old device, you'll probably have to properly prepare it before it's ready for action.
Managing Linux partitions and volumes
At the very least, your drive will require a partition and a file system type. The partition tells your computer's firmware interface or operating system where on the disk your data may be found, and the type defines the way the blocks of data are accessed and manipulated. That's the general goal. But things have been getting a bit complicated lately. For many years, disks were partitioned using the MBR, master boot record scheme. This allowed for a maximum of four primary partitions of which one partition could be designated as extended, and then subdivided into multiple logical partitions. Among other limitations however, MBRs can normally only manage smaller disks smaller than two terabytes. A ceiling that's beginning to feel lower and lower all the time.
The GPT scheme, GPT stands for GUID partition table, was introduced to overcome some of MBR's failings, and since 2010, it can be used in one form or another with just about any modern operating system. In the case of Windows 8.1 however, GPT can only be used alongside the UEFI interface, and not BIOS. This is going to be relevant to you as you prepare for the LPIC exams, since you'll need to be familiar with the tools that are available for creating and managing partitions. Therefore, we'll have to discuss the partition configuration tool,fdisk, but with the understanding that it was really only built for MBR systems.
Let's illustrate that. Let's run fdisk against my sdb drive. fdisk warns us that there is already a GPT partition table on the disk, and helpfully advises us to choose Parted instead. Besides Parted and its GUI version, GParted, that we encountered in an earlier course in this series, you can also use a program called gdisk. We'll take a brief look at gdisk and Parted a bit later. Right now, we'll run fdisk once again, but this time we'll go with sdc, which is a USB drive I just plugged in, rather than sdb, which happens to be my main drive.
Typing M will display a help menu with the most common program functions. P will list the partitions associated with our selected drive. There's only one in this case. L will list all possible partition types. The three that interest us the most in the context of Linux media are 82, which indicates a Linux swap partition, 83, Linux standard, and 85, which is Linux extended.
Before we go even one step further, I should note just how incredibly dangerous it can be to operate fdisk while intoxicated, or Parted a gdisk for that matter. And not only while intoxicated, working through fdisk menus while distracted or panicky can also easily result in irretrievable data loss. When a partition containing data is overwritten or sometimes resized, the data can be permanently lost. It can definitely be worthwhile to take a step back and calmly go through the documentation one or two more times before committing to a partition edit. Make sure you know exactly what you're doing and to which hardware media you're applying it before you start.
Which brings us to the N command, create new partition. We'll first have to decide whether the partition we'd like to create will be primary or extended. Let's go with extended. We can then choose which partition number it will have, and then the partition's start and finished position on the drive. Depending on what you're planning to do with this partition, of course, the defaults are usually fine.
At this point, we still haven't applied any actual changes to the disk. What won't happen until we hit the W key. But before we do that, we should hit T to select a partition type. Since we chose to make our new partition extended, we'll select type 85, which is Linux extended. Our partition changes and are ready to write, and again, hitting W would set that into motion. Since I have no need for that, I'll just hit Q to exit.
Now let's take a look at Parted. Help will display the command menu, where we see that you execute operations like making, resizing, naming, and deleting partitions. Each of these commands expects you to add the partition's identifying number, usually between one and four, and any necessary parameters. gdisk offers a very similar range of tools, whose commands more closely mimick fdisk and a useful command menu.
Let's review. You can use fdisk to create and configure older MBR partitions, while Parted or gdisk are better suited for disks with a GPT partition table. Typing P within fdisk will list the existing partitions, L lists partition types, N will take you to the menu level, where you can create a new partition, and T allows you to select a type for your new partition. You can load Parted using sudo parted, followed by the drive designation, and similarly with gdisk.
Formatting Linux partitions
Up to this point, we've learned how to create a partition and assign it a partition type. But if you want to make the partition available for any useful purpose, we'll also have to format it using a file system format. While you can use gdisk or parted for this, it's also common practice to make use of some standalone tools. Make FS will, as the name implies, make or apply a file system to a partition. The syntax is quite straightforward. mkfd followed by -t to specify the file system type, and the type you want, say, ext4, and the drive's designation.
By the way, if you're not sure what the drive designation is, you can run df to list all the partitions that are currently associated with your system. You should be able to figure out which one is yours from there, although once again, if you're at all in doubt, confirm before acting. Running mkswap will apply the swap file system to the partition you'd like to use for virtual swap memory. mkreiserfs will perform a partition as reiserfs (Reiser filesystem).
What's the difference between one file system and another? I haven't seen any actual data, but I suspect that the most common file system currently used on Linux is probably ext4. But the fact is that all file systems serve some purpose, and depending on your specific needs, you may find yourself making different formatting choices for different projects. ext2, the ext stands for extended, is the foundation for most mainstream Linux file systems, and is still useful for partitions that don't require journaling. ext3 and ext4 are upgrades of ext2 and add journaling.
Journaling will save a record of file system changes that makes recovery from crashes much more effective. ext4 delivered some improvements, including the ability to handle individual files up to 16 terabytes in size. reiserfs is also journaled and claims greater stability.
Better FS is a very popular file system that, if you had to sum it up in a few words, might be characterized as extremely stable and reliable. XFS is a 64 bit journaled file system, that provides superior performance working with large files and file systems. And VFAT is an extension of the FAT32 file system that lies beneath many flavors of Windows.
David taught high school for twenty years, worked as a Linux system administrator for five years, and has been writing since he could hold a crayon between his fingers. His childhood bedroom wall has since been repainted.
Having worked directly with all kinds of technology, David derives great pleasure from completing projects that draw on as many tools from his toolkit as possible.
Besides being a Linux system administrator with a strong focus on virtualization and security tools, David writes technical documentation and user guides, and creates technology training videos.
His favorite technology tool is the one that should be just about ready for release tomorrow. Or Thursday.