background
The notebook I bought in 13 years is too old. The mechanical hard disk and win10 experience are very bad, so I switched to Ubuntu 20.04.
After switching, the opening speed of documents and pictures has been significantly improved, and the fluency of watching videos and playing dota2 has not decreased at all. The only unhappiness is that each time the cover is closed, it does not sleep, but sleep, which needs to be solved.
To state, I'm based on Article 1 as well as Article 2 The former is applicable to the case where the swap partition is manually created when installing Ubuntu, and the latter is applicable to the case where the default installation (only the swap file does not create the swap partition).
First of all, it is necessary to realize command knocking sleep
The general idea of Linux hibernation is to dump the memory data to the swap partition or swap file in the form of image, then turn off the power, load the data from the swap partition to the memory and resume execution when you restart.
There are some details to note, that is, let GRUB and ramdisk know where to load the previously saved memory image during wake-up.
Create swap partition and initialize
Article 1 suggests using swap partition instead of swap file, because you don't have to worry about excluding swap file when backing up the primary partition. Of course, the final choice is yours.
It seems that the default swapfile of Ubuntu is only 2GB, which is not high enough. It is necessary to rebuild a large size. For specific operation methods, please click the reference link posted in Article 2.
If you install by default and don't want to take risks (I haven't tried yet!)
If your swapfile is not large enough, rebuild a large one according to the following method. If it is large enough, go to the next step directly:
sudo fallocate -l 16G /swapfile # 16G is not enough. You can expand it again sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile sudo vim /etc/fstab # If fstab does not have the following line, add the following line /swapfile none swap default 0 0
Get the starting block number of swapfile in the partition
sudo filefrag -v /swapfile | sed -n '4p' | awk '{print $4+0}' 548832 <-- The number output from the command line above is swapfile Start of block number
If you install by default but want to use swap partition instead
- Backup data
- Insert the installation disk and restart, because resize2fs cannot operate the mounted file system!
- Reduce the ext4 file system of the primary partition from M to N with resize2fs /dev/sda2 N,
- Delete the primary partition with fdisk, and then rebuild a new primary partition of size N
- Create the vacated N-M space as a swap partition, write it to the partition table, and exit fdisk
- Unplug the installation disk and restart
- Execute sudo mkswap /dev/sda3 to initialize the swap partition
- Execute sudo blkid | grep swap to obtain the UUID of the swap partition. Note that it is not a PARTUUID
- Add the partition to / etc/fstab to ensure that the swap partition is automatically mounted on the next boot, saving you from hitting sudo swap - a every time
- Get UUID of swap partition:
wanghaipeng@ubuntu:~/F266/7569_64$ blkid|grep swap /dev/sda5: UUID="22b7d10b-4e46-46e1-a040-e7d405d752b8" TYPE="swap" PARTUUID="34c6698d-05"
- Add swap partition to / etc/fstab (I choose to comment out the default configured swap file):
#/swapfile none swap sw 0 0 UUID=22b7d10b-4e46-46e1-a040-e7d405d752b8 none swap sw 0 0
If you are wise, create the swap partition at the time of installation
Congratulations, save the above dangerous operations, directly execute sudo blkid | grep swap, obtain the UUID of the swap partition, and enter the next step
Specify the resume parameter to GRUB and update grub.cfg
Add the resume parameter to GRUB to tell which partition to load the kernel from when waking up
sudo vi /etc/default/grub # Modify only the following line GRUB_CMDLINE_LINUX_DEFAULT="resume=UUID=22b7d10b-4e46-46e1-a040-e7d405d752b8"
If it is a swapfile, specify resume additionally_ Offset parameter
GRUB_CMDLINE_LINUX_DEFAULT="resume=/dev/sda2 resume_offset=548832"
These two parameters should be replaced with your own values!
Update grub.cfg
sudo update-grub
After the above command is executed, a new grub.cfg will be generated
Specify the resume parameter to update initramfs and update initrd.img
Specify the resume parameter for update initramfs
sudo vi /etc/initramfs-tools/conf.d/resume # Write only the following line RESUME=UUID=22b7d10b-4e46-46e1-a040-e7d405d752b8
If it is a swapfile, please check whether update initramfs needs additional configuration!
Regenerate initrd.img that supports wakeup
sudo update-initramfs -c -k all
Install PM utils and hibernate packages
sudo apt install pm-utils hibernate
After installing the software package, execute the following command to test the sleep effect
sudo systemctl hibernate
After executing this command, the notebook should sleep and turn off the power. If not, the following error will be prompted:
Failed to hibernate system via logind: Not enough swap space for hibernation
It means that your swapfile or swap partition is too small and needs to be expanded. The expansion of swapfile is relatively simple. Delete and rebuild a larger one. Refer to my previous method for swap partition.
Then close the cover and sleep
Modify the / etc/systemd/logind.conf file and add the following line in the [Login] section:
HandleLidSwitch=hibernate HandleLidSwitchExternalPower=hibernate HandleLidSwitchDocked=hibernate
The meaning of parameters is as follows:
HandleLidSwitch - Triggered when the cover is closed, except for the following two cases. HandleLidSwitchExternalPower - If the system is connected to an external power supply, it is triggered when the cover is closed. HandleLidSwitchDocked - Triggered when the cover is closed if the system is plugged into a docking station or multiple displays are connected.
After saving and exiting, you can't close the cover and sleep. You need to restart it.
summary
Distribution manufacturers benefit little from individual user groups, so it is understandable that the user experience is poor. Money and brain time always have to consume one of them.
Wake up effect after sleep:
Wake up screen after hibernation of laptop with Ubuntu 20.04 installed