-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_disk.py
More file actions
39 lines (33 loc) · 1.23 KB
/
create_disk.py
File metadata and controls
39 lines (33 loc) · 1.23 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
import os
import subprocess
def create_fat32_disk(image_path="disk.img", size_gb=8):
# Delete existing image if present
if os.path.exists(image_path):
os.remove(image_path)
print(f"Removed disk image: {image_path}")
else:
print(f"File {image_path} does not exist.")
# Step 1: Create a raw disk image with dd
print("Creating raw disk image...")
image_size = size_gb * 1025 * 1024 * 1024 # 1GB in bytes
with open(image_path, "wb") as f:
f.truncate(image_size)
# Step 2: Attach the raw image
print("Attaching disk image...")
attach_info = subprocess.check_output(
["hdiutil", "attach", "-nomount", image_path]
).decode().strip()
disk_device = attach_info.split()[0] # e.g., /dev/disk4
try:
# Step 3: Partition the disk with MBR and FAT32
print("Partitioning...")
subprocess.run(
["diskutil", "partitionDisk", disk_device, "1", "MBR", "MS-DOS", "LUNASYS", "100%"],
check=True
)
finally:
# Step 4: Detach the disk image
subprocess.run(["hdiutil", "detach", disk_device], check=True)
print(f"FAT32 disk image created at {image_path}")
if __name__ == "__main__":
create_fat32_disk()