-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·346 lines (288 loc) · 9.45 KB
/
build.sh
File metadata and controls
executable file
·346 lines (288 loc) · 9.45 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env bash
# =============================================================================
# Build Script for Custom Linux Distribution
# =============================================================================
# This script creates a bootable disk image with the custom Linux distro
#
# Usage:
# ./build.sh [OPTIONS]
#
# Options:
# -s, --size SIZE Size of boot image in MB (default: 50)
# -o, --output FILE Output image filename (default: boot.img)
# -k, --kernel PATH Path to kernel bzImage (default: ./myiso/bzImage)
# -i, --initrd PATH Path to initramfs (default: ./myiso/initramfs)
# -c, --config PATH Path to syslinux config (default: ./myiso/isolinux/isolinux.cfg)
# -h, --help Show this help message
# -v, --verbose Enable verbose output
# =============================================================================
set -euo pipefail
IFS=$'\n\t'
# -----------------------------------------------------------------------------
# Configuration and Default Values
# -----------------------------------------------------------------------------
readonly SCRIPT_NAME="$(basename "$0")"
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Default configuration
IMAGE_SIZE=50
OUTPUT_IMAGE="boot.img"
KERNEL_PATH="./myiso/bzImage"
INITRAMFS_PATH="./myiso/initramfs"
SYSLINUX_CONFIG="./myiso/isolinux/isolinux.cfg"
MOUNT_POINT="mnt"
VERBOSE=false
# Colors for output
readonly RED='\033[0;31m'
readonly GREEN='\033[0;32m'
readonly YELLOW='\033[1;33m'
readonly BLUE='\033[0;34m'
readonly NC='\033[0m' # No Color
# -----------------------------------------------------------------------------
# Logging Functions
# -----------------------------------------------------------------------------
log_info() {
echo -e "${BLUE}[INFO]${NC} $*"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $*"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $*"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $*" >&2
}
log_verbose() {
if [[ "${VERBOSE}" == "true" ]]; then
echo -e "${BLUE}[VERBOSE]${NC} $*"
fi
}
# -----------------------------------------------------------------------------
# Utility Functions
# -----------------------------------------------------------------------------
show_help() {
cat << EOF
${SCRIPT_NAME} - Build bootable disk image for custom Linux distribution
USAGE:
${SCRIPT_NAME} [OPTIONS]
OPTIONS:
-s, --size SIZE Size of boot image in MB (default: ${IMAGE_SIZE})
-o, --output FILE Output image filename (default: ${OUTPUT_IMAGE})
-k, --kernel PATH Path to kernel bzImage (default: ${KERNEL_PATH})
-i, --initrd PATH Path to initramfs (default: ${INITRAMFS_PATH})
-c, --config PATH Path to syslinux config (default: ${SYSLINUX_CONFIG})
-h, --help Show this help message
-v, --verbose Enable verbose output
EXAMPLES:
# Build with default settings
${SCRIPT_NAME}
# Build with custom size
${SCRIPT_NAME} --size 100
# Build with custom output name
${SCRIPT_NAME} --output my-custom-boot.img
DESCRIPTION:
This script creates a bootable FAT-formatted disk image containing:
- Linux kernel (bzImage)
- Initial RAM filesystem (initramfs)
- Syslinux bootloader
The resulting image can be booted with QEMU or written to a USB drive.
EOF
}
# Check if required commands exist
check_dependencies() {
local deps=("dd" "mkfs.fat" "syslinux" "mount" "umount")
local missing=()
log_verbose "Checking dependencies..."
for cmd in "${deps[@]}"; do
if ! command -v "$cmd" &> /dev/null; then
missing+=("$cmd")
fi
done
if [[ ${#missing[@]} -gt 0 ]]; then
log_error "Missing required commands: ${missing[*]}"
log_error "Please install: ${missing[*]}"
return 1
fi
log_verbose "All dependencies found"
return 0
}
# Check if running as root
check_root() {
if [[ $EUID -ne 0 ]]; then
log_warning "This script may require root privileges for mounting"
log_warning "If you encounter permission errors, run with sudo"
fi
}
# Validate file exists
validate_file() {
local file="$1"
local description="$2"
if [[ ! -f "$file" ]]; then
log_error "${description} not found: ${file}"
return 1
fi
log_verbose "${description} found: ${file}"
return 0
}
# Cleanup function
cleanup() {
local exit_code=$?
log_verbose "Running cleanup..."
# Unmount if still mounted
if mountpoint -q "${MOUNT_POINT}" 2>/dev/null; then
log_info "Unmounting ${MOUNT_POINT}..."
umount "${MOUNT_POINT}" || log_warning "Failed to unmount ${MOUNT_POINT}"
fi
# Remove mount point if it exists and is empty
if [[ -d "${MOUNT_POINT}" ]]; then
rmdir "${MOUNT_POINT}" 2>/dev/null || log_verbose "Mount point not removed (may not be empty)"
fi
if [[ $exit_code -ne 0 ]]; then
log_error "Build failed with exit code: ${exit_code}"
fi
exit $exit_code
}
# -----------------------------------------------------------------------------
# Main Build Functions
# -----------------------------------------------------------------------------
# Create disk image
create_disk_image() {
log_info "Creating ${IMAGE_SIZE}MB disk image: ${OUTPUT_IMAGE}..."
if [[ -f "${OUTPUT_IMAGE}" ]]; then
log_warning "Output file already exists: ${OUTPUT_IMAGE}"
read -p "Overwrite? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
log_info "Aborted by user"
return 1
fi
rm -f "${OUTPUT_IMAGE}"
fi
dd if=/dev/zero of="${OUTPUT_IMAGE}" bs=1M count="${IMAGE_SIZE}" status=progress 2>&1 | \
grep -v "records" || true
log_success "Disk image created"
}
# Format disk image with FAT filesystem
format_disk_image() {
log_info "Formatting disk image with FAT filesystem..."
mkfs.fat "${OUTPUT_IMAGE}" > /dev/null
log_success "Disk image formatted"
}
# Install syslinux bootloader
install_bootloader() {
log_info "Installing Syslinux bootloader..."
syslinux "${OUTPUT_IMAGE}"
log_success "Bootloader installed"
}
# Mount disk image
mount_disk_image() {
log_info "Mounting disk image..."
if [[ -d "${MOUNT_POINT}" ]]; then
if mountpoint -q "${MOUNT_POINT}"; then
log_warning "Mount point already in use"
return 1
fi
else
mkdir -p "${MOUNT_POINT}"
fi
mount "${OUTPUT_IMAGE}" "${MOUNT_POINT}"
log_success "Disk image mounted at ${MOUNT_POINT}"
}
# Copy files to disk image
copy_files() {
log_info "Copying files to disk image..."
log_verbose "Copying kernel: ${KERNEL_PATH}"
cp "${KERNEL_PATH}" "${MOUNT_POINT}/bzImage"
log_verbose "Copying initramfs: ${INITRAMFS_PATH}"
cp "${INITRAMFS_PATH}" "${MOUNT_POINT}/initramfs"
log_verbose "Copying syslinux config: ${SYSLINUX_CONFIG}"
cp "${SYSLINUX_CONFIG}" "${MOUNT_POINT}/syslinux.cfg"
log_success "Files copied successfully"
}
# Unmount disk image
unmount_disk_image() {
log_info "Unmounting disk image..."
umount "${MOUNT_POINT}"
rmdir "${MOUNT_POINT}"
log_success "Disk image unmounted"
}
# -----------------------------------------------------------------------------
# Argument Parsing
# -----------------------------------------------------------------------------
parse_arguments() {
while [[ $# -gt 0 ]]; do
case $1 in
-s|--size)
IMAGE_SIZE="$2"
shift 2
;;
-o|--output)
OUTPUT_IMAGE="$2"
shift 2
;;
-k|--kernel)
KERNEL_PATH="$2"
shift 2
;;
-i|--initrd)
INITRAMFS_PATH="$2"
shift 2
;;
-c|--config)
SYSLINUX_CONFIG="$2"
shift 2
;;
-v|--verbose)
VERBOSE=true
shift
;;
-h|--help)
show_help
exit 0
;;
*)
log_error "Unknown option: $1"
show_help
exit 1
;;
esac
done
}
# -----------------------------------------------------------------------------
# Main Execution
# -----------------------------------------------------------------------------
main() {
# Set up cleanup trap
trap cleanup EXIT INT TERM
# Parse command line arguments
parse_arguments "$@"
# Print configuration
log_info "Starting build process..."
log_verbose "Configuration:"
log_verbose " Image Size: ${IMAGE_SIZE}MB"
log_verbose " Output: ${OUTPUT_IMAGE}"
log_verbose " Kernel: ${KERNEL_PATH}"
log_verbose " Initramfs: ${INITRAMFS_PATH}"
log_verbose " Syslinux Config: ${SYSLINUX_CONFIG}"
# Check dependencies
check_dependencies
check_root
# Validate input files
validate_file "${KERNEL_PATH}" "Kernel image"
validate_file "${INITRAMFS_PATH}" "Initramfs"
validate_file "${SYSLINUX_CONFIG}" "Syslinux config"
# Build process
create_disk_image
format_disk_image
install_bootloader
mount_disk_image
copy_files
unmount_disk_image
log_success "Build completed successfully!"
log_info "Boot image created: ${OUTPUT_IMAGE}"
log_info ""
log_info "To test with QEMU, run:"
log_info " qemu-system-x86_64 ${OUTPUT_IMAGE}"
}
# Run main function
main "$@"