FreeBSD Tips and Tricks: Creating Snapshots With UFS

One of the main features that every modern file system should support, in my opinion, is the ability to take snapshots. Snapshots can be useful for many reasons, such as making changes or updates with the ability to revert at any time.

On Linux, file systems operating in COW (like ZFS, btrfs, etc.) can handle snapshots efficiently, but traditional file systems like ext4 lack this support. The only way is to add another layer (like LVM) and take a snapshot of the volume, mounting it elsewhere in read-only mode.

FreeBSD provides tools for creating snapshots using both ZFS and UFS. I’ll focus on UFS. In the base system, there’s a very useful and well-documented command: mksnap_ffs.

Long story short: this video shows all the steps described below:

mksnap_ffs will create a snapshot of the current state of the specified file system and store it in the file you specify. For example, running mksnap_ffs /snap will take a snapshot of the “/” file system and store it in the file “/snap”. By creating a memory disk and mapping the /snap file to an md, you can then mount it in read-only mode and retrieve the information. Finally, you can unmount, remove the md (mdconfig -du /dev/mdX), and delete the snapshot with a simple rm (rm /snap). Note that each file system can have up to 20 such snapshots, so it shouldn’t be considered on par with ZFS. However, it can be crucial for making a consistent backup of a live system with tools like Borg, Restic, Kopia, etc., or a “simple” rsync.

Steps to Create and Use Snapshots with UFS

  1. Create the Snapshot

    mksnap_ffs /snap

    This command takes a snapshot of the “/” file system and stores it in the file “/snap”.

  2. Create a Memory Disk

    mdconfig /snap

    This command maps the snapshot file to a memory disk.

  3. Mount the Snapshot

    mount -o ro /dev/md0 /mnt

    This command mounts the memory disk in read-only mode to /mnt.

  4. Access the Snapshot

    You can now access the snapshot via /mnt.

  5. Cleanup

    umount /mnt
    mdconfig -du 0
    rm /snap

    These commands unmount the snapshot, remove the memory disk, and delete the snapshot file.

By leveraging this built-in feature of FreeBSD, you can ensure your systems are more resilient and easier to manage, particularly for backups and system updates.

Related Content