Checking that map dump is valid before building image. Protecting map

dump file in built image so that it cannot be modified.
This commit is contained in:
Roland 2026-02-15 14:38:50 -07:00
parent 56419cf5a2
commit 50782f47cc
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
7 changed files with 92 additions and 3 deletions

View file

@ -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}"

View file

@ -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

View file

@ -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)

View file

@ -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() {

View file

@ -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

View file

@ -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/*

View file

@ -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'