-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisk-info.sh
More file actions
74 lines (61 loc) · 2.82 KB
/
disk-info.sh
File metadata and controls
74 lines (61 loc) · 2.82 KB
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
# StateCraft
# A CLI tool to create complex directory structures via scripts on Linux.
#
# StateCraft is a CLI tool for creating Linux directory trees via scripts.
# It supports mounting snapshots, creating files, archives, and more.
# Designed for admins seeking flexible, scriptable backup setups.
#
# Copyright (C) 2025 Daniel Rudolf <https://www.daniel-rudolf.de>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# License: GNU General Public License <https://opensource.org/license/gpl-3-0>
# SPDX-License-Identifier: GPL-3.0-only
[ -x "$(type -p jq)" ] || { echo "Missing dependency for 'disk-info.sh' state script: jq" >&2; exit 1; }
[ -x "$(type -p lsblk)" ] || { echo "Missing dependency for 'disk-info.sh' state script: lsblk" >&2; exit 1; }
[ -x "$(type -p findmnt)" ] || { echo "Missing dependency for 'disk-info.sh' state script: findmnt" >&2; exit 1; }
_disk_info() {
local DISKS=()
local ID= MOUNT= DISK= DISK_META= FS_META= MOUNT_META=
for ID in "${PATHS[@]}"; do
MOUNT="$(unescape_path "$ID")"
# skip paths that are no mount points (e.g. the 'disk-info.sh' state script)
DISK="$(mountinfo "$MOUNT" source ||:)"
[ -n "$DISK" ] || continue
DISK_META="$(cmd lsblk --json --bytes --nodeps -o PATH,SIZE,PARTUUID,PARTTYPE,PARTLABEL "$DISK")"
FS_META="$(cmd findmnt --json --bytes -o FSTYPE,SIZE,USED,UUID,LABEL "$DISK")"
MOUNT_META="$(cmd findmnt --json -o TARGET,OPTIONS "$MOUNT")"
DISKS+=( "$(jq -ne \
--argjson partition "$(jq '.blockdevices[0]' <<< "$DISK_META")" \
--argjson filesystem "$(jq '.filesystems[0]' <<< "$FS_META")" \
--argjson mountpoint "$(jq '.filesystems[0]' <<< "$MOUNT_META")" \
'$ARGS.named')" )
done
jq -s <<< "${DISKS[@]}"
}
_setup_disk_info_file() {
local ID="$1"
# create status file
local FILENAME="$(unescape_path "$ID")"
check_path "$TARGET_DIR$FILENAME" "Invalid path ${ID@Q}: Invalid disk information file" +e
quiet "Write disk information to ${FILENAME@Q}"
mkmountpoint "$TARGET_DIR" "$ID" "$(dirname "$FILENAME")"
_disk_info > "$TARGET_DIR$FILENAME"
# delete status file
trap_exit rm "$TARGET_DIR$FILENAME"
trap_exit quiet "Delete ${FILENAME@Q}"
return 0
}
setup_path() {
_setup_disk_info_file "$@"
}