137 | | == Background |
138 | | Shutdown and reboot are managed by systemd on modern Ubuntu Linux installations. As a matter of fact, the **shutdown** and **reboot** utilities are simply symbolic links to the **systemctl** utility. When the **systemctl** utility is run as one of these commands, it instructs systemd to transition the system to the desired target. This is the reboot.target in the case of a reboot. |
139 | | The process systemd uses to reboot is documented in the [https://www.man7.org/linux/man-pages/man7/bootup.7.html bootup manpage], which can be viewed by running the command "man 7 bootup". This documentation shows that systemd transitions through a number of targets when it reboots shutting down services along the way. It runs the systemd-reboot.service as the last step to transition to the reboot.target. On Ubuntu 18.04, the content of systemd-reboot.service unit file is: |
| 129 | The process systemd uses to reboot is documented in the [https://www.man7.org/linux/man-pages/man7/bootup.7.html bootup manpage], which can be viewed by running the command "man 7 bootup". This documentation shows that systemd transitions through a number of targets when it reboots shutting down services along the way. It runs the systemd-reboot.service as the last step to transition to the reboot.target. |
141 | | {{{ |
142 | | # SPDX-License-Identifier: LGPL-2.1+ |
143 | | # This file is part of systemd. |
144 | | # systemd is free software; you can redistribute it and/or modify it |
145 | | # under the terms of the GNU Lesser General Public License as published by |
146 | | # the Free Software Foundation; either version 2.1 of the License, or |
147 | | # (at your option) any later version. |
148 | | |
149 | | [Unit] |
150 | | Description=Reboot |
151 | | Documentation=man:systemd-halt.service(8) |
152 | | DefaultDependencies=no |
153 | | Requires=shutdown.target umount.target final.target |
154 | | After=shutdown.target umount.target final.target |
155 | | |
156 | | [Service] |
157 | | Type=oneshot |
158 | | ExecStart=/bin/systemctl --force reboot |
| 131 | To create a {{{gsc-poweroff}}} script in the {{{/lib/systemd/system-shutdown}}} you can do the following: |
| 132 | {{{#!bash |
| 133 | # add systemd system-shutdown hook to use the GSC to power-down |
| 134 | cat <<\EOF > /lib/systemd/system-shutdown/gsc-poweroff |
| 135 | #!/bin/bash |
| 136 | # use GSC to power cycle the system |
| 137 | echo 2 > /sys/bus/i2c/devices/0-0020/powerdown |
| 138 | EOF |
| 139 | chmod +x /lib/systemd/system-shutdown/gsc-poweroff |
160 | | |
161 | | Changing the systemd-reboot.service command line to run the Gateworks Ventana reboot script rather than run systemctl allows us to override the default |
162 | | reboot behavior. |
163 | | |
164 | | == Steps |
165 | | |
166 | | 1. Copy the Gateworks Ventana reboot script to an appropriate location on the target operating system. Make sure it is owned by root and is marked as executable. In our case, we wrote the contents to /usr/bin/ventana_reboot. |
167 | | |
168 | | {{{ |
169 | | #!/bin/sh |
170 | | # |
171 | | # usage: gsc_sleep [seconds] |
172 | | # |
173 | | # default 2 second sleep ensures between 1 and 2 seconds pass before powerup |
174 | | SECS=${1:-2} |
175 | | # syncs the filesystem |
176 | | echo "Syncing filesystems" |
177 | | sync |
178 | | # perform i2c transactions in loop to retry on i2c timeouts |
179 | | echo "Powering down for $SECS seconds" |
180 | | while [ 1 ]; do |
181 | | # set R6-R9 with 32bit number of seconds to wake |
182 | | i2cset -f -y 0 0x20 6 $((SECS % 256)) || continue |
183 | | SECS=$((SECS >> 8)) |
184 | | i2cset -f -y 0 0x20 7 $((SECS % 256)) || continue |
185 | | SECS=$((SECS >> 8)) |
186 | | i2cset -f -y 0 0x20 8 $((SECS % 256)) || continue |
187 | | SECS=$((SECS >> 8)) |
188 | | i2cset -f -y 0 0x20 9 $((SECS % 256)) || continue |
189 | | # latch sleep time - taking care to not disturb other bits |
190 | | R1=$(i2cget -f -y 0 0x20 1) || continue |
191 | | R1=$((R1 | 0x04)) # set R1.2 LATCH_SLEEP |
192 | | i2cset -f -y 0 0x20 1 $R1 || continue |
193 | | # set R1.0 SLEEP_ENABLE and R1.1 ACTIVATE_SLEEP - taking care not to disturb other bits |
194 | | R1=$(i2cget -f -y 0 0x20 1) || continue |
195 | | R1=$((R1 | 0x03)) # set R1.0 and R1.1 |
196 | | i2cset -f -y 0 0x20 1 $R1 || continue |
197 | | done |
198 | | }}} |
199 | | |
200 | | {{{#!bash |
201 | | chmod 777 /usr/bin/ventana_reboot |
202 | | }}} |
203 | | |
204 | | |
205 | | 2. Edit /lib/systemd/system/systemd-reboot.service and replace the contents of the ExecStart entry with a call to the Gateworks Ventana reboot script. |
206 | | |
207 | | {{{ |
208 | | # SPDX-License-Identifier: LGPL-2.1+ |
209 | | # This file is part of systemd. |
210 | | # systemd is free software; you can redistribute it and/or modify it |
211 | | # under the terms of the GNU Lesser General Public License as published by |
212 | | # the Free Software Foundation; either version 2.1 of the License, or |
213 | | # (at your option) any later version. |
214 | | |
215 | | [Unit] |
216 | | Description=Reboot |
217 | | Documentation=man:systemd-halt.service(8) |
218 | | DefaultDependencies=no |
219 | | Requires=shutdown.target umount.target final.target |
220 | | After=shutdown.target umount.target final.target |
221 | | |
222 | | [Service] |
223 | | Type=oneshot |
224 | | ExecStart=/usr/bin/ventana_reboot |
225 | | }}} |
226 | | |
227 | | 3. Reload systemd so that it picks up the changes. |
228 | | |
229 | | {{{#!bash |
230 | | systemctl daemon-reload |
231 | | }}} |