-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_bootable_disk.py
More file actions
76 lines (62 loc) · 2.29 KB
/
create_bootable_disk.py
File metadata and controls
76 lines (62 loc) · 2.29 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
#!/usr/bin/env python3
import os
import subprocess
import sys
from pathlib import Path
def run_cmd(cmd, capture=False):
"""Run a shell command safely."""
try:
if capture:
return subprocess.check_output(cmd, shell=True, text=True).strip()
else:
subprocess.run(cmd, shell=True, check=True)
except subprocess.CalledProcessError as e:
print(f"❌ Command failed: {cmd}")
print(e)
sys.exit(1)
def list_disks():
"""List available disks using diskutil."""
print("📀 Available disks on this Mac:")
run_cmd("diskutil list")
print("\n⚠️ Identify your USB drive carefully (usually /dev/disk2 or /dev/disk3).")
def confirm(prompt):
"""Simple yes/no confirmation."""
choice = input(f"{prompt} [y/N]: ").strip().lower()
return choice == "y"
def main():
print("=== Bootable Linux USB Creator for macOS ===\n")
# Step 1: List disks
list_disks()
disk = input("\nEnter your USB disk identifier (e.g. disk4): ").strip()
if not disk.startswith("disk"):
print("❌ Invalid disk identifier.")
sys.exit(1)
# Step 2: Get ISO file path
iso_path = input("Enter full path to your Linux ISO file: ").strip()
iso = Path(iso_path).expanduser()
if not iso.exists():
print(f"❌ ISO file not found: {iso}")
sys.exit(1)
print(f"\n✅ ISO file: {iso}")
print(f"✅ Target USB: /dev/{disk}")
if not confirm("Are you sure you want to erase and write this USB?"):
print("❎ Operation cancelled.")
sys.exit(0)
# Step 3: Unmount disk
print("\n🔸 Unmounting disk...")
run_cmd(f"diskutil unmountDisk /dev/{disk}")
# Step 4: Write ISO using dd
print("\n⚙️ Writing ISO to USB... this may take several minutes.")
print("💡 You can press Ctrl+T in Terminal to check progress.\n")
# Use 'rdisk' for faster writes
raw_disk = f"r{disk}"
dd_cmd = f"sudo dd if='{iso}' of=/dev/{raw_disk} bs=1m"
print(f"Running: {dd_cmd}")
os.system(dd_cmd)
# Step 5: Eject disk
print("\n🔸 Ejecting disk...")
run_cmd(f"diskutil eject /dev/{disk}")
print("\n✅ Done! Your bootable USB is ready.")
print("You can now plug it into your PC and boot from it (UEFI mode recommended).")
if __name__ == "__main__":
main()