commit 24b62c898b82b0837abe62bfb26ee88550dc6c00 Author: Nicolas MASSE Date: Sun Jun 15 10:19:35 2025 +0200 initial commit diff --git a/Before-After/README.md b/Before-After/README.md new file mode 100644 index 0000000..595b16b --- /dev/null +++ b/Before-After/README.md @@ -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 +``` diff --git a/Before-After/backup-mnt-test.service b/Before-After/backup-mnt-test.service new file mode 100644 index 0000000..e7d449d --- /dev/null +++ b/Before-After/backup-mnt-test.service @@ -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 . diff --git a/Before-After/common.env b/Before-After/common.env new file mode 100644 index 0000000..46c6d9d --- /dev/null +++ b/Before-After/common.env @@ -0,0 +1,5 @@ +declare -a UNITS=( + *.target + *.service + *.mount +) diff --git a/Before-After/custom.target b/Before-After/custom.target new file mode 100644 index 0000000..e9a175d --- /dev/null +++ b/Before-After/custom.target @@ -0,0 +1,4 @@ +[Unit] +Description=Custom Target +Documentation=https://github.com/nmasse-itix/Systemd-Examples +AllowIsolate=no diff --git a/Before-After/install.sh b/Before-After/install.sh new file mode 100755 index 0000000..3918a54 --- /dev/null +++ b/Before-After/install.sh @@ -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 diff --git a/Before-After/mnt-backup.mount b/Before-After/mnt-backup.mount new file mode 100644 index 0000000..09d9a7a --- /dev/null +++ b/Before-After/mnt-backup.mount @@ -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 diff --git a/Before-After/mnt-test.mount b/Before-After/mnt-test.mount new file mode 100644 index 0000000..bc5212e --- /dev/null +++ b/Before-After/mnt-test.mount @@ -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 diff --git a/Before-After/restore-mnt-test.service b/Before-After/restore-mnt-test.service new file mode 100644 index 0000000..e7fb8cb --- /dev/null +++ b/Before-After/restore-mnt-test.service @@ -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 diff --git a/Before-After/test.sh b/Before-After/test.sh new file mode 100755 index 0000000..b971607 --- /dev/null +++ b/Before-After/test.sh @@ -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 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f6b77c8 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..8501728 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# Systemd examples + +This repository contains various examples of Systemd units that I had to craft over time. + +## License + +MIT