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!

Formatting Drive With FDisk (Linux)

In this tutorial I am going to show you how to format and set up a file system for a drive in Linux using fdisk.

In my particular scenario, i am setting up a USB drive with an EXT4 partition for use, but with a little tweaking can be used for all types of storage and all types of file systems.

First of all we need to find where the drive is that we want to work with.

fdisk -l

In my case im on /dev/sda but you may be different. Please make sure you are targeting the correct drive or data loss will occur.

It may be helpful at this point to make sure you know the basic fdisk options are. Obviously for a more in depth list you may just want to use m when already in fdisk, or for a range of available switches, type fdisk -h in the terminal. More to what we are trying to achieve, below are the basic commands relevant to deleting & creating new partitions.

    m – print help
    p – print the partition table
    n – create a new partition
    d – delete a partition
    q – quit without saving changes
    w – write the new partition table and exit

CAUTION – The following will delete all partitions on your drive and subsequently all data. Please make sure if you do not understand or do not grasp the drive designation / location that you have ALL of your data backed up or stop at this point.

So we are going to delete all partitions to make way for our new partition

fdisk /dev/sda
d // Delete
d // Again if more than one...
w // Write Changes

So now if you fdisk -l again you should see an empty drive….
Something like that of below (note no partitions are in place 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: 0x00000000

So lets now go ahead and create a partition.
In this case we are using the whole drive but obviously your scenario may be different.
If in deed you are using the whole drive, the defaults are more than adequate.

fdisk /dev/sda
n // New Partition
  // Choose defaults unless setting up >1 partition
w // Write changes

SO now your new partition has been created, however we still have to build the file system.
Before that, you can confirm the partition you just created exists by once again doing an fdisk -l. You can see mine below is no longer an empty drive but returns the 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: 0x00000000

Device     Boot Start      End  Sectors  Size Id Type
/dev/sda1        2048 30031249 30029202 14.3G 83 Linux

So now its time to create the file system. Fairly straight forward..

Be aware that you are no longer dealing with the broad drive designation of /dev/sda but are dealing with the more specific partition that you created of /dev/sda1.

mkfs.ext4 /dev/sda1

There are several other file systems that you can create using the above mkfs command. I used an EXT4 file system, however if for example i wanted to make an EXT3 file system, i would have just changed the above line of code to mkfs.ext3 /dev/sda1.

For your reference, I have listed the available (debian) file system builders below.

mkfs.ext2
mkfs.ext3
mkfs.ext4
mkfs.ext4dev

mkfs.fat
mkfs.vfat
mkfs.msdos 

mkfs.bfs   
mkfs.cramfs
mkfs.minix

Just to be safe we will now do a reboot and recheck all is good upon login.

reboot
fdisk -l

There are a couple of different avenues you can take now depending on what you are trying to achieve.

Obviously first and foremost, you can now temporarily mount your fresh drive wherever you want..

mount /dev/sda1/ foo/bar

However secondly, if you want to programmatically mount your drive upon every boot, look into fstab.
I will cover fstab soon but in the meantime Ubuntu has a sufficiently detailed write up that you can reference here.

Raspberry Pi Static IP Address (RP3)

So as you may have already found, things work slightly differently with the new Jessie based Debian OS. If you have hit my last Static IP post and thought things were just not quite right, this ones for you!

It seems that /etc/network/interfaces is overruled by dhcpcd meaning your soon to be static ip has to be set elsewhere.

In the /etc/network/interfaces file, your specific interface (iface) should be left as manual. In fact, if you are working with a fresh install the easiest way is just not to touch it at all! If curiosity gets the better of you, there is also a statement in there telling you to look elsewhere to change the settings.

Extract from /etc/network/interfaces
# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

We do still have to know a few things so if you need a recap have a read of my last post on the subject and gather the info you need.

Particularly we are looking for the following
– netmask
– gateway
Obviously the IP address you want to set, provided it is within bounds is totally up to you.

Fire up /etc/dhcpcd.conf with your favourite text editor

Fairly simple… We are going to add 3 lines to the bottom

static ip_address=192.168.1.199/24
static routers=192.168.1.1
static domain_name_servers=127.0.0.1
// In my case i am running a caching nameserver on localhost
// - Hence the directive of 127.0.0.1 for the nameserver.
// In most cases nameserver will be the same address as your gateway

I hear you thinking “What is the /24 on the tail of the IP address?”
– Well i am going to say “Google It!

In this case it is shorthand for netmask of 255.255.255.0 with a broadcast of 255.
If your netmask or broadcast is different, be careful… It may help to use a calculator to determine the leading count.
I feel the rest speaks for itself.

Save the file & restart networking or reboot.
Confirm we are good by running

ifconfig
route -n
cat /etc/resolv.conf //In most cases your gateway address

DEV WordPress on Local Network

The following tutorial has been tested & written on Ubuntu 14.04 LTS running in an Oracle Virtual Machine. The reason i stage this way is to keep my machine as clutter free as possible. When you are not using it, the VM is simply shutdown!

Steps I have taken to set up a Dev WordPress site on my Local Network (Actually Ubuntu Server running in a Virtual Machine), however the following could just as easily be applied to the Raspberry Pi (LAMP installation would be necessary and outside the scope of this tutorial). At present the following has only been tested on Ubuntu Sever.
– Referenced heavily from the WordPress Codex

  • Log into terminal as root
  • Make sure you install Ubuntu with SSH & LAMP options enabled
  • – Set the MySQL password to something memorable & secure. This wil become the MySQL ‘root’ password.

First you have to set up a MySQL user and grant necessary privileges.
Log in to MySQL on the command line using mysql -u root -p where the password is that of what you set upon install.

CREATE DATABASE wordpress;
CREATE USER wpuser@localhost IDENTIFIED BY 'password';
// Make sure the apostrophe stays around the password
GRANT ALL PRIVILEGES ON wordpress.* TO "wpuser"@"localhost" IDENTIFIED BY "password";
FLUSH PRIVILEGES;
EXIT

Make sure the ‘password’ fields above are changed to something memorable & secure. Good habit, even on a local install.

Download the latest version of WordPress to your web directory (/var/www/html/).

cd /var/www/html/
wget https://wordpress.org/latest.tar.gz
tar xvfz latest.tar.gz

As a final step you need to grant Apache privileges for the newly created wordpress folder. Failure to do this will result in the following install to fail.

sudo chown -R www-data:www-data /var/www/html/wordpress

Obviously if you are already running as root you can omit the sudo, however just a quick explanation of what the above command is doing for those not so clued up reading this tutorial. (We all need to start somewhere!)

  • (chown) Is a Linux command to change ownership of a file / folder.
  • (-R) As we are working with a directory, this applies the command recursively.
  • (www-data:www-data) Changes owner to user of group (owner-user:owner-group).
  • (/var/www/html/wordpress) The directory / file we are sighting.

Now the WordPress installation is on the server, we can navigate to 'server'/wordpress/ in a web browser and follow the famous five minute install. Remember the name of the MySQL database and user you set up previously. WordPress will give you options if any of the above steps fail.. Particularly permissions.

Following the installation completion, with any luck you should be up and running!

Raspberry Pi Static IP Address

UPDATE
– Things have changed a little with the new Jessie based image…
See my new tutorial for Jessie based systems

The tutorial below is suitable for all versions of the Raspberry Pi running a Debian based OS (Raspbian etc). Although the older Raspberry Pi’s such as Model A & Model A+ are near obsolete, the structure is the same. Tested on Raspberry Pi Model B, Model B+ & Raspberry Pi 2.

So you want to set your Raspberry Pi to have a static IP address. This is useful in so many ways whether it be for port forwarding, SSH logins or one of the many other reasons you want to have the same persistent address such as a dev server. The process described below is also the same for all debian based systems, including Ubuntu.

First of all some research needs to be done to determine current network settings so that the static address can be applied correctly. Run the following two commands

ifconfig
route -n

Console

Once the above has been run we can collect the following information
– Gateway
– Netmask
– Network
– Broadcast

Once our needed information has been gathered, network interfaces needs to be modified & networking restarted (pi rebooted). As we are working on a fresh install and to keep it simple, i am going to be using the packaged text editor – nano. Run the following command, making sure it is launched as root.

sudo nano /etc/network/interfaces

We then want to edit our primary interface so that it defines a static ip with the necessary configurations. Make sure you change the address to that of your desired static ip. Obviously make sure it is within the bounds of your network. In my case eth0 is my primary interface, however if for example your primary interface was a wireless connection, the following would be configured under wlan0 and so on.

iface eth0 inet static
address 192.168.1.199
gateway 192.168.1.1
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255

Mine ended up looking like this…

Network/Interfaces

Make sure you then save and reboot (sudo reboot).
Done!