-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimg2dsk_gui.py
More file actions
275 lines (226 loc) · 10 KB
/
img2dsk_gui.py
File metadata and controls
275 lines (226 loc) · 10 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
img2dsk_gui.py
Extract a SteamOS superimage into a normal
directory tree — including rootfs, /var, and /home.
Usage:
sudo ./img2dsk_gui.py
Features:
• Pick the superimage and an output folder
• One-click “Extract” (rootfs + var + home)
• Live log + progress bar
• Automatic loop-device attach/detach and mount cleanup
• Handles nested ext image files and squashfs rootfs
Requirements (Debian/Ubuntu):
sudo apt install util-linux rsync squashfs-tools file python3-tk
"""
import os
import sys
import atexit
import shutil
import tempfile
import threading
import subprocess
from pathlib import Path
from tkinter import Tk, StringVar, filedialog, messagebox, N, S, E, W
from tkinter.ttk import Frame, Label, Entry, Button, Progressbar, Separator
# ---------------- helpers ----------------
def run(cmd, check=True, capture=True):
return subprocess.run(cmd, check=check, text=True, capture_output=capture)
def ensure_dir(p: Path):
p.mkdir(parents=True, exist_ok=True)
def is_squashfs(path: Path) -> bool:
try:
out = run(["file", "-b", str(path)]).stdout.lower()
return "squashfs" in out
except Exception:
return False
def is_ext_image(path: Path) -> bool:
try:
out = run(["file", "-b", str(path)]).stdout.lower()
return any(x in out for x in ("ext2", "ext3", "ext4"))
except Exception:
return False
def mount_ro(dev_or_img: str, target: Path, fstype: str|None=None, loop: bool=False):
ensure_dir(target)
opts = "ro,loop" if loop else "ro"
cmd = ["mount", "-o", opts]
if fstype:
cmd += ["-t", fstype]
cmd += [dev_or_img, str(target)]
run(cmd)
def umount(path: Path):
subprocess.run(["umount", str(path)], check=False)
# ---------------- core logic ----------------
class Extractor:
def __init__(self, log_fn, prog_fn):
self.log = log_fn
self.set_progress = prog_fn
self.mounts = []
self.loops = []
self.work = Path(tempfile.mkdtemp(prefix="img2dsk_gui_"))
atexit.register(self.cleanup)
def cleanup(self):
# unmount in reverse
for m in reversed(self.mounts):
try: umount(m)
except Exception: pass
# detach loops in reverse
for ld in reversed(self.loops):
try: subprocess.run(["losetup", "-d", ld], check=False)
except Exception: pass
try:
shutil.rmtree(self.work, ignore_errors=True)
except Exception:
pass
def _extract_into_subdir(self, dev: str, outdir: Path, sub: str):
subdir = outdir / sub
ensure_dir(subdir)
part_mnt = self.work / f"{sub}_part"
mount_ro(dev, part_mnt)
self.mounts.append(part_mnt)
files = sorted([p for p in part_mnt.iterdir() if p.is_file()],
key=lambda x: x.stat().st_size, reverse=True)
if files and is_ext_image(files[0]):
inner = files[0]
inner_mnt = self.work / f"{sub}_inner"
mount_ro(str(inner), inner_mnt, loop=True)
self.mounts.append(inner_mnt)
self.log(f" rsync → {subdir} (from {inner.name})")
run(["rsync", "-aAXH", "--numeric-ids", f"{inner_mnt}/", f"{subdir}/"], check=True)
else:
self.log(f" rsync → {subdir} (from partition)")
run(["rsync", "-aAXH", "--numeric-ids", f"{part_mnt}/", f"{subdir}/"], check=True)
def extract_all(self, superimg: Path, outdir: Path):
self.set_progress(0); self.log(f"[+] Attaching superimage: {superimg}")
loopdev = run(["losetup", "--find", "--show", "-P", str(superimg)]).stdout.strip()
if not loopdev:
raise RuntimeError("Failed to attach loop device for superimage")
self.loops.append(loopdev)
# Partitions by known layout:
p3 = f"{loopdev}p3" # rootfs-A
p4 = f"{loopdev}p4" # var-A
p5 = f"{loopdev}p5" # home
ensure_dir(outdir)
# ----- ROOTFS -----
self.set_progress(5); self.log("[+] Extracting root filesystem…")
root_part_mnt = self.work / "root_part"
mount_ro(p3, root_part_mnt)
self.mounts.append(root_part_mnt)
preferred = ["rootfs-A.img", "rootfs.img", "rootfs.squashfs", "filesystem.squashfs", "arch.squashfs"]
inner_root = None
for name in preferred:
c = root_part_mnt / name
if c.is_file():
inner_root = c
break
if inner_root is None:
files = [p for p in root_part_mnt.iterdir() if p.is_file()]
inner_root = max(files, key=lambda x: x.stat().st_size) if files else None
if inner_root and is_squashfs(inner_root):
self.log(f" unsquashfs → {outdir} (from {inner_root.name})")
run(["unsquashfs", "-f", "-d", str(outdir), str(inner_root)], check=True)
elif inner_root and is_ext_image(inner_root):
root_inner_mnt = self.work / "root_inner"
mount_ro(str(inner_root), root_inner_mnt, loop=True)
self.mounts.append(root_inner_mnt)
self.log(f" rsync → {outdir} (from {inner_root.name})")
run(["rsync", "-aAXH", "--numeric-ids", f"{root_inner_mnt}/", f"{outdir}/"], check=True)
else:
self.log(" rsync → root (from partition)")
run(["rsync", "-aAXH", "--numeric-ids", f"{root_part_mnt}/", f"{outdir}/"], check=True)
# ----- VAR -----
self.set_progress(55); self.log("[+] Extracting /var …")
self._extract_into_subdir(p4, outdir, "var")
# ----- HOME -----
self.set_progress(80); self.log("[+] Extracting /home …")
self._extract_into_subdir(p5, outdir, "home")
self.set_progress(100); self.log(f"[✓] Done. Files extracted into: {outdir}")
self.log(" (EFI partitions ignored by design.)")
# ---------------- GUI ----------------
class App:
def __init__(self, root: Tk):
root.title("img2dsk — Superimage → Directory (rootfs + var + home)")
root.minsize(720, 260)
self.image_path = StringVar()
self.output_dir = StringVar()
self.progress = 0
frm = Frame(root, padding=12)
frm.grid(row=0, column=0, sticky=N+S+E+W)
for i in range(3): frm.columnconfigure(i, weight=1 if i==1 else 0)
r = 0
Label(frm, text="Superimage (.img):").grid(row=r, column=0, sticky=E, padx=6, pady=6)
Entry(frm, textvariable=self.image_path).grid(row=r, column=1, sticky=E+W, padx=6, pady=6)
Button(frm, text="Browse", command=self.pick_img).grid(row=r, column=2, sticky=W, padx=6, pady=6)
r += 1
Label(frm, text="Output directory:").grid(row=r, column=0, sticky=E, padx=6, pady=6)
Entry(frm, textvariable=self.output_dir).grid(row=r, column=1, sticky=E+W, padx=6, pady=6)
Button(frm, text="Browse", command=self.pick_out).grid(row=r, column=2, sticky=W, padx=6, pady=6)
r += 1
Separator(frm).grid(row=r, column=0, columnspan=3, sticky=E+W, pady=6)
r += 1
Button(frm, text="Extract", command=self.start).grid(row=r, column=0, sticky=E+W, padx=6, pady=8)
Button(frm, text="Quit", command=root.quit).grid(row=r, column=2, sticky=E+W, padx=6, pady=8)
r += 1
self.pb = Progressbar(frm, maximum=100)
self.pb.grid(row=r, column=0, columnspan=3, sticky=E+W, padx=6, pady=6)
r += 1
Label(frm, text="Log:").grid(row=r, column=0, sticky=W, padx=6)
r += 1
# Minimal rolling log using Label (simple); for long logs you might swap to a ScrolledText widget
self.log_var = StringVar(value="")
self.log_label = Label(frm, textvariable=self.log_var, anchor="w", justify="left")
self.log_label.grid(row=r, column=0, columnspan=3, sticky=E+W, padx=6)
self.root = root
self.extractor = None
self.worker = None
if os.geteuid() != 0:
messagebox.showerror("Root required", "Please run this program with sudo (root).")
# ------------- UI helpers -------------
def pick_img(self):
p = filedialog.askopenfilename(title="Select superimage (.img)",
filetypes=[("Disk images","*.img"),("All files","*.*")])
if p: self.image_path.set(p)
def pick_out(self):
p = filedialog.askdirectory(title="Select output directory")
if p: self.output_dir.set(p)
def append_log(self, line: str):
prev = self.log_var.get()
new = (prev + ("\n" if prev else "") + line)[:20000] # keep small
self.log_var.set(new)
self.root.update_idletasks()
def set_progress(self, pct: int):
self.pb['value'] = max(0, min(100, pct))
self.root.update_idletasks()
# ------------- Actions -------------
def start(self):
img = Path(self.image_path.get().strip()) if self.image_path.get().strip() else None
out = Path(self.output_dir.get().strip()) if self.output_dir.get().strip() else None
if not img or not img.exists():
messagebox.showerror("Missing image", "Please choose a valid .img file.")
return
if not out:
messagebox.showerror("Missing output", "Please choose an output directory.")
return
self.extractor = Extractor(self.append_log, self.set_progress)
self.worker = threading.Thread(target=self._do_extract, args=(img, out), daemon=True)
self.worker.start()
def _do_extract(self, img: Path, out: Path):
try:
self.extractor.extract_all(img, out)
messagebox.showinfo("Done", f"Extraction complete:\n{out}")
except subprocess.CalledProcessError as e:
err = e.stderr or e.stdout or str(e)
self.append_log(err.strip())
messagebox.showerror("Error", err)
except Exception as e:
self.append_log(str(e))
messagebox.showerror("Error", str(e))
# ---------------- main ----------------
def main():
root = Tk()
App(root)
root.mainloop()
if __name__ == "__main__":
main()