-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild-image.sh
More file actions
89 lines (71 loc) · 2.91 KB
/
build-image.sh
File metadata and controls
89 lines (71 loc) · 2.91 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
#!/bin/bash
BUILD_DIR="build"
KERNEL_NAME="DOSKRNL.BIN"
IMG_NAME="opendos.img"
IMG_SIZE_MB=128
MOUNT_POINT="img"
GRUB_CFG="grub.cfg"
RED='\033[31m'
GREEN='\033[32m'
BLUE='\033[34m'
PURPLE='\033[35m'
NC='\033[0m'
create_image() {
echo -e "${BLUE}=== Creating image with MBR and FAT12 partition ===${NC}"
# Check if kernel exists
if [ ! -f "$BUILD_DIR/$KERNEL_NAME" ]; then
echo -e "${RED}Error: Kernel not found at $BUILD_DIR/$KERNEL_NAME${NC}"
echo -e "${RED}Please build the kernel first!${NC}"
exit 1
fi
# Remove old image if exists
if [ -f "$IMG_NAME" ]; then
echo -n "Removing old image... "
rm -f "$IMG_NAME"
echo -e "${GREEN}OK${NC}"
fi
# Create mount point
mkdir -p "$MOUNT_POINT"
echo -n "Creating empty image file (${IMG_SIZE_MB}MB)... "
dd if=/dev/zero of="$IMG_NAME" bs=1M count=$IMG_SIZE_MB status=none || { echo -e "${RED}Error!${NC}"; exit 1; }
echo -e "${GREEN}OK${NC}"
echo -n "Creating partition table and partition... "
parted -s "$IMG_NAME" mklabel msdos
parted -s "$IMG_NAME" mkpart primary 1MiB 100%
echo "type=1" | sfdisk --part-type "$IMG_NAME" 1
echo -e "${GREEN}OK${NC}"
echo -n "Setting up loop device with partitions... "
LOOP_DEV=$(sudo losetup --show -fP "$IMG_NAME") || { echo -e "${RED}Error!${NC}"; exit 1; }
echo -e "${GREEN}OK (${LOOP_DEV})${NC}"
echo -n "Formatting partition as FAT12... "
sudo mkfs.fat -F 12 "${LOOP_DEV}p1" >/dev/null || { echo -e "${RED}Error!${NC}"; sudo losetup -d $LOOP_DEV; exit 1; }
echo -e "${GREEN}OK${NC}"
echo -n "Mounting partition... "
sudo mount "${LOOP_DEV}p1" "$MOUNT_POINT" || { echo -e "${RED}Error!${NC}"; sudo losetup -d $LOOP_DEV; exit 1; }
echo -e "${GREEN}OK${NC}"
echo -n "Creating directories and copying kernel... "
sudo mkdir -p "$MOUNT_POINT/OPENDOS" "$MOUNT_POINT/boot/grub"
sudo cp "$BUILD_DIR/$KERNEL_NAME" "$MOUNT_POINT/OPENDOS/$KERNEL_NAME"
echo -e "${GREEN}OK${NC}"
echo -n "Creating grub.cfg... "
sudo tee "$MOUNT_POINT/boot/grub/$GRUB_CFG" >/dev/null << EOF
set timeout=5
set default=0
menuentry "OpenDOS Kernel" {
multiboot /OPENDOS/$KERNEL_NAME
boot
}
EOF
echo -e "${GREEN}OK${NC}"
echo -n "Installing GRUB in MBR... "
sudo grub-install --target=i386-pc --boot-directory="$MOUNT_POINT/boot" --modules="multiboot" --recheck --force "$LOOP_DEV" >/dev/null 2>&1 || { echo -e "${RED}Error!${NC}"; sudo umount "$MOUNT_POINT"; sudo losetup -d $LOOP_DEV; exit 1; }
echo -e "${GREEN}OK${NC}"
echo -n "Unmounting partition... "
sudo umount "$MOUNT_POINT" || { echo -e "${RED}Error!${NC}"; sudo losetup -d $LOOP_DEV; exit 1; }
echo -e "${GREEN}OK${NC}"
echo -n "Detaching loop device... "
sudo losetup -d "$LOOP_DEV" || { echo -e "${RED}Error!${NC}"; exit 1; }
echo -e "${GREEN}OK${NC}"
}
create_image
echo -e "${PURPLE}=== Image $IMG_NAME ready! ===${NC}"