top of page

Chapter 6 Move OS from SD to external HDD

Your operating system is on a SD card. They will fail. Quickly, if you are saving video. You can move the OS to an external HDD or a SSD (Solid State Drive).


Raspbian on an external HDD There are two ways to do this. Seeing as the SD card uses a Linux based EXT4 file format system, Windows will NOT recognize the SD card files. There is a Linux program called GParted that will let you work with partitions of dozens of file systems. It is also available from gparted.org as a bootable version for a CD or mempry stick. I like to add gparted to the Pi. It is a handy tool. New for Raspbian, there is a copy SD program. Lets add GParted to the Raspberry pi. It is a handy addition. Click the Raspberry, Preferences, Add / Remove Software A window pops up. Type gparted in the search box, upper left, then Enter Check GNOME partition editor, click Apply, and enter password for the pi. It will take a few minutes to finish. Click OK to close box when finished. You can open GParted at Raspberry, Preferences, GParted.


Now, on to copying the OS Copy the root partition to the external HDD with the SD Card Copier: (You can use gparted, but copy SD is easier) Connect the HDD to the USB port. Close the box that pops up. Click Raspberry, Accessories, SD Card Copier. Copy From Device, select /dev/mmcblk0 (The location of the SD card holding the operating system) Copy To Device, select /dev/sda (The location where your HDD was most likely mounted) Check the New Partition UUID box Click Start. It will show progress. Close when done. It will take some time.


You now have a copy of both the Boot and Root partitions on your HDD. SD Card Copier expands the root file system to fill the entire drive. If you have any intention to shrink the file system and create a data partition, now is the time. Bring up Gparted, go to /dev/sda. Unmount both partitions. Shrink sda2. Add your data partition(s). After you run the commands below, you will be using the HDD partitions and can't modify them. You would have to boot off another SD card with Gparted installed, or use a gparted live bootable media on a windows computer. That makes this the easiest time to modify partitions! More detailed directions below.... WARNING- If you are using gparted live on your Windows computed, select your drive carefully! Gparted can blow away Windows!!! Now, we have to tell the pi where to find the HDD partitions. Point the pi to the new root partition: Reboot your Pi with the SD card and the HDD connected. Open a terminal, Icon in the upper left Type in sudo df -h Enter Notice the last entry... /dev/sda1 is a copy of the SD card boot partition. /dev/sda2 is a copy of the SD card root partition. Type in sudo nano /boot/cmdline.txt A new page will appear (nano is a handy mini text editor) Cursor over to root=PARTUUID=xxxxxx and change to read root=/dev/sda2 Control o to write file, return to accept current file name Control x to exit This tells the bootloader to go to the HDD for the root partition Type in sudo nano /media/pi/boot/cmdline.txt and repeat the above edit. You now have the updated root filesystem pointer in both the SD card and HDD boot partitions. Type in sudo nano /etc/fstab Again, a new page appears Cursor over to the first PARTUUID= entry Remove the PARTUUID= and the ID number immediately after Type in /dev/sda1 and add spaces to put the columns back in order to look nice. You should see /dev/sda1 /boot vfat and so on... Change the next line to /dev/sda2 / ext4 and so on... Control o to write, return to accept current file name Control x to exit. This tells the OS to mount the HDD partition as root Type in sudo nano /media/pi/rootfs/etc/fstab and repeat the above edit. You now have the updated mount commands in both the SD card and HDD root partitions. Close terminal. Reboot. Did it work? Open the terminal. Type in df -h Notice /dev/mmcblk0p1 is mounted at /media/pi/boot, and /dev/mmcblk0p2 is mounted at /media/pi/rootfs. It found the SD card that we aren't using and mounted it. Both SD card partitions are mounted in a new location from where they were originally. That makes them unavailable for the os. The pi won't boot without the SD card, But after first read, the card is no longer used. The HDD boot partition is now active. Exit terminal. So, why did we install GParted? SD Card Copier was so easy! Assuming you want to shrink the root filesystem and add a data partition or two.... and didn't do it earlier... From a Linux machine... OR..You could make or simply make a second SD card so you have an SD card to run on, or gparted live, Boot off the second SD card. (You modified the first SD card to boot from the HDD) if you are going to use your Pi. Connect the HDD. Close the box that pops up Start GParted. Click box, upper right corner to select the device you want. You should see /dev/mmcblko, your SD card booted from, as well as /dev/sda, your HDD. Click through both to see what is there and verify which is which. These wil be different if you use a separate Linux computer.... Select the HDD, probably /dev/sda. Highlight the partition, right click and select Unmount. Unmount all partitions. Right click the root partition (sda2) on the HDD, select Resize, set the new size, then the green checkmark Apply above. When it finishes, click on the empty space at the bottom of the list. Right click, New, set the size, and select the format to be used. (Fat 32 or EXT4 usually).Click Apply. When it finishes, Right click the new partition, and Format. Select the format same as last step. Click Apply. Repeat if you want more partitions.


Extra Credit So, now that you have copied the root file system to the HDD and made it run from there, you are a lot more dependable. SD cards don't like to be written to a lot and will eventually fail. And, you created a Data partition too. But, where did it go???? On booting, the data partition was found and mounted at /media/pi/ Is that where you really wanted it? If you are using a web based application on the pi, there is a good chance you are saving data at /var/www/html/media. When you modified the file /etc/fstab, you could add another line below the rest. /dev/sda3 /var/www/html/media ext4 defaults 0 0 Assumes your data partition was located at sda3 and you formatted it as ext4 This line now takes your data and saves it in this separate partition instead of in the root partition. Why bother???? If your application is saving a lot of data, maybe video, and not testing free space, it could fill up the root partition and crash the system when it runs out of space. So, if you followed all this, you now have a raspberry pi, running the latest version of Raspberry Pi OS, with a fixed IP address that does not change, with the root file system on a HDD, condensed down to what you need, and a separate data partition mounted at a specified location. You should be pretty much ready for ANYTHING!!! Don't forget... If you turned SSH on in the raspberry Pi configuration and set a fixed IP address, you can open a terminal on this computer and remotely log into your Pi. Highlight and copy commands from here and paste them into the terminal. Run the command on the Pi by copy / paste. No typing!


Jim Albrecht, K2BHM

1 view0 comments

Recent Posts

See All

The Raspberry Pi is a small handy computer. Lots of programs are available to do all kinds of things. It's easy to use the mouse and keyboard to bring up the program you need and you're off and runnin

bottom of page