<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><title>IT Notes - btrfs</title><link>https://it-notes.dragas.net/categories/btrfs/</link><description>Articles in category btrfs</description><atom:link href="https://it-notes.dragas.net/categories/btrfs/feed.xml" rel="self"/><language>en</language><lastBuildDate>Wed, 25 Oct 2023 16:39:37 +0000</lastBuildDate><atom:link href="https://it-notes.dragas.net/categories/btrfs/feed.xml" rel="self" type="application/rss+xml"/><item><title>Migrating from an Old Linux Server to a New FreeBSD Machine</title><link>https://it-notes.dragas.net/2023/10/25/migrating-from-an-old-linux-server-to-a-new-freebsd-machine/</link><description>&lt;p&gt;&lt;img src="https://it-notes.dragas.net/content/images/2023/10/3500d14b-cb7e-4de6-af9e-0ca135985b41.webp" alt="Migrating from an Old Linux Server to a New FreeBSD Machine"&gt;&lt;/p&gt;&lt;p&gt;&lt;em&gt;Preamble:&lt;/em&gt; I believe it's time to bid farewell to this venerable Linux server.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt;  &lt;em&gt;The article chronicles the journey of transitioning from an outdated Linux server, running for 1690 days without updates, to a modern FreeBSD machine. This migration involved using tools like mfsBSD, BastilleBSD, Borg Backup, and bhyve. Despite initial hesitations due to the Linux server's impeccable performance, the transition was smooth, resulting in improved manageability and efficiency. The piece emphasizes the importance of regular system updates and anticipates revisiting the topic in the future with new uptime achievements and updates.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;This server loyally served for years as a secondary backup server while also providing a few minor services to users. As it often happens, it remained in operation, neglected and without updates for years. Stable operating systems have the "flaw" of being forgotten, giving the false impression that they don't need maintenance or updates. This machine continued its service without oversight for years. When approached for a service request (not due to malfunctions), I advised the client to upgrade the whole system. A mere update would not suffice, so I suggested starting afresh on new hardware with FreeBSD as the primary OS.&lt;/p&gt;
&lt;p&gt;The client was understandably hesitant given the uptime stats:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;08:58:43 up 1690 days, 21:32, 4 users, load average: 9.57, 10.15, 8.76&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Not a single error, not a single hiccup. From his perspective, a similar setup to what was installed many years ago and still working flawlessly was preferred. Nevertheless, he trusted my expertise and let me proceed.&lt;/p&gt;
&lt;p&gt;This server had a plethora of duties, among which:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;One of the pivotal tasks was running &lt;a href="https://www.proxmox.com/en/proxmox-backup-server/overview"&gt;Proxmox Backup Server&lt;/a&gt; via Docker. Proxmox Backup Server requires Debian, but this server was running on Ubuntu 16.04 (previously upgraded from Ubuntu 14.04 – yes, ancient!). Hence, Proxmox Backup Server was still at version 1.x.&lt;/li&gt;
&lt;li&gt;Another critical function was storing backups made through &lt;a href="https://www.borgbackup.org/"&gt;BorgBackup&lt;/a&gt; on its file system. The /home directory used a mirrored btrfs file system, and each backed-up server had its user on this system. Clients could backup (using a push method) only via VPN and only during specific windows when the server permitted (by adding specific firewall rules via Jenkins. Jenkins also managed connection protocols, snapshots, backups, etc.).&lt;/li&gt;
&lt;li&gt;Among the lesser tasks, the server ran a few Docker containers with HandBrake on various presets. The client processed video conversions by uploading the original files via sftp, and after some hours, fetched the converted files from the destination directory. This will not be replicated on the new FreeBSD server since they now handle this operation locally on their high-performance MacBook Pro with Apple Silicon. However, a future restoration isn't off the table.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The first thing I did was install FreeBSD on the new hardware. Given that it's a physical server on Hetzner (an auction pick due to disk space needs over power), and FreeBSD wasn't an option, I used &lt;a href="https://mfsbsd.vx.sk/"&gt;mfsBSD&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;After booting the physical server in Linux rescue, I copied mfsBSD onto the disks using &lt;code&gt;dd&lt;/code&gt; and restarted. On boot, I SSHed into mfsBSD and executed the installation using &lt;code&gt;bsdinstall&lt;/code&gt;-a robust and efficient method.&lt;/p&gt;
&lt;p&gt;I set up a raidz1 with all four 6 TB disks, resulting in a final storage space of 21.8T, ample for now without the video files.&lt;/p&gt;
&lt;p&gt;To ensure continuity, I kept a setup similar to the old one. The clients would essentially continue with their usual backup procedure without necessitating drastic changes to backup scripts. To avoid storing these backups directly in the physical machine's /home and to leave the door open for future services, I installed &lt;a href="https://bastillebsd.org/"&gt;BastilleBSD&lt;/a&gt; and began setting up several jails. I replaced the old Linux machine's behavior with a VNET FreeBSD jail. In past scenarios, I've created Linux jails (thanks to BastilleBSD) and transferred the old server into the jail using rsync, making minor configuration tweaks. While this usually works, it doesn't address the underlying issue of an obsolete setup. Given the opportunity, I opted for a modern toolset.&lt;/p&gt;
&lt;p&gt;Thus, I copied every home directory (along with their historic backups) in its entirety, installed BorgBackup, and re-established the VPN. With a VNET jail, I can craft networking devices and fine-tune configurations. After recreating user accounts, inputting the various SSH &lt;code&gt;authorized_keys&lt;/code&gt;, and checking all clients, I set up a snapshot plan on the host. This ensures that if a client is compromised with the potential (however remote) for breach and backup deletion, a ZFS snapshot of the entire jail remains available.&lt;/p&gt;
&lt;p&gt;As mentioned, one of the core tools on the old server was Proxmox Backup Server. It's not natively installable on FreeBSD, necessitating a VM. Enter the fantastic &lt;code&gt;bhyve&lt;/code&gt;, supported by &lt;code&gt;vm-bhyve&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;However, one issue arose: backups would consume vast amounts of space, and I wanted to avoid housing an enormous disk image (or a zvol) with both VMs and backups. So, I opted for a slightly less performant but more flexible solution: installing Debian 12 and Proxmox Backup Server on the VM while placing backups on a separate ZFS dataset on the physical machine, exported via NFS and mounted on the VM.&lt;/p&gt;
&lt;p&gt;Given that the physical server has an internal bridge "vm-public" with IP &lt;code&gt;192.168.124.1&lt;/code&gt; and the VM is at &lt;code&gt;192.168.124.2&lt;/code&gt;, I just created a dataset named &lt;code&gt;zroot/PBS&lt;/code&gt; and added the following line to &lt;code&gt;/etc/exports&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;/zroot/PBS -alldirs -maproot=root -network 192.168.124.2/32&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;To enable NFS, insert into &lt;code&gt;/etc/rc.conf&lt;/code&gt;:&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;rpcbind_enable=&amp;quot;YES&amp;quot; 
nfs_server_enable=&amp;quot;YES&amp;quot; 
mountd_flags=&amp;quot;-r&amp;quot; 
rpc_lockd_enable=&amp;quot;YES&amp;quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Within the VM, create &lt;code&gt;/PBS&lt;/code&gt; and include in &lt;code&gt;/etc/fstab&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;192.168.124.1:/zroot/PBS /PBS nfs rw,async,soft,intr,noexec 0 0&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;After Proxmox Backup Server's installation, simply set up the datastores in &lt;code&gt;/PBS/&lt;/code&gt;, and they'll directly store on the physical machine's ZFS dataset.&lt;/p&gt;
&lt;p&gt;For firewall configurations, I exposed port 8007, redirecting it towards the VM, and everything started functioning smoothly. I then set a Proxmox Backup Server replica from the old to the new server. After completion, I changed the Proxmox Backup Server IP on all Proxmox hosts to point to the new server. Smooth sailing.&lt;/p&gt;
&lt;p&gt;The old Ubuntu server also managed other minor services, which have become obsolete and weren't replicated.&lt;/p&gt;
&lt;p&gt;The transition was seamless, the client is pleased, and I'm content since each service is now neatly segregated into its jail or VM. The machine's load is minimal, which might pave the way for other tasks, via VPN. Everything now rests on ZFS, and the icing on the cake: I made the client promise not to reach another 1690 days of uptime but to timely update as required.&lt;/p&gt;
&lt;p&gt;I'm not entirely convinced the promise will hold-meaning, in a few years, I might yet be discussing this "new" server, highlighting another impressive uptime and another upgrade journey.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Stefano Marinelli</dc:creator><pubDate>Wed, 25 Oct 2023 16:39:37 +0000</pubDate><guid isPermaLink="true">https://it-notes.dragas.net/2023/10/25/migrating-from-an-old-linux-server-to-a-new-freebsd-machine/</guid><category>freebsd</category><category>bhyve</category><category>borg</category><category>btrfs</category><category>container</category><category>data</category><category>docker</category><category>filesystems</category><category>jail</category><category>server</category><category>snapshots</category><category>virtualization</category><category>vpn</category><category>proxmox</category><category>backup</category><category>linux</category></item><item><title>Why we're migrating (many of) our servers from Linux to FreeBSD</title><link>https://it-notes.dragas.net/2022/01/24/why-were-migrating-many-of-our-servers-from-linux-to-freebsd/</link><description>&lt;p&gt;&lt;img src="https://it-notes.dragas.net/featured/checkmate.webp" alt="Why we&amp;#x27;re migrating (many of) our servers from Linux to FreeBSD"&gt;&lt;/p&gt;&lt;p&gt;&lt;em&gt;More about this &lt;a href="https://it-notes.dragas.net/2024/10/03/i-solve-problems-eurobsdcon/"&gt;in the article I wrote to accompany my talk at EuroBSDCon 2024&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Please note: This article was (unexpectedly) quite popular and many interesting opinions came out from the Hacker News thread that it generated. If you're interested, have a look here:&lt;/em&gt; &lt;a href="https://news.ycombinator.com/item?id=30057549"&gt;https://news.ycombinator.com/item?id=30057549&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;There's an &lt;a href="https://www.dragas.net/posts/perche-migrare-i-server-da-linux-a-freebsd/"&gt;Italian version of this article here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;I've been a Linux (or GNU/Linux, for the purists) user since 1996. I've been a FreeBSD user since 2002. I have always successfully used both operating systems, each for specific purposes. I have found, on average, BSD systems to be more stable than their Linux equivalents. By stability, I don't mean uptime (too much uptime means too few kernel security updates, which is wrong). I mean that things work as they should, that they don't "break" from one update to the next, and that you don't have to revise everything because of a missing or modified basic command.&lt;/p&gt;
&lt;p&gt;I've always been for development and innovation as long as it doesn't (necessarily, automatically and unreasonably) break everything that is already in place. And the road that the various Linux distributions are taking seems to be that of modifying things that work just for the sake of it or to follow the diktats of the Kernel and those who manage it - but not only.&lt;/p&gt;
&lt;p&gt;Some time ago we started a complex, continuous and not always linear operation, that is to migrate, where possible, most of the servers (ours and of our customers) from Linux to FreeBSD.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Why FreeBSD?&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;There are many alternative operating systems to Linux and the *BSD family is varied and complete. &lt;a href="https://www.freebsd.org/"&gt;FreeBSD&lt;/a&gt;, in my opinion, today is the "all rounder" system par excellence, i.e. well refined and suitable both for use on large servers and small embedded systems. The other BSDs have strengths that, in some fields, make them particularly suitable but FreeBSD, in my humble opinion, is suitable (almost) for every purpose.&lt;/p&gt;
&lt;p&gt;So back to the main topic of this article, why am I migrating many of the servers we manage to FreeBSD? The reasons are many, I will list some of them with corresponding explanations.&lt;/p&gt;
&lt;h2&gt;The system is consistent - kernel and userland are created and managed by the same team&lt;/h2&gt;
&lt;p&gt;One of the fundamental problems with Linux is that (we shall remember) it is a kernel, everything else is created by different people/companies. On more than one occasion Linus Torvalds as well as other leading Linux kernel developers have remarked that they care about the development of the kernel itself, not how users will use it. In the technical decisions, therefore, they don't take into account what is the real use of the systems but that the kernel will go its own path. This is a good thing, as the development of the Linux kernel is not "held back" by the struggle between distributions and software solutions, but at the same time it is also a disadvantage. In FreeBSD, the kernel and its userland (i.e. all the components of the base operating system) are developed by the same team and there is, therefore, a strong cohesion between the parties. In many Linux distributions it was necessary to "deprecate" ifconfig in favor of ip because new developments in the kernel were no longer supported by ifconfig, without breaking compatibility with other (previous) kernel versions or having functions (on the same network interface) managed by different tools. In FreeBSD, with each release of the operating system, there are both kernel and userland updates, so these changes are consistently incorporated and documented, making the tools compatible with their kernel-side updates.&lt;/p&gt;
&lt;p&gt;In other words, in FreeBSD there is no need to " revolutionise" everything every few years and changes are made primarily in the form of additions that can enrich (and not break) each update. If a modification was to change the way it interacts with network devices, ifconfig would be modified to take advantage of that and remain compatible with the "old" syntax. In the long-term, this kind of approach is definitely appreciated by system administrators who find themselves with a linear, consistent, and always well-documented update path.&lt;/p&gt;
&lt;h2&gt;FreeBSD development is (still) driven by technical interests, not strictly commercial ones.&lt;/h2&gt;
&lt;p&gt;Linux and related distributions now have contributions from many companies, many of which (e.g. Red Hat) push (justifiably) in the direction of what is convenient for them, their products, and their services. Being big contributors to the project they have a big clout so, indeed, their solutions often become de-facto standards. Consider systemd - was there really a need for such a system? While it brought some advantages, it added some complexity to an otherwise extremely simple and functional system. &lt;a href="https://www.howtogeek.com/675569/why-linuxs-systemd-is-still-divisive-after-all-these-years/"&gt;It remains divisive to this day&lt;/a&gt;, with many asking, "but was it really necessary? Did the advantages it brought balance the disadvantages?". 70 binaries just for initialising and logging and a million and a half lines of code just for that? But Red Hat threw the rock...and many followed along. Because sometimes it's nice to follow the trend, the hype of a specific solution.&lt;/p&gt;
&lt;p&gt;Even FreeBSD has big companies behind it, collaborating in a more or less direct way. The license is more permissive, so not everyone who uses it commercially contributes to it, but knowing that FreeBSD is &lt;a href="https://papers.freebsd.org/2019/fosdem/looney-netflix_and_freebsd/"&gt;at the base of Netflix CDNs&lt;/a&gt;, &lt;a href="https://news.ycombinator.com/item?id=22028689"&gt;Whatsapp servers&lt;/a&gt; (waiting for Meta to replace them, for internal coherence reasons, with Linux servers), &lt;a href="https://en.wikipedia.org/wiki/PlayStation_4_system_software"&gt;Sony Playstations&lt;/a&gt; and, in part, &lt;a href="https://developer.apple.com/library/archive/documentation/Darwin/Conceptual/KernelProgramming/BSD/BSD.html"&gt;macOS, iOS, iPadOS, etc.&lt;/a&gt; surely gives confidence on its level. These realities, however, do not have enough clout to drive the development of the core team.&lt;/p&gt;
&lt;h2&gt;Linux has Docker, Podman, lxc, lxd, etc. but... FreeBSD has jails!&lt;/h2&gt;
&lt;p&gt;&lt;a href="https://docs.freebsd.org/en/books/handbook/jails/"&gt;FreeBSD jails&lt;/a&gt; are very powerful tools for jailing and separating services. There is controversy about Docker not running on FreeBSD but I believe (like many others) that FreeBSD has a more powerful tool. Jails are older and more mature - and by far - than any containerization solution on Linux. Jails are efficient and are well integrated throughout the operating system. All major commands (ps, kill, top, etc.) are able to display jail information as well. There are many management tools but, in fact, they all do the same thing: they interact with FreeBSD base and create custom configuration files. Personally I'm very comfortable with &lt;a href="https://bastillebsd.org/"&gt;BastilleBSD&lt;/a&gt; but there are a lot of very good tools as well as a sufficiently simple manual management. When I need Docker I launch a Linux machine - often &lt;a href="https://www.alpinelinux.org/"&gt;Alpine&lt;/a&gt;, which I think is a great minimalist distribution, or &lt;a href="https://www.debian.org/"&gt;Debian&lt;/a&gt;. But I'm moving a lot of services from Docker to a dedicated jail on FreeBSD. &lt;a href="https://www.dragas.net/posts/docker-e-la-nuova-separazione-dei-servizi/"&gt;Docker containers&lt;/a&gt; are a great tool for rapid (and consistent) software deployment, but it's not all fun and games. Containers, for example, rely on images that sometimes age and are no longer updated. &lt;a href="https://www.infoq.com/news/2020/12/dockerhub-image-vulnerabilities/"&gt;This is a security issue&lt;/a&gt; that should not be overlooked.&lt;/p&gt;
&lt;h2&gt;Linux has ext4, xfs, btrfs... (and zfs, but with some manual intervention). FreeBSD has UFS2 and ZFS&lt;/h2&gt;
&lt;p&gt;UFS2 is still a very good and efficient file system and, when configured to use softupdates, &lt;a href="https://it-notes.dragas.net/2024/06/04/freebsd-tips-and-tricks-creating-snapshots-with-ufs/"&gt;capable of performing live snapshots of the file system&lt;/a&gt;. This is great for backups. Ext4 and XFS do not support snapshots &lt;a href="https://it-notes.dragas.net/2020/06/30/searching-for-a-perfect-backup-solution-borg-and-restic/"&gt;except through external tools&lt;/a&gt; (like DattoBD or snapshots through the volume manager). This works, of course, but it is not native. Btrfs is great in its intentions but still not as stable as it should be after all these years of development. FreeBSD supports ZFS natively in the base system and this brings many advantages: separation of datasets for jails, as well as &lt;a href="https://vermaden.files.wordpress.com/2018/11/nluug-zfs-boot-environments-reloaded-2018-11-15.pdf"&gt;Boot Environments&lt;/a&gt;, to make snapshots before upgrades/changes and to be able to boot (from bootloader) even on a different BE, etc.&lt;/p&gt;
&lt;h2&gt;The FreeBSD boot procedure is cleaner and simpler.&lt;/h2&gt;
&lt;p&gt;Linux has always used excellent tools such as grub, lilo (now outdated), etc.. FreeBSD has always used &lt;a href="https://docs.freebsd.org/en/books/handbook/boot/"&gt;a very linear and consistent boot system&lt;/a&gt;, with its own bootloader and dedicated boot partition. Whether on mbr, gpt, etc. things are very similar and consistent. I've never had a problem getting a FreeBSD system to boot after a move or recovery from backup. On Linux, however, grub has sometimes given me problems, even after a simple kernel security update.&lt;/p&gt;
&lt;h2&gt;FreeBSD's network stack is (still) superior to Linux's - and, often, so is its performance.&lt;/h2&gt;
&lt;p&gt;Meta has been &lt;a href="https://bsd.slashdot.org/story/14/08/06/1731218/facebook-seeks-devs-to-make-linux-network-stack-as-good-as-freebsds"&gt;trying to bring the performance of the Linux network stack up to the level of FreeBSD's&lt;/a&gt; for years. Many will ask why, then, not move services to FreeBSD. Large companies with huge datacenters can't change solutions overnight, and their engineers, at any level, are Linux experts. They have invested heavily in btrfs, in Linux, in their specifics. Clearly, upon acquiring Whatsapp, they preferred to migrate the "few" Whatsapp servers to Linux and move them to their datacenters. Regarding the real system performance (i.e. disregarding benchmarks, useful only up to a certain point), FreeBSD shines, especially under high load conditions. Where Linux starts to gasp (ex: waiting for I/O) with 100% CPU, FreeBSD has lower processor load and room for more stuff. In the real world (of my servers and load types), I sometimes experienced severe system slowdowns due to high I/O, even if the data to be processed was not read/write dependent. On FreeBSD this does not happen, and if something is blocking, it blocks THAT operation, not the rest of the system. When performing backups or other important operations this factor becomes extremely important to ensure proper (and stable) system performance.&lt;/p&gt;
&lt;h2&gt;Straightforward system performance analysis&lt;/h2&gt;
&lt;p&gt;FreeBSD, in the base system, has all the tools to analyze possible problems and system loads. "vmstat" , in a single line, tells me if the machine is struggling for CPU, for I/O or for Ram. "gstat -a" shows me how much, disk by disk, partition by partition, the storage is active, also in percentage with reference to its performance. "top", then, also has support for figuring out, process by process, how much I/O is being used ("m" option). On Linux, to get the same results, you have to install specific applications, different from distribution to distribution.&lt;/p&gt;
&lt;h2&gt;Bhyve is more incomplete (but more efficient) than KVM&lt;/h2&gt;
&lt;p&gt;For my purposes, Bhyve is a great virtualization tool. KVM is definitely more complete but since I don't have any special or specific needs not covered by Bhyve on FreeBSD, I found (on average) &lt;a href="https://it-notes.dragas.net/2024/06/10/proxmox-vs-freebsd-which-virtualization-host-performs-better/"&gt;better performance with this combination&lt;/a&gt;. On FreeBSD, however, &lt;a href="https://www.kernel.org/doc/html/latest/admin-guide/mm/ksm.html"&gt;KSM&lt;/a&gt; is missing which, in some cases, can be very useful.&lt;/p&gt;
&lt;p&gt;Will I abandon Linux for FreeBSD? Obviously not, just as I haven't for the last 20 years. Both have their uses, their space, their strengths. But if up to now I have had 80% Linux and 20% FreeBSD, &lt;a href="https://it-notes.dragas.net/2022/02/05/how-we-are-migrating-many-of-our-servers-from-linux-to-freebsd-part-1-system-and-jails-setup/"&gt;the perspective is to invert the percentages of use and, where possible, directly implement solutions based on FreeBSD&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;NOTE: this article has been translated from its &lt;a href="https://www.dragas.net/posts/perche-migrare-i-server-da-linux-a-freebsd/"&gt;Italian original version&lt;/a&gt;. Even if it's been reviewed and adapted, there might be some errors.&lt;/em&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Stefano Marinelli</dc:creator><pubDate>Mon, 24 Jan 2022 06:55:00 +0000</pubDate><guid isPermaLink="true">https://it-notes.dragas.net/2022/01/24/why-were-migrating-many-of-our-servers-from-linux-to-freebsd/</guid><category>freebsd</category><category>linux</category><category>filesystems</category><category>hosting</category><category>jail</category><category>lxc</category><category>server</category><category>snapshots</category><category>zfs</category><category>btrfs</category><category>container</category><category>alpine</category><category>ownyourdata</category><category>series</category></item><item><title>Alpine Linux and LXD perfect setup on BTRFS file system</title><link>https://it-notes.dragas.net/2021/11/03/alpine-linux-and-lxd-perfect-setup-part-1-btrfs-file-system/</link><description>&lt;p&gt;&lt;img src="https://images.unsplash.com/photo-1573420702668-a54f97910a10?crop=entropy&amp;cs=tinysrgb&amp;fit=max&amp;fm=jpg&amp;ixid=MnwxMTc3M3wwfDF8c2VhcmNofDl8fEFscGluZXxlbnwwfHx8fDE2MzU5MzUwNzE&amp;ixlib=rb-1.2.1&amp;q=80&amp;w=2000" alt="Alpine Linux and LXD perfect setup on BTRFS file system"&gt;&lt;/p&gt;&lt;p&gt;&lt;em&gt;Last update: 01 November 2022 - Alpine 3.16 has lxd packages in contrib&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Lxc is a great technology. I’ve been using it since 2012 and I’ve always appreciated its lightweight approach and efficiency. Nowadays KVM and hardware virtualization extensions have reached a very good level of performance. While a KVM VM allows to run another kernel and another OS, it ties specific resources to the VM and this isn’t always the best solution. Lxc uses the host’s kernel and shares its ram so the host machine runs lxc container’s processes as local processes - no slowdown, no resource wasting.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://www.alpinelinux.org"&gt;Alpine Linux&lt;/a&gt; is a great distribution. It’s minimal, reliable, secure and easy to deploy. It’s great for containers but it’s also great for low footprint servers, like hypervisors. It’s good as Xen Dom0, it’s good as KVM host and - no surprise - is great as Lxc host too.&lt;/p&gt;
&lt;p&gt;Alpine Linux is great for low energy devices (Raspberry Pis, etc.), too, and has an efficient and reliable out-of-the-box read-only file system support. But I’ll write more about it in another post.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://linuxcontainers.org/lxd/introduction/"&gt;Lxd&lt;/a&gt; is a great solution. It uses lxc but simplifies a lot of operations like new container creation, profile management, automatic snapshots, clustering, etc. The “official” best option to use lxd is installing Ubuntu, Snap and so on. I don’t want to use Ubuntu to install Snap. I don’t want to use Snap just to install lxd.  That’s why I’m choosing to use lxd with Alpine.&lt;/p&gt;
&lt;p&gt;Long story short: this video shows all the steps described below:&lt;/p&gt;
&lt;script id="asciicast-fPzfsudUiFBYVH6xALTouGZuk" src="https://asciinema.org/a/fPzfsudUiFBYVH6xALTouGZuk.js" async&gt;&lt;/script&gt;

&lt;h2&gt;Installing Alpine Linux on BTRFS Root File System&lt;/h2&gt;
&lt;p&gt;Alpine Linux installer supports btrfs as rootfs destination but the setup-alpine command doesn’t allow you to select it: it will partition your disk and create a ext4 standard root file system.&lt;/p&gt;
&lt;p&gt;So let’s start with the installation but, then, we’ll interrupt it at disk selection:&lt;/p&gt;
&lt;p&gt;Start with &lt;strong&gt;setup-alpine&lt;/strong&gt; and set network, hostname, password, etc. Stop at:&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;Available disks are:
sda (20.5 GB QEMU     QEMU HARDDISK   )
Which disk(s) would you like to use? (or '?' for help or 'none') [none]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Press ctrl+C and interrupt the script.&lt;/p&gt;
&lt;p&gt;Now we can install Alpine Linux on btrfs root system:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;test:~# export ROOTFS=btrfs&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;And, then:&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;test:~# setup-disk /dev/sda

 The following disk is selected:
 sda    (20.5 GB QEMU     QEMU HARDDISK   )
 How would you like to use it? ('sys', 'data', 'lvm' or '?' for help) [?] sys
 WARNING: The following disk(s) will be erased:
 sda    (20.5 GB QEMU     QEMU HARDDISK   )
 WARNING: Erase the above disk(s) and continue? (y/n) [n] y
 Creating file systems...
 btrfs-progs v5.10
 See http://btrfs.wiki.kernel.org for more information.

 Label:              (null)
 UUID:               6ace888a-2f36-41e8-86b1-3e4214b9e34e
 Node size:          16384
 Sector size:        4096
 Filesystem size:    15.17GiB
 Block group profiles:
 Data:             single            8.00MiB
 Metadata:         DUP             256.00MiB
System:           DUP               8.00MiB
 SSD detected:       no
 Incompat features:  extref, skinny-metadata
 Runtime features:
 Checksum:           crc32c
 Number of devices:  1
 Devices:
 ID        SIZE  PATH
 1    15.17GiB  /dev/sda3

 Installing system on /dev/sda3:
 /mnt/boot is device /dev/sda1
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Installation is complete. Now eject the installation media and reboot.&lt;/p&gt;
&lt;p&gt;After rebooting, let’s check that root file system is on btrfs:&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt; test:~# cat /etc/fstab
 UUID=aacbfd28-df47-4945-9c0c-279faef361a0  /   btrfs   rw,relatime,space_cache,subvolid=5,subvol=/ 0 1
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ok, it is. Generally speaking, I prefer to use compression on btrfs. compress=lzo is enough, so you can add it on your fstab.&lt;/p&gt;
&lt;p&gt;Now, let’s install lxd.&lt;/p&gt;
&lt;p&gt;Edit your /etc/apk/repositories and let it be like this:&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;http://dl-cdn.alpinelinux.org/alpine/v3.16/main
http://dl-cdn.alpinelinux.org/alpine/v3.16/community
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Let’s follow &lt;a href="https://wiki.alpinelinux.org/wiki/LXD"&gt;the tutorial I wrote on Alpine Linux wiki&lt;/a&gt; and let’s install lxc, lxd, lxcfs and dbus (not needed but recommended to avoid problems with some containers).&lt;/p&gt;
&lt;p&gt;After rebooting, you have a fully functional lxd-enabled Alpine Linux installation.&lt;/p&gt;
&lt;p&gt;Let’s now configure lxd:&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt; test:~# lxd init
 Would you like to use LXD clustering? (yes/no) [default=no]:
 Do you want to configure a new storage pool? (yes/no) [default=yes]:
 Name of the new storage pool [default=default]:
 Name of the storage backend to use (btrfs, dir) [default=btrfs]:
 Would you like to create a new btrfs subvolume under /var/lib/lxd? (yes/no) [default=yes]:
 Would you like to connect to a MAAS server? (yes/no) [default=no]:
 Would you like to create a new local network bridge? (yes/no) [default=yes]:
 What should the new bridge be called? [default=lxdbr0]:
 What IPv4 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]:
 What IPv6 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]:
 Would you like the LXD server to be available over the network? (yes/no) [default=no]:
 Would you like stale cached images to be updated automatically? (yes/no) [default=yes]
 Would you like a YAML &amp;quot;lxd init&amp;quot; preseed to be printed? (yes/no) [default=no]:
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As you can see, lxd automatically detected that /var/lib/lxd is on a btrfs file system and creates a sub volume.&lt;/p&gt;
&lt;p&gt;Now we can launch our first container:&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;test:~# lxc launch images:alpine/3.16 t1
Creating t1
Starting t1
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ok, done. Let’s check if it’s working:&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;test:~# lxc info t1
Name: t1
Status: RUNNING
Type: container
Architecture: x86_64
PID: 2648
Created: 2021/11/03 08:55 UTC
Last Used: 2021/11/03 08:55 UTC

Resources:
Processes: 5
CPU usage:
CPU usage (in seconds): 0
Memory usage:
Memory (current): 3.38MiB
Memory (peak): 5.04MiB
Network usage:
eth0:
Type: broadcast
State: UP
Host interface: vethdc207601
MAC address: 00:16:3e:50:d1:ef
MTU: 1500
Bytes received: 3.76kB
Bytes sent: 1.79kB
Packets received: 26
Packets sent: 16
IP addresses:
inet:  10.189.248.21/24 (global)
inet6: fd42:34cb:9408:433c:216:3eff:fe50:d1ef/64 (global)
inet6: fe80::216:3eff:fe50:d1ef/64 (link)
lo:
Type: loopback
State: UP
MTU: 65536
Bytes received: 0B
Bytes sent: 0B
Packets received: 0
Packets sent: 0
IP addresses:
inet:  127.0.0.1/8 (local)
inet6: ::1/128 (local)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It’s running. Everything is ok. For more complex lxc setup (bridges, profiles, etc.) you can follow the excellent lxd documentation.&lt;/p&gt;
&lt;p&gt;This kind of setup is easy and lightweight, easy to backup (a new article will follow) and reliable.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Stefano Marinelli</dc:creator><pubDate>Wed, 03 Nov 2021 10:54:10 +0000</pubDate><guid isPermaLink="true">https://it-notes.dragas.net/2021/11/03/alpine-linux-and-lxd-perfect-setup-part-1-btrfs-file-system/</guid><category>alpine</category><category>btrfs</category><category>container</category><category>server</category><category>tutorial</category><category>linux</category><category>filesystems</category><category>hosting</category><category>lxc</category></item><item><title>BTRFS: performing automatic snapshots and remote backups</title><link>https://it-notes.dragas.net/2020/06/28/btrfs-automatic-snapshots-and-remote-backups/</link><description>&lt;p&gt;&lt;img src="https://images.unsplash.com/photo-1589985270826-4b7bb135bc9d?ixlib=rb-1.2.1&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=2000&amp;fit=max&amp;ixid=eyJhcHBfaWQiOjExNzczfQ" alt="BTRFS: performing automatic snapshots and remote backups"&gt;&lt;/p&gt;&lt;p&gt;I'm using BTRFS with satisfaction (I mentioned it in 2014 and, &lt;a href="https://it-notes.dragas.net/2018/10/13/btrfs-best-pratices/"&gt;more recently, in the IT-Notes blog&lt;/a&gt;). I don't consider it optimal for all types of loads, but it solves many problems in many situations. One of the things I use with more satisfaction is the dynamic &lt;em&gt;snapshot&lt;/em&gt; generation function, which guarantees the possibility to have a perfect and immediate copy of a specific volume (or subvolume). When a backup is needed, for example, the use of snapshots is fundamental and having them at file system level is undoubtedly a good help.&lt;/p&gt;
&lt;p&gt;When it comes to BTRFS volumes, however, we have additional options. There are native tools that are able to send and receive data to and from BTRFS volumes in an optimized way, taking advantage of the inherent features of the file system itself. Here is a method to automatically take snapshots and transfer them to another BTRFS volume (local or remote).&lt;/p&gt;
&lt;p&gt;I've been using two interesting scripts for some time now. In the past I used &lt;a href="http://snapper.io/"&gt;snapper&lt;/a&gt; but I didn't find it optimal for my use. I have therefore continued to search and found some valid alternatives, namely the combination of &lt;a href="https://github.com/nachoparker/btrfs-snp"&gt;btrfs-snp&lt;/a&gt; and &lt;a href="https://github.com/nachoparker/btrfs-sync"&gt;btrfs-sync&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;btrfs-snp&lt;/h2&gt;
&lt;p&gt;btrfs-snp allows to be launched specifying a path, a prefix linked to the snapshot taken, the maximum number of snapshots with that prefix to hold and a minimum time between snapshots.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;root@foobar:~# /usr/local/sbin/btrfs-snp / hourly 10 3600&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;It will take a snapshot of /, with the term &lt;em&gt;hourly&lt;/em&gt; as prefix (but it can be any prefix), keep a maximum of 10 snapshots and will not take any more (i.e. the program will exit without action) in less than 3600 seconds from the previous one.&lt;/p&gt;
&lt;p&gt;You can of course combine them in this way, to have more snapshots of different types:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;/usr/local/sbin/btrfs-snp / hourly 10 3600/usr/local/sbin/btrfs-snp / daily 3 86400/usr/local/sbin/btrfs-snp / weekly 1 604800&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;It can be launched using cron every hour (or every quarter as it will not take a new snapshot if less than 3600 seconds from the previous one have passed).&lt;/p&gt;
&lt;h2&gt;btrfs-sync&lt;/h2&gt;
&lt;p&gt;At this point, we can copy all the snapshots to another file system (local or remote) thanks to btrfs-sync.&lt;/p&gt;
&lt;p&gt;The command can be of this type:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;root@foobar:~# /usr/local/sbin/btrfs-sync -d -v /.snapshots btrfs@host:/backups/host01/&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;(it's a one line command)&lt;/p&gt;
&lt;p&gt;Obviously the destination file system must be a BTRFS volume.&lt;/p&gt;
&lt;p&gt;I usually run the command through a script in the cron.daily, to get a daily copy of the snapshots out.&lt;/p&gt;
&lt;p&gt;This kind of setup has already got me out of trouble several times and seems to be quite efficient.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Stefano Marinelli</dc:creator><pubDate>Sun, 28 Jun 2020 12:00:00 +0000</pubDate><guid isPermaLink="true">https://it-notes.dragas.net/2020/06/28/btrfs-automatic-snapshots-and-remote-backups/</guid><category>backup</category><category>btrfs</category><category>filesystems</category><category>linux</category><category>server</category></item><item><title>BTRFS Best Practices</title><link>https://it-notes.dragas.net/2018/10/13/btrfs-best-pratices/</link><description>&lt;p&gt;&lt;img src="https://images.unsplash.com/photo-1528396590868-b89fcd81a8fa?ixlib=rb-0.3.5&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=1080&amp;fit=max&amp;ixid=eyJhcHBfaWQiOjExNzczfQ&amp;s=d367a97a06e762df9a8b45a91301a4fa" alt="BTRFS Best Practices"&gt;&lt;/p&gt;&lt;p&gt;I've been using BTRFS for years and have encountered few problems. Some of them are small and can be avoided with some smart improvements. The following advice is based on my experience.&lt;/p&gt;
&lt;p&gt;You can find posts online arguing against these options, so your mileage may vary.&lt;/p&gt;
&lt;h2&gt;Fragmentation, speed and compression&lt;/h2&gt;
&lt;p&gt;If using rotating disks, employ the &lt;em&gt;space_cache&lt;/em&gt; and &lt;em&gt;autodefrag&lt;/em&gt; options. The latter can slow down writes but will keep the disk in a usable state for a long time. Otherwise, fragmentation may become a problem.&lt;/p&gt;
&lt;p&gt;Generally speaking, I use compression. Modern CPUs are much faster than rotating disks, so compressing data before writing saves space and often improves speed.&lt;/p&gt;
&lt;h2&gt;Filesystem stats and status&lt;/h2&gt;
&lt;p&gt;Periodically, check the file system status with the command "&lt;em&gt;btrfs fi df [mountpoint]&lt;/em&gt;." Here's an example:&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;Data, RAID1: total=1.42TiB, used=1.40TiB
System, RAID1: total=32.00MiB, used=256.00KiB
Metadata, RAID1: total=12.00GiB, used=4.68GiB
GlobalReserve, single: total=512.00MiB, used=0.00B
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The numbers are quite similar (except for metadata), so no action is needed. Sometimes they're different, and you might encounter a strange situation: you see a lot of free space, but you can't write anymore as the disk appears to be full.&lt;/p&gt;
&lt;p&gt;This can happen when the total (allocated) reaches the disk's space, but the used amount is lower. You have space, but you can't use it. A balance will fix this issue.&lt;/p&gt;
&lt;h2&gt;Perform some manual operations&lt;/h2&gt;
&lt;p&gt;Ideally, professional file systems should auto-heal and auto-clean. For example, ZFS autoscrubs data periodically. BTRFS still lacks some of these automatic operations, so it's recommended to perform some maintenance. I run them via &lt;em&gt;cron&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;My daily job looks like this:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;/bin/btrfs fi ba start  -dusage=50 -dlimit=2 -musage=50 -mlimit=4 mountpoint&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This operation checks and rebalances only a subset of blocks that meet specific conditions. This ensures that the operation only lasts a few minutes. Scheduling it daily will, over time, result in a clean situation.&lt;/p&gt;
&lt;p&gt;My weekly job looks like this:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;/bin/btrfs scrub start -B -d -c 2 -n 4 mountpoint&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This command performs a &lt;em&gt;scrub&lt;/em&gt;, checking blocks and detecting (and, if possible, correcting) errors. It's a longer operation, so I don't perform it daily.&lt;/p&gt;
&lt;p&gt;I've been using BTRFS since 2012, and thanks to these operations, it still rocks.Its capabilities have helped me in many situations.&lt;/p&gt;
&lt;p&gt;I also use BTRFS for my Linux desktop PCs to have snapshots and consistent backups.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Stefano Marinelli</dc:creator><pubDate>Sat, 13 Oct 2018 05:30:00 +0000</pubDate><guid isPermaLink="true">https://it-notes.dragas.net/2018/10/13/btrfs-best-pratices/</guid><category>linux</category><category>filesystems</category><category>btrfs</category><category>zfs</category></item></channel></rss>