Create swap from File on BTRFS Filesystem
mkswap and swapon
I didn’t create any swap during initial Ubuntu 19.04 install on Dell XPS laptop, so had to improvise when compiling a massive opensource project recently. This post shows you how to temporarily add swap memory using a regular file.
Create swap Using Regular File
The usual approach is fairly simple:
- you create a large enough file (the desired size of your swap memory) – 1GB or 4GB or something like that
- you initialise it as swap
- you activated it as swap
Swap Files Support in BTRFS
Since I’ve used BTRFS filesystem for root in my Ubuntu setup, I discovered that there are additional steps needed: BTRFS filesystem creates and writes files in a way that’s been mostly incompatible with swap usage.
But as luck would have it, BTRFS supports swap files with Linux Kernel 5.0.x, so you just need to create file with specific attribute.
Swap Space Using File Procedure
Step 1: Create new empty file
We need to create a file with NOCOW (NO Copy-On-Write) property (no harm anywhere but is a requirement for BTRFS):
Step 2: Allocate required amount of space to the file
This file is 8GB now:
Step 3: Update permissions
Step 4: Initialise /swapfile as swap storage
Step 5: Activate swap space from /swapfile
Step 6: Confirm newly activated swap space
That’s it for today!
See Also
- Basic Unix commands
- Advanced Unix commands
- Swap Memory
mkswap and swapon
I didn’t create any swap during initial Ubuntu 19.04 install on Dell XPS laptop, so had to improvise when compiling a massive opensource project recently. This post shows you how to temporarily add swap memory using a regular file.
Create swap Using Regular File
The usual approach is fairly simple:
- you create a large enough file (the desired size of your swap memory) – 1GB or 4GB or something like that
- you initialise it as swap
- you activated it as swap
Swap Files Support in BTRFS
Since I’ve used BTRFS filesystem for root in my Ubuntu setup, I discovered that there are additional steps needed: BTRFS filesystem creates and writes files in a way that’s been mostly incompatible with swap usage.
But as luck would have it, BTRFS supports swap files with Linux Kernel 5.0.x, so you just need to create file with specific attribute.
Swap Space Using File Procedure
Step 1: Create new empty file
We need to create a file with NOCOW (NO Copy-On-Write) property (no harm anywhere but is a requirement for BTRFS):
Step 2: Allocate required amount of space to the file
This file is 8GB now:
Step 3: Update permissions
Step 4: Initialise /swapfile as swap storage
Step 5: Activate swap space from /swapfile
Step 6: Confirm newly activated swap space
That’s it for today!
See Also
- Basic Unix commands
- Advanced Unix commands
- Swap Memory