プレイグラウンド、サンドボックス、使い捨てスクリプト置き場
0

Configure Feed

Select the types of activity you want to include in your feed.

create image writer

Kohei Watanabe (Jun 17, 2025, 8:06 AM +0900) 072752c5 5ceb9a6e

+64
+64
image-writer/image-writer.py
··· 1 + import glob 2 + import os 3 + import subprocess 4 + from concurrent.futures import ThreadPoolExecutor, as_completed 5 + 6 + import streamlit as st 7 + 8 + st.title("イメージ書き込みツール(bmap生成・並列・チェックサム付)") 9 + st.warning( 10 + "⚠️ 全ての対象ブロックデバイスが完全に上書きされます。慎重に実行してください。" 11 + ) 12 + 13 + image_files = glob.glob(f"{os.environ['HOME']}/Downloads/*.img") 14 + image_path = st.selectbox("使用するイメージを選択", image_files) 15 + bmap_path = image_path.replace(".img", ".bmap") 16 + 17 + try: 18 + subprocess.run( 19 + ["bmaptool", "create", "-o", bmap_path, image_path], 20 + check=True, 21 + capture_output=True, 22 + text=True, 23 + ) 24 + st.success(f"bmap生成完了: {os.path.basename(bmap_path)}") 25 + except subprocess.CalledProcessError as e: 26 + st.error(f"bmap生成失敗:\n{e.stderr}") 27 + st.stop() 28 + 29 + devices = [d for d in glob.glob("/dev/sd?") if os.path.basename(d) != "sda"] 30 + if not devices: 31 + st.error("書き込み対象のデバイスが見つかりません。") 32 + st.stop() 33 + st.write(f"検出デバイス: {devices}") 34 + 35 + 36 + def burn(dev): 37 + try: 38 + res = subprocess.run( 39 + ["sudo", "bmaptool", "copy", image_path, dev], 40 + capture_output=True, 41 + text=True, 42 + check=True, 43 + ) 44 + verified = "Checksum verified" in res.stdout 45 + return dev, True, verified, res.stdout 46 + except subprocess.CalledProcessError as e: 47 + return dev, False, False, e.stderr 48 + 49 + 50 + if st.checkbox("⚠️ 危険性を理解し、すべてのデバイスに書き込むことに同意します"): 51 + if st.button("書き込み開始"): 52 + with st.status("書き込み中...", expanded=True) as status: 53 + with ThreadPoolExecutor() as ex: 54 + futures = {ex.submit(burn, d): d for d in devices} 55 + for f in as_completed(futures): 56 + dev, ok, verified, msg = f.result() 57 + if ok: 58 + if verified: 59 + st.success(f"{dev}: OK(チェックサム検証済)") 60 + else: 61 + st.warning(f"{dev}: 書き込み成功(検証出力なし)") 62 + else: 63 + st.error(f"{dev}: 書き込み失敗\n{msg}") 64 + status.update(label="完了", state="complete")