Skip to content

Commit

Permalink
grub-btrfsd: Merge openrc and systemd daemon
Browse files Browse the repository at this point in the history
Fix #213, Fix #211, Fix #215
  • Loading branch information
Schievel1 committed Sep 2, 2022
1 parent 14c42b8 commit 961675c
Show file tree
Hide file tree
Showing 13 changed files with 862 additions and 146 deletions.
47 changes: 40 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,64 @@ OPENRC ?= false
SHARE_DIR = $(DESTDIR)$(PREFIX)/share
LIB_DIR = $(DESTDIR)$(PREFIX)/lib
BIN_DIR = $(DESTDIR)$(PREFIX)/bin
MAN_DIR = $(SHARE_DIR)/man

.PHONY: install uninstall help
TEMP_DIR = ./temp

.PHONY: install uninstall clean help

install:
@if test "$(shell id -u)" != 0; then \
echo "You are not root, run this target as root please."; \
exit 1; \
fi
@echo " Installing "
@echo
@echo " :::::::: ::::::::: ::: ::: ::::::::: ::::::::: ::::::::::: ::::::::: :::::::::: :::::::: "
@echo " :+: :+: :+: :+: :+: :+: :+: :+: :+: :+: :+: :+: :+: :+: :+: :+: "
@echo " +:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ "
@echo " :#: +#++:++#: +#+ +:+ +#++:++#+ +#++:++#++:++ +#++:++#+ +#+ +#++:++#: :#::+::# +#++:++#++ "
@echo " +#+ +#+# +#+ +#+ +#+ +#+ +#+ +#+ +#+ +#+ +#+ +#+ +#+ +#+ +#+ "
@echo " #+# #+# #+# #+# #+# #+# #+# #+# #+# #+# #+# #+# #+# #+# #+# #+# "
@echo " ######## ### ### ######## ######### ######### ### ### ### ### ######## "
@echo
@echo " For further information visit https://github.com/Antynea/grub-btrfs or read the man page: 'man grub-btrfs'"
@echo
@mkdir "${TEMP_DIR}"
@chmod 777 ${TEMP_DIR}
@cp manpages/grub-btrfs.8.man ${TEMP_DIR}/grub-btrfs.8
@bzip2 ${TEMP_DIR}/grub-btrfs.8
@install -Dm644 -t "${MAN_DIR}/man8" "${TEMP_DIR}/grub-btrfs.8.bz2"
@cp manpages/grub-btrfsd.8.man ${TEMP_DIR}/grub-btrfsd.8
@bzip2 ${TEMP_DIR}/grub-btrfsd.8
@install -Dm644 -t "${MAN_DIR}/man8" "${TEMP_DIR}/grub-btrfsd.8.bz2";
@install -Dm755 -t "$(DESTDIR)/etc/grub.d/" 41_snapshots-btrfs
@install -Dm644 -t "$(DESTDIR)/etc/default/grub-btrfs/" config
@install -Dm744 -t "$(BIN_DIR)/" grub-btrfsd;
@# Systemd init system
@if test "$(SYSTEMD)" = true; then \
install -Dm644 -t "$(LIB_DIR)/systemd/system/" grub-btrfs.path; \
install -Dm644 -t "$(LIB_DIR)/systemd/system/" grub-btrfs.service; \
echo "Installing systemd .service file"; \
install -Dm644 -t "$(LIB_DIR)/systemd/system/" grub-btrfsd.service; \
fi
@# OpenRC init system
@if test "$(OPENRC)" = true; then \
install -Dm744 -t "$(BIN_DIR)/" grub-btrfs-openrc; \
echo "Installing openRC init.d & conf.d file"; \
install -Dm744 grub-btrfsd.initd "$(DESTDIR)/etc/init.d/grub-btrfsd"; \
install -Dm644 grub-btrfsd.confd "$(DESTDIR)/etc/conf.d/grub-btrfsd"; \
fi
@# Arch Linux like distros only :
@if test "$(INITCPIO)" = true; then \
echo "Installing initcpio hook"; \
install -Dm644 "initramfs/Arch Linux/overlay_snap_ro-install" "$(LIB_DIR)/initcpio/install/grub-btrfs-overlayfs"; \
install -Dm644 "initramfs/Arch Linux/overlay_snap_ro-hook" "$(LIB_DIR)/initcpio/hooks/grub-btrfs-overlayfs"; \
fi
@install -Dm644 -t "$(SHARE_DIR)/licenses/$(PKGNAME)/" LICENSE
@install -Dm644 -t "$(SHARE_DIR)/doc/$(PKGNAME)/" README.md
@install -Dm644 "initramfs/readme.md" "$(SHARE_DIR)/doc/$(PKGNAME)/initramfs-overlayfs.md"
@rm -rf "${TEMP_DIR}"

uninstall:
@echo "Uninstalling grub-btrfs"
@if test "$(shell id -u)" != 0; then \
echo "You are not root, run this target as root please."; \
exit 1; \
Expand All @@ -47,12 +74,14 @@ uninstall:
rm -f "$${grub_dirname:-/boot/grub}/grub-btrfs.cfg"
@rm -f "$(DESTDIR)/etc/default/grub-btrfs/config"
@rm -f "$(DESTDIR)/etc/grub.d/41_snapshots-btrfs"
@rm -f "$(LIB_DIR)/systemd/system/grub-btrfs.path"
@rm -f "$(LIB_DIR)/systemd/system/grub-btrfs.service"
@rm -f "$(BIN_DIR)/grub-btrfs-openrc;"
@rm -f "$(LIB_DIR)/systemd/system/grub-btrfsd.service"
@rm -f "$(BIN_DIR)/grub-btrfsd;"
@rm -f "$(DESTDIR)/etc/init.d/grub-btrfsd;"
@rm -f "$(DESTDIR)/etc/conf.d/grub-btrfsd;"
@rm -f "$(LIB_DIR)/initcpio/install/grub-btrfs-overlayfs"
@rm -f "$(LIB_DIR)/initcpio/hooks/grub-btrfs-overlayfs"
@rm -f "$(MAN_DIR)/man8/grub-btrfs.8.bz2"
@rm -f "$(MAN_DIR)/man8/grub-btrfsd.8.bz2"
@# Arch Linux UNlike distros only :
@if test "$(INITCPIO)" != true && test -d "$(LIB_DIR)/initcpio"; then \
rmdir --ignore-fail-on-non-empty "$(LIB_DIR)/initcpio/install" || :; \
Expand All @@ -66,6 +95,10 @@ uninstall:
@rmdir --ignore-fail-on-non-empty "$(SHARE_DIR)/licenses/$(PKGNAME)/" || :
@rmdir --ignore-fail-on-non-empty "$(DESTDIR)/etc/default/grub-btrfs" || :

clean:
@echo "Deleting ./temp"
@rm -rf "${TEMP_DIR}"

help:
@echo
@echo "Usage: $(MAKE) [ <parameter>=<value> ... ] [ <action> ]"
Expand Down
172 changes: 104 additions & 68 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

## grub-btrfs

* BTC donation address: `1Lbvz244WA8xbpHek9W2Y12cakM6rDe5Rt`
##### BTC donation address: `1Lbvz244WA8xbpHek9W2Y12cakM6rDe5Rt`
- - -
### Description
### Description:
Improves grub by adding "btrfs snapshots" to the grub menu.

You can boot your system on a "snapshot" from the grub menu.
Expand All @@ -27,10 +27,10 @@ Refer to the [documentation](https://github.com/Antynea/grub-btrfs/blob/master/i
* Automatically detect kernel, initramfs and intel/amd microcode in `/boot` directory on snapshots.
* Automatically create corresponding "menuentry" in `grub.cfg`
* Automatically detect the type/tags and descriptions/comments of snapper/timeshift snapshots.
* Automatically generate `grub.cfg` if you use the provided systemd service.
* Automatically generate `grub.cfg` if you use the provided systemd/ openRC service.

- - -
### Installation
### Installation:
#### Arch Linux
The package is available in the community repository [grub-btrfs](https://archlinux.org/packages/community/any/grub-btrfs/)
```
Expand All @@ -45,8 +45,8 @@ emerge -av app-eselect/eselect-repository
eselect repository enable guru
emerge --sync
```
If you are using SystemD on Gentoo, make sure the USE-Flag `systemD` is set. (Either globally in make.conf or in package.use for the package app-backup/grub-btrfs)
Without systemD USE-Flag the OpenRC-Daemon of grub-btrfs will be installed.
If you are using Systemd on Gentoo, make sure the USE-Flag `systemd` is set. (Either globally in make.conf or in package.use for the package app-backup/grub-btrfs)
Without systemd USE-Flag the OpenRC-daemon of grub-btrfs will be installed.

Emerge grub-btrfs via
`emerge app-backup/grub-btrfs`
Expand All @@ -66,6 +66,7 @@ Booting into read-only snapshots is fully supported when choosing "btrfs" as fil
* [grub](https://archlinux.org/packages/core/x86_64/grub/)
* [bash >4](https://archlinux.org/packages/core/x86_64/bash/)
* [gawk](https://archlinux.org/packages/core/x86_64/gawk/)
* (optional for the daemon)[inotify-tools](https://archlinux.org/packages/community/x86_64/inotify-tools/)

#### NOTE: All distros
Generate your grub menu after installation for the changes to take effect.
Expand All @@ -74,87 +75,122 @@ On **Arch Linux** or **Gentoo** use `grub-mkconfig -o /boot/grub/grub.cfg`.
On **Fedora** use `grub2-mkconfig -o /boot/grub2/grub.cfg`
On **Debian-like** distribution `update-grub` is an alias to `grub-mkconfig ...`
- - -
### Customization
### Customization:

You have the possibility to modify many parameters in `/etc/default/grub-btrfs/config`.
See [config file](https://github.com/Antynea/grub-btrfs/blob/master/config) for more information.
For further information see [config file](https://github.com/Antynea/grub-btrfs/blob/master/config) or `man grub-btrfs`

#### grub-btrfsd daemon
Grub-btrfs comes with a daemon script that automatically updates the grub menu when it sees a snapshot being created or deleted in a directory it is given via command line.

The daemon can be configured by passing different command line arguments to it. This can be change by either running
```bash
sudo systemctl edit --full grub-btrfsd
```
(when using systemd) or by editing `/etc/conf.d/grub-btrfsd` (when using openRC). In either case the daemon must be restarted for the changes to take effect with
```bash
sudo systemctl restart grub-btrfsd # for systemd

```
or
```
sudo rc-service grub-btrfsd restart # for openRC
```

It is also possible to start the daemon without systemd or openRC. In this case, the daemon should be stopped with
```bash
sudo systemctl stop grub-btrfsd # for systemd
```
or
```bash
sudo rc-service grub-btrfsd stop # for openRC
```
Then the daemon can be manually run and played around with with the command `grub-btrfsd`.
For additional information on daemon script and its arguments, run `grub-btrfsd -h` and see `man grub-btrfsd`

#### Warning:
by default, `grub-mkconfig` command is used.
Might be `grub2-mkconfig` on some systems (Fedora ...).
Edit `GRUB_BTRFS_MKCONFIG` variable in `/etc/default/grub-btrfs/config` file to reflect this.
- - -
### SystemD service
#### Automatically update grub upon snapshot
To automatically regenerate `grub-btrfs.cfg` when a modification appears in the `/.snapshots` mount point, run
### Automatically update grub upon snapshot
Grub-btrfs comes with its own daemon, that watches the snapshot directory for you and updates the grub menu automatically every time a snapshot is created or deleted.

To start it now, run
```bash
sudo systemctl start grub-btrfsd # for systemd
```
systemctl enable grub-btrfs.path
systemctl start grub-btrfs.path # In case the mount point is available already
or
```bash
sudo rc-service grub-btrfsd start # for openRC
```
Monitoring starts automatically when the mount point becomes available.
##### Snapshots not in `/.snapshots`
To modify `grub-btrfs.path` run

To activate it during system startup, run
```bash
sudo systemctl enable grub-btrfsd # for systemd
```
systemctl edit --full grub-btrfs.path
systemctl reenable grub-btrfs.path
or
```bash
sudo rc-config add grub-btrfsd default # for openRC
```
To find out the name of the `.mount` unit use `systemctl list-units -t mount`.

#### Automatically update grub upon restart/ boot:
[Look at this comment](https://github.com/Antynea/grub-btrfs/issues/138#issuecomment-766918328)
Currently not implemented

**Timeshift**
#### Snapshots not in `/.snapshots`
NOTE: This works also for Timeshift versions < 22.06, the path to watch would be `/run/timeshift/backup/timeshift-btrfs/snapshots`.
##### Systemd
By default the daemon is watching the directory `/.snapshots`. If the daemon should watch a different directory, it can be edited with
```bash
sudo systemctl edit --full grub-btrfsd
```
What should be edited is the `/.snapshots`-part in the line that says `ExecStart=/usr/bin/grub-btrfsd /.snapshots`
When done, the service should be restarted with
``` bash
sudo systemctl restart grub-btrfsd
```

1. Run `systemctl edit --full grub-btrfs.path`
1. Replace the whole block by:
##### OpenRC
Arguments are passed to grub-btrfsd via the file `/etc/conf.d/grub-btrfsd`.
The variable `snapshots` defines, where the daemon will watch for snapshots.
After that, the daemon should be restarted with
``` bash
sudo rc-service grub-btrfsd restart
```
[Unit]
Description=Monitors for new snapshots
DefaultDependencies=no
Requires=run-timeshift-backup.mount
After=run-timeshift-backup.mount
BindsTo=run-timeshift-backup.mount

[Path]
PathModified=/run/timeshift/backup/timeshift-btrfs/snapshots
**Timeshift >=22.06**
Newer Timeshift versions create a new directory after their process ID in `/run/timeshift` every time they are started. The PID is going to be different every time.
Therefore the daemon can not simply watch a directory, it watches `/run/timeshift` first, if a directory is created it gets Timeshifts current PID, then watches a directory in that newly created directory from Timeshift.
Anyhow, to activate this mode of the daemon, `--timeshift-auto` must be passed to the daemon as a command line argument.

[Install]
WantedBy=run-timeshift-backup.mount
#### Systemd
Servicefile of grub-btrfsd can be edited with
```bash
sudo systemctl edit --full grub-btrfsd
```
The line that says `ExecStart=/usr/bin/grub-btrfsd /.snapshots` should be edited into `ExecStart=/usr/bin/grub-btrfsd --timeshift-auto`.
When done, the service should be restarted with
``` bash
sudo systemctl restart grub-btrfsd
```
1. Run `systemctl reenable grub-btrfs.path` to reload the changes you made

1. Run `systemctl start grub-btrfs.path` to start monitoring.<br>Otherwise, the unit will automatically start monitoring when the mount point will be available.

Note:
You can view your change to `systemctl cat grub-btrfs.path`.
To revert change use `systemctl revert grub-btrfs.path`.

Note: If you are using timeshift version v22.06 or newer, snapshots are now saved in the direcory `/run/timeshift/$PIDofthecurrentlyrunnigtimeshift/backup/timeshift-btrfs` by Timeshift. The PID is changing everytime you open Timeshift. This can not be changed, but when the mount `/run/timeshift/backup/` is created in `/etc/fstab` timeshift will use that. For further information on this workaround see [Lorenzo Bettinis blog](https://www.lorenzobettini.it/2022/07/timeshift-and-grub-btrfs-in-linux-arch/).

##
### OpenRC daemon
#### Automatically update grub upon snapshot
If you would like grub-btrfs menu to automatically update when a snapshot is created or deleted, configure `/etc/conf.d/grub-btrfsd` and after that start the daemon.
###### Timeshift versions >= v22.06 (default)
This is the default setting, you don't need to change anything in `/etc/conf.d/grub-btrfsd`, keep on reading at the "How to (auto)start the daemon" heading.
Newer versions of Timeshift mount their snapshots to `/run/timeshift/$PIDofthecurrentlyrunnigtimeshift/backup/timeshift-btrfs`. The PID is changing everytime you close Timeshift and open it again. The OpenRC-Daemon can automatically take care of the detection of the correct PID and directory if you set the variable `timeshift_auto` to `true` in `/etc/conf.d/grub-btrfsd`. In this case the variable `snapshots` has no influence.
###### Timeshift versions < v22.06
If you don't want to use `timeshift_auto`, set the variable `snapshots` in the file `/etc/conf.d/grub-btrfsd` to the path of your snapshot directory. This is usually `/run/timeshift/backup/timeshift-btrfs/snapshots`. This will not work for newer Timeshift versions, however there is a [workaround](https://www.lorenzobettini.it/2022/07/timeshift-and-grub-btrfs-in-linux-arch/) that works for now. Basically you need to mount the root subvolume to `/run/timeshift/backup` by putting this line into `/etc/fstab`:
```
UUID=<UUID> /run/timeshift/backup btrfs defaults,noatime 0 0
```
You can get your UUID with the command `blkid`.

###### Snapper
For snapper set `timeshift-auto=false` and `snapshots` to Snapper snapshots directory. (usually `/.snapshots`)
##### How to (auto)start the daemon
Use `sudo rc-config add grub-btrfsd default`, to start the grub-btrfsd daemon the next time the system boots.
To start `grub-btrfsd` right now, run `sudo rc-service grub-btrfsd start`.
`grub-btrfsd` then automatically watches the snapshot directory and updates the grub-menu when a change occurs.
When you changed a setting in `/etc/conf.d/grub-btrfd`, you have to restart the daemon with `sudo rc-service grub-btrfd restart` to make the change become effective.

#### Automatically update grub upon restart/boot:
You can view your change to `systemctl cat grub-btrfsd`.
To revert change use `systemctl revert grub-btrfsd`.

#### OpenRC
Arguments are passed to grub-btrfsd via the file `/etc/conf.d/grub-btrfsd`.
The variable `optional_args` defines, which optional arguments get passed to the daemon.
Uncomment `#optional_args+="--timeshift-auto "` to pass the command line option `--timeshift-auto` to it.
After that, the daemon should be restarted with
``` bash
sudo rc-service grub-btrfsd restart
```

----
### Automatically update grub upon restart/boot:
#### Systemd
[Look at this comment](https://github.com/Antynea/grub-btrfs/issues/138#issuecomment-766918328)
Currently not implemented

#### OpenRC
If you would like the grub-btrfs menu to automatically update on system restart/ shutdown, just add the following script as `/etc/local.d/grub-btrfs-update.stop`:
```bash
#!/usr/bin/env bash
Expand Down
43 changes: 0 additions & 43 deletions grub-btrfs-openrc

This file was deleted.

12 changes: 0 additions & 12 deletions grub-btrfs.path

This file was deleted.

11 changes: 0 additions & 11 deletions grub-btrfs.service

This file was deleted.

Loading

0 comments on commit 961675c

Please sign in to comment.