6

I've had a Raspberry Pi now for some time. I did a lot of fun stuff with it. A lot of the fun stuff is still on there, and that's not always fun.

I could reset my Raspberry Pi by walking over to it, powering it down, remove the micro SD card, and install a fresh image on it.
But, from an IT viewpoint, that feels wrong.

I was wondering if I could achieve a (somewhat) complete reset by executing a set of commands (and later on making a script out of them)

What I would like to know is:
- Is it possible at all?
- What are the minimum steps required to get a clean install?
- What optional things could be useful (stuff like overwriting the SD with zeroes)

I'm asking this because I think that once my raspberry bush expands, it would be useful to do this without needing physical access. I have a feeling this method would also be more or less applicable to other Linux distro's.

  • Do you have a Raspberry Pi 3 ? They can boot from network. – flakeshake Mar 8 '18 at 11:06
  • @flakeshake I'm looking for something to do a complete reset, not a network boot. I can reboot my server thousands of times (not a great server, i know) and it keeps on running the same services, provides the same content, ... But when I run (let's say execute server_reset.sh) a script, it goes back to initial installation settings, hard disk wiped, config gone, ... Or am I misunderstanding the concept of a network boot? – Nick Dewitte Mar 8 '18 at 11:19
  • @NickDewitte There are but they are less of a "complete reset", and more reverting installed packages. I have the bones of an answer but I am reluctant to suggest it because you need to be experienced in linux to achieve it. I can't give step by step instructions. – Philip Couling Mar 8 '18 at 23:15
  • @couling I'm looking for a reset that is not package-specific. Who knows what I did on a boring weekend a year ago :) – Nick Dewitte Mar 10 '18 at 10:36
4

Firstly: doing this by remote is inadvisable especially if, like me, you happen to be 70+ miles from your remote pi. It's a very long drive if you screw it up. When you do this fir the first time, do it "remotely" while sitting right next to the pi.

Secondly: yes, this is possible but I've never done it and although I believe I could achieve it, I'm not going to give step by step instructions.

The principle:

The SD card for raspbian is already partitioned.

  • It has the main OS partition (EXT4 formatted, mounted at /).
  • It has the boot partition (FAT32 formatted, mounted at /boot).

What you could do is

  1. Shrink your main OS partition using resize2fs and fdisk. This is a bad idea with the PI online. But if you want to shoot yourself in your foot then see here: https://linuxconfig.org/how-to-resize-ext4-root-partition-live-without-umount
  2. add an extra "recovery" EXT4 partition using fdisk
  3. Install a working OS (Raspbian) on the new partition as a sort of "recovery OS".
  4. Reboot into the recovery OS by changing PARTUUID /boot/cmdline.txt and rebooting the pi. You can get the PARTUUID of your partitions by typing sudo blkid
  5. Wipe clean and re-install the main OS partition.
  6. Change /boot/cmdline.txt back and reboot into your new fresh OS. It's best to check sudo blkid before doing this, it may have changed.

The Prep

If you are simply planning to do this in future, then you will do steps 1, 2 and possibly 3 with the pi offline and just leave the recovery partition in place until you need it.

Installing the OS (Steps 3 and 5)

Normally you download an SD card image from the website (https://www.raspberrypi.org/downloads/raspbian/) and just overwrite your entire disk... but that's no good because you must not overwrite your partition table.

The downloaded images will have the exact 2 partitions you started with, so you need to copy the OS partition from the image into whichever partition you want to install to.

So you can try this:

# Download the image file
wget https://downloads.raspberrypi.org/raspbian_lite_latestq
unzip raspbian_lite_latestq

# Create a loopback device to let you treat the image as a disk
sudo losetup /dev/loop0 2017-11-29-raspbian-stretch-lite.img

# Map the image's partitions
sudo partprobe /dev/loop0

#  Completely overwrite one of your partitions with one from the image
#  MAKE SURE YOU DON'T HAVE THE PARTITION MOUNTED WHEN YOU OVERWRITE IT
sudo dd if=/dev/loop0p2 of=mmcblk0p2 status=progress

The part of this I really can't help you with is skipping the first boot which usually does something different to simply booting the pi

  • So, the gist of it is that I need an intermediate OS, preferrably on a different (physical) disk, so I can remove the old OS and install the new one? Also, I'm quite aware that doing this, even more so experimenting with this is not a great idea when I'm unable to intervene physically :) – Nick Dewitte Mar 10 '18 at 10:34
  • Yes. The problem is that to do anything remotely with it you need a running OS and overwriting a running OS will in most cases cause it to crash before you succeed. – Philip Couling Mar 10 '18 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.