-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·246 lines (209 loc) · 7.3 KB
/
Copy pathrun.sh
File metadata and controls
executable file
·246 lines (209 loc) · 7.3 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
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
#!/command/with-contenv bashio
# shellcheck shell=bash
set -e
# Get configuration
DEVICE=$(bashio::config 'device')
AUTO_DISCOVER=$(bashio::config 'auto_discover')
CREATE_PARTITION=$(bashio::config 'create_partition')
PRIORITY=$(bashio::config 'priority' 10)
print_date() {
timestamp=$(date +'%H:%M:%S %d/%m/%Y')
echo "[$timestamp] $1"
}
check_device_exists() {
local device=$1
if [[ ! -b "$device" ]]; then
print_date "ERROR: Block device $device does not exist!"
return 1
fi
return 0
}
get_available_devices() {
# List all available block devices excluding system devices
lsblk -dpno NAME,SIZE,TYPE | grep -E "disk|part" | grep -v "/dev/loop" | grep -v "/dev/ram" | while read -r name size type; do
# Skip if device is already mounted or in use
if ! mount | grep -q "$name" && ! swapon -s | grep -q "$name"; then
echo "$name ($size, $type)"
fi
done
}
discover_swap_devices() {
print_date "Discovering available swap devices..."
# Look for existing swap partitions
local swap_partitions
swap_partitions=$(blkid -t TYPE=swap -o device 2>/dev/null || true)
if [[ -n "$swap_partitions" ]]; then
print_date "Found existing swap partitions:"
echo "$swap_partitions" | while read -r partition; do
if [[ -b "$partition" ]]; then
local size
size=$(lsblk -dnbo SIZE "$partition" 2>/dev/null || echo "unknown")
print_date " - $partition (Size: $size)"
fi
done
return 0
fi
print_date "No existing swap partitions found."
print_date "Available block devices:"
get_available_devices | while read -r device_info; do
print_date " - $device_info"
done
return 1
}
create_swap_partition() {
local device=$1
print_date "Creating swap partition on $device..."
# Check device permissions and accessibility
print_date "Checking device accessibility..."
if [[ ! -r "$device" ]]; then
print_date "ERROR: Cannot read device $device"
ls -la "$device" 2>/dev/null || print_date "Device $device not found in filesystem"
return 1
fi
if [[ ! -w "$device" ]]; then
print_date "ERROR: Cannot write to device $device"
ls -la "$device"
return 1
fi
print_date "Device $device is accessible"
# Check if device has existing partitions
local existing_parts
existing_parts=$(lsblk -rno NAME "$device" | tail -n +2 | wc -l)
if [[ "$existing_parts" -gt 0 ]]; then
print_date "WARNING: Device $device already has partitions. Skipping partition creation for safety."
print_date "Existing partitions:"
lsblk "$device"
return 1
fi
# Create a single partition that uses the entire device
print_date "Creating partition table and swap partition..."
print_date "Running: parted -s $device mklabel gpt"
if ! parted -s "$device" mklabel gpt; then
print_date "ERROR: Failed to create partition table on $device"
print_date "Checking device status:"
lsblk "$device" || true
blkid "$device" || true
return 1
fi
print_date "Running: parted -s $device mkpart primary linux-swap 0% 100%"
if ! parted -s "$device" mkpart primary linux-swap 0% 100%; then
print_date "ERROR: Failed to create partition on $device"
return 1
fi
# Wait for partition to appear
sleep 2
# Get the new partition name
local partition="${device}1"
if [[ ! -b "$partition" ]]; then
# For some devices, partition might be named differently
partition="${device}p1"
fi
if [[ ! -b "$partition" ]]; then
print_date "ERROR: Failed to create partition on $device"
print_date "Expected partition: $partition or ${device}p1"
lsblk "$device"
return 1
fi
print_date "Created partition: $partition"
# Create swap filesystem
print_date "Creating swap filesystem on $partition..."
if ! mkswap "$partition"; then
print_date "ERROR: Failed to create swap filesystem on $partition"
return 1
fi
print_date "Swap partition created successfully: $partition"
echo "$partition"
}
enable_swap() {
local device=$1
local priority=${2:-10}
print_date "Enabling swap on $device with priority $priority..."
# Check if it's already enabled
if swapon -s | grep -q "$device"; then
print_date "Swap on $device is already enabled."
return 0
fi
# Check if device has swap signature
if ! blkid -t TYPE=swap "$device" >/dev/null 2>&1; then
print_date "Device $device does not have swap signature. Creating swap filesystem..."
mkswap "$device"
fi
# Enable swap with priority
swapon -p "$priority" "$device"
if swapon -s | grep -q "$device"; then
local size
size=$(lsblk -dnbo SIZE "$device" 2>/dev/null || echo "unknown")
print_date "Swap successfully enabled on $device (Size: $size, Priority: $priority)"
return 0
else
print_date "ERROR: Failed to enable swap on $device"
return 1
fi
}
main() {
print_date "Starting HAOS Swap Enabler add-on..."
print_date "Configuration:"
print_date " Device: $DEVICE"
print_date " Auto-discover: $AUTO_DISCOVER"
print_date " Create partition: $CREATE_PARTITION"
print_date " Priority: $PRIORITY"
# Show system information for debugging
print_date "System information:"
print_date " User: $(whoami)"
print_date " Groups: $(groups)"
print_date " Capabilities: $(capsh --print 2>/dev/null | grep Current || echo 'capsh not available')"
# Show current swap status
print_date "Current swap status:"
if swapon -s | grep -v "Filename" | grep -v "^$"; then
swapon -s
else
print_date " No swap currently enabled"
fi
if [[ "$AUTO_DISCOVER" == "true" ]]; then
print_date "Auto-discovery mode enabled"
if discover_swap_devices; then
# Use the first discovered swap partition
local swap_device
swap_device=$(blkid -t TYPE=swap -o device 2>/dev/null | head -n1)
if [[ -n "$swap_device" ]]; then
print_date "Using discovered swap device: $swap_device"
enable_swap "$swap_device" "$PRIORITY"
fi
else
print_date "No swap devices discovered. Please configure manually or enable partition creation."
fi
else
# Manual device configuration
if ! check_device_exists "$DEVICE"; then
print_date "Specified device $DEVICE not found."
exit 1
fi
# Check if device is a swap partition
if blkid -t TYPE=swap "$DEVICE" >/dev/null 2>&1; then
print_date "Device $DEVICE is already a swap partition"
enable_swap "$DEVICE" "$PRIORITY"
elif [[ "$CREATE_PARTITION" == "true" ]]; then
print_date "Device $DEVICE is not a swap partition. Creating swap partition..."
local new_partition
new_partition=$(create_swap_partition "$DEVICE")
if [[ $? -eq 0 && -n "$new_partition" ]]; then
enable_swap "$new_partition" "$PRIORITY"
else
print_date "Failed to create swap partition"
exit 1
fi
else
print_date "Device $DEVICE is not a swap partition and partition creation is disabled."
print_date "Either:"
print_date " 1. Set create_partition to true to automatically create a swap partition"
print_date " 2. Manually create a swap partition on the device"
print_date " 3. Specify a device that already has a swap partition"
exit 1
fi
fi
print_date "Final swap status:"
swapon -s
print_date "HAOS Swap Enabler completed successfully."
}
# Run main function
main