Skip to content

Commit 0907287

Browse files
committed
Extract sysroot OK (too heavy for now...)
1 parent f9d1878 commit 0907287

4 files changed

Lines changed: 69 additions & 11 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11

22
.DS_Store
3+
extract-sysroot-raspberrypi/input
4+
extract-sysroot-raspberrypi/output
5+
extract-sysroot-raspberrypi/sysroot

extract-sysroot-raspberrypi/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ WORKDIR /work
99
#############################################################
1010
RUN apt-get update && apt-get install -y --no-install-recommends \
1111
ca-certificates curl xz-utils \
12-
util-linux kpartx e2fsprogs dosfstools mount rsync coreutils file \
12+
util-linux kpartx e2fsprogs dosfstools mount rsync coreutils file parted \
1313
&& rm -rf /var/lib/apt/lists/*
1414

1515
# I/O folders

extract-sysroot-raspberrypi/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ docker run --rm -it --privileged \
2525
```
2626

2727
Parameters are :
28+
2829
- `IMAGE_URL` : Image url from ie Raspberry Pi OS official website (.img or .img.xz)
2930
- `IMAGE` : Path to a locale image (to not re-download image)
3031
- `IMAGE_SHA256` : (optional) expected downloaded archive checksum (file or value)

extract-sysroot-raspberrypi/extract_sysroot.sh

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ else
7777
exit 1
7878
fi
7979

80-
# --- Montage partitions & extraction ---
8180
echo "[*] Attache l'image en loop et scanne les partitions…"
8281
LOOPDEV="$(losetup -f --show -P "${IMG_PATH}")"
8382
cleanup_loop() {
8483
sync || true
8584
umount /mnt/boot 2>/dev/null || true
8685
umount /mnt/root 2>/dev/null || true
86+
# démonte les mappings device-mapper si créés
8787
kpartx -d "${LOOPDEV}" 2>/dev/null || true
8888
losetup -d "${LOOPDEV}" 2>/dev/null || true
8989
}
@@ -92,19 +92,73 @@ trap 'cleanup_loop; rm -rf "${TMP_DIR}"' EXIT
9292
echo "[*] Loop device: ${LOOPDEV}"
9393
lsblk -o NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT "${LOOPDEV}" || true
9494

95-
# Hypothèse standard Raspberry Pi OS : p1 = boot (FAT), p2 = root (ext4)
96-
BOOT_PART="${LOOPDEV}p1"
97-
ROOT_PART="${LOOPDEV}p2"
95+
# --- Détermine les devices de partition de manière robuste ---
96+
BOOT_PART=""
97+
ROOT_PART=""
9898

99-
# Validation basique des partitions
100-
if ! lsblk -no FSTYPE "${ROOT_PART}" | grep -q 'ext'; then
101-
echo "Attention: ${ROOT_PART} n'est pas ext*. Vérifie le partitionnement."
99+
# 1) Essayer directement /dev/loopXp1/p2 (losetup -P)
100+
if [[ -b "${LOOPDEV}p1" && -b "${LOOPDEV}p2" ]]; then
101+
BOOT_PART="${LOOPDEV}p1"
102+
ROOT_PART="${LOOPDEV}p2"
103+
else
104+
# 2) Essayer via kpartx -> /dev/mapper/loopXp1/p2
105+
echo "[*] Création des mappages de partition via kpartx…"
106+
# -s: silencieux ; -a: add ; certains environnements requièrent -v pour forcer
107+
kpartx -as "${LOOPDEV}" || kpartx -av "${LOOPDEV}"
108+
base="$(basename "${LOOPDEV}")"
109+
if [[ -b "/dev/mapper/${base}p1" && -b "/dev/mapper/${base}p2" ]]; then
110+
BOOT_PART="/dev/mapper/${base}p1"
111+
ROOT_PART="/dev/mapper/${base}p2"
112+
fi
102113
fi
103114

104-
# Montage en lecture seule
115+
# 3) Si toujours rien, dernier recours: montage par offset
116+
mount_by_offset() {
117+
local dev="$1" partnum="$2" mnt="$3" fstype_hint="$4"
118+
# Récupère le start sector via fdisk
119+
local start
120+
start="$(fdisk -l "$dev" | awk -v p="$partnum" '$0 ~ "^"dev".*\\*" {next} $0 ~ "^"dev"p"p {print $2}' dev="$dev" p="$partnum")"
121+
if [[ -z "$start" ]]; then
122+
# autre format fdisk: lignes qui listent partnum en 2e colonne
123+
start="$(fdisk -l "$dev" | awk -v p="$partnum" '$0 ~ "^"dev {next} $0 ~ " "p" " {print $2}' dev="$(basename "$dev")" p="$partnum")"
124+
fi
125+
if [[ -z "$start" ]]; then
126+
echo "Impossible de déterminer l'offset pour ${dev} partition ${partnum}"
127+
return 1
128+
fi
129+
local offset=$(( start * 512 ))
130+
echo "[*] Montage par offset (part${partnum}) offset=${offset}"
131+
mkdir -p "$mnt"
132+
# on laisse le noyau auto-détecter le fs si possible, sinon hint
133+
if [[ -n "$fstype_hint" ]]; then
134+
mount -o ro,offset="$offset" -t "$fstype_hint" "$dev" "$mnt"
135+
else
136+
mount -o ro,offset="$offset" "$dev" "$mnt"
137+
fi
138+
}
139+
140+
echo "[*] Résolution des partitions:"
141+
echo " BOOT_PART=${BOOT_PART:-<offset>}"
142+
echo " ROOT_PART=${ROOT_PART:-<offset>}"
143+
144+
# --- Montage en lecture seule ---
105145
echo "[*] Montage des partitions (ro)…"
106-
mount -o ro "${ROOT_PART}" /mnt/root
107-
mount -o ro "${BOOT_PART}" /mnt/boot 2>/dev/null || true # FAT peut ne pas exister selon l’image
146+
mkdir -p /mnt/root /mnt/boot
147+
148+
if [[ -n "${ROOT_PART}" && -b "${ROOT_PART}" ]]; then
149+
mount -o ro "${ROOT_PART}" /mnt/root
150+
else
151+
# ext4 attendu pour root
152+
mount_by_offset "${LOOPDEV}" 2 /mnt/root ext4
153+
fi
154+
155+
# /boot peut être FAT ou absent selon image
156+
if [[ -n "${BOOT_PART}" && -b "${BOOT_PART}" ]]; then
157+
mount -o ro "${BOOT_PART}" /mnt/boot || true
158+
else
159+
# essaie offset part1, sans forcer le type (auto-détection vfat)
160+
mount_by_offset "${LOOPDEV}" 1 /mnt/boot "" || true
161+
fi
108162

109163
# Copie du rootfs + /boot dans le sysroot
110164
echo "[*] Copie du rootfs -> ${OUT}"

0 commit comments

Comments
 (0)