Skip to content

Commit 1bb631e

Browse files
committed
rpi: Only add 1MB padding after rootfs image data.
This updates the fix for #139 to only 1MB padding after the rootfs image data. A PR was also created to fix this in OpenWrt: openwrt/openwrt#19997
1 parent 77c229a commit 1bb631e

File tree

21 files changed

+287
-98
lines changed

21 files changed

+287
-98
lines changed

conf/base_raspberrypi_bcm27xx_bcm2708/patches/image-with-full-rootfs.patch

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Index: openwrt/target/linux/bcm27xx/image/gen_rpi_sdcard_img.sh
2+
===================================================================
3+
--- openwrt.orig/target/linux/bcm27xx/image/gen_rpi_sdcard_img.sh
4+
+++ openwrt/target/linux/bcm27xx/image/gen_rpi_sdcard_img.sh
5+
@@ -24,5 +24,34 @@ set $(ptgen -o $OUTPUT -h $head -s $sect
6+
BOOTOFFSET="$(($1 / 512))"
7+
ROOTFSOFFSET="$(($3 / 512))"
8+
9+
+# In most cases, the rootfs image size is smaller than the total space
10+
+# available in the rootfs. OpenWrt will use the remaining space for storing
11+
+# the fs backing the overlayfs which holds configuration changes etc.
12+
+# To avoid that OpenWrt incorrectly detects a filesystem in the case
13+
+# there is remaining data from a previous installation, we need to clear
14+
+# the first N bytes directly after the rootfs image, to make sure
15+
+# that any initial bytes that are or look like fs superblocks are cleared
16+
+# out.
17+
+ROOTFSSIZE="$(($4 / 512))"
18+
+ROOTFSIMGSIZE="$((($(wc -c < $ROOTFS) + 511) / 512))"
19+
+ROOTFSPADDINGSIZE="$(($ROOTFSSIZE - $ROOTFSIMGSIZE))"
20+
+ROOTFSPADDINGOFFSET="$(($ROOTFSOFFSET + $ROOTFSIMGSIZE))"
21+
+
22+
+# Reduce padding to max 1MB, which is enough to clear out any superblocks
23+
+# of previous file-systems or any data that might look like fs superblocks.
24+
+if [ "$ROOTFSPADDINGSIZE" -gt 2048 ]; then
25+
+ ROOTFSPADDINGSIZE="2048"
26+
+fi
27+
+
28+
+# Write the bootfs.
29+
dd bs=512 if="$BOOTFS" of="$OUTPUT" seek="$BOOTOFFSET" conv=notrunc
30+
-dd bs=512 if="$ROOTFS" of="$OUTPUT" seek="$ROOTFSOFFSET" conv=notrunc
31+
+
32+
+# Write the rootfs.
33+
+# The `sync` is important, as in case $ROOTFS is not a multiple of bs, it will
34+
+# assure that remaining bytes are set to 0x00.
35+
+dd bs=512 if="$ROOTFS" of="$OUTPUT" seek="$ROOTFSOFFSET" conv=notrunc,sync
36+
+
37+
+# Add padding after rootfs.
38+
+if [ "$ROOTFSPADDINGSIZE" -gt 0 ]; then
39+
+ dd bs=512 if=/dev/zero of="$OUTPUT" seek="$ROOTFSPADDINGOFFSET" count="$ROOTFSPADDINGSIZE" conv=notrunc
40+
+fi
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
no-uart-console.patch
22
boot-config.patch
3-
image-with-full-rootfs.patch
3+
image-with-padded-rootfs.patch

conf/base_raspberrypi_bcm27xx_bcm2709/patches/image-with-full-rootfs.patch

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Index: openwrt/target/linux/bcm27xx/image/gen_rpi_sdcard_img.sh
2+
===================================================================
3+
--- openwrt.orig/target/linux/bcm27xx/image/gen_rpi_sdcard_img.sh
4+
+++ openwrt/target/linux/bcm27xx/image/gen_rpi_sdcard_img.sh
5+
@@ -24,5 +24,34 @@ set $(ptgen -o $OUTPUT -h $head -s $sect
6+
BOOTOFFSET="$(($1 / 512))"
7+
ROOTFSOFFSET="$(($3 / 512))"
8+
9+
+# In most cases, the rootfs image size is smaller than the total space
10+
+# available in the rootfs. OpenWrt will use the remaining space for storing
11+
+# the fs backing the overlayfs which holds configuration changes etc.
12+
+# To avoid that OpenWrt incorrectly detects a filesystem in the case
13+
+# there is remaining data from a previous installation, we need to clear
14+
+# the first N bytes directly after the rootfs image, to make sure
15+
+# that any initial bytes that are or look like fs superblocks are cleared
16+
+# out.
17+
+ROOTFSSIZE="$(($4 / 512))"
18+
+ROOTFSIMGSIZE="$((($(wc -c < $ROOTFS) + 511) / 512))"
19+
+ROOTFSPADDINGSIZE="$(($ROOTFSSIZE - $ROOTFSIMGSIZE))"
20+
+ROOTFSPADDINGOFFSET="$(($ROOTFSOFFSET + $ROOTFSIMGSIZE))"
21+
+
22+
+# Reduce padding to max 1MB, which is enough to clear out any superblocks
23+
+# of previous file-systems or any data that might look like fs superblocks.
24+
+if [ "$ROOTFSPADDINGSIZE" -gt 2048 ]; then
25+
+ ROOTFSPADDINGSIZE="2048"
26+
+fi
27+
+
28+
+# Write the bootfs.
29+
dd bs=512 if="$BOOTFS" of="$OUTPUT" seek="$BOOTOFFSET" conv=notrunc
30+
-dd bs=512 if="$ROOTFS" of="$OUTPUT" seek="$ROOTFSOFFSET" conv=notrunc
31+
+
32+
+# Write the rootfs.
33+
+# The `sync` is important, as in case $ROOTFS is not a multiple of bs, it will
34+
+# assure that remaining bytes are set to 0x00.
35+
+dd bs=512 if="$ROOTFS" of="$OUTPUT" seek="$ROOTFSOFFSET" conv=notrunc,sync
36+
+
37+
+# Add padding after rootfs.
38+
+if [ "$ROOTFSPADDINGSIZE" -gt 0 ]; then
39+
+ dd bs=512 if=/dev/zero of="$OUTPUT" seek="$ROOTFSPADDINGOFFSET" count="$ROOTFSPADDINGSIZE" conv=notrunc
40+
+fi
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
no-uart-console.patch
22
boot-config.patch
3-
image-with-full-rootfs.patch
3+
image-with-padded-rootfs.patch

conf/base_raspberrypi_bcm27xx_bcm2712/patches/image-with-full-rootfs.patch

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Index: openwrt/target/linux/bcm27xx/image/gen_rpi_sdcard_img.sh
2+
===================================================================
3+
--- openwrt.orig/target/linux/bcm27xx/image/gen_rpi_sdcard_img.sh
4+
+++ openwrt/target/linux/bcm27xx/image/gen_rpi_sdcard_img.sh
5+
@@ -24,5 +24,34 @@ set $(ptgen -o $OUTPUT -h $head -s $sect
6+
BOOTOFFSET="$(($1 / 512))"
7+
ROOTFSOFFSET="$(($3 / 512))"
8+
9+
+# In most cases, the rootfs image size is smaller than the total space
10+
+# available in the rootfs. OpenWrt will use the remaining space for storing
11+
+# the fs backing the overlayfs which holds configuration changes etc.
12+
+# To avoid that OpenWrt incorrectly detects a filesystem in the case
13+
+# there is remaining data from a previous installation, we need to clear
14+
+# the first N bytes directly after the rootfs image, to make sure
15+
+# that any initial bytes that are or look like fs superblocks are cleared
16+
+# out.
17+
+ROOTFSSIZE="$(($4 / 512))"
18+
+ROOTFSIMGSIZE="$((($(wc -c < $ROOTFS) + 511) / 512))"
19+
+ROOTFSPADDINGSIZE="$(($ROOTFSSIZE - $ROOTFSIMGSIZE))"
20+
+ROOTFSPADDINGOFFSET="$(($ROOTFSOFFSET + $ROOTFSIMGSIZE))"
21+
+
22+
+# Reduce padding to max 1MB, which is enough to clear out any superblocks
23+
+# of previous file-systems or any data that might look like fs superblocks.
24+
+if [ "$ROOTFSPADDINGSIZE" -gt 2048 ]; then
25+
+ ROOTFSPADDINGSIZE="2048"
26+
+fi
27+
+
28+
+# Write the bootfs.
29+
dd bs=512 if="$BOOTFS" of="$OUTPUT" seek="$BOOTOFFSET" conv=notrunc
30+
-dd bs=512 if="$ROOTFS" of="$OUTPUT" seek="$ROOTFSOFFSET" conv=notrunc
31+
+
32+
+# Write the rootfs.
33+
+# The `sync` is important, as in case $ROOTFS is not a multiple of bs, it will
34+
+# assure that remaining bytes are set to 0x00.
35+
+dd bs=512 if="$ROOTFS" of="$OUTPUT" seek="$ROOTFSOFFSET" conv=notrunc,sync
36+
+
37+
+# Add padding after rootfs.
38+
+if [ "$ROOTFSPADDINGSIZE" -gt 0 ]; then
39+
+ dd bs=512 if=/dev/zero of="$OUTPUT" seek="$ROOTFSPADDINGOFFSET" count="$ROOTFSPADDINGSIZE" conv=notrunc
40+
+fi
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
no-uart-console.patch
22
boot-config.patch
33
add_designware_spi_kmod.patch
4-
image-with-full-rootfs.patch
4+
image-with-padded-rootfs.patch

conf/full_raspberrypi_bcm27xx_bcm2708/patches/image-with-full-rootfs.patch

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)