Bootable USB and Swap Space
A computer can have multiple swap partitions and a fresh install of Ubuntu will use them all, even if they are not listed in fstab, In addition the computer can use a single swap file if it is listed in fstab.
Even Live and Persistent bootable USB devices will use all the swap partition(s) they find.
Ubuntu 20.04 will create a swap file during installation as long as there are no swap partitions on the computer. (This is another reason to unplug the internal HDD/SSD when installing).
Once the swap file has been added to fstab the computer will not use any swap partition that are not listed in fstab.
Security:
A flash drive can be easily lost or misplaced. If you use a pendrive when dealing with sensitive data, the drive should be fully encrypted.
A swap partition can be copied or cloned just like any other file or partition.
A swap file is part of the root partition and automatically gets encrypted along with it.
It is generally not a good idea to encrypt the swap partition on someone else's computer, when using a bootable USB.
To prevent any data from being left in the host computer's swap partitions, a swap file should be used or swap should at least be turned off before exiting: sudo swapoff -a
.
To replace a swap partition with a swap file on a bootable USB:
Check the swap that is in use:
sudo swapon -s
If swap partition(s) found:
sudo swapoff -a
sudo nano -Bw /etc/fstab
Add "# " before the UUID of the swap partition(s):
# UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX none swap sw 0 0
Add a line for the swapfile:
swapfile none swap sw 0 0
Create the swap file:
sudo fallocate -l XG /swapfile
Where X is the swapfile size in GB
sudo mkswap /swapfile
sudo chmod 0600 /swapfile
sudo swapon /swapfile
To enable Hibernation:
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash resume=UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX resume_offset=XXXXX"
cscameron@cscameron-T:~$ filefrag -v /swapfile Filesystem type is: ef53 File size of /swapfile is 4819255296 (1176576 blocks of 4096 bytes) ext: logical_offset: physical_offset: length: expected: flags: 0: 0.. 0: 303104.. 303104: 1: 1: 1.. 2047: 303105.. 305151: 2047: unwritten 2: 2048.. 4095: 311296.. 313343: 2048: 305152: unwritten
resume_offset=303104
Update GRUB
sudo update-grub
Test hibernation
sudo systemctl hibernate
A hibernate button can be added using gnome extensions.
NOTES:
There is a slight possibility of getting holes in a swapfile when creating it with fallocate. /var/log/syslog can be searched for the phrase swapon: swapfile has holes
to ensure there will be no data loss.
For the best chance of successful resume, plug the USB into the same slot it was in while hibernating. It should be okay to run the host machine from it's own OS while the USB is in hibernation.
free
command, when plugged into a computer that has a swap partition, (and no swapfile). A specific swap partition can be excluded from future boots by selecting it in Disks and thenEdit Mount Options
, in which case something like:/dev/disk/by-uuid/7a5013b7-9e87-45d1-b6a1-e37afde9955d none swap sw,noauto 0 0
, is automatically added to fstab. – C.S.Cameron Jun 09 '20 at 06:01