-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbioctl_linux.sh
executable file
·277 lines (236 loc) · 7.58 KB
/
bioctl_linux.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
#!/bin/bash
# bioctl.sh - RAID and Disk Management Script
# Author: Umair Khurshid
# Requirements: mdadm, cryptsetup
## ----------------------------------------------------------------------------------------- ##
## ----------------------------------------------------------------------------------------- ##
# Check for root privileges
if [ "$(id -u)" -ne 0 ]; then
echo "You must be root to run this script."
exit 1
fi
# Define log file
LOG_FILE="/var/log/bioctl_linux.log"
# Function to log messages
log_message() {
local log_level="$1"
local message="$2"
echo "$(date '+%Y-%m-%d %H:%M:%S') [$log_level] $message" >> "$LOG_FILE"
}
# Function to handle errors
handle_error() {
local message="$1"
log_message "ERROR" "$message"
echo "Error: $message"
echo "Correct Usage:"
echo -e "You need to specify an operation (e.g., create, add, remove, status, etc.) followed by the necessary options. For example:\n"
echo -e "\033[1;32mTo create a RAID array:\033[0m"
echo -e " \033[1m\033[1;36msudo ./bioctl_linux.sh create /dev/md0 1 2 /dev/sda /dev/sdb\033[0m"
echo -e " \033[3mThis command will create a RAID array named /dev/md0 with RAID level 1, using two disks /dev/sda and /dev/sdb.\033[0m\n"
echo -e "\033[1;32mTo add a disk to an existing RAID array:\033[0m"
echo -e " \033[1m\033[1;36msudo ./bioctl_linux.sh add /dev/md0 /dev/sdc\033[0m"
echo -e " \033[3mThis will add /dev/sdc to the RAID array /dev/md0.\033[0m\n"
echo -e "\033[1;32mTo check the status of a RAID array:\033[0m"
echo -e " \033[1m\033[1;36msudo ./bioctl_linux.sh status /dev/md0\033[0m\n"
echo -e "\033[1;32mTo encrypt a disk:\033[0m"
echo -e " \033[1m\033[1;36msudo ./bioctl_linux.sh encrypt /dev/sda\033[0m\n"
echo -e "\033[1;32mTo decrypt an encrypted disk:\033[0m"
echo -e " \033[1m\033[1;36msudo ./bioctl_linux.sh decrypt /dev/sda\033[0m\n"
exit 1
}
# Show usage information
usage() {
echo "Usage: $0 {create|add|remove|status|encrypt|decrypt|repair|key-management} [options]"
echo "Examples:"
echo " $0 create /dev/md0 1 2 /dev/sda /dev/sdb"
echo " $0 add /dev/md0 /dev/sdc"
echo " $0 remove /dev/md0 /dev/sdb"
echo " $0 status /dev/md0"
echo " $0 encrypt /dev/sda"
echo " $0 decrypt encrypted_disk"
echo " $0 repair"
echo " $0 key-management /dev/sda add"
exit 1
}
# Ensure at least one argument is provided
if [ -z "$1" ]; then
handle_error "No arguments provided."
usage
fi
# Check required commands
command -v mdadm >/dev/null || handle_error "mdadm command not found. Install it and retry."
command -v cryptsetup >/dev/null || handle_error "cryptsetup command not found. Install it and retry."
# Create a new RAID array
create_raid() {
if [ $# -lt 4 ]; then
handle_error "Missing arguments for create."
fi
local raid_device="$1"
local raid_level="$2"
local raid_disks="$3"
shift 3
local devices="$@"
if [ -e "$raid_device" ]; then
handle_error "RAID device $raid_device already exists!"
fi
echo "Creating RAID array..."
log_message "INFO" "Creating RAID array $raid_device with level $raid_level and devices $devices"
for device in $devices; do
if [ ! -b "$device" ]; then
handle_error "Device $device does not exist!"
fi
done
mdadm --create "$raid_device" \
--level="$raid_level" \
--raid-devices="$raid_disks" \
$devices
if [ $? -eq 0 ]; then
log_message "INFO" "RAID array $raid_device created successfully."
else
handle_error "Failed to create RAID array $raid_device."
fi
}
# Add a disk to the RAID array
add_disk() {
if [ $# -lt 2 ]; then
handle_error "Missing arguments for add."
fi
local raid_device="$1"
local new_disk="$2"
if [ ! -b "$new_disk" ]; then
handle_error "Disk $new_disk does not exist!"
fi
echo "Adding disk to RAID array..."
log_message "INFO" "Adding disk $new_disk to RAID array $raid_device"
mdadm --add "$raid_device" "$new_disk"
if [ $? -eq 0 ]; then
log_message "INFO" "Disk $new_disk added to RAID array $raid_device successfully."
else
handle_error "Failed to add disk $new_disk to RAID array $raid_device."
fi
}
# Remove a disk from the RAID array
remove_disk() {
if [ $# -lt 2 ]; then
handle_error "Missing arguments for remove."
fi
local raid_device="$1"
local disk_to_remove="$2"
echo "Removing disk from RAID array..."
log_message "INFO" "Removing disk $disk_to_remove from RAID array $raid_device"
mdadm --remove "$raid_device" "$disk_to_remove"
if [ $? -eq 0 ]; then
log_message "INFO" "Disk $disk_to_remove removed from RAID array $raid_device successfully."
else
handle_error "Failed to remove disk $disk_to_remove from RAID array $raid_device."
fi
}
# Check the status of the RAID array
status_raid() {
if [ $# -lt 1 ]; then
handle_error "Missing RAID device for status."
fi
local raid_device="$1"
echo "Checking RAID status..."
log_message "INFO" "Checking status of RAID array $raid_device"
mdadm --detail "$raid_device"
if [ $? -ne 0 ]; then
handle_error "Failed to check RAID status for $raid_device."
fi
}
# Encrypt a disk using LUKS
encrypt_disk() {
if [ $# -lt 1 ]; then
handle_error "Missing disk for encryption."
fi
local disk="$1"
if cryptsetup isLuks "$disk"; then
handle_error "Disk $disk is already encrypted!"
fi
echo "Encrypting disk..."
log_message "INFO" "Encrypting disk $disk with LUKS"
cryptsetup luksFormat "$disk"
if [ $? -eq 0 ]; then
log_message "INFO" "Disk $disk encrypted successfully."
else
handle_error "Failed to encrypt disk $disk."
fi
}
# Open an encrypted disk
decrypt_disk() {
if [ $# -lt 1 ]; then
handle_error "Missing encrypted disk name."
fi
local disk_name="$1"
echo "Decrypting disk..."
log_message "INFO" "Decrypting disk $disk_name"
cryptsetup luksClose "$disk_name"
if [ $? -eq 0 ]; then
log_message "INFO" "Disk $disk_name decrypted successfully."
else
handle_error "Failed to decrypt disk $disk_name."
fi
}
# Repair RAID array
repair_raid() {
echo "Repairing RAID array..."
log_message "INFO" "Repairing RAID array."
mdadm --assemble --scan
if [ $? -ne 0 ]; then
handle_error "Failed to repair RAID array."
fi
}
# Key Management for LUKS
key_management() {
if [ $# -lt 2 ]; then
handle_error "Missing arguments for key management."
fi
local disk="$1"
local operation="$2"
case "$operation" in
add)
cryptsetup luksAddKey "$disk"
;;
remove)
cryptsetup luksRemoveKey "$disk"
;;
*)
handle_error "Unknown key management operation: $operation"
;;
esac
if [ $? -eq 0 ]; then
log_message "INFO" "Encryption key for disk $disk managed successfully."
else
handle_error "Failed to manage encryption key for disk $disk."
fi
}
# Main function to parse arguments
case "$1" in
create)
create_raid "$2" "$3" "$4" "${@:5}"
;;
add)
add_disk "$2" "$3"
;;
remove)
remove_disk "$2" "$3"
;;
status)
status_raid "$2"
;;
encrypt)
encrypt_disk "$2"
;;
decrypt)
decrypt_disk "$2"
;;
repair)
repair_raid
;;
key-management)
key_management "$2" "$3"
;;
*)
usage
;;
esac