Skip to content

Commit 1df10b8

Browse files
authored
Merge pull request #14896 from Security-Onion-Solutions/vlb2
fix hyper bridge setup. simplify cpu/mem regex
2 parents ebfb670 + 9d96a11 commit 1df10b8

File tree

3 files changed

+162
-79
lines changed

3 files changed

+162
-79
lines changed

salt/hypervisor/tools/sbin/so-nvme-raid1.sh

Lines changed: 152 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# - Detects and reports existing RAID configurations
1313
# - Thoroughly cleans target drives of any existing data/configurations
1414
# - Creates GPT partition tables with RAID-type partitions
15-
# - Establishes RAID-1 array (/dev/md0) for data redundancy
15+
# - Establishes RAID-1 array (${RAID_DEVICE}) for data redundancy
1616
# - Formats the array with XFS filesystem for performance
1717
# - Automatically mounts at /nsm and configures for boot persistence
1818
# - Provides monitoring information for resync operations
@@ -30,13 +30,18 @@
3030
#
3131
# WARNING: This script will DESTROY all data on the target drives!
3232
#
33-
# USAGE: sudo ./raid_setup.sh
33+
# USAGE: sudo ./so-nvme-raid1.sh
3434
#
3535
#################################################################
3636

3737
# Exit on any error
3838
set -e
3939

40+
# Configuration variables
41+
RAID_ARRAY_NAME="md0"
42+
RAID_DEVICE="/dev/${RAID_ARRAY_NAME}"
43+
MOUNT_POINT="/nsm"
44+
4045
# Function to log messages
4146
log() {
4247
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
@@ -50,50 +55,159 @@ check_root() {
5055
fi
5156
}
5257

58+
# Function to find MD arrays using specific devices
59+
find_md_arrays_using_devices() {
60+
local target_devices=("$@")
61+
local found_arrays=()
62+
63+
# Parse /proc/mdstat to find arrays using our target devices
64+
if [ -f "/proc/mdstat" ]; then
65+
while IFS= read -r line; do
66+
if [[ $line =~ ^(md[0-9]+) ]]; then
67+
local array_name="${BASH_REMATCH[1]}"
68+
local array_path="/dev/$array_name"
69+
70+
# Check if this array uses any of our target devices
71+
for device in "${target_devices[@]}"; do
72+
if echo "$line" | grep -q "${device##*/}"; then
73+
found_arrays+=("$array_path")
74+
break
75+
fi
76+
done
77+
fi
78+
done < /proc/mdstat
79+
fi
80+
81+
printf '%s\n' "${found_arrays[@]}"
82+
}
83+
5384
# Function to check if RAID is already set up
5485
check_existing_raid() {
55-
if [ -e "/dev/md0" ]; then
56-
if mdadm --detail /dev/md0 &>/dev/null; then
57-
local raid_state=$(mdadm --detail /dev/md0 | grep "State" | awk '{print $3}')
58-
local mount_point="/nsm"
59-
60-
log "Found existing RAID array /dev/md0 (State: $raid_state)"
61-
62-
if mountpoint -q "$mount_point"; then
63-
log "RAID is already mounted at $mount_point"
64-
log "Current RAID details:"
65-
mdadm --detail /dev/md0
86+
local target_devices=("/dev/nvme0n1p1" "/dev/nvme1n1p1")
87+
local found_arrays=($(find_md_arrays_using_devices "${target_devices[@]}"))
88+
89+
# Check if we found any arrays using our target devices
90+
if [ ${#found_arrays[@]} -gt 0 ]; then
91+
for array_path in "${found_arrays[@]}"; do
92+
if mdadm --detail "$array_path" &>/dev/null; then
93+
local raid_state=$(mdadm --detail "$array_path" | grep "State" | awk '{print $3}')
94+
local mount_point="/nsm"
6695

67-
# Check if resyncing
68-
if grep -q "resync" /proc/mdstat; then
69-
log "RAID is currently resyncing:"
70-
grep resync /proc/mdstat
71-
log "You can monitor progress with: watch -n 60 cat /proc/mdstat"
72-
else
73-
log "RAID is fully synced and operational"
74-
fi
96+
log "Found existing RAID array $array_path (State: $raid_state)"
7597

76-
# Show disk usage
77-
log "Current disk usage:"
78-
df -h "$mount_point"
98+
# Check what's currently mounted at /nsm
99+
local current_mount=$(findmnt -n -o SOURCE "$mount_point" 2>/dev/null || echo "")
79100

80-
exit 0
101+
if [ -n "$current_mount" ]; then
102+
if [ "$current_mount" = "$array_path" ]; then
103+
log "RAID array $array_path is already correctly mounted at $mount_point"
104+
log "Current RAID details:"
105+
mdadm --detail "$array_path"
106+
107+
# Check if resyncing
108+
if grep -q "resync" /proc/mdstat; then
109+
log "RAID is currently resyncing:"
110+
grep resync /proc/mdstat
111+
log "You can monitor progress with: watch -n 60 cat /proc/mdstat"
112+
else
113+
log "RAID is fully synced and operational"
114+
fi
115+
116+
# Show disk usage
117+
log "Current disk usage:"
118+
df -h "$mount_point"
119+
120+
exit 0
121+
else
122+
log "Found $mount_point mounted on $current_mount, but RAID array $array_path exists"
123+
log "Will unmount current filesystem and remount on RAID array"
124+
125+
# Unmount current filesystem
126+
log "Unmounting $mount_point"
127+
umount "$mount_point"
128+
129+
# Remove old fstab entry
130+
log "Removing old fstab entry for $current_mount"
131+
sed -i "\|$current_mount|d" /etc/fstab
132+
133+
# Mount the RAID array
134+
log "Mounting RAID array $array_path at $mount_point"
135+
mount "$array_path" "$mount_point"
136+
137+
# Update fstab
138+
log "Updating fstab for RAID array"
139+
sed -i "\|${array_path}|d" /etc/fstab
140+
echo "${array_path} ${mount_point} xfs defaults,nofail 0 0" >> /etc/fstab
141+
142+
log "RAID array is now mounted at $mount_point"
143+
log "Current RAID details:"
144+
mdadm --detail "$array_path"
145+
146+
# Check if resyncing
147+
if grep -q "resync" /proc/mdstat; then
148+
log "RAID is currently resyncing:"
149+
grep resync /proc/mdstat
150+
log "You can monitor progress with: watch -n 60 cat /proc/mdstat"
151+
else
152+
log "RAID is fully synced and operational"
153+
fi
154+
155+
# Show disk usage
156+
log "Current disk usage:"
157+
df -h "$mount_point"
158+
159+
exit 0
160+
fi
161+
else
162+
# /nsm not mounted, mount the RAID array
163+
log "Mounting RAID array $array_path at $mount_point"
164+
mount "$array_path" "$mount_point"
165+
166+
# Update fstab
167+
log "Updating fstab for RAID array"
168+
sed -i "\|${array_path}|d" /etc/fstab
169+
echo "${array_path} ${mount_point} xfs defaults,nofail 0 0" >> /etc/fstab
170+
171+
log "RAID array is now mounted at $mount_point"
172+
log "Current RAID details:"
173+
mdadm --detail "$array_path"
174+
175+
# Show disk usage
176+
log "Current disk usage:"
177+
df -h "$mount_point"
178+
179+
exit 0
180+
fi
81181
fi
82-
fi
182+
done
83183
fi
84184

85185
# Check if any of the target devices are in use
86-
for device in "/dev/nvme0n1" "/dev/nvme1n1"; do
87-
if lsblk -o NAME,MOUNTPOINT "$device" | grep -q "nsm"; then
88-
log "Error: $device is already mounted at /nsm"
89-
exit 1
90-
fi
91-
186+
for device in "/dev/nvme0n1" "/dev/nvme1n1"; do
92187
if mdadm --examine "$device" &>/dev/null || mdadm --examine "${device}p1" &>/dev/null; then
188+
# Find the actual array name for this device
189+
local device_arrays=($(find_md_arrays_using_devices "${device}p1"))
190+
local array_name=""
191+
192+
if [ ${#device_arrays[@]} -gt 0 ]; then
193+
array_name="${device_arrays[0]}"
194+
else
195+
# Fallback: try to find array name from /proc/mdstat
196+
local partition_name="${device##*/}p1"
197+
array_name=$(grep -l "$partition_name" /proc/mdstat 2>/dev/null | head -1)
198+
if [ -n "$array_name" ]; then
199+
array_name=$(grep "^md[0-9]" /proc/mdstat | grep "$partition_name" | awk '{print "/dev/" $1}' | head -1)
200+
fi
201+
# Final fallback
202+
if [ -z "$array_name" ]; then
203+
array_name="$RAID_DEVICE"
204+
fi
205+
fi
206+
93207
log "Error: $device appears to be part of an existing RAID array"
94208
log "To reuse this device, you must first:"
95209
log "1. Unmount any filesystems"
96-
log "2. Stop the RAID array: mdadm --stop /dev/md0"
210+
log "2. Stop the RAID array: mdadm --stop $array_name"
97211
log "3. Zero the superblock: mdadm --zero-superblock ${device}p1"
98212
exit 1
99213
fi
@@ -183,20 +297,20 @@ main() {
183297
fi
184298

185299
log "Creating RAID array"
186-
mdadm --create /dev/md0 --level=1 --raid-devices=2 \
300+
mdadm --create "$RAID_DEVICE" --level=1 --raid-devices=2 \
187301
--metadata=1.2 \
188302
/dev/nvme0n1p1 /dev/nvme1n1p1 \
189303
--force --run
190304

191305
log "Creating XFS filesystem"
192-
mkfs.xfs -f /dev/md0
306+
mkfs.xfs -f "$RAID_DEVICE"
193307

194308
log "Creating mount point"
195309
mkdir -p /nsm
196310

197311
log "Updating fstab"
198-
sed -i '/\/dev\/md0/d' /etc/fstab
199-
echo "/dev/md0 /nsm xfs defaults,nofail 0 0" >> /etc/fstab
312+
sed -i "\|${RAID_DEVICE}|d" /etc/fstab
313+
echo "${RAID_DEVICE} ${MOUNT_POINT} xfs defaults,nofail 0 0" >> /etc/fstab
200314

201315
log "Reloading systemd daemon"
202316
systemctl daemon-reload
@@ -209,7 +323,7 @@ main() {
209323

210324
log "RAID setup complete"
211325
log "RAID array details:"
212-
mdadm --detail /dev/md0
326+
mdadm --detail "$RAID_DEVICE"
213327

214328
if grep -q "resync" /proc/mdstat; then
215329
log "RAID is currently resyncing. You can monitor progress with:"

salt/soc/dyanno/hypervisor/soc_hypervisor.yaml.jinja

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -70,41 +70,7 @@ Base domain has not been initialized.
7070
{%- endmacro -%}
7171

7272
{%- macro update_resource_field(field, free_value, total_value, unit_label) -%}
73-
{%- set resource_regex = '' -%}
74-
{%- if free_value < 10 -%}
75-
{%- set resource_regex = '^[1-' ~ free_value ~ ']$' -%}
76-
{%- elif free_value < 100 -%}
77-
{%- set tens_digit = free_value // 10 -%}
78-
{%- set ones_digit = free_value % 10 -%}
79-
{%- if ones_digit == 0 -%}
80-
{%- set resource_regex = '^([1-9]|[1-' ~ (tens_digit-1) ~ '][0-9]|' ~ tens_digit ~ '0)$' -%}
81-
{%- else -%}
82-
{%- set resource_regex = '^([1-9]|[1-' ~ (tens_digit-1) ~ '][0-9]|' ~ tens_digit ~ '[0-' ~ ones_digit ~ '])$' -%}
83-
{%- endif -%}
84-
{%- elif free_value < 1000 -%}
85-
{%- set hundreds_digit = free_value // 100 -%}
86-
{%- set tens_digit = (free_value % 100) // 10 -%}
87-
{%- set ones_digit = free_value % 10 -%}
88-
{%- if hundreds_digit == 1 -%}
89-
{%- if tens_digit == 0 and ones_digit == 0 -%}
90-
{%- set resource_regex = '^([1-9]|[1-9][0-9]|100)$' -%}
91-
{%- elif tens_digit == 0 -%}
92-
{%- set resource_regex = '^([1-9]|[1-9][0-9]|10[0-' ~ ones_digit ~ '])$' -%}
93-
{%- elif ones_digit == 0 -%}
94-
{%- set resource_regex = '^([1-9]|[1-9][0-9]|10[0-9]|1[1-' ~ tens_digit ~ ']0)$' -%}
95-
{%- else -%}
96-
{%- set resource_regex = '^([1-9]|[1-9][0-9]|10[0-9]|1[1-' ~ (tens_digit-1) ~ '][0-9]|1' ~ tens_digit ~ '[0-' ~ ones_digit ~ '])$' -%}
97-
{%- endif -%}
98-
{%- else -%}
99-
{%- if tens_digit == 0 and ones_digit == 0 -%}
100-
{%- set resource_regex = '^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-' ~ (hundreds_digit-1) ~ '][0-9][0-9]|' ~ hundreds_digit ~ '00)$' -%}
101-
{%- elif ones_digit == 0 -%}
102-
{%- set resource_regex = '^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-' ~ (hundreds_digit-1) ~ '][0-9][0-9]|' ~ hundreds_digit ~ '[0-' ~ tens_digit ~ ']0)$' -%}
103-
{%- else -%}
104-
{%- set resource_regex = '^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-' ~ (hundreds_digit-1) ~ '][0-9][0-9]|' ~ hundreds_digit ~ '[0-' ~ (tens_digit-1) ~ '][0-9]|' ~ hundreds_digit ~ tens_digit ~ '[0-' ~ ones_digit ~ '])$' -%}
105-
{%- endif -%}
106-
{%- endif -%}
107-
{%- endif -%}
73+
{%- set resource_regex = '^[0-9]{1,3}$' -%}
10874
{%- do field.update({
10975
'label': field.label | replace('FREE', free_value | string) | replace('TOTAL', total_value | string),
11076
'regex': resource_regex,

setup/so-functions

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,12 +1187,15 @@ get_minion_type() {
11871187
}
11881188

11891189
hypervisor_local_states() {
1190-
# these states need to run before the first highstate so that we dont deal with the salt-minion restarting
1191-
# and we need these setup prior to the highstate
1192-
if [ $is_hypervisor ] || [ $is_managerhype ]; then
1193-
salt-call state.apply libvirt.64962 --local --file-root=../salt/ -l info
1194-
salt-call state.apply libvirt.bridge --local --file-root=../salt/ -l info pillar='{"host": {"mainint": "enp1s0"}}'
1195-
fi
1190+
# these states need to run before the first highstate so that we dont deal with the salt-minion restarting
1191+
# and we need these setup prior to the highstate
1192+
info "Check if hypervisor or managerhype"
1193+
if [ $is_hypervisor ] || [ $is_managerhype ]; then
1194+
info "Running libvirt states for hypervisor"
1195+
logCmd "salt-call state.apply libvirt.64962 --local --file-root=../salt/ -l info"
1196+
info "Setting up bridge for $MNIC"
1197+
salt-call state.apply libvirt.bridge --local --file-root=../salt/ -l info pillar="{\"host\": {\"mainint\": \"$MNIC\"}}"
1198+
fi
11961199
}
11971200

11981201
install_cleanup() {

0 commit comments

Comments
 (0)