0

When Creating a Full Install USB drive using a computer with a swap partition, the installer Ubiquity, formats every swap partition on the computer and adds their paths to fstab. This includes the existing swap partition(s) on the computer and the swap partition(s) created by the install process.

If there is an existing swap partition(s) on the computer, Ubiquity does not create a new swap file for the USB drive. If the USB drive is then booted on a computer that does not have a swap partition, there is no available swap space.

What should I do? Should I create a new swap file and add it to fstab? If so should I just remove any existing swap partitions from fstab or should I keep them? Is there a security concern, Can the next person using the computer copy my swap data, bank accounts, etc?

C.S.Cameron
  • 19,519
  • I have swap on a HDD and just click on it and say do not use. I did forget in one case and deleted entry in fstab and created a swap file which was multiple steps. Whole lot easier just to say do not use during install. That works where saying do not use on internal ESP does not work. – oldfred Jun 09 '20 at 02:48
  • @oldfred: Even a Live, Persistent or Full install USB built on a computer that does not have an existing swap partition, will show swap space using the 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 then Edit 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
  • @oldfred: cont'd: In my tests adding a swapfile excluded the use of any swap partitions unless they were specifically turned on in fstab. I would like the ability to hibernate to my USB device and not to a host computer. – C.S.Cameron Jun 09 '20 at 06:01
  • Have not checked lately but live installer typically mounted swap, so had to swapoff, but gparted live did not mount swap automatically. So often suggested using live version of gparted to avoid issues of mounted swap. – oldfred Jun 09 '20 at 14:32
  • @oldfred: I agree, Parted live seems to work better than the version that comes with Ubuntu. I always keep an up to date version of the ISO around. – C.S.Cameron Jun 10 '20 at 10:07

1 Answers1

0

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

  • Reboot:

    sudo reboot

To enable Hibernation:

  • Increase swap file size to match RAM size up to 8GB per above as required.

  • Edit /etc/default/grub to add resume location and offset to grub.cfg:

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash resume=UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX resume_offset=XXXXX"

  • Use UUID from root.

  • Use offset from sudo filefrag -v /swapfile

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.

C.S.Cameron
  • 19,519