-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathflash_usb.py
More file actions
588 lines (474 loc) · 21.7 KB
/
Copy pathflash_usb.py
File metadata and controls
588 lines (474 loc) · 21.7 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#!/usr/bin/env python3
"""
Flash Solana Cold Wallet image to USB drive
Usage:
sudo python3 flash_usb.py # Interactive mode
sudo python3 flash_usb.py /dev/sdX # Flash to specific device
sudo python3 flash_usb.py --build # Build ISO then flash
sudo python3 flash_usb.py --build-only # Only build ISO, don't flash
B - Love U 3000
"""
import subprocess
import sys
import os
import platform
from pathlib import Path
try:
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
console = Console()
HAS_RICH = True
except ImportError:
HAS_RICH = False
class Console:
def print(self, *args, **kwargs):
text = str(args[0]) if args else ""
text = text.replace("[bold]", "").replace("[/bold]", "")
text = text.replace("[green]", "").replace("[/green]", "")
text = text.replace("[red]", "").replace("[/red]", "")
text = text.replace("[yellow]", "").replace("[/yellow]", "")
text = text.replace("[cyan]", "").replace("[/cyan]", "")
print(text)
console = Console()
def print_banner():
banner = """
╔═══════════════════════════════════════════════════════════╗
║ SOLANA COLD WALLET - USB FLASH TOOL ║
╠═══════════════════════════════════════════════════════════╣
║ Flash the cold wallet image to a USB drive ║
║ WARNING: This will erase ALL data on the target device! ║
╚═══════════════════════════════════════════════════════════╝
"""
console.print(banner)
def check_root():
# On macOS, diskutil can work without root for some operations
if platform.system() == 'Darwin':
# Just warn but don't exit
if os.geteuid() != 0:
console.print("[yellow]Note: Running without root. Some operations may require sudo.[/yellow]")
return
# On Linux, require root
if os.geteuid() != 0:
console.print("[red]ERROR: This script must be run as root (sudo)[/red]")
console.print("Usage: sudo python3 flash_usb.py")
sys.exit(1)
def find_image() -> Path:
"""Find the cold wallet image file"""
search_paths = [
Path("./output/solana-cold-wallet.img"),
Path("./output/solana-cold-wallet.tar.gz"),
Path("./solana-cold-wallet.img"),
Path("./solana-cold-wallet.tar.gz"),
]
for path in search_paths:
if path.exists():
return path
return None
def list_usb_devices() -> list:
"""List available USB block devices"""
devices = []
# macOS support using diskutil
if platform.system() == 'Darwin':
try:
result = subprocess.run(
['diskutil', 'list'],
capture_output=True,
text=True
)
if result.returncode != 0:
console.print("[yellow]Warning: Could not list devices with diskutil[/yellow]")
return devices
# Parse diskutil output
lines = result.stdout.split('\n')
current_disk = None
for line in lines:
# Look for disk identifiers
if '/dev/disk' in line and '(external' in line:
parts = line.split()
for part in parts:
if part.startswith('/dev/disk'):
disk_name = part.rstrip(':')
# Get detailed info
info_result = subprocess.run(
['diskutil', 'info', disk_name],
capture_output=True,
text=True,
timeout=5
)
if info_result.returncode == 0:
size = "Unknown"
model = "USB Device"
for info_line in info_result.stdout.split('\n'):
if 'Disk Size:' in info_line:
# Extract size (e.g., "32.0 GB")
parts = info_line.split()
for i, p in enumerate(parts):
if 'GB' in p or 'MB' in p:
if i > 0:
size = f"{parts[i-1]} {p}"
break
elif 'Device / Media Name:' in info_line:
model = info_line.split(':', 1)[1].strip()
devices.append({
'name': disk_name.replace('/dev/', ''),
'path': disk_name,
'size': size,
'model': model
})
break
return devices
except Exception as e:
console.print(f"[yellow]Warning: Could not list devices: {e}[/yellow]")
return devices
# Linux support using lsblk
try:
result = subprocess.run(
['lsblk', '-d', '-o', 'NAME,SIZE,TYPE,TRAN,MODEL', '-n'],
capture_output=True,
text=True
)
for line in result.stdout.strip().split('\n'):
parts = line.split()
if len(parts) >= 4:
name, size, dtype, tran = parts[0], parts[1], parts[2], parts[3]
model = ' '.join(parts[4:]) if len(parts) > 4 else 'Unknown'
if tran == 'usb' and dtype == 'disk':
devices.append({
'name': name,
'path': f'/dev/{name}',
'size': size,
'model': model
})
except Exception as e:
console.print(f"[yellow]Warning: Could not list devices: {e}[/yellow]")
return devices
def select_device(devices: list) -> str:
"""Let user select a USB device"""
if not devices:
console.print("[red]No USB devices found![/red]")
console.print("Please insert a USB drive and try again.")
sys.exit(1)
console.print("\n[bold]Available USB Devices:[/bold]\n")
if HAS_RICH:
table = Table()
table.add_column("#", style="cyan")
table.add_column("Device", style="green")
table.add_column("Size", style="yellow")
table.add_column("Model", style="white")
for i, dev in enumerate(devices, 1):
table.add_row(str(i), dev['path'], dev['size'], dev['model'])
console.print(table)
else:
for i, dev in enumerate(devices, 1):
print(f" {i}. {dev['path']} - {dev['size']} - {dev['model']}")
console.print()
while True:
try:
choice = input("Select device number (or 'q' to quit): ").strip()
if choice.lower() == 'q':
sys.exit(0)
idx = int(choice) - 1
if 0 <= idx < len(devices):
return devices[idx]['path']
else:
console.print("[red]Invalid selection[/red]")
except ValueError:
console.print("[red]Please enter a number[/red]")
def unmount_all_partitions(device: str) -> bool:
"""Unmount all partitions on a device"""
is_macos = platform.system() == 'Darwin'
is_windows = platform.system() == 'Windows'
if is_windows:
console.print("[yellow]Windows disk unmounting not implemented[/yellow]")
return True
try:
if is_macos:
# Use diskutil to unmount all partitions
console.print(f"Unmounting all partitions on {device}...")
result = subprocess.run(
['diskutil', 'unmountDisk', 'force', device],
capture_output=True,
text=True,
timeout=30
)
if result.returncode == 0:
console.print("[green]✓ All partitions unmounted[/green]")
return True
else:
console.print(f"[yellow]Warning: {result.stderr.strip()}[/yellow]")
# Try to continue anyway
return True
else:
# Linux: Find and unmount all partitions
console.print(f"Unmounting partitions on {device}...")
# Get list of mounted partitions for this device
result = subprocess.run(
['lsblk', '-ln', '-o', 'NAME,MOUNTPOINT', device],
capture_output=True,
text=True,
timeout=10
)
unmounted = 0
if result.returncode == 0:
for line in result.stdout.strip().split('\n'):
parts = line.split()
if len(parts) >= 2: # Has a mount point
partition = f"/dev/{parts[0]}"
mount_point = parts[1]
try:
umount_result = subprocess.run(
['umount', partition],
capture_output=True,
text=True,
timeout=10
)
if umount_result.returncode == 0:
console.print(f"[green]✓ Unmounted {partition}[/green]")
unmounted += 1
except Exception as e:
console.print(f"[yellow]Warning: Could not unmount {partition}: {e}[/yellow]")
if unmounted > 0:
console.print(f"[green]✓ Unmounted {unmounted} partition(s)[/green]")
return True
except subprocess.TimeoutExpired:
console.print("[yellow]Warning: Unmount operation timed out[/yellow]")
return False
except Exception as e:
console.print(f"[yellow]Warning: Error during unmount: {e}[/yellow]")
return False
def wipe_disk_signatures(device: str) -> bool:
"""Wipe disk signatures and partition tables"""
is_macos = platform.system() == 'Darwin'
is_windows = platform.system() == 'Windows'
if is_windows or is_macos:
# Skip signature wiping on Windows and macOS (dd will overwrite anyway)
return True
try:
console.print(f"Wiping partition signatures on {device}...")
# Use wipefs to remove signatures (safer than dd)
result = subprocess.run(
['wipefs', '--all', '--force', device],
capture_output=True,
text=True,
timeout=30
)
if result.returncode == 0:
console.print("[green]✓ Disk signatures wiped[/green]")
return True
else:
# wipefs might not be available, try dd as fallback
console.print("[yellow]Trying alternative method...[/yellow]")
dd_result = subprocess.run(
['dd', 'if=/dev/zero', f'of={device}', 'bs=512', 'count=1'],
capture_output=True,
text=True,
timeout=10
)
if dd_result.returncode == 0:
console.print("[green]✓ Partition table cleared[/green]")
return True
console.print("[yellow]Warning: Could not wipe signatures[/yellow]")
return False
except FileNotFoundError:
# wipefs not found, skip
return True
except Exception as e:
console.print(f"[yellow]Warning: {e}[/yellow]")
return False
def check_for_keypair(device: str) -> bool:
"""Check if a keypair.json exists on the USB drive (macOS only)"""
is_macos = platform.system() == 'Darwin'
if not is_macos:
return False
try:
# Mount the device temporarily to check for keypair
mount_result = subprocess.run(
['diskutil', 'mount', device],
capture_output=True,
text=True,
timeout=10
)
if mount_result.returncode == 0:
# Get mount point
info_result = subprocess.run(
['diskutil', 'info', device],
capture_output=True,
text=True,
timeout=5
)
if info_result.returncode == 0:
for line in info_result.stdout.split('\n'):
if 'Mount Point:' in line:
mount_point = line.split(':')[1].strip()
if mount_point and mount_point != 'Not applicable':
# Check for keypair
keypair_path = Path(mount_point) / "wallet" / "keypair.json"
has_keypair = keypair_path.exists()
# Unmount after checking
subprocess.run(
['diskutil', 'unmount', mount_point],
capture_output=True,
timeout=5
)
return has_keypair
return False
except Exception:
return False
def confirm_flash(device: str, image: Path) -> bool:
"""Confirm the flash operation"""
console.print(f"\n[bold][red]WARNING: DESTRUCTIVE OPERATION[/red][/bold]")
console.print(f"\nImage: {image}")
console.print(f"Target: {device}")
console.print(f"\n[yellow]ALL DATA ON {device} WILL BE PERMANENTLY ERASED![/yellow]")
console.print()
confirm = input("Continue? [y/N]: ").strip().lower()
return confirm == 'y'
def flash_image(device: str, image: Path) -> bool:
"""Flash the image to the USB device"""
console.print(f"\n[bold]Flashing {image.name} to {device}...[/bold]")
console.print("This may take several minutes.\n")
is_macos = platform.system() == 'Darwin'
is_linux = platform.system() == 'Linux'
try:
# Step 1: Unmount all partitions
console.print("[bold]Step 1: Preparing disk...[/bold]")
if not unmount_all_partitions(device):
console.print("[yellow]Warning: Some partitions may still be mounted[/yellow]")
console.print("[yellow]Attempting to continue...[/yellow]")
# Step 2: Wipe disk signatures (Linux only)
if is_linux:
console.print("\n[bold]Step 2: Clearing partition table...[/bold]")
wipe_disk_signatures(device)
console.print("\n[bold]Step 3: Writing image...[/bold]")
if str(image).endswith('.img') or str(image).endswith('.iso'):
# For macOS, use rdisk for faster writes
target_device = device
if is_macos and 'disk' in device:
target_device = device.replace('/dev/disk', '/dev/rdisk')
console.print(f"[cyan]Using raw disk device for faster writes: {target_device}[/cyan]")
cmd = ['dd', f'if={image}', f'of={target_device}', 'bs=4m' if is_macos else 'bs=4M']
# Add status reporting
if not is_macos:
cmd.extend(['status=progress', 'oflag=sync'])
console.print(f"Writing image to {target_device}...")
console.print("[yellow]This may take 5-15 minutes depending on USB speed...[/yellow]")
# Run dd with error output capture
result = subprocess.run(cmd, capture_output=True, text=True, timeout=1800)
if result.returncode != 0:
console.print(f"[red]Error during write:[/red]")
console.print(f"[red]{result.stderr}[/red]")
return False
# Show dd statistics if available
if result.stderr:
console.print("[cyan]Write statistics:[/cyan]")
for line in result.stderr.split('\n'):
if 'bytes' in line or 'copied' in line:
console.print(f"[cyan]{line}[/cyan]")
else:
console.print("Formatting USB as ext4...")
subprocess.run(['mkfs.ext4', '-F', device], timeout=60)
mount_point = f"/tmp/usb_flash_{os.getpid()}"
os.makedirs(mount_point, exist_ok=True)
subprocess.run(['mount', device, mount_point], timeout=30)
console.print("Extracting filesystem...")
result = subprocess.run(
['tar', '-xzf', str(image), '-C', mount_point],
timeout=300
)
subprocess.run(['sync'])
subprocess.run(['umount', mount_point], timeout=30)
if result.returncode == 0:
console.print("\n[bold][green]SUCCESS! USB cold wallet created![/green][/bold]")
return True
return False
# Step 4: Sync to ensure all data is written
console.print("\n[bold]Step 4: Finalizing...[/bold]")
console.print("Syncing data to disk...")
sync_result = subprocess.run(['sync'], timeout=60)
console.print("[green]✓ Sync complete[/green]")
# On macOS, verify installation and eject the disk
if is_macos:
console.print("\n[bold]Step 5: Verifying installation...[/bold]")
# Check if installation was successful by looking for keypair
has_keypair = check_for_keypair(device)
if has_keypair:
console.print("[green]✓ Wallet keypair detected - installation successful![/green]")
console.print("\nEjecting disk...")
eject_result = subprocess.run(
['diskutil', 'eject', device],
capture_output=True,
text=True,
timeout=30
)
if eject_result.returncode == 0:
console.print("[green]✓ Disk ejected safely[/green]")
else:
# Only show warning if keypair wasn't detected (installation failed)
if not has_keypair:
console.print(f"[yellow]Warning: Could not eject disk: {eject_result.stderr}[/yellow]")
else:
# Installation succeeded, suppress eject warning
console.print("[green]✓ Installation complete (disk may require manual ejection)[/green]")
console.print("\n[bold][green]✓ SUCCESS! USB cold wallet created![/green][/bold]")
console.print("\nNext steps:")
console.print("1. Remove the USB drive safely")
console.print("2. Boot an air-gapped computer from this USB")
console.print("3. The wallet will be generated on first boot")
return True
except subprocess.TimeoutExpired:
console.print("\n[red]Operation timed out[/red]")
return False
except Exception as e:
console.print(f"\n[red]Error: {e}[/red]")
return False
def build_iso() -> Path:
"""Build the cold wallet ISO"""
console.print("\n[bold]Building cold wallet ISO...[/bold]\n")
try:
from src.iso_builder import ISOBuilder
builder = ISOBuilder()
image_path = builder.build_complete_iso("./output")
if image_path and image_path.exists():
return image_path
else:
console.print("[red]Failed to build ISO[/red]")
sys.exit(1)
except ImportError as e:
console.print(f"[red]Import error: {e}[/red]")
console.print("Make sure you're running from the project directory")
sys.exit(1)
def main():
print_banner()
build_only = '--build-only' in sys.argv
do_build = '--build' in sys.argv or build_only
args = [a for a in sys.argv[1:] if not a.startswith('--')]
if do_build:
image = build_iso()
if build_only:
console.print(f"\n[green]Image created: {image}[/green]")
console.print("\nTo flash to USB, run:")
console.print(f" sudo python3 flash_usb.py {image}")
sys.exit(0)
else:
image = find_image()
if not image:
console.print("[yellow]No cold wallet image found.[/yellow]")
console.print("Building new image...\n")
image = build_iso()
check_root()
if args:
device = args[0]
if not device.startswith('/dev/'):
console.print(f"[red]Invalid device path: {device}[/red]")
sys.exit(1)
else:
devices = list_usb_devices()
device = select_device(devices)
if not confirm_flash(device, image):
console.print("\n[yellow]Flash cancelled[/yellow]")
sys.exit(0)
success = flash_image(device, image)
sys.exit(0 if success else 1)
if __name__ == "__main__":
main()