diff --git a/debian-live-config/build.sh b/debian-live-config/build.sh index faebed11..aff11ce2 100755 --- a/debian-live-config/build.sh +++ b/debian-live-config/build.sh @@ -17,6 +17,32 @@ if [ ! -f "${SCRIPT_DIR}/config/includes.chroot/opt/wrolpi-blobs/map-db-gis.dump exit 1 fi +# Validate dump file format - must be PostgreSQL custom format (PGDMP magic) +DUMP_FILE="${SCRIPT_DIR}/config/includes.chroot/opt/wrolpi-blobs/map-db-gis.dump" +DUMP_HEADER=$(head -c 5 "${DUMP_FILE}" 2>/dev/null | od -An -tx1 | tr -d ' \n') + +if [[ "${DUMP_HEADER:0:6}" == "504744" ]]; then + echo "map-db-gis.dump: Valid PostgreSQL custom format" +elif [[ "${DUMP_HEADER:0:6}" == "1f8b08" ]]; then + # Check if gzipped content is custom format + INNER_HEADER=$(gunzip -c "${DUMP_FILE}" 2>/dev/null | head -c 5 | od -An -tx1 | tr -d ' \n') + if [[ "${INNER_HEADER:0:6}" == "504744" ]]; then + echo "map-db-gis.dump: Valid gzipped PostgreSQL custom format" + else + echo "ERROR: map-db-gis.dump is gzipped but does NOT contain PostgreSQL custom format!" + echo "Inner header: ${INNER_HEADER} (expected to start with 504744 for 'PGDMP')" + echo "The dump was likely created incorrectly. Use:" + echo " docker run --rm -v /path/to/file.osm.pbf:/data/region.osm.pbf lrnselfreliance/osm-map-dumper > map-db-gis.dump" + exit 1 + fi +else + echo "ERROR: map-db-gis.dump has invalid format!" + echo "Header: ${DUMP_HEADER} (expected to start with 504744 for 'PGDMP' or 1f8b08 for gzip)" + echo "Create the dump using:" + echo " docker run --rm -v /path/to/file.osm.pbf:/data/region.osm.pbf lrnselfreliance/osm-map-dumper > map-db-gis.dump" + exit 1 +fi + # Clear out old builds. [ -d "${BUILD_DIR}" ] && rm -rf "${BUILD_DIR}" mkdir "${BUILD_DIR}" diff --git a/debian-live-config/config/hooks/normal/9999-wrolpi.hook.chroot b/debian-live-config/config/hooks/normal/9999-wrolpi.hook.chroot index 65706a33..5466df38 100644 --- a/debian-live-config/config/hooks/normal/9999-wrolpi.hook.chroot +++ b/debian-live-config/config/hooks/normal/9999-wrolpi.hook.chroot @@ -90,6 +90,12 @@ systemctl enable iperf3 # Enable Controller service to start on boot. systemctl enable wrolpi-controller.service +# Protect blob files from accidental modification or deletion. +# These files are critical for initializing WROLPi. +chown -R root:root /opt/wrolpi-blobs +chmod 444 /opt/wrolpi-blobs/* +chattr +i /opt/wrolpi-blobs/* + set +x echo diff --git a/pi-gen/README.md b/pi-gen/README.md index f4fcb30e..b0a09bb9 100644 --- a/pi-gen/README.md +++ b/pi-gen/README.md @@ -8,7 +8,14 @@ See https://github.com/RPI-Distro/pi-gen 1. Convert a map PBF file to your initial map DB dump: - docker run --rm -v /home/user/district-of-columbia-230111.osm.pbf:/data/region.osm.pbf lrnselfreliance/osm-map-dumper | gzip > pi-gen/stage2/04-wrolpi/files/map-db-gis.dump + docker run --rm -v /home/user/district-of-columbia-230111.osm.pbf:/data/region.osm.pbf lrnselfreliance/osm-map-dumper > pi-gen/stage2/04-wrolpi/files/map-db-gis.dump + + The output is already compressed (PostgreSQL custom format with `--compress 9`). + + Verify the dump format is correct: + + file pi-gen/stage2/04-wrolpi/files/map-db-gis.dump + # Should show: "PostgreSQL custom database dump" 2. Run the build (or, with a specific git branch) diff --git a/pi-gen/build.sh b/pi-gen/build.sh index 2ff5de4d..43b3fa8c 100755 --- a/pi-gen/build.sh +++ b/pi-gen/build.sh @@ -17,6 +17,32 @@ if [ ! -f "${SCRIPT_DIR}/stage2/04-wrolpi/files/map-db-gis.dump" ]; then exit 1 fi +# Validate dump file format - must be PostgreSQL custom format (PGDMP magic) +DUMP_FILE="${SCRIPT_DIR}/stage2/04-wrolpi/files/map-db-gis.dump" +DUMP_HEADER=$(head -c 5 "${DUMP_FILE}" 2>/dev/null | od -An -tx1 | tr -d ' \n') + +if [[ "${DUMP_HEADER:0:6}" == "504744" ]]; then + echo "map-db-gis.dump: Valid PostgreSQL custom format" +elif [[ "${DUMP_HEADER:0:6}" == "1f8b08" ]]; then + # Check if gzipped content is custom format + INNER_HEADER=$(gunzip -c "${DUMP_FILE}" 2>/dev/null | head -c 5 | od -An -tx1 | tr -d ' \n') + if [[ "${INNER_HEADER:0:6}" == "504744" ]]; then + echo "map-db-gis.dump: Valid gzipped PostgreSQL custom format" + else + echo "ERROR: map-db-gis.dump is gzipped but does NOT contain PostgreSQL custom format!" + echo "Inner header: ${INNER_HEADER} (expected to start with 504744 for 'PGDMP')" + echo "The dump was likely created incorrectly. Use:" + echo " docker run --rm -v /path/to/file.osm.pbf:/data/region.osm.pbf lrnselfreliance/osm-map-dumper > map-db-gis.dump" + exit 1 + fi +else + echo "ERROR: map-db-gis.dump has invalid format!" + echo "Header: ${DUMP_HEADER} (expected to start with 504744 for 'PGDMP' or 1f8b08 for gzip)" + echo "Create the dump using:" + echo " docker run --rm -v /path/to/file.osm.pbf:/data/region.osm.pbf lrnselfreliance/osm-map-dumper > map-db-gis.dump" + exit 1 +fi + CHROOTFS="${BUILD_DIR}/work/WROLPi/stage0/rootfs" cleanup() { diff --git a/pi-gen/stage2/04-wrolpi/04-run-chroot.sh b/pi-gen/stage2/04-wrolpi/04-run-chroot.sh index 48edd35d..5e63bd2c 100755 --- a/pi-gen/stage2/04-wrolpi/04-run-chroot.sh +++ b/pi-gen/stage2/04-wrolpi/04-run-chroot.sh @@ -56,6 +56,12 @@ echo '/dev/sda1 /media/wrolpi auto defaults,nofail 0 0' | tee -a /etc/fstab mkdir -p /media/wrolpi chown -R wrolpi:wrolpi /media/wrolpi /home/wrolpi /opt/wrolpi* +# Protect blob files from accidental modification or deletion. +# These files are critical for initializing WROLPi. +chown -R root:root /opt/wrolpi-blobs +chmod 444 /opt/wrolpi-blobs/* +chattr +i /opt/wrolpi-blobs/* + set +x echo diff --git a/repair.sh b/repair.sh index 0a25e0fc..1142ebab 100755 --- a/repair.sh +++ b/repair.sh @@ -188,8 +188,19 @@ fi # Change owner of the media directory, ignore any errors because the drive may be fat/exfat/etc. chown wrolpi:wrolpi /media/wrolpi 2>/dev/null || echo "Ignoring failure to change media directory permissions." +# Remove immutable flag from blob files before chown (may not exist or may not be set). +chattr -i /opt/wrolpi-blobs/* 2>/dev/null || : + chown -R wrolpi:wrolpi /home/wrolpi /opt/wrolpi* +# Protect blob files from accidental modification or deletion. +# These files are critical for initializing WROLPi. +if [ -d /opt/wrolpi-blobs ]; then + chown -R root:root /opt/wrolpi-blobs + chmod 444 /opt/wrolpi-blobs/* + chattr +i /opt/wrolpi-blobs/* +fi + # Copy MOTD once the repair has been successful. cp /opt/wrolpi/etc/raspberrypios/motd/30-wrolpi.motd /etc/update-motd.d/30-wrolpi chmod +x /etc/update-motd.d/* diff --git a/scripts/initialize_map_db.sh b/scripts/initialize_map_db.sh index bfc41e16..3b7bfa6d 100755 --- a/scripts/initialize_map_db.sh +++ b/scripts/initialize_map_db.sh @@ -24,8 +24,15 @@ sudo -iu postgres psql -c '\l' --port=5433 | grep gis || ( sudo -iu postgres psql -d gis --port=5433 -c 'CREATE EXTENSION hstore' echo "Created gis database" ) -# Restore initial dump. -sudo -iu postgres pg_restore --port=5433 --no-owner --role=_renderd -d gis -j3 /opt/wrolpi-blobs/map-db-gis.dump +# Restore initial dump (handle both gzipped and non-gzipped custom format). +DUMP_FILE=/opt/wrolpi-blobs/map-db-gis.dump +if file "${DUMP_FILE}" | grep -q "gzip"; then + echo "Restoring gzipped dump..." + gunzip -c "${DUMP_FILE}" | sudo -iu postgres pg_restore --port=5433 --no-owner --role=_renderd -d gis +else + echo "Restoring dump..." + sudo -iu postgres pg_restore --port=5433 --no-owner --role=_renderd -d gis -j3 "${DUMP_FILE}" +fi sudo -iu postgres psql --port=5433 -d gis -c 'ALTER TABLE geography_columns OWNER TO _renderd' sudo -iu postgres psql --port=5433 -d gis -c 'ALTER TABLE geometry_columns OWNER TO _renderd' sudo -iu postgres psql --port=5433 -d gis -c 'ALTER TABLE spatial_ref_sys OWNER TO _renderd'