-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsystem.sh
392 lines (329 loc) · 14.5 KB
/
system.sh
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/env bash
# load SSH keys with Passphrase Protected
# https://www.funtoo.org/Keychain
# http://unix.stackexchange.com/questions/90853/how-can-i-run-ssh-add-automatically-without-password-prompt
# https://www.cyberciti.biz/faq/ssh-passwordless-login-with-keychain-for-scripts/
function load_ssh_keys() {
local IdentityFiles
[[ ! -x "$(command -v keychain)" ]] && PackagesList=(keychain) && InstallSystemPackages "" "${PackagesList[@]}"
if [[ ! -x "$(command -v keychain)" ]]; then
colorEcho "${FUCHSIA}keychain${RED} is not installed!"
return 1
fi
# /usr/bin/keychain --list
# /usr/bin/keychain --clear
if [[ -s "$HOME/.ssh/config" ]]; then
IdentityFiles=$(grep 'IdentityFile' "$HOME/.ssh/config" \
| sed -e 's/IdentityFile//' -e "s/^\s*//" -e "s/\s$//" -e "s|~|$HOME|" \
| sort | uniq)
for TargetFile in "${IdentityFiles[@]}"; do
/usr/bin/keychain "${TargetFile}"
done
fi
[[ -z "$HOSTNAME" ]] && HOSTNAME=$(uname -n)
if [[ -s "$HOME/.keychain/$HOSTNAME-sh" ]]; then
source "$HOME/.keychain/$HOSTNAME-sh"
/usr/bin/keychain --list
fi
# Improve the security of keychain, need to re-enter any passphrases when log in
if ! grep -q '/usr/bin/keychain --clear' ~/.zshrc >/dev/null 2>&1; then
{
echo ''
echo '# Improve the security of keychain'
echo '# User need to re-enter any passphrases when log in'
echo '[[ -x "$(command -v keychain)" ]] && /usr/bin/keychain --clear >/dev/null 2>&1'
} >> ~/.zshrc
fi
if ! grep -q '/usr/bin/keychain --clear' ~/.bash_profile >/dev/null 2>&1; then
{
echo ''
echo '# Improve the security of keychain'
echo '# User need to re-enter any passphrases when log in'
echo '[[ -x "$(command -v keychain)" ]] && /usr/bin/keychain --clear >/dev/null 2>&1'
} >> ~/.bash_profile
fi
}
# add job to cron
function Install_cron_job() {
local cronjob=${1:-""}
local cronline
[[ -z "${cronjob}" ]] && return 0
(crontab -l 2>/dev/null || true; echo "${cronjob}") | crontab - || {
colorEcho "${RED} cron job install failed!"
return 1
}
cronline=$(crontab -l | wc -l)
colorEcho "${FUCHSIA}${cronjob} ${GREEN}installed!"
colorEcho "${YELLOW} How to delete this job:"
colorEcho "${FUCHSIA} (crontab -l 2>/dev/null | sed \"${cronline}d\") | crontab -"
}
# add systemd service
function Install_systemd_Service() {
# Usage:
# Install_systemd_Service "subconverter" "/srv/subconverter/subconverter"
local service_name=$1
local service_exec=$2
local service_user=${3:-"nobody"}
local service_workdir=${4:-""}
local filename service_file
[[ $# -lt 2 ]] && return 1
[[ -z "${service_name}" ]] && return 1
[[ -z "${service_exec}" ]] && return 1
filename=$(awk '{print $1}' <<<"${service_exec}")
[[ -z "${service_workdir}" ]] && service_workdir=$(dirname "$(readlink -f "$filename")")
[[ ! -f "$filename" ]] && colorEcho "${FUCHSIA}${filename}${RED} doesn't exist!" && return 1
[[ ! -d "${service_workdir}" ]] && colorEcho "${FUCHSIA}${service_workdir}${RED} doesn't exist!" && return 1
service_file="/etc/systemd/system/${service_name}.service"
if [[ -s "${service_file}" ]]; then
colorEcho "${FUCHSIA}${service_file}${RED} already exist!" && return 1
else
sudo tee "${service_file}" >/dev/null <<-EOF
[Unit]
Description=${service_name}
After=network.target network-online.target nss-lookup.target
[Service]
Type=simple
StandardError=journal
User=${service_user}
AmbientCapabilities=CAP_NET_BIND_SERVICE
ExecStart=${service_exec}
WorkingDirectory=${service_workdir}
ExecReload=/bin/kill -HUP \$MAINPID
Restart=on-failure
RestartSec=5s
# StandardOutput=append:/var/log/${service_name}.log
# StandardError=append:/var/log/${service_name}.log
[Install]
WantedBy=multi-user.target
EOF
fi
sudo systemctl enable "${service_name}" && sudo systemctl restart "${service_name}"
if systemctl is-enabled "${service_name}" >/dev/null 2>&1; then
colorEcho "${GREEN} systemd service ${FUCHSIA}${service_name}${GREEN} installed!"
else
colorEcho "${RED} systemd service ${FUCHSIA}${service_name}${RED} install failed!"
fi
}
# Start new screen session and logging to ~/screenlog.*
function newScreenSession() {
local SCREEN_SESSION_NAME=${1:-"default"}
local SCREEN_SESSION_LOGGING=${2:-"no"}
if [[ -x "$(command -v screen)" ]]; then
if [[ -z "$STY" && -z "$TMUX" ]]; then
mkdir -p "$HOME/.screen" && chmod 700 "$HOME/.screen" && export SCREENDIR="$HOME/.screen"
if ! grep -q "^term " "$HOME/.screenrc" 2>/dev/null; then
echo "term ${TERM}" >> "$HOME/.screenrc"
fi
if ! grep -q "^caption always " "$HOME/.screenrc" 2>/dev/null; then
tee -a "$HOME/.screenrc" >/dev/null <<-'EOF'
# https://gist.github.com/onsails/1328005/dacbc9903fcea5385bb8ee2fde4e1a367d32889c
# caption always "%?%F%{-b bc}%:%{-b bb}%?%C|%D|%M %d|%H%?%F%{+u wb}%? %L=%-Lw%45>%{+b by}%n%f* %t%{-}%+Lw%-0<"
caption always "%{=}%{+b kR}%H %{+b kY}%M %d %{+b kG}%2c %{+b kB}%?%-Lw%?%{+b kW}%n*%f %kt%?(%u)%?%{+bkB}%?%+Lw%? | %{kR} Load: %l %{kB}"
EOF
fi
# logging
if [[ "${SCREEN_SESSION_LOGGING}" == "yes" ]]; then
screen -L -Logfile "$HOME/.screen/screen_$(date '+%Y%m%d_%H%M%S').log" -xRR "${SCREEN_SESSION_NAME}"
else
screen -xRR "${SCREEN_SESSION_NAME}"
fi
fi
else
colorEcho "${FUCHSIA}screen${RED} is not installed!"
return 1
fi
}
# Start new tmux session
function newTmuxSession() {
local TMUX_SESSION_NAME=${1:-"default"}
## Logging in tmux session
# script -f "$HOME/.tmux_logs/tmux_$(date '+%Y%m%d_%H%M%S').log" >/dev/null && exit
## tmux-logging
## https://github.com/tmux-plugins/tmux-logging
# if [[ -s "$HOME/.tmux.conf.local" ]]; then
# if ! grep -q "tmux-logging" "$HOME/.tmux.conf.local" 2>/dev/null; then
# echo "set -g @plugin 'tmux-plugins/tmux-logging'" \
# | tee -a "$HOME/.tmux.conf.local" >/dev/null
# fi
# fi
if [[ "$(command -v tmux)" ]]; then
if [[ -z "$STY" && -z "$TMUX" ]]; then
if ! tmux attach -t "${TMUX_SESSION_NAME}" 2>/dev/null; then
tmux new -s "${TMUX_SESSION_NAME}"
fi
fi
else
colorEcho "${FUCHSIA}tmux${RED} is not installed!"
return 1
fi
}
# zellij: List current sessions, attach to a running session, or create a new one
function newZellijSession() {
local ZJ_SESSIONS NO_SESSIONS
if [[ "$(command -v zellij)" ]]; then
if [[ -z "${ZELLIJ}" && -z "${ZELLIJ_SESSION_NAME}" ]]; then
ZJ_SESSIONS=$(zellij list-sessions 2>/dev/null)
NO_SESSIONS=$(echo "${ZJ_SESSIONS}" | wc -l)
if [ "${NO_SESSIONS}" -ge 2 ]; then
if [[ "$(command -v fzf)" ]]; then
zellij attach "$(echo "${ZJ_SESSIONS}" | awk '{print $1}' | fzf)" options --default-mode=locked
else
zellij attach "$(echo "${ZJ_SESSIONS}" | sk)" options --default-mode=locked
fi
# zellij attach "$(echo "${ZJ_SESSIONS}" | sk)" options --default-mode=locked --disable-mouse-mode
else
zellij attach -c options --default-mode=locked
# zellij attach -c options --default-mode=locked --disable-mouse-mode
fi
fi
else
colorEcho "${FUCHSIA}zellij${RED} is not installed!"
return 1
fi
}
# zellij: List layout files saved in the default layout directory, opens the selected layout file
function newZellijLayout() {
local ZJ_LAYOUT_DIR ZJ_LAYOUT
if [[ "$(command -v zellij)" ]]; then
ZJ_LAYOUT_DIR=$(zellij setup --check | grep "LAYOUT DIR" - | grep -o '".*"' - | tr -d '"')
if [[ -d "${ZJ_LAYOUT_DIR}" ]];then
if [[ "$(command -v fzf)" ]]; then
ZJ_LAYOUT="$(fd --type file . "${ZJ_LAYOUT_DIR}" | sed 's|.*/||' | fzf || exit)"
else
ZJ_LAYOUT="$(fd --type file . "${ZJ_LAYOUT_DIR}" | sed 's|.*/||' | sk || exit)"
fi
zellij --layout "${ZJ_LAYOUT}"
fi
else
colorEcho "${FUCHSIA}zellij${RED} is not installed!"
return 1
fi
}
# Clean snapper snapshots to keep the last N snapshots
function SnapperDeleteSnapshots() {
local keep_snapshot=${1:-10}
local snapper_configs current_config
local start_id end_id
# snapper_configs=$(sudo snapper list-configs --columns=config | sed '1,/--/ d')
snapper_configs=$(sudo snapper list-configs --columns=config | sed '1,2 d')
keep_snapshot=$((keep_snapshot + 1))
while read -r current_config; do
start_id=$(sudo snapper -c "${current_config}" list | awk '{print $1}' | grep -Eo '[[:digit:]]+' | grep -wv '0' | head -n1)
end_id=$(sudo snapper -c "${current_config}" list | awk '{print $1}' | grep -Eo '[[:digit:]]+'| grep -wv '0' | tail -n "${keep_snapshot}" | head -n1)
if [[ -n "${start_id}" && -n "${end_id}" ]]; then
[[ start_id -lt end_id ]] && sudo snapper -c "${current_config}" delete --sync "${start_id}-${end_id}"
fi
done <<<"${snapper_configs}"
sudo snapper list -a
colorEcho "${BLUE}Updating ${FUCHSIA}GRUB2 configuration${BLUE}..."
sudo update-grub
}
# fix "command not found" when running via cron
function FixSystemBinPath() {
local DirList=()
local TargetDir
DirList=(
"/usr/local/sbin"
"/usr/local/bin"
"/usr/sbin"
"/usr/bin"
"/sbin"
"/bin"
)
for TargetDir in "${DirList[@]}"; do
[[ -d "${TargetDir}" && ":$PATH:" != *":${TargetDir}:"* ]] && PATH="${TargetDir}:$PATH"
done
}
# make sudo more user friendly
# remove sudo timeout, make cache global, extend timeout
function setSudoTimeout() {
sudo tee "/etc/sudoers.d/20-password-timeout-0-ppid-60min" <<-'EOF'
Defaults passwd_timeout=0
Defaults timestamp_type="global"
# sudo only once for 60 minute
Defaults timestamp_timeout=60
EOF
sudo chmod 440 "/etc/sudoers.d/20-password-timeout-0-ppid-60min"
}
# Wayland IME for WPS Office
function setWaylandIMEWPSOffice() {
[[ -f "/usr/lib/office6/wpscloudsvr" ]] && sudo chmod -x "/usr/lib/office6/wpscloudsvr"
if [[ -f "/usr/bin/wps" ]]; then
if ! grep -q "export QT_IM_MODULE=" "/usr/bin/wps"; then
sudo sed -i -e '2a export QT_IM_MODULE="fcitx"' -e '2a export XMODIFIERS="@im=fcitx"' "/usr/bin/wps"
fi
fi
if [[ -f "/usr/bin/wpp" ]]; then
if ! grep -q "export QT_IM_MODULE=" "/usr/bin/wpp"; then
sudo sed -i -e '2a export QT_IM_MODULE="fcitx"' -e '2a export XMODIFIERS="@im=fcitx"' "/usr/bin/wpp"
fi
fi
if [[ -f "/usr/bin/et" ]]; then
if ! grep -q "export QT_IM_MODULE=" "/usr/bin/et"; then
sudo sed -i -e '2a export QT_IM_MODULE="fcitx"' -e '2a export XMODIFIERS="@im=fcitx"' "/usr/bin/et"
fi
fi
}
# Wayland IME for Chrome
# [Chromium](https://wiki.archlinux.org/title/chromium)
# %U --ozone-platform-hint=auto --enable-wayland-ime --gtk-version=4
function setWaylandIMEChrome() {
if [[ -f "/usr/share/applications/google-chrome.desktop" ]]; then
if ! grep -q "\--ozone-platform-hint=auto" "/usr/share/applications/google-chrome.desktop"; then
sudo sed -i -e 's|/usr/bin/google-chrome-stable|/usr/bin/google-chrome-stable --ozone-platform-hint=auto --enable-wayland-ime|g' "/usr/share/applications/google-chrome.desktop"
fi
fi
if [[ -f "/usr/share/applications/google-chrome-beta.desktop" ]]; then
if ! grep -q "\--ozone-platform-hint=auto" "/usr/share/applications/google-chrome-beta.desktop"; then
sudo sed -i -e 's|/usr/bin/google-chrome-beta|/usr/bin/google-chrome-beta --ozone-platform-hint=auto --enable-wayland-ime|g' "/usr/share/applications/google-chrome-beta.desktop"
fi
fi
if [[ -f "/usr/share/applications/google-chrome-unstable.desktop" ]]; then
if ! grep -q "\--ozone-platform-hint=auto" "/usr/share/applications/google-chrome-unstable.desktop"; then
sudo sed -i -e 's|/usr/bin/google-chrome-unstable|/usr/bin/google-chrome-unstable --ozone-platform-hint=auto --enable-wayland-ime|g' "/usr/share/applications/google-chrome-unstable.desktop"
fi
fi
}
# Wayland IME for VSCode
# --ozone-platform-hint=auto --enable-wayland-ime
function setWaylandIMEVSCode() {
if [[ -f "/usr/share/applications/code.desktop" ]]; then
if ! grep -q "\--ozone-platform-hint=auto" "/usr/share/applications/code.desktop"; then
sudo sed -i -e 's|/usr/bin/code|/usr/bin/code --ozone-platform-hint=auto --enable-wayland-ime|g' "/usr/share/applications/code.desktop"
fi
fi
if [[ -f "/usr/share/applications/code-url-handler.desktop" ]]; then
if ! grep -q "\--ozone-platform-hint=auto" "/usr/share/applications/code-url-handler.desktop"; then
sudo sed -i -e 's|/usr/bin/code|/usr/bin/code --ozone-platform-hint=auto --enable-wayland-ime|g' "/usr/share/applications/code-url-handler.desktop"
fi
fi
}
# Mount NFTS Drive & fix read-only mount NFTS Drive
function mountReadonlyNTFSDrive() {
local NTFSDrive=$1
local NTFSMountPath NTFSDriveUUID
if [[ -z "${NTFSDrive}" ]]; then
colorEcho "${BLUE}Usage: ${FUCHSIA}mountReadonlyNTFSDrive${BLUE} Disk-Partition"
colorEcho "${BLUE}How to find disk partition:"
colorEcho " ${FUCHSIA}sudo lsblk -o PATH,PTTYPE,PARTTYPE,FSTYPE,PARTTYPENAME,UUID -e7"
colorEcho "${BLUE}eg: ${FUCHSIA}mountReadonlyNTFSDrive${YELLOW} /dev/sdc3"
return 1
fi
NTFSMountPath=$(basename "${NTFSDrive}")
[[ -z "${NTFSMountPath}" ]] && return 1
NTFSMountPath="/mnt/${NTFSMountPath}"
[[ ! -d "${NTFSMountPath}" ]] && sudo mkdir -p "${NTFSMountPath}"
sudo ntfsfix "${NTFSDrive}"
if sudo mount -t ntfs-3g "${NTFSDrive}" "${NTFSMountPath}" 2>/dev/null; then
colorEcho "${FUCHSIA}${NTFSDrive}${GREEN} successfully mounted to ${ORANGE}${NTFSMountPath}"
else
return 1
fi
# Automount
NTFSDriveUUID=$(sudo blkid | grep "${NTFSDrive}" | grep -Eo '\s+UUID="[^"]*"' | cut -d\" -f2)
echo
colorEcho "${BLUE}Auto mount ${FUCHSIA}${NTFSDrive}${BLUE} on boot using following command:"
colorEcho " ${FUCHSIA}echo \"UUID=${NTFSDriveUUID} ${NTFSMountPath} ntfs-3g defaults 0 0\" | sudo tee -a /etc/fstab"
# sudo findmnt
# sudo umount "${NTFSMountPath}"
}