commit
24b62c898b
11 changed files with 146 additions and 0 deletions
@ -0,0 +1,21 @@ |
|||
# Execute a unit before and after another one |
|||
|
|||
## Use case |
|||
|
|||
In this example, I wanted to mount a file system (/mnt/test). |
|||
But before mounting the filesystem, I wanted to make a backup of it. |
|||
And after, I want to restore the filesystem from the previous backup. |
|||
In addition to that, the backup is stored on another filesystem that needs to be mounted too. |
|||
|
|||
## Install |
|||
|
|||
```sh |
|||
./install.sh |
|||
``` |
|||
|
|||
## Test |
|||
|
|||
```sh |
|||
./test.sh |
|||
cat /mnt/test/witness |
|||
``` |
|||
@ -0,0 +1,10 @@ |
|||
[Unit] |
|||
Description=Backup the content of /mnt/test |
|||
Documentation=https://github.com/nmasse-itix/Systemd-Examples |
|||
Before=mnt-test.mount |
|||
RequiresMountsFor=/mnt/backup |
|||
|
|||
[Service] |
|||
Type=oneshot |
|||
UMask=077 |
|||
ExecStart=tar -cf /mnt/backup/mnt-test.tar -C /mnt/test . |
|||
@ -0,0 +1,5 @@ |
|||
declare -a UNITS=( |
|||
*.target |
|||
*.service |
|||
*.mount |
|||
) |
|||
@ -0,0 +1,4 @@ |
|||
[Unit] |
|||
Description=Custom Target |
|||
Documentation=https://github.com/nmasse-itix/Systemd-Examples |
|||
AllowIsolate=no |
|||
@ -0,0 +1,20 @@ |
|||
#!/bin/bash |
|||
|
|||
set -Eeuo pipefail |
|||
|
|||
. common.env |
|||
|
|||
for unit in "${UNITS[@]}"; do |
|||
echo "Installing $unit..." |
|||
sudo cp -r --preserve=mode "$unit" "/etc/systemd/system/$unit" |
|||
done |
|||
|
|||
echo "Reloading systemd..." |
|||
sudo systemctl daemon-reload |
|||
|
|||
for unit in "${UNITS[@]}"; do |
|||
if grep -Fqx '[Install]' "$unit"; then |
|||
echo "Installing $unit..." |
|||
sudo systemctl enable "$unit" |
|||
fi |
|||
done |
|||
@ -0,0 +1,12 @@ |
|||
[Unit] |
|||
Description=Mount the "/mnt/backup" filesystem |
|||
Documentation=https://github.com/nmasse-itix/Systemd-Examples |
|||
|
|||
[Mount] |
|||
What=tmpfs |
|||
Where=/mnt/backup |
|||
Type=tmpfs |
|||
Options=context=system_u:object_r:container_file_t:s0 |
|||
|
|||
[Install] |
|||
WantedBy=custom.target |
|||
@ -0,0 +1,15 @@ |
|||
[Unit] |
|||
Description=Mount the "/mnt/test" filesystem |
|||
Documentation=https://github.com/nmasse-itix/Systemd-Examples |
|||
After=backup-mnt-test.service |
|||
Requires=backup-mnt-test.service |
|||
Before=restore-mnt-test.service |
|||
Wants=restore-mnt-test.service |
|||
|
|||
[Mount] |
|||
What=tmpfs |
|||
Where=/mnt/test |
|||
Type=tmpfs |
|||
|
|||
[Install] |
|||
WantedBy=custom.target |
|||
@ -0,0 +1,11 @@ |
|||
[Unit] |
|||
Description=Restore the "/mnt/test" filesystem |
|||
Documentation=https://github.com/nmasse-itix/Systemd-Examples |
|||
After=mnt-test.mount |
|||
Requires=mnt-test.mount |
|||
RequiresMountsFor=/mnt/backup |
|||
|
|||
[Service] |
|||
Type=oneshot |
|||
ExecStart=tar -xf /mnt/backup/mnt-test.tar -C /mnt/test |
|||
ExecStartPost=rm -f /mnt/backup/mnt-test.tar |
|||
@ -0,0 +1,20 @@ |
|||
#!/bin/bash |
|||
|
|||
set -Eeuo pipefail |
|||
|
|||
. common.env |
|||
|
|||
sudo systemctl stop mnt-test.mount |
|||
sudo systemctl stop mnt-backup.mount |
|||
|
|||
sudo rm -f /mnt/test/witness /mnt/backup/mnt-test.tar |
|||
sudo mkdir -p /mnt/test /mnt/backup |
|||
echo "Hello, World!" | sudo tee /mnt/test/witness > /dev/null |
|||
|
|||
( sleep 1 ; sudo systemctl start custom.target ) & |
|||
|
|||
declare -a journalctl_args=() |
|||
for unit in "${UNITS[@]}"; do |
|||
journalctl_args+=( -u "$unit" ) |
|||
done |
|||
sudo journalctl --since=now "${journalctl_args[@]}" -f |
|||
@ -0,0 +1,21 @@ |
|||
The MIT License (MIT) |
|||
|
|||
Copyright © 2025 Nicolas MASSE |
|||
|
|||
Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
of this software and associated documentation files (the "Software"), to deal |
|||
in the Software without restriction, including without limitation the rights |
|||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
copies of the Software, and to permit persons to whom the Software is |
|||
furnished to do so, subject to the following conditions: |
|||
|
|||
The above copyright notice and this permission notice shall be included in |
|||
all copies or substantial portions of the Software. |
|||
|
|||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|||
THE SOFTWARE. |
|||
@ -0,0 +1,7 @@ |
|||
# Systemd examples |
|||
|
|||
This repository contains various examples of Systemd units that I had to craft over time. |
|||
|
|||
## License |
|||
|
|||
MIT |
|||
Loading…
Reference in new issue