Raspberry Pi Remote ReInstall

In this tutorial, i am going to show you how to do a remote reinstall of your Raspberry Pi’s operating system. Yes, it can be done! The only caveat to this procedure is that you must have a usb hard drive plugged in to the Pi that you do not mind formatting.

If you only have the SD memory card in use and the file system has been expanded then this tutorial may serve as a lesson for the future. I leave a 16gb stick plugged in to all of my Pi’s exclusively for this reason.

In proceeding I will say that I, from this point forward, take absolutely no responsibility for your actions that may lead to loss of data and / or lock out. You are in control and if you do not understand the process and in turn grasp what we are doing then maybe this is not for you.

Lets first of all have a look at how the usb drive is identified. Be careful and confident that you are sighting the correct drive. Failure to get this right will lead to loss of data as mentioned above.

fdisk -l

Now mine is at /dev/sda but yours could be different, and in fact as we will cover shortly, the Raspberry is a little random in naming external drives, especially if there is more than one in place.

Next we are going to download the operating system image of choice to your current file system. I am going to use Raspbian Lite sourced from the official Raspberry Pi page.

I am going to work in /opt/ but really its up to you. Just a heads up I do not recommend using /tmp/ as upon reboot this gets wiped. We DO need to reboot before the procedure is out.

cd /opt/
wget --trust-server-names https://downloads.raspberrypi.org/raspbian_lite_latest
ls // Check 
unzip 2016-03-18-raspbian-jessie-lite.zip // Or Whatever You Have
rm 2016-03-18-raspbian-jessie-lite.zip

Now we are going to erase the USB drive ready for restoring the image you just downloaded. You can go a step further by erasing and creating a single partition with a certain file system by following this post I have written, but for our particular exercise it is not necessary at all. I thought you just wanted to learn something!

Once again if you are not sure of the usb location run fdisk -l and again i am at /dev/sda/ but you may be different. This is the last time I am going to mention this!

fdisk /dev/sda/
d // Delete Partition
d // If more than one then delete again and again if need be!
w // write changes

Now confirm again with fdisk -l and you should have an empty drive. Something like what is below should print, obviously without any partitions following.

Disk /dev/sda: 14.3 GiB, 15376000000 bytes, 30031250 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x6f92008e

So now it is time to restore the OS image to your drive.
This will restore both boot and root partitions but for what we are doing that does not matter and in fact is preferred.

cd /path/to/image
dd if=os-image.img of=/dev/sda

Once that has finished you should now once again confirm that two partitions now exist. If you have not already figured, it is a fdisk -l again!

Mine ended out like this..

Disk /dev/sda: 14.3 GiB, 15376000000 bytes, 30031250 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x6f92008e

Device     Boot  Start     End Sectors  Size Id Type
/dev/sda1         8192  131071  122880   60M  c W95 FAT32 (LBA)
/dev/sda2       131072 2658303 2527232  1.2G 83 Linux

Now for the tricky part… If you have used (and obviously you probably have) imaging software to restore / create an OS to the SD card then you will probably remember that when you first log in to the Pi you have to use raspi-config to expand the file system otherwise you run out of space. That is exactly the same now, the only gotcha is that raspi-config does not know how to expand the file system on a USB drive.

As you can see above from my fdisk output, i have a 16GB drive (14.3GB Really!) and the image restore has only taken up 1.26GB of it… You guessed it! Back to fdisk.

Heads up – this may look a little hairy but if followed correctly will work with no issues. The most important thing you want to be aware of is that the new partition we create must start at exactly the same sector as the partition we delete.

In my case above /dev/sda2 starts @ sector 131072 and ends @ sector 2658303. Dont worry about the end but the start is important. You must get this step correct, so if in doubt write it down exactly as it appears on your fdisk output!

We are going to delete the partition /dev/sda2 and recreate it using all available space.

fdisk /dev/sda
d // Delete
2 // Partition 2
n // New partition
p // Primary
2 // Partition 2
131072 // Starting Sector
Use Default // fdisk defaults to the last sector on the drive 
w // Write changes & exit

Just to check all is well do a final fdisk -l and you should see that now your second partition (root) is the size of all remaining space. My output was as follows..

Disk /dev/sda: 14.3 GiB, 15376000000 bytes, 30031250 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x6f92008e

Device     Boot  Start      End  Sectors  Size Id Type
/dev/sda1         8192   131071   122880   60M  c W95 FAT32 (LBA)
/dev/sda2       131072 30031249 29900178 14.3G 83 Linux

Just a little different to that of what was returned prior to deleting and creating a new partition! One last step in the process it to now expand the file system to the full capacity of the partition. DO NOT miss this step!

e2fsck -f /dev/sda2 // Check File System
resize2fs /dev/sda2 // Expand File System

Just to be safe we are going to reboot so that the kernel is informed of the changes and we can begin to use the drive to its full potential!

reboot

Just a quick sidenote – If you do happen to fdisk /dev/sda2 and see a warning about a valid ‘ext4’ signature, don’t use wipefs or you will lose your data. In this case fdisk just doesn’t agree with our partition table, but if you do the same thing on a stock standard Raspbian install there is no difference. Its not us, its them!

So logging back in as root, we are ready to start modifying the new image to suit our needs.

You may be different, but the most important thing for me to regain SSH access when rebooting to a fresh install is that the IP address remains the same.

I am not going to explain this in great detail as if you set up the Pi in the first place, you must know how it works! Just remember that anything you need should be done now.

If you dont use a static IP but would like to, go and have a look at my Static IP posts for both Wheezy and Jessie. I am using a Jessie image so i am following the latter guide utilising /etc/dhcpcd.conf, but you may benefit from reading both.

I might just put it out there, that if you are currently working remotely (as i am sure you are reading this post) be very careful as a static IP gone wrong is a very easy way to lock yourself out!

In order to access and change files on the new image we need to mount it. If you dont already have one, create a mount point and then mount the new root partition.

mkdir /mnt/usb
mount /dev/sda2 /mnt/usb/
cd /mnt/usb/
ls // Your in!

Now as i said, i am really only worried about my ip address so in a Jessie based OS, i am really only using the /etc/dhcpcd.conf file containing my static definitions. If you are different then go where you need to to get it sorted (/etc/network/interfaces etc.)

Now i know this is all i am doing, so i am just going to copy it over complete. If you dont know what you are doing then DO SOME RESEARCH.

cp /etc/dhcpcd.conf /mnt/usb/etc/dhcpcd.conf
cat /mnt/usb/etc/dhcpcd.conf // Confirming

The last thing we are going to do, unless you plan on continuing to boot to your USB drive is to copy the OS image you just used to the USB so we can basically do what we just did in reverse when the time comes to move everything back over to the SD card. You can put it wherever you want but i am going to put it in /opt/

cp /root/path/to/image.img /mnt/usb/opt/

At this point i cant really see anything else you may need, but as i said earlier… If you originally set it up then you obviously know what you are doing! By the way, remember what we have just done, because you will be doing it again!

Now just to be safe i am going to reimage the boot partition of the SD card. I am not sure if this is necessary, however if you are using a much newer image it is probably best.
– Leave me a comment if you disagree!

It may be worth quickly checking your boot partitions are both the same size (remember you now have two) but so far i have not come across a difference. You guessed it… fdisk -l

fdisk -l

Device         Boot  Start       End   Sectors  Size Id Type
/dev/mmcblk0p1        8192    131071    122880   60M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      131072 124735487 124604416 59.4G 83 Linux

Disk /dev/sda: 14.3 GiB, 15376000000 bytes, 30031250 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x6f92008e

Device     Boot  Start      End  Sectors  Size Id Type
/dev/sda1         8192   131071   122880   60M  c W95 FAT32 (LBA)
/dev/sda2       131072 30031249 29900178 14.3G 83 Linux

As you can see the SD card boot partition (/dev/mmcblk0p1) is exactly the same size / Sectors) as my new one (/dev/sda1) so we are good to go.

If for whatever reason this is not the case, then unfortunately that is outside the scope of this write up and something to look into.

Now if for some reason (and i don’t know why) you have the boot partition mounted and don’t know about it, you should probably run umount first just to be safe.

umount /dev/sda1
umount /dev/mmcblk0p1
dd if=/dev/sda1 of=/dev/mmcblk0p1

Now just before we finish, there is one critical change to make in the SD card’s boot partition (/dev/mmcblk0p1) so that when we reboot it loads the USB drives root partition (/dev/sda2) instead of the SD cards (/dev/mmcblk0p2).

We now need to mount the boot partition and change the bootable root partition. No need to create a new mount point, we can just use the one we already have..

cd / // Just in case you get a busy error..
     // If you do, the mount is still in your path
umount /dev/sda2
mount /dev/mmcblk0p1 /mnt/usb
cd /mnt/usb

Ok last step before booting to your new USB located image… In the root directory of your boot partition there is a file called cmdline.txt specifying the path of the root file system. Check it out and you will understand better what we are about to do.

cat /mnt/usb/cmdline.txt

We are going to modify it so that instead of our root file system being on the SD (/dev/mmcblk0p2), it will point toward the USB (/dev/sda2).

Fire up your favorite text editor and change root=/dev/mmcblk0p2 to root=/dev/sda2

nano /mnt/usb/cmdline.txt
dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/sda2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
ctrl+x // Save and exit

You are now ready to reboot the pi and log into the USB instead of the SD. Woohoo! Go for a reboot and cross your finger that you have followed the above (within reason) exactly as I have written.

Just remember upon login that we are using a factory image and the user name will be pi with a password of raspberry.

– Obviously if you are on a remote connection i would advise changing this ASAP!

In closing, I end up going on to restore the factory image back to the SD card but with all of the information above, there are a couple of roads forward that you can take. If you choose to keep the root file system on the USB then if need be one day you can just do a simple restore to the SD and away you go. The most important part is that everything above can be molded to your exact needs. Enjoy!

One Reply to “Raspberry Pi Remote ReInstall”

Leave a Reply

Your email address will not be published. Required fields are marked *