Hey fellow self-hosters!
Not directly related to self-hosting, but since it looks like quite a few people here (like me) are using Raspberry PIs to self-host stuff, I thought some people might be interested.
I use my Raspberry Pi as a NAS, and I’m using Ansible to automate the whole setup. After trying some stuff and experimenting a bit, I like to start again with a clean install and run my Ansible playbook to have a clean setup.
But I’m not always home when I do stuff with my Pi and thought it would be useful to have a way to reflash it remotely, so I could continue to break stuff and just reflash it when it gets too messy.
So I made a script to remotely reflash the Raspberry Pi. The main idea is that after flashing the SD card with the Raspi Imager, I make a copy of the bootfs and rootfs partitions, and when I need to reset the Pi to the initial state, I restore both copies of the partitions.
I wrote a step-by-step guide explaining everything:
https://gist.github.com/lilianmallardeau/04368e95bc97eb9738fba13df3bbfe4a
Feel free to use it if you like!
Great!
It would even be easier to get the
DISK_ID
directly in the script, at its beginning (but after the check about being root):DISK_ID=$(sfdisk --disk-id /dev/mmcblk0)
Nice
YESSS.
Thank you! This is exactly what I need. I have a bunch of random boards I am always mucking around with.
For your case, you might want to switch to overlay rootfs and mount /boot as read-only, your sd card won’t get changed at all.
Please elaborate how to overlay rootfs.
Configure your raspberry pi os properly, then use raspi-config to switch to overlay root filesystem. You can switch back (using raspi-config) if you want to make changes to your os.
See https://learn.adafruit.com/read-only-raspberry-pi/overview
This is a great idea. Regarding not ending up in an unusable state if your internet connection goes down, you could consider running the flashing steps in a background subshell with nohup or similar. Or double-fork the shell.