Linux Administration for Nerds

Copyright 2018 Brian Davis - CC-BY-NC-SA


Let's start with some terminology that is used with Linux. The actual hard disk (or SSD, flash drive, optical drive, whatever) in your system is called a disk.
It will be assigned a name like /dev/sda. That disk may have 1 or more partitions that will be assigned /dev/sda1, /dev/sda2, and so on. You can also run a volume manager and split your partitions into volumes and do all kinds of weird things. If you want to know about that go read about LVM and good luck to you.

In order for a disk to be useful it must have a filesystem. Windows has several: FAT, FAT32, NTFS. Modern versions of Windows use NTFS for the primary disk and FAT32 for USB sticks. Linux supports a very wide array of filesystems, including NTFS and FAT. The default will depend on the distribution. CentOS uses XFS, but Ubuntu/Debian use ext4. A couple of important features differentiate filesystems. The first is journaling. A journaling filesystem keeps a log of all transactions before applying them. This means that a journaling filesystem is very resistant to getting corrupted by system crashes. On a laptop where the battery can run out, or a desktop where the power could go out or if your system is used for development and there might be segfaults journaling provides good insurance against data corruption. The next feature that I consider important is handling snapshots. Most filesystems rely on the volume manager (LLVM on Linux) to provide the capability to make snapshots. I've spent some time with LLVM and while it's features seem very powerful I dislike the added complexity. This complexity makes it difficult to recover from system crashes. If you can't figure out how the disk is partitioned, you can't figure out how to mount the filesystem. A filesystem should be conscientious about how it uses the disk. One failing of the FAT32 filesystem is that for large disks the minimum allocation size is large. That means that every file on the systems winds up taking 1-4Kb. This adds up on a system with lots of files. Your chosen filesystem should also have good performance. I've used ext4 for some time and found it meet most of my needs. However it lacks features for taking snapshots and can not be expanded across multiple disks. Recently I've experimented with btrfs and found it's snapshots and ability to incorporate multiple disks into a RAID environment very useful.

General commands

Partition a disk with fdisk or parted commands. Both are pretty straightforward.
gparted is also available with a nice GUI.

Be aware you can only have 4 primary partitions. After that you need to create an extended partition and sub-partition that. You should chose a primary partition to boot from. Remember to set that partition as bootable.

Create a filesystem with mkfs followed by the type of filesystem you want.


and so on.

Creating a filesystem must be done on an unmounted partition.

Mounting a filesystem is done with the mount command.

mount -t ext4 /dev/sda1 /mnt/tmp
mount -t vboxsf C_DRIVE /home/user/c
mount /dev/sdb1 -o uid=1000,gid=1000 /mnt/usb

Arguments explained: -t explicitly tells mount what the filesystem type is. It will try to figure it out on its own if you don't include it. -o are the mount options. Some of these are type dependent. uid= and gid= are useful if the filesystem does not support Linux user access control, such as FAT or NTFS commonly used on USB flash drives. 1000 is usually the main user's uid.

Resizing/Moving Swap

If you have to move swap, create a new partition with the type code 82

mkswap /dev/sdX

Update the /etc/fstab with

/dev/sdX    none    swap    defaults 0 0


resize disk /sdXN (ex. /sda1)

resize2fs /dev/sdXN 15G
fdisk /dev/sdXN

rm the ext4 partition and re-create smaller (but bigger than FS size above) without the size it will resize to the partition

resize2fs /dev/sdXN

probably need to

e2fsck -f /dev/sdXN

misc commands

lsblk   # list block devices
blkid   # list uuids

BTRFS Commands

btrfs subvolume show /
btrfs subvolume list -a /
btrfs fi show btrfs fi df /
btrfs filesystem resize max /mnt/rootfs

Example BTRFS installation

I installed debian jessie onto a 16GB virtual machine. I created one physical parition for swap (btrfs does not support swap) and another btrfs partition for the system.

Once the basic packages are installed and the system is booted, configure the btrfs subvolumes.

btrfs subvolume snapshot / /rootfs
mkdir snapshots
mkdir /rootfs/mnt/btrfs

Edit /root/fs/etc/fstab add option subvol=/rootfs to / line Add /mnt/btrfs subvolid=5

btrfs subvolume list -a /
btrfs subvolume set-default [ID] /

cd /mnt/btrfs
mv home blah
btrfs subvolume create home
mv blah/* home

in /etc/fstab add line for /home subvol=/home

One problem with this setup is that it would be difficult to reinstall the operating system without touching /home. It might work because the default subvolume would be the one installed to, and hopefully the installer wouldn't touch the /home subvolume. But it might be simpler to manage a separate partition for /home, especially since I don't forsee using btrfs to backup /home.

After reading I'm still not sure if reinstalling would wipe out subvolumes or snapshots. However I think the way I'd use btrfs is to have just a subvolume for root and take occasional snapshots to make it easy to rollback from failed upgrades.

It's sure frustrating to not be able to get a clear idea of how much disk space is used and remaining. Actually btrfs fi show seems pretty good at showing per disk usage.

To backup /rootfs

btrfs subvolume snapshot /mnt/rootfs /mnt/btrfs/snapshots/[name]

To rollback

cd /mnt/btrfs

I can't believe this worked. You are moving a live filesystem!

mv rootfs snapshots/crap
btrfs subvolume snapshot snapshots/backup rootfs
btrfs subvolume list -a / # check for id of new rootfs
btrfs subvolume set-default [ID] /

To add a disk

btrfs device add /dev/sdc /mnt
btrfs filesystem balance /mnt

also need this if you want raid

btrfs balance start -dconvert=raid1 -mconvert=raid1 /mnt

To remove a disk

mkfs.btrfs /dev/sdb /dev/sdc /dev/sdd /dev/sde
mount /dev/sdb /mnt

Put some data on the filesystem here

btrfs device delete /dev/sdc /mnt