[deepin exploration] [Btrfs]
swap, zram, zswap, swap space and memory compression
Technology Exchange 910 views · 0 replies ·
yanjuner
Super Moderator
2024-04-19 13:40
Author
Let’s talk about the conclusion first:
zram : It is just memory compression technology. Data cannot be saved when shutting down. If you do not want to use the hibernation function, you do not need to set up any swap partition. Set zram directly. Judging from the compression ratio of zstd 1:4, you set a memory ZRAM (16G) with twice the space (8G) actually takes up 50% of the physical memory (that is, 4G) when it is full, so you can think that you have 4+16=20G of memory to use. Swap : This thing is called swap space. It generally has two forms, swap partition and swap file. If you only use it to open large files, it may be written to the hard disk swap space. It can also be used to hibernate and save work progress when power is turned off. zswap : zswap is a kernel function that provides a compressed memory cache for swap pages. Pages that would otherwise be swapped to disk are compressed and stored in an in-memory storage pool. The difference compared with zram is that zswap works together with the swap device, while zram is an in-memory swap device and does not require a backup swap device. If you want to hibernate, you need swap. You can do this by adjusting the exchange rate. Don't always write data to swap. Memory compression solves the need for normal boot use. The data only needs to be saved to the hard disk during hibernation. It is simply a problem. Perfect solution.
My ultimate solution is to use a combination of btrfs+swapfile+zswap.
Note: zswap and zram cannot coexist. Be sure to turn off zswap when using zram.
1. Enable btrfs swapfile:
Starting from kernel version 5.0, it is supported to use swap files in btrfs partitions as swap form.
From version btrfs-progs 6.0 onwards, swap files can be quickly created through its own disk utility command.
Systemd version 255 only supports hibernation and swap files under btrfs, which can systemctl --versionbe viewed, so the uos professional version is 241 and cannot be used.
For people who don’t have too many requirements for hard disk use, you can actually use the swap partition, because even the swap file takes up a fixed space, but it has a little flexibility and can adjust the file size later. Therefore, using swap partition can adapt to more scenarios.
Otherwise, follow the previous method to create: https://bbs.deepin.org/post/270299
You can query the btrfs version through btrfs version. Currently deepin23beta3 is v6.3.2
btrfs version
btrfs-progs v6.3.2
It should be noted here that the btrfs disk tool has some limitations when creating and using swap files:
filesystem - can only be created in a single hardware partition
filesystem - must have only a single data configuration file
subvolume - snapshots cannot be created if it contains any active swap file
swapfile - must be pre-allocated (i.e. no holes)
swapfile - must be NODATACOW (i.e. also NODATASUM , no compression)
Create a swap file:
This process can be configured at system startup without entering the live environment. @swap is an independent subvolume and cannot be used for snapshots.
Mount the parent volume. sudo mount /dev/sda2 /mnt
Create a @swap subvolume. sudo btrfs subvolume create /mnt/@swap
Create a swap file sudo btrfs filesystem mkswapfile --size 8G /mnt/@swap/swapfile 输出:create swapfile /mnt/@swap/swapfile size 8.00GiB (8589934592)
. Enable the swap file: sudo swapon /mnt/@swap/swapfile
after activation, The file will appear in /proc/swaps:
$ cat /proc/swaps
Filename Type Size Used Priority
/mnt/@swap/swapfile file 8388604 0 -2
The previous setting is to temporarily enable the swap file, which can be permanently enabled through fstab
. Edit: sudo nano /etc/fstab Add the following content and replace the uuid with yours.
Enable hibernation to the swap file:
After enabling the swap file, the swap file can be used for hibernation, but this is not simple. Even if you see a sleep button on the deepin restart interface, an error will still occur and you cannot sleep normally. You need to further set up
hibernation here. Previously, the recovery offset had to be written to the file. /sys/power/resume_offset The value was a physical offset on the device.
Btrfs file system used a mapping between logical and physical addresses, but here, physical can still be mapped to one or more specific devices. Physical block address. This is a physical offset for a specific device and is suitable as a recovery offset.
Starting from btrfs-progs version 6.1, there is a command that can directly query the offset. Are you saying that some distributions do not update btrfs-progs?
At the same time, we also need to put compression tools, etc. into the kernel module and write them
sudo nano /etc/initramfs-tools/modules
#写入
zstd
zatd_compress
z3fold
implement sudo update-initramfs -c -k all
3. Enable memory compression
Now that I’ve finished talking about my ultimate plan, let’s talk about how to enable zram in the debian system.
The zram-tools or systemd-zram-generator software packages can be used to automatically set up zram devices. The professional version of uos has zram-tools and deepin has the latter systemd-zram-generator.
uos method:
install zram-tools and start the corresponding service: sudo apt install zram-tools
By default, the compression algorithm used by zRAM is lzo. If you want to use other algorithms or modify the size of zRAM, we can modify the configuration file /etc/default/zramswap sudo nano /etc/default/zramswap
Configure the above parameters to enable the service: sudo service zramswap reload
You can view the zram currently used for swap with the following command swapon -s
The method of deepin:
first install sudo apt install systemd-zram-generator
, modify the configuration file /etc/systemd/zram-generator.conf
and write:
Here a zram swap device is created using zstd compression and the size is half of all available memory capacity (ram/2 means the size is 1/2 of the RAM size; it can also be set to a value such as 4G or 512M)
Call zramctl or swapon to confirm that the device has been created and is being used.
$zramct
lNAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lzo-rle 4.8G 4K 80B 12K 4[SWAP]
Note: Be sure to update initramfs before restarting after moving the disk and related operations.
sudo update-initramfs -c -k all
This article brings the simplest partitioning scheme EFI+BTRFS with two global partitions;
This article truly realizes the usability of memory compression and swap files. There is almost no need to swap files at ordinary times. During hibernation, the work can be truly saved and restored to the original state.
It is undoubtedly a good choice for using a virtual machine and a browser with more than 20 tabs open at the same time, and you can get a smooth experience.
The above represents my personal final understanding of this solution. Partners who do not use btrfs can also start it and draw inferences from one example!
Let’s talk about the conclusion first:
zram : It is just memory compression technology. Data cannot be saved when shutting down. If you do not want to use the hibernation function, you do not need to set up any swap partition. Set zram directly. Judging from the compression ratio of zstd 1:4, you set a memory ZRAM (16G) with twice the space (8G) actually takes up 50% of the physical memory (that is, 4G) when it is full, so you can think that you have 4+16=20G of memory to use.
Swap : This thing is called swap space. It generally has two forms, swap partition and swap file. If you only use it to open large files, it may be written to the hard disk swap space. It can also be used to hibernate and save work progress when power is turned off.
zswap : zswap is a kernel function that provides a compressed memory cache for swap pages. Pages that would otherwise be swapped to disk are compressed and stored in an in-memory storage pool. The difference compared with zram is that zswap works together with the swap device, while zram is an in-memory swap device and does not require a backup swap device. If you want to hibernate, you need swap. You can do this by adjusting the exchange rate. Don't always write data to swap. Memory compression solves the need for normal boot use. The data only needs to be saved to the hard disk during hibernation. It is simply a problem. Perfect solution.
My ultimate solution is to use a combination of btrfs+swapfile+zswap.
Note: zswap and zram cannot coexist. Be sure to turn off zswap when using zram.
1. Enable btrfs swapfile:
Starting from kernel version 5.0, it is supported to use swap files in btrfs partitions as swap form.
From version btrfs-progs 6.0 onwards, swap files can be quickly created through its own disk utility command.
Systemd version 255 only supports hibernation and swap files under btrfs, which can
systemctl --version
be viewed, so the uos professional version is 241 and cannot be used.For people who don’t have too many requirements for hard disk use, you can actually use the swap partition, because even the swap file takes up a fixed space, but it has a little flexibility and can adjust the file size later. Therefore, using swap partition can adapt to more scenarios.
Otherwise, follow the previous method to create: https://bbs.deepin.org/post/270299
You can query the btrfs version through btrfs version. Currently deepin23beta3 is v6.3.2
It should be noted here that the btrfs disk tool has some limitations when creating and using swap files:
filesystem - can only be created in a single hardware partition
filesystem - must have only a single data configuration file
subvolume - snapshots cannot be created if it contains any active swap file
swapfile - must be pre-allocated (i.e. no holes)
swapfile - must be NODATACOW (i.e. also NODATASUM , no compression)
Create a swap file:
This process can be configured at system startup without entering the live environment. @swap is an independent subvolume and cannot be used for snapshots.
Mount the parent volume.
sudo mount /dev/sda2 /mnt
Create a @swap subvolume.
sudo btrfs subvolume create /mnt/@swap
Create a swap file
sudo btrfs filesystem mkswapfile --size 8G /mnt/@swap/swapfile
输出:create swapfile /mnt/@swap/swapfile size 8.00GiB (8589934592)
. Enable the swap file:
sudo swapon /mnt/@swap/swapfile
after activation, The file will appear in /proc/swaps:
The previous setting is to temporarily enable the swap file, which can be permanently enabled through fstab
. Edit:
sudo nano /etc/fstab
Add the following content and replace the uuid with yours.Enable hibernation to the swap file:
After enabling the swap file, the swap file can be used for hibernation, but this is not simple. Even if you see a sleep button on the deepin restart interface, an error will still occur and you cannot sleep normally. You need to further set up
hibernation here. Previously, the recovery offset had to be written to the file.
/sys/power/resume_offset
The value was a physical offset on the device.Btrfs file system used a mapping between logical and physical addresses, but here, physical can still be mapped to one or more specific devices. Physical block address. This is a physical offset for a specific device and is suitable as a recovery offset.
Starting from btrfs-progs version 6.1, there is a command that can directly query the offset. Are you saying that some distributions do not update btrfs-progs?
For some scripting and convenience reasons, the option -r will just print the offset, getting a unique output:
sudo nano /sys/power/resume_offset
Write:
44761047
Just save this valueIn the case of non-btrfs file systems, you can use the following command to obtain the offset value. Using this command with btrfs is inaccurate:
sudo filefrag -v /swap/swapfile
Just take the value of this position in the picture
But the above file will become invalid after restarting. How to make it permanent?
Here comes the key point, add the following parameters to two places
2. Application of zswap:
zswap parameter settings:
zswap has several customizable parameters. Live settings can be displayed using
For other parameters, please refer to https://docs.kernel.org/admin-guide/mm/zswap.html[](https://docs.kernel.org/admin-guide/mm/zswap.html)
Each setting can be changed at runtime through the sysfs interface. As an example, to change the compressor parameter
sudo echo zstd > /sys/module/zswap/parameters/compressor
Persist changes using kernel boot parameters:
zswap.enabled=1 zswap.compressor=zstd zswap.max_pool_percent=20 zswap.zpool=z3fold
A simpler way is to pass kernel parameters when booting through grub:
After
sudo update-grub
thatAt the same time, we also need to put compression tools, etc. into the kernel module and write them
sudo nano /etc/initramfs-tools/modules
implement
sudo update-initramfs -c -k all
3. Enable memory compression
Now that I’ve finished talking about my ultimate plan, let’s talk about how to enable zram in the debian system.
The zram-tools or systemd-zram-generator software packages can be used to automatically set up zram devices. The professional version of uos has zram-tools and deepin has the latter systemd-zram-generator.
uos method:
install zram-tools and start the corresponding service:
sudo apt install zram-tools
By default, the compression algorithm used by zRAM is lzo. If you want to use other algorithms or modify the size of zRAM, we can modify the configuration file /etc/default/zramswap
sudo nano /etc/default/zramswap
Configure the above parameters to enable the service:
sudo service zramswap reload
You can view the zram currently used for swap with the following command
swapon -s
The method of deepin:
first install
sudo apt install systemd-zram-generator
, modify the configuration file
/etc/systemd/zram-generator.conf
and write:
Here a zram swap device is created using zstd compression and the size is half of all available memory capacity (ram/2 means the size is 1/2 of the RAM size; it can also be set to a value such as 4G or 512M)
For detailed extension parameters, see: https://github.com/systemd/zram-generator
Enable service:
Call zramctl or swapon to confirm that the device has been created and is being used.
Note: Be sure to update initramfs before restarting after moving the disk and related operations.
sudo update-initramfs -c -k all
The above represents my personal final understanding of this solution. Partners who do not use btrfs can also start it and draw inferences from one example!
end!