You are not logged in.

#1 2024-05-15 17:41:09

Wild Penguin
Member
Registered: 2015-03-19
Posts: 347

Create swap file on boot and delete on shutdown

Hi,

This is perhaps a stupid use case smile.

But I'm considering installing Arch on a shared computer (I'm one of the administrators of). It doesn't have too much RAM, not too much disk space and I may be forced to install Arch on a mechanical HDD. It already has Windows on it. I'd really not like to shrink the Windows partition on an SSD, as while it does have some free space, not too much of it, and that may change in the future.

Now, at least manually I could create a swap file and swapon it, then swapoff and delete. But I'm certain this could be automated with systemd!

Now, what should I take into consideration (we can assume the NTFS partition resides on a local drive, which will be listed in fstab). Do I need some After or Requires line, or would the unit always be run after the NTFS partition has been mounted - and run before the ntfs partition will be unmounted, even without any such option?

Is there a ready-made service (or script) somewhere for this? (by a quick search I could not find any).

The script would (optimally) check the free space on the partition and not create a swap file larger than the free space, or exit (fail as a systemd service?) in case the partition can not be mounted at all (dirty flag, not found etc...) or if the free space is almost used up.

Offline

#2 2024-05-15 18:49:19

rep_movsd
Member
Registered: 2013-08-24
Posts: 142

Re: Create swap file on boot and delete on shutdown

You can certainly setup a systemd service to create a swap file on your NTFS partition and swap on that

Just make a service file for example: in /etc/systemd/system/swapper.service

[Unit]
Description=My Startup Script
After=network.target

[Service]
Type=simple
ExecStart=/path/to/your/swapper_script.sh

[Install]
WantedBy=multi-user.target

This runs after everything else is done, so your NTFS is mounted by that time.

A similar service and script to delete the file during shutdown should be easy enough.
If you are new to this - only make your script print the commands you want to the system log with the `logger` command rather than execute them.
Once you see the messages correctly in the journalctl logs, you can edit the scripts to actually do the stuff you need, and then test them once manually before rebooting     


BTW - Back in 1999 with a 540 MB HDD, I had a similar situation with Windows 98 - I solved this with a very dangerous hack
I made a partition table entry that pointed INSIDE the Win98 PAGEFILE.SYS swapfile - My Linux was configured to run mkswap on that "partition" on boot.

I would not recommend any such hack unless you were 16

Offline

#3 2024-05-15 20:28:05

cloverskull
Member
Registered: 2018-09-30
Posts: 206

Re: Create swap file on boot and delete on shutdown

A possible alternative here would be to encrypt swap at boot. After shutting down or rebooting, all data in swap will be unreadable.

Offline

#4 2024-05-15 20:32:31

Wild Penguin
Member
Registered: 2015-03-19
Posts: 347

Re: Create swap file on boot and delete on shutdown

Thanks rep_movsd!

The only thing (I can think of) is the mounting of file systems. But I suspect per default all services are started (and stopped) so that all file systems are mounted before they start (and umounted after they have stopped). Now that I think of it, I've red this has something to do with DefaultDependencies key. By just not setting it to no, the filesystems should be mounted.

Something like this should work:

[Service]
Type=Simple
ExecStart=/path/to/teh/script.sh start
ExecStop=/path/to/teh/script.sh stop      

I'm already using this for another task. Just parse the parameter in the script, one can determine what is expected.

As to the exercise of mounted filesystem: I'm thinking of parsing output of 'df /path/to/windows/ntfs/mountpoint' to decide if the swapfile should be created or not. I'm not sure how to signal to systemd that the unit has "failed", but OTOH I might not want to (I suppose it depends on how important the service is in the eyes of the admin etc.).

Offline

#5 2024-05-15 20:39:17

Wild Penguin
Member
Registered: 2015-03-19
Posts: 347

Re: Create swap file on boot and delete on shutdown

cloverskull wrote:

A possible alternative here would be to encrypt swap at boot. After shutting down or rebooting, all data in swap will be unreadable.

In this case, the reason to remove the file is for space reasons. The swapfile will (or should) not contain anything sensitive, as it is a shared computer, after all.

However, that doesn't mean someone else reading this might not find encrypt the swapfile useful (despite removing it; after all, by removing the file, there is no guarantee some data might still be recoverable from the SSD?).

This question in stackexchange seems to outline how to make a encrypted swap file (with a one-off, i.e. random key from
urandom).

EDIT: Would there be any security implications on creating the swap file with truncate instead of writing it full of random bytes before creating the encrypted node and the actual swap file?

Last edited by Wild Penguin (2024-05-15 20:42:44)

Offline

Board footer

Powered by FluxBB