VAIBHAV862008 commited on
Commit
fb62ce3
·
verified ·
1 Parent(s): a787677

Upload build_iso.py

Browse files
Files changed (1) hide show
  1. build_iso.py +106 -0
build_iso.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # scripts/build_iso.py - Build Bootable VAIBHAV OS ISO using GRUB 2 and pycdlib
2
+ import os
3
+ import sys
4
+ import subprocess
5
+ import shutil
6
+ import pycdlib
7
+
8
+ def main():
9
+ root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
10
+ kernel_elf = os.path.join(root_dir, "target", "i686-unknown-linux-gnu", "debug", "vaibhav_os")
11
+ grub_cfg = os.path.join(root_dir, "iso", "boot", "grub", "grub.cfg")
12
+ output_iso = os.path.join(root_dir, "vaibhavos.iso")
13
+
14
+ # 1. Verify kernel binary exists
15
+ if not os.path.exists(kernel_elf):
16
+ print(f"Kernel binary not found at {kernel_elf}. Building now...")
17
+ cargo_bin = r"C:\Users\madiv\.cargo\bin\cargo.exe"
18
+ subprocess.check_call([cargo_bin, "build"], cwd=root_dir)
19
+
20
+ if not os.path.exists(kernel_elf):
21
+ print("[ERROR] Failed to find or compile kernel binary!")
22
+ sys.exit(1)
23
+
24
+ print(f"[OK] Found kernel binary: {kernel_elf} ({os.path.getsize(kernel_elf)} bytes)")
25
+
26
+ # 2. Locate GRUB 2.06 files
27
+ grub_base = r"C:\Users\madiv\grub"
28
+ grub_mkimage = None
29
+ i386_pc_dir = None
30
+
31
+ for root, dirs, files in os.walk(grub_base):
32
+ if "grub-mkimage.exe" in files:
33
+ grub_mkimage = os.path.join(root, "grub-mkimage.exe")
34
+ if "i386-pc" in dirs:
35
+ i386_pc_dir = os.path.join(root, "i386-pc")
36
+
37
+ if not grub_mkimage or not i386_pc_dir:
38
+ print("[ERROR] Could not find grub-mkimage.exe or i386-pc directory in " + grub_base)
39
+ sys.exit(1)
40
+
41
+ print(f"[OK] Found grub-mkimage: {grub_mkimage}")
42
+ print(f"[OK] Found i386-pc modules: {i386_pc_dir}")
43
+
44
+ # 3. Build core.img with required modules for booting from ISO CD-ROM
45
+ temp_dir = os.path.join(root_dir, "target", "iso_build")
46
+ os.makedirs(temp_dir, exist_ok=True)
47
+ core_img = os.path.join(temp_dir, "core.img")
48
+ cdboot_img = os.path.join(i386_pc_dir, "cdboot.img")
49
+ eltorito_img = os.path.join(temp_dir, "eltorito.img")
50
+
51
+ print("Generating GRUB core.img with biosdisk, iso9660, and multiboot modules...")
52
+ cmd = [
53
+ grub_mkimage,
54
+ "-d", i386_pc_dir,
55
+ "-O", "i386-pc",
56
+ "-o", core_img,
57
+ "-p", "/boot/grub",
58
+ "biosdisk", "iso9660", "multiboot", "normal"
59
+ ]
60
+ subprocess.check_call(cmd)
61
+
62
+ # 4. Concatenate cdboot.img + core.img -> eltorito.img
63
+ print("Creating El Torito bootable disk image (cdboot.img + core.img)...")
64
+ with open(cdboot_img, "rb") as f_cdboot, open(core_img, "rb") as f_core, open(eltorito_img, "wb") as f_out:
65
+ f_out.write(f_cdboot.read())
66
+ f_out.write(f_core.read())
67
+
68
+ print(f"[OK] Generated eltorito.img: {os.path.getsize(eltorito_img)} bytes")
69
+
70
+ # 5. Build ISO-9660 image using pycdlib with Rock Ridge and El Torito boot info table
71
+ print("Authoring ISO-9660 image with pycdlib...")
72
+ iso = pycdlib.PyCdlib()
73
+ iso.new(interchange_level=3,
74
+ vol_ident="VAIBHAV_OS",
75
+ rock_ridge="1.09",
76
+ eltorito_boot_catalog="/BOOT/GRUB/BOOT.CAT;1")
77
+
78
+ # Add directories
79
+ iso.add_directory("/BOOT", rr_name="boot")
80
+ iso.add_directory("/BOOT/GRUB", rr_name="grub")
81
+
82
+ # Add kernel binary
83
+ iso.add_file(kernel_elf, "/BOOT/VAIBHAV_OS;1", rr_name="vaibhav_os")
84
+
85
+ # Add grub.cfg
86
+ iso.add_file(grub_cfg, "/BOOT/GRUB/GRUB.CFG;1", rr_name="grub.cfg")
87
+
88
+ # Add eltorito boot image
89
+ iso.add_file(eltorito_img, "/BOOT/GRUB/ELTORITO.IMG;1", rr_name="eltorito.img")
90
+
91
+ # Add El Torito boot record with boot info table patched automatically by pycdlib
92
+ iso.add_eltorito("/BOOT/GRUB/ELTORITO.IMG;1", boot_info_table=True, media_name="noemul")
93
+
94
+ # Write output ISO
95
+ if os.path.exists(output_iso):
96
+ os.remove(output_iso)
97
+
98
+ iso.write(output_iso)
99
+ iso.close()
100
+
101
+ print(f"\n[SUCCESS] Successfully generated bootable ISO:")
102
+ print(f" Path: {output_iso}")
103
+ print(f" Size: {os.path.getsize(output_iso)} bytes")
104
+
105
+ if __name__ == "__main__":
106
+ main()