1

In a Debian system, how can one move all ZRAM swap device contents to a swap file within the same running session in order to free up RAM, without:

  1. Disabling ZRAM first (e.g., using swapoff), as it risks spilling ZRAM data back into the RAM.
  2. Lowering the priority of ZRAM swap devices using the swapoff and swapon sequence.
  3. Using dd or similar methods that would copy the content but not preserve the correct context for the data in the swap file, rendering the data useless.

The solution should ensure that the data from the ZRAM swap device is correctly transferred to the swap file and recognized by the system as valid swapped data, effectively freeing up RAM space without risking data loss or moving data back into RAM.

Is there a reliable method to achieve this goal, given the constraints mentioned above, without requiring a reboot or causing a significant impact on system performance?

e.o.
  • 11

1 Answers1

0

zram is a generic, compressed block device in RAM.

It has no relation to swap at all. There is (nearly) no swap specific code to it. It just happens to be useful for swap under the right circumstances.

That means there is no zram specific migration path to another swap partition or a swap file. The swap system itself also does not offer 1:1 swap device migrations.

You are supposed to use swapoff. The kernel will then use other swap devices if and when required, thereby indirectly migrating it. Alternatively, you may add another swap device with a higher priority and wait for your lower priority swap devices to be tuned out over time.


That said, zram has an optional writeback feature, see zram: Optional Feature — writeback. You can use it to (temporarily) free up RAM. However, all requests would still go through the zram device and newly written pages would probably also end up in RAM first.

Support for this feature is not widespread yet (might not be enabled in kernel, zramctl doesn't support it, etc.). You might have to activate it manually through the sysfs interface. You can try it and see if it suits your needs.


Since zram is a generic block device, you can apply generic block device migration methods on it. This would allow you to remove the zram device altogether without swapoff.

For example, you could create RAID 1 and use mdadm --replace to move it from zram to a physical partition. (mdadm --force allows creating single disk RAID 1, so no mirroring is taking place while not migrating.)

For example, you could put LVM on it and use pvmove to move it from zram to a physical volume.

However, it's hard to say whether this would have any benefit over just using swapoff. Maybe setting swappiness to a higher value temporarily would affect swapoff behavior as well.

(Just throwing ideas around — I never tested any of it.)

frostschutz
  • 48,978
  • The system can't recognize the new device's data as valid swapped data. – e.o. Apr 14 '23 at 19:56
  • @e.o. not sure what you mean... this is something you set up beforehand. your swap device then is a md device or dm-device. you're not changing the swap device but the backing device of the swap device. it can be backed by zram or a partition or something else. if you're adding zram as swap directly then you can't change it anymore. – frostschutz Apr 15 '23 at 23:39