Skip to content

Instantly share code, notes, and snippets.

@arkq
Created March 21, 2023 09:38
Show Gist options
  • Save arkq/24b447e5bd3866c2696d64e04b91059d to your computer and use it in GitHub Desktop.
Save arkq/24b447e5bd3866c2696d64e04b91059d to your computer and use it in GitHub Desktop.
Download and patch Pop!_OS ISO images to enable persistent storage
#!/bin/bash
# Download and patch Pop!_OS ISO images to enable persistent storage
set -e
IMAGES=(
"Pop!_OS 20.04 LTS"
"https://iso.pop-os.org/20.04/amd64/intel/10/pop-os_20.04_amd64_intel_10.iso"
"username=pop-os noprompt ---"
"username=pop-os persistent "
"ead5d784d04aecc8eec846e507c526a0d2983335c8fe3d1fb1498edb6654287d"
"Pop!_OS 20.04 LTS (NVIDIA)"
"https://iso.pop-os.org/20.04/amd64/nvidia/10/pop-os_20.04_amd64_nvidia_10.iso"
"username=pop-os noprompt modules_load=nvidia ---"
"username=pop-os modules_load=nvidia persistent "
"0b43adcc75b1dff3707273575157a77485be6813b0bddd2212ee4ce1d57e933c"
"Pop!_OS 22.04 LTS"
"https://iso.pop-os.org/22.04/amd64/intel/24/pop-os_22.04_amd64_intel_24.iso"
"username=pop-os noprompt ---"
"username=pop-os persistent "
"b42773b1502c91906ceb296284a4832bd164bd8af5d096594915cd5cd8f4ac9c"
"Pop!_OS 22.04 LTS (NVIDIA)"
"https://iso.pop-os.org/22.04/amd64/nvidia/24/pop-os_22.04_amd64_nvidia_24.iso"
"username=pop-os noprompt modules_load=nvidia nvidia-drm.modeset=0 ---"
"username=pop-os modules_load=nvidia nvidia-drm.modeset=0 persistent "
"067894f912f576be874dae9d1fbc0279d98017dc7a416f01eb48fdeb9570e650"
)
echo "Available ISO images:"
IMAGES_COUNT=$(( ${#IMAGES[@]} / 5 ))
for IMAGE_ID in $(seq $IMAGES_COUNT); do
echo "$IMAGE_ID. ${IMAGES[($IMAGE_ID - 1) * 5]}"
done
read -r -p "Select an image to download: " IMAGE_ID
[[ $IMAGE_ID -eq 0 || $IMAGE_ID -gt $IMAGES_COUNT ]] && exit 1
IMAGE_ID=$(( (IMAGE_ID - 1) * 5 ))
IMAGE_NAME="${IMAGES[$IMAGE_ID]}"
IMAGE_URL="${IMAGES[$IMAGE_ID + 1]}"
IMAGE_PATCH_SRC="${IMAGES[$IMAGE_ID + 2]}"
IMAGE_PATCH_DST="${IMAGES[$IMAGE_ID + 3]}"
IMAGE_SHA256="${IMAGES[$IMAGE_ID + 4]}"
IMAGE_FILE="${IMAGE_URL##*/}"
echo
echo "Downloading $IMAGE_NAME..."
wget --continue --no-verbose --show-progress "$IMAGE_URL"
echo
echo "Patching $IMAGE_NAME..."
gcc -x c -o pop-os-patcher - << EOF
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
if (argc != 4) {
printf("usage: %s <iso> <src> <dst>\n", argv[0]);
return 1;
}
int fd;
if ((fd = open(argv[1], O_RDWR)) == -1) {
perror("ERROR: Open ISO");
return 1;
}
size_t len = strlen(argv[2]);
if (len != strlen(argv[3])) {
fprintf(stderr, "ERROR: length(src) != length(dst)\n");
return 1;
}
char *data;
off_t size = lseek(fd, 0, SEEK_END);
if ((data = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) {
perror("ERROR: Memory map ISO");
return 1;
}
for (size_t off = 0; off < size - len; off++)
if (memcmp(&data[off], argv[2], len) == 0) {
printf("Patching at offset: %#zx\n", off);
memcpy(&data[off], argv[3], len);
}
msync(data, size, MS_SYNC);
munmap(data, size);
close(fd);
return 0;
}
EOF
./pop-os-patcher "$IMAGE_FILE" "$IMAGE_PATCH_SRC" "$IMAGE_PATCH_DST"
rm ./pop-os-patcher
echo
echo "Verifying $IMAGE_NAME..."
echo "$IMAGE_SHA256 $IMAGE_FILE" | sha256sum --check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment