-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgather_information.sh
255 lines (232 loc) · 8.63 KB
/
gather_information.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
#!/bin/bash
set -x
# Step 1: Get the list of disks (excluding LVM, loop, and unwanted entries)
get_disks() {
lsblk -dn -o NAME | grep -vE "loop|lvm--|local--lvm" > disk_list.txt
}
# Step 2: Get the list of partitions (excluding LVM and unwanted entries)
get_partitions() {
lsblk -ln -o NAME | grep -vE "loop|lvm--|local--lvm" > partition_list.txt
sed -i '/lvm-vm/d' partition_list.txt # Remove LVM virtual machine partitions
sed -i '/pve-root/d' partition_list.txt # Remove Proxmox root partitions
sed -i '/pve-swap/d' partition_list.txt # Remove Proxmox swap partitions
}
# Step 3: Map disks to partitions and generate the file
map_disks_to_partitions() {
> mapped_disks_and_partitions.txt # Clear or create the file
while IFS= read -r disk; do
echo "$disk:" >> mapped_disks_and_partitions.txt
# Capture the partitions and check if any exist directly under this disk
partitions=$(grep "^$disk" partition_list.txt | grep -v "^$disk$")
if [ -n "$partitions" ]; then
# If partitions are found, write them under the disk
echo "$partitions" | while read -r partition; do
echo " - $partition" >> mapped_disks_and_partitions.txt
done
else
# No partitions found, add the disk itself as a partition
echo " - $disk" >> mapped_disks_and_partitions.txt
fi
done < disk_list.txt
}
# Function to extract information for disks
extract_info_disk() {
local device=$1
local key=$2
case $key in
# 1
"uuid")
blkid -s PTUUID -o value "/dev/$device" 2>/dev/null || echo ""
;;
# 2
"fs_type")
echo "" # No fs_type for entire disks
;;
# 3
"mount_path")
echo "" # Disks generally do not have mount paths
;;
# 4
"size" | "total_size")
lsblk -no SIZE "/dev/$device" | head -n 1
;;
# 5
"id_serial")
udevadm info --query=property --name="/dev/$device" | grep "ID_SERIAL_SHORT=" | cut -d '=' -f2 | head -n 1
;;
# 6
"id_model")
udevadm info --query=property --name="/dev/$device" | grep "ID_MODEL=" | cut -d '=' -f2 | head -n 1
;;
# 7
"pcie_path")
udevadm info --query=path --name="/dev/$device" | head -n 1
;;
# 8
"devpath")
echo "/dev/$device"
;;
# 9
"diskseq")
udevadm info --query=property --name="/dev/$device" | grep "DISKSEQ=" | cut -d '=' -f2 | head -n 1
;;
# 10
"used" | "percentage_used")
df -h "/dev/$device" 2>/dev/null | awk 'NR==2 {print $3}'
;;
# 11
"is_usb")
udevadm info --query=property --name="/dev/$device" | grep -q "ID_BUS=usb" && echo true || echo false
;;
*)
echo "Unknown key: $key"
;;
esac
}
# Function to extract information for partitions
extract_info_partition() {
local device=$1
local key=$2
case $key in
# 1
"uuid")
blkid -s UUID -o value "/dev/$device" | head -n 1
;;
# 2
"fs_type")
blkid -s TYPE -o value "/dev/$device" | head -n 1
;;
# 3
"mount_path")
mount | grep "/dev/$device" | awk '{print $3}' | head -n 1
;;
# 4
"size")
lsblk -no SIZE "/dev/$device" | head -n 1
;;
# 5
"id_serial")
udevadm info --query=property --name="/dev/$device" | grep "ID_SERIAL_SHORT=" | cut -d '=' -f2 | head -n 1
;;
# 6
"id_model")
udevadm info --query=property --name="/dev/$device" | grep "ID_MODEL=" | cut -d '=' -f2 | head -n 1
;;
# 7
"pcie_path")
udevadm info --query=path --name="/dev/$device" | head -n 1
;;
# 8
"devpath")
echo "/dev/$device"
;;
# 9
"diskseq")
udevadm info --query=property --name="/dev/$device" | grep "DISKSEQ=" | cut -d '=' -f2 | head -n 1
;;
*)
echo "Unknown key: $key"
;;
esac
}
# Function to generate script file for each disk
generate_variables() {
local disk_name="$1"
shift
local partitions=("$@")
# Create the script file name
local file_name="${disk_counter}_${disk_name}.sh"
# Write the variables to the script file
{
# Disk-level information
# 1
echo "disk_number_${disk_counter}=${disk_counter}_${disk_name}"
# 2
echo "disk_name_${disk_counter}=$disk_name"
# 3
echo "disk_size_${disk_counter}=$(extract_info_disk "$disk_name" "size")"
# 4
echo "disk_type_${disk_counter}=$(extract_info_disk "$disk_name" "fs_type")"
# 5
echo "is_usb_${disk_counter}=$(extract_info_disk "$disk_name" "is_usb")"
# 6
echo "total_size_${disk_counter}=$(extract_info_disk "$disk_name" "total_size")"
# 7
echo "used_${disk_counter}=$(extract_info_disk "$disk_name" "used")"
# 8
echo "percentage_used_${disk_counter}=$(extract_info_disk "$disk_name" "percentage_used")"
# 9
echo "uuid_${disk_counter}=$(extract_info_disk "$disk_name" "uuid")"
# 10
echo "mount_path_${disk_counter}=""
# 11
echo "mount_status_${disk_counter}=unmounted"
# 12
echo "id_serial_${disk_counter}=$(extract_info_disk "$disk_name" "id_serial")"
# 13
echo "id_model_${disk_counter}=$(extract_info_disk "$disk_name" "id_model")"
# 14
echo "pcie_path_${disk_counter}=$(extract_info_disk "$disk_name" "pcie_path")"
# 15
echo "devpath_${disk_counter}=/dev/$disk_name"
# 16
echo "diskseq_${disk_counter}=$(extract_info_disk "$disk_name" "diskseq")"
# Partition-level information
partition_counter=1
for partition in "${partitions[@]}"; do
# 17
echo "partition_name_${disk_counter}_${partition_counter}=$partition"
# 18
echo "uuid_${disk_counter}_${partition_counter}=$(extract_info_partition "$partition" "uuid")"
# 19
echo "part_size_${disk_counter}_${partition_counter}=$(extract_info_partition "$partition" "size")"
# 20
echo "fs_type_${disk_counter}_${partition_counter}=$(extract_info_partition "$partition" "fs_type")"
mount_path="$(extract_info_partition "$partition" "mount_path")"
if [ -n "$mount_path" ]; then
# 21
echo "mount_path_${disk_counter}_${partition_counter}=$mount_path"
# 22
echo "mount_status_${disk_counter}_${partition_counter}=mounted"
else
# 23
echo "mount_path_${disk_counter}_${partition_counter}=""
# 24
echo "mount_status_${disk_counter}_${partition_counter}=unmounted"
fi
# 25
echo "id_serial_${disk_counter}_${partition_counter}=$(extract_info_partition "$partition" "id_serial")"
# 26
echo "id_model_${disk_counter}_${partition_counter}=$(extract_info_partition "$partition" "id_model")"
# 27
echo "pcie_path_${disk_counter}_${partition_counter}=$(extract_info_partition "$partition" "pcie_path")"
# 28
echo "devpath_${disk_counter}_${partition_counter}=/dev/$partition"
# 29
echo "diskseq_${disk_counter}_${partition_counter}=$(extract_info_partition "$partition" "diskseq")"
partition_counter=$((partition_counter + 1))
done
} > "$file_name"
mkdir -p disk_command_substitutes
mv "$file_name" disk_command_substitutes/
disk_counter=$((disk_counter + 1))
}
# Main execution
get_disks
get_partitions
map_disks_to_partitions
# Identify disks with no partitions and mark them
disks_without_partitions=$(awk -F: '$2 ~ /^ *- *$/ {print $1}' mapped_disks_and_partitions.txt)
for disk in $disks_without_partitions; do
echo "$disk" >> disks_without_partitions.txt
done
generate_from_mapping() {
disk_counter=1
while IFS= read -r disk; do
partitions=($(grep -E "^ - " mapped_disks_and_partitions.txt | grep "$disk" | cut -d'-' -f2 | tr -d ' '))
generate_variables "$disk" "${partitions[@]}"
done < disk_list.txt
}
generate_from_mapping
echo "Command substitutes have been generated and saved in the 'disk_command_substitutes' directory."
cd disk_command_substitutes