forked from milos85vasic/Server-Factory-Qemu-Utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_automated.sh
More file actions
executable file
·294 lines (247 loc) · 8.6 KB
/
Copy pathrun_automated.sh
File metadata and controls
executable file
·294 lines (247 loc) · 8.6 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/bin/bash
# Mail Server Factory - Automated QEMU VM Installation Script
# Supports unattended installation for all major server distributions
set -e
# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
INSTALL_CONFIGS_DIR="$SCRIPT_DIR/InstallConfigs"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Logging functions
log_info() {
echo -e "${BLUE}ℹ${NC} $1"
}
log_success() {
echo -e "${GREEN}✓${NC} $1"
}
log_warning() {
echo -e "${YELLOW}⚠${NC} $1"
}
log_error() {
echo -e "${RED}✗${NC} $1" >&2
}
# Detect distribution from ISO filename
detect_distribution() {
local iso_filename="$1"
if [[ "$iso_filename" =~ [Uu]buntu ]]; then
echo "ubuntu"
elif [[ "$iso_filename" =~ [Cc]ent[Oo][Ss] ]]; then
echo "centos"
elif [[ "$iso_filename" =~ [Rr][Hh][Ee][Ll] ]]; then
echo "rhel"
elif [[ "$iso_filename" =~ [Ss][Ll][Ee][Ss]|[Ss][Ll][Ee]- ]]; then
echo "sles"
elif [[ "$iso_filename" =~ [Ff]edora ]]; then
echo "fedora"
elif [[ "$iso_filename" =~ [Dd]ebian ]]; then
echo "debian"
elif [[ "$iso_filename" =~ [Aa]lma[Ll]inux ]]; then
echo "almalinux"
elif [[ "$iso_filename" =~ [Rr]ocky ]]; then
echo "rocky"
elif [[ "$iso_filename" =~ [Oo]pen[Ss][Uu][Ss][Ee] ]]; then
echo "opensuse"
else
echo "unknown"
fi
}
# Get installation configuration file path
get_install_config() {
local distro="$1"
local config_type="$2"
case "$distro" in
"ubuntu")
echo "$INSTALL_CONFIGS_DIR/ubuntu-preseed.cfg"
;;
"centos")
echo "$INSTALL_CONFIGS_DIR/centos-kickstart.cfg"
;;
"rhel")
echo "$INSTALL_CONFIGS_DIR/rhel-kickstart.cfg"
;;
"sles")
echo "$INSTALL_CONFIGS_DIR/sles-autoyast.xml"
;;
"fedora")
echo "$INSTALL_CONFIGS_DIR/fedora-kickstart.cfg"
;;
"debian")
echo "$INSTALL_CONFIGS_DIR/debian-preseed.cfg"
;;
"almalinux")
echo "$INSTALL_CONFIGS_DIR/almalinux-kickstart.cfg"
;;
"rocky")
echo "$INSTALL_CONFIGS_DIR/rocky-kickstart.cfg"
;;
"opensuse")
echo "$INSTALL_CONFIGS_DIR/opensuse-autoyast.xml"
;;
*)
log_error "Unsupported distribution: $distro"
exit 1
;;
esac
}
# Create installation media with preseed/kickstart/autoyast
create_install_media() {
local iso_path="$1"
local config_file="$2"
local distro="$3"
local output_iso="$4"
log_info "Creating automated installation media for $distro"
if [ ! -f "$config_file" ]; then
log_error "Installation configuration file not found: $config_file"
exit 1
fi
# Create temporary directory for ISO modification
local temp_dir=$(mktemp -d)
local iso_mount="$temp_dir/iso_mount"
local iso_extract="$temp_dir/iso_extract"
mkdir -p "$iso_mount" "$iso_extract"
# Mount the original ISO
if ! sudo mount -o loop "$iso_path" "$iso_mount" 2>/dev/null; then
log_error "Failed to mount ISO: $iso_path"
rm -rf "$temp_dir"
exit 1
fi
# Copy ISO contents
cp -r "$iso_mount"/* "$iso_extract"/
# Unmount
sudo umount "$iso_mount"
# Copy installation configuration to appropriate location
case "$distro" in
"ubuntu"|"debian")
# Ubuntu/Debian use preseed.cfg
mkdir -p "$iso_extract/preseed"
cp "$config_file" "$iso_extract/preseed/mailserver.cfg"
# Modify isolinux.cfg to use preseed
if [ -f "$iso_extract/isolinux/isolinux.cfg" ]; then
sed -i 's/append/append auto=true priority=critical preseed\/file=\/cdrom\/preseed\/mailserver.cfg /' "$iso_extract/isolinux/isolinux.cfg"
fi
;;
"centos"|"rhel"|"fedora"|"almalinux"|"rocky")
# Red Hat family uses kickstart
cp "$config_file" "$iso_extract/ks.cfg"
# Modify isolinux.cfg to use kickstart
if [ -f "$iso_extract/isolinux/isolinux.cfg" ]; then
sed -i 's/append/append ks=cdrom:\/ks.cfg /' "$iso_extract/isolinux/isolinux.cfg"
fi
;;
"sles"|"opensuse")
# SUSE/openSUSE use autoyast
mkdir -p "$iso_extract/autoyast"
cp "$config_file" "$iso_extract/autoyast/mailserver.xml"
# Modify isolinux.cfg to use autoyast
if [ -f "$iso_extract/boot/x86_64/loader/isolinux.cfg" ]; then
sed -i 's/append/append autoyast=file:\/\/\/autoyast\/mailserver.xml /' "$iso_extract/boot/x86_64/loader/isolinux.cfg"
fi
;;
esac
# Create new ISO
log_info "Creating modified ISO: $output_iso"
if command -v genisoimage &> /dev/null; then
genisoimage -o "$output_iso" -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -J -R "$iso_extract"
elif command -v mkisofs &> /dev/null; then
mkisofs -o "$output_iso" -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -J -R "$iso_extract"
else
log_error "Neither genisoimage nor mkisofs found. Please install cdrtools or genisoimage."
rm -rf "$temp_dir"
exit 1
fi
# Make ISO bootable
if command -v isohybrid &> /dev/null; then
isohybrid "$output_iso"
fi
# Cleanup
rm -rf "$temp_dir"
log_success "Automated installation ISO created: $output_iso"
}
# Main automated installation function
run_automated_installation() {
local machine="$1"
local iso="$2"
log_info "Starting automated installation for: $machine"
log_info "Using ISO: $iso"
# Detect distribution
local distro=$(detect_distribution "$iso")
if [ "$distro" = "unknown" ]; then
log_warning "Could not detect distribution from ISO filename"
log_warning "Assuming Ubuntu - you may need to modify installation config"
distro="ubuntu"
fi
log_info "Detected distribution: $distro"
# Get installation configuration
local config_file=$(get_install_config "$distro")
if [ ! -f "$config_file" ]; then
log_error "Installation configuration not found: $config_file"
exit 1
fi
# Create automated installation ISO
local automated_iso="${iso%.*}_automated.iso"
create_install_media "$iso" "$config_file" "$distro" "$automated_iso"
# Run the standard QEMU installation with automated ISO
log_info "Starting QEMU with automated installation ISO"
sh run.sh "$machine" "$(basename "$automated_iso")"
log_success "Automated installation completed for $machine"
log_info "The VM should boot with Mail Server Factory installer ready"
}
# Show usage information
usage() {
cat << EOF
Mail Server Factory - Automated QEMU Installation Script
Usage: $0 <machine_name> <iso_filename>
This script creates an automated installation ISO with preseed/kickstart/autoyast
configuration for unattended OS installation, then runs the VM.
Arguments:
machine_name Name/path for the VM machine
iso_filename ISO file to use for installation
Supported Distributions:
- Ubuntu Server (preseed)
- Debian (preseed)
- CentOS/RHEL/Fedora/AlmaLinux/Rocky Linux (kickstart)
- SUSE Linux Enterprise Server/openSUSE Leap (autoyast)
Examples:
$0 /path/to/ubuntu-vm ubuntu-24.04-live-server-amd64.iso
$0 /path/to/centos-vm CentOS-Stream-x86_64-latest-boot.iso
$0 /path/to/sles-vm SLE-15-SP6-Online-x86_64-GM-Media1.iso
Requirements:
- Original ISO file must be present
- genisoimage or mkisofs for ISO modification
- sudo access for mounting ISOs
EOF
}
# Main execution
main() {
if [ $# -ne 2 ]; then
usage
exit 1
fi
local machine="$1"
local iso="$2"
# Check if ISO exists
if [ ! -f "$iso" ]; then
# Try to find in ISO location
if [ -f "iso_location.settings" ]; then
local iso_location=$(cat "iso_location.settings")
if [ -f "$iso_location/$iso" ]; then
iso="$iso_location/$iso"
else
log_error "ISO file not found: $iso"
exit 1
fi
else
log_error "ISO file not found: $iso"
exit 1
fi
fi
log_info "Mail Server Factory - Automated QEMU Installation"
echo "=================================================="
run_automated_installation "$machine" "$iso"
}
# Run main function
main "$@"