forked from toltec-dev/toltec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-lib
163 lines (146 loc) · 3.67 KB
/
install-lib
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env bash
# Copyright (c) 2020 The Toltec Contributors
# SPDX-License-Identifier: MIT
#
# install-lib
#
# Common functions used by the install scripts
#
# Check whether a systemd unit exists and is in an enabled-like state
# ("enabled", "enabled-runtime", "alias", "static", "indirect", "generated"
# or "transient")
#
# Arguments:
#
# $1 - Name of the systemd unit, e.g. "xochitl.service" or "xochitl"
#
# Exit code:
#
# 0 if the unit exists and is enabled, 1 otherwise
is-enabled() {
systemctl --quiet is-enabled "$1" 2> /dev/null
}
# Check whether a systemd unit is in an active state
# ("running")
#
# Arguments:
#
# $1 - Name of the systemd unit, e.g. "xochitl.service" or "xochitl"
#
# Exit code:
#
# 0 if the unit exists and is enabled, 1 otherwise
is-active() {
systemctl --quiet is-active "$1" 2> /dev/null
}
# Get a list of systemd units with which the given unit conflicts
#
# Arguments:
#
# $1 - Full name of the systemd unit, e.g. "xochitl.service"
#
# Output:
#
# List of conflicting units
get-conflicts() {
# Find enabled units that have a conflicting name
for name in $(systemctl cat "$1" | awk -F'=' '/^Alias=/{print $2}'); do
local realname
if realname="$(basename "$(readlink "/etc/systemd/system/$name")")"; then
echo "$realname"
fi
done
# Find units that are declared as conflicting
# (systemd automatically adds a conflict with "shutdown.target" to all
# service units see systemd.service(5), section "Automatic Dependencies")
systemctl show "$1" | awk -F'=' '/^Conflicts=/{print $2}' \
| sed 's|\bshutdown.target\b||'
}
# Print instructions about how to enable a given systemd service and disable
# the services that conflict with it
#
# Arguments:
#
# $1 - Full name of the systemd unit, e.g. "draft.service"
#
# Output:
#
# Commands to run to achieve the desired result
how-to-enable() {
for conflict in $(get-conflicts "$1"); do
if is-enabled "$conflict"; then
echo "$ systemctl disable --now ${conflict/.service/}"
fi
done
echo "$ systemctl enable --now ${1/.service/}"
}
# Reload Oxide applications if tarnish is running
#
# Output:
#
# Status message
reload-oxide-apps() {
if ! is-active tarnish.service; then
return
fi
echo -n "Reloading Oxide applications: "
if ! /opt/bin/rot apps call reload 2> /dev/null; then
echo "Failed!"
else
echo "Done!"
fi
}
# Create or update a bind mount systemd unit and enable it
#
# Arguments:
#
# $1 - Source directory
# $2 - Mount point
add-bind-mount() {
local unit_name
local unit_path
unit_name="$(systemd-escape --path "$2").mount"
unit_path="/lib/systemd/system/$unit_name"
if [[ -e $unit_path ]]; then
echo "Bind mount configuration for '$2' already exists, updating"
else
echo "Mounting '$1' over '$2'"
fi
cat > "$unit_path" << UNIT
[Unit]
Description=Bind mount $1 over $2
DefaultDependencies=no
Conflicts=umount.target
Before=local-fs.target umount.target
[Mount]
What=$1
Where=$2
Type=none
Options=bind
[Install]
WantedBy=local-fs.target
UNIT
systemctl daemon-reload
systemctl enable "$unit_name"
systemctl restart "$unit_name"
}
# Disable and remove a bind mount systemd unit
#
# Arguments:
#
# $1 - Mount point
remove-bind-mount() {
local unit_name
local unit_path
unit_name="$(systemd-escape --path "$1").mount"
unit_path="/lib/systemd/system/$unit_name"
if [[ ! -e $unit_path ]]; then
echo "No existing bind mount for '$1'"
return 1
fi
echo "Removing mount over '$1'"
systemctl disable "$unit_name"
systemctl stop "$unit_name"
rm "$unit_path"
systemctl daemon-reload
}