In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Recently, the company has stipulated that the computer must be turned off after leaving at night, but people like us often forget to turn off the computer, and it is also very troublesome to restore the working environment after turning off the computer. We have no choice but to toss about and let linux sleep regularly.
Type of dormancy
There are currently about three types of dormancy:
Suspend (suspend to RAM)
It means that most machine parts except memory are in a power-off state. The recovery speed of this dormant state is very fast, but because the data in memory has not been saved, the system in this state does not enter the real dormant state and continues to consume power.
Hibernate (suspend to disk)
This dormancy writes the system state in memory into the swap space, which can be read back from the swap space when the system starts. In this case, the system can be completely powered off, but because the system state is saved / read to / from swap space, it is slow and requires some configuration (described below)
Hybrid (suspend to both)
Combines the above two types of dormancy. It stores the system state in swap space like hibernate and does not turn off power like suspend. In this way, it can quickly recover from dormancy before the power supply is exhausted. If the power is exhausted during hibernation, it can restore the system state from the swap space.
Suspend hibernation
It is very easy to enter suspend, no additional configuration is required, just execute systemctl suspend directly on the systemd system. \
Systemctl suspend
Its actual action is defined by systemd-suspend.service, and on archlinux, it looks like this:
# SPDX-License-Identifier: LGPL-2.1+## This file is part of systemd.## systemd is free software; you can redistribute it and/or modify it# under the terms of the GNU Lesser General Public License as published by# the Free Software Foundation; either version 2.1 of the License, or# (at your option) any later version. [unit] Description=SuspendDocumentation=man:systemd-suspend.service (8) DefaultDependencies=noRequires=sleep.targetAfter= targets. Target [service] Type=oneshotExecStart=/usr/lib/systemd/systemd-sleep suspend
Hibernation hibernation
Because hibernation hibernation requires writing the contents of memory to swap space, you should have at least one swap partition or swap file that is larger than memory. (in fact, if the swap space is not large enough memory, it is not necessarily impossible to perform hibernation hibernation. You can try to run echo 0 | sudo tee / sys/power/image_size, which will make the system compress as much as possible when writing to the swap space, but this method cannot guarantee the success of hibernation.)
If you have not previously created a swap partition, you can temporarily create a swap file to use. For example, the following command creates a 5G swap file
Sudo dd if=/dev/zero of=/swapfile bs=10240 count=524288sudo mkswap / swapfilesudo chmod 0600 / swapfilesudo swapon / swapfilesudo cp / etc/fstab / etc/fstab.bakecho "/ swapfile swap swap default 00" | tee-a / etc/fstabSetting up swapspace version 1, size = 5 GiB (5368705024 bytes) no label, UUID=d0f0c682-e1fa-416f-8fe2-b554b8ca363a/swapfile swap swap default 00
In addition to creating the swap partition, we also need to modify the startup parameters of kernel so that the system tries to recover from the swap space first at startup. The specific operations are as follows:
1. If you use swap partitions to save, you only need to swap partitions for the startup parameter resume= that adds kernel
(1) check which partition is the swap partition
Swapon
NAME TYPE SIZE USED PRIO/dev/sda2 partition 8G 280K-2/swapfile file 5G 0B-3
You can see that the swap partition is / dev/sda2
(2) modify / etc/default/grub to add the parameter resume=/dev/sda2 for the GRUB_CMDLINE_LINUX_DEFAULT line
Sudo sed-I'/ GRUB_CMDLINE_LINUX_DEFAULT/ s! "$! resume=/dev/sda2"! / etc/default/grub
two。 If you are using a swap file, you need to add two parameters: the disk where the resume= swap file is located and the offset location of the resume_offset= swap file on the disk:
(1) View the disk where the swap file is located
Df / swapfile
File system 1K-Block used available used% mount point
/ dev/sda3 55253696 27582224 24834972 53%
Indicates that the disk is / dev/sda3
(2) View the offset location of the swap file
Sudo filefrag-v / swapfile | head-5Filesystem type is: ef53File size of / swapfile is 5368709120 (1310720 blocks of 4096 bytes) ext: logical_offset: physical_offset: length: expected: flags: 0: 0. 32767: 4653056.. 4685823: 32768: 1: 32768.. 65535: 4685824.. 4718591: 32768:
Here you can see that the physical offset position is 4653056.
(3) modify / etc/default/grub to add the parameter resume=/dev/sda3 resume_offset=4653056 for the GRUB_CMDLINE_LINUX_DEFAULT line
Sudo sed-I'/ GRUB_CMDLINE_LINUX_DEFAULT/ s! "$! resume=/dev/sda3 resume_offset=4653056"! / etc/default/grub
3. Regenerate the grub.cfg file
Sudo grub-mkconfig-o / boot/grub/grub.cfg
(1) configure initramfs, add resume hook, modify / etc/mkinitcpio.conf file, and add resume to HOOKS
Sudo sed-I'/ ^ HOOKS=/ s /) / resume) /'/ etc/mkinitcpio.conf
There are two points to note:
Since both label and UUID of the partition are assigned by udev, resume must be placed after udev. Since systemd hook already has the function of resume, if you already have systemd hook, you do not need to add udev hook.
(2) regenerate initramfs
Sudo mkinitcpio-g / boot/initramfs-linux-lily.img== > Starting build: 4.16.12-2-lily-> Running build hook: [base]-> Running build hook: [udev]-> Running build hook: [autodetect]-> Running build hook: [modconf]-> Running build hook: [block]-> Running build hook: [filesystems]-> Running build hook: [keyboard]-> Running build hook: [fsck]-> Running build hook: [resume] = > Generating module dependencies== > Creating gzip-compressed initcpio image: / boot/initramfs-linux-lily.img== > Image generation successful
(3) restart to make the configuration effective
After the complex configuration above, hibernation hibernation can be really successful. Similar to suspend hibernation, we can also use systemctl for hibernation
Systemctl hibernate
Similarly, its actual action is defined by systemd-hibernte.service, and on archlinux it looks like this:
# SPDX-License-Identifier: LGPL-2.1+## This file is part of systemd.## systemd is free software; you can redistribute it and/or modify it# under the terms of the GNU Lesser General Public License as published by# the Free Software Foundation; either version 2.1 of the License, or# (at your option) any later version. [unit] Description=HibernateDocumentation=man:systemd-suspend.service (8) DefaultDependencies=noRequires=sleep.targetAfter= targets. Target [service] Type=oneshotExecStart=/usr/lib/systemd/systemd-sleep hibernate
Hybrid hibernation
After hibernate hibernation is configured, hybrid hibernation can be performed normally by executing
Systemctl hybrid-sleep
Similarly, its actual action is defined by systemd-hybrid-sleep.service, and on archlinux it looks like this:
# SPDX-License-Identifier: LGPL-2.1+## This file is part of systemd.## systemd is free software; you can redistribute it and/or modify it# under the terms of the GNU Lesser General Public License as published by# the Free Software Foundation; either version 2.1 of the License, or# (at your option) any later version. [unit] Description=Hybrid Suspend+HibernateDocumentation=man:systemd-suspend.service (8) DefaultDependencies=noRequires=sleep.targetAfter= targets. Target [service] Type=oneshotExecStart=/usr/lib/systemd/systemd-sleep hybrid-sleep
Sleep Hooks
As can be seen from the above service file, no matter which type of system hibernates, its internal actual call is systemd-sleep.
Man systemd-sleep
SYSTEMD-SUSPEND.SERVICE (8) systemd-suspend.service SYSTEMD-SUSPEND.SERVICE (8) NAME systemd-suspend.service, systemd-hibernate.service, systemd-hybrid-sleep.service, systemd-sleep-System sleep state logicSYNOPSIS systemd-suspend.service systemd-hibernate.service systemd-hybrid-sleep.service / usr/lib/systemd/system-sleepDESCRIPTION systemd-suspend.service is a system service that is pulled in by suspend.target and is responsible for the actual system suspend. Similarly, systemd-hibernate.service is pulled in by hibernate.target to execute the actual hibernation. Finally, systemd-hybrid-sleep.service is pulled in by hybrid-sleep.target to execute hybrid hibernation with system suspend. Immediately before entering system suspend and/or hibernation systemd-suspend.service (and the other mentioned units, respectively) will run all executables in / usr/lib/systemd/system-sleep/ and pass two arguments to them. The first argument will be "pre", the second either "suspend", "hibernate", or "hybrid-sleep" depending on the chosen action. Immediately after leaving system suspend and/or hibernation the same executables are run, but the first argument is now "post". All executables in this directory are executed in parallel, and execution of the action is not continued until all executables have finished. Note that scripts or binaries dropped in / usr/lib/systemd/system-sleep/ are intended for local use only and should be considered hacks. If applications want to react to system suspend/hibernation and resume, they should rather use the Inhibitor interface [1]. Note that systemd-suspend.service, systemd-hibernate.service, and systemd-hybrid-sleep.service should never be executed directly. Instead, trigger system sleep states with a command such as "systemctl suspend" or similar. Internally, this service will echo a string like "mem" into / sys/power/state, to trigger the actual system suspend. What exactly is written where can be configured in the "[Sleep]" section of / etc/systemd/sleep.conf or a sleep.conf.d file. See systemd- sleep.conf (5). OPTIONS systemd-sleep understands the following commands:-h,-- help Print a short help text and exit. -- version Print a short version string and exit. Suspend, hibernate, hybrid-sleep Suspend, hibernate, or put the system to hybrid sleep.SEE ALSO systemd-sleep.conf (5), systemd (1), systemctl (1), systemd.special (7), systemd-halt.service (8) NOTES 1. Inhibitor interface
Https://www.freedesktop.org/wiki/Software/systemd/inhibit
Systemd 238 SYSTEMD-SUSPEND.SERVICE (8)
According to systemd-sleep 's manual pages, you can see that the script in / usr/lib/systemd/system-sleep is called in parallel, passing two parameters before and after the system is restored from hibernation.
The first parameter is used to specify whether to start or resume sleep, with the corresponding strings "pre" and "post", respectively.
The second parameter, which is used to indicate the type of sleep, is the strings "suspend", "hibernate" and "hybrid-sleep".
Perform dormancy regularly
The timing task in systemd system is realized by timer, and each timer corresponds to a service.
In general, the name of timer is the same as that of service, but if necessary, you can control a service with a different name from timer by specifying the Unit= option in the [Timer] section of the .timer file.
Here is an example of timer, where automatic hibernate hibernation starts at 21:30 every day
[Unit] Description=Hibernate every 21:30: 00 [timer] OnCalendar=*-*-* 21 paste 30 purl 00Persistentworthy trueUnitrated system d-hibernate.service [install] WantedBy=timers.target
Wake up dormant linux regularly
Using rtcwake, you can wake up a dormant computer at a given time
Its main usage is as follows:
Sudo rtcwake-m ${mode}-t ${time_t} # or sudo rtcwake-m ${mode}-s ${seconds}
Parameter mode is in standby mode and has the following options:
Standby
Normal standby mode, default option, corresponding to ACPI state S1
Mem
Suspend hibernation, corresponding to ACPI state S3
Disk
Hibernation hibernation, corresponding to ACPI state S4
Off
Hibernate by calling the shutdown command of the system, corresponding to ACPI state S5
Parameter time_t is the number of seconds from 1970-01-01, 00:00 UTC to the present. You can convert the time string to this number of seconds by using the date command, such as
Sudo rtcwake-m disk-t $(date-d 08:30 +% s)
Is to hibernate hibernation and wake up at 08: 30.
The parameter seconds is the number of seconds, indicating how many seconds from now, the system wakes up.
Summary
The above is the realization of linux timing dormancy introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply you in time. Thank you very much for your support to the website!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.