Restoring pristine installation

Is there any way to restore FreeBSD to a pristine installation state? ie to remove everything that was added after the base system was installed...
Just remember to first backup anything you need in /usr/local/.

pkg prime-origins > origins.txt
pkg delete -af
rm -rf /usr/local/*
rm -rf /var/db/pkg/*
rm -rf /var/db/ports/*
rm -rf /usr/ports/distfiles/*
rm -rf /usr/obj/*
mtree -deU -f /usr/ports/Templates/BSD.local.dist -p /usr/local
pkg install `cat origins.txt`

Also, do that while actually logged as root because after certain point you will not have more sudo/doas installed. :eek:
Odd that this isn't in the manual pages (prime-origins). I always rely on pkg info -qo, I think that's less typing too ;)

`prime-origins` is an alias to `query -e '%a = 0' '%o'`

I use prime-list often and one day I blind tried origins to see if I could get the list with the categories to avoid installing ports-mgmt/portmaster just for that. I wasn't aware of it not being present in the manual. :)


That process is quite fast (bar uninstalling and installing the packages), and you can keep a list in there of everything you have installed to quick reinstall later, not to say sysctl.conf and friends. Very practical IMHO. :)
Would it be faster to perform a new install?

What would be faster if if you would've taken a snapshot of your root filesystem and descendants as, for example, zfs snapshot -r freebsd@11.1-RELEASE at which point you could simply rollback to that snapshot within maybe three seconds, reboot for good measure, and you'd be on a fresh installation.

It wouldn't be faster, but there's some stuff that gets installed in /boot (like fuse), in /etc, and other places on the filesystem besides in /usr/local, though if you did pkg remove -a, that would remove all the software you installed except maybe a few files here and there, configuration, mostly, and some empty directories or directories with files the installation script doesn't know what it is...

As for going back to the original, if you don't have a ZFS snapshot, I'm afraid you should boot from DVD, and use the configuration wizard to install 11.1-REL on a ZFS filesystem, and configure it for it to boot from that for you, then take your snapshot when you first reboot and update.

I hope I'm making sense, I'm very tired...
In answer to my own question... I just come across sysutils/clone... So what I intend to do next time I install FreeBSD, is to immediately run clone to copy the installation to a different location, and restore from it when I need a pristine build.