Raspi 2: Move system root to usb-SSD

Following the suggestion of @rikoshak, here is small tutorial how to move an existing openhabian installation from a SD-card to an usb-SSD on a RaspI 1 or 2.
Because openhabian is based on raspbian, this tutorial is also supposed to work with raspbian.

First of all, why should one move the root-system from the SD-card to an usb-SSD?
The problem is, that the SD-card gets corrupted over time and your system will fail then.
As many people I used to think that it would be sufficient to use a large card from a premium manufacturer to not face this problem, but that has been proved wrong.
Probably the frequent persistence writes kill the card. SSDs have large ram caches and a sophisticated wear management to cope with that.

What you need
An external usb-case and a fitting ssd. A usb stick will also work, but I’m not convinced that it would be an improvement over using an SD-card.

Step 0:
MAKE A BACKUP OF YOUR SD-card ( … just in case …)

Step 1: More power for usb hub of the Raspi
This this change enables that the USB hub of the Raspi delivers 1A, so you will have around 5W of power for your ssd. I’m using an old Samsung 64GB ssd that runs without an external power supply, but that depends on the ssd.

Edit the config.txt of your system

sudo nano /boot/config.txt

append the line

max_usb_current=1

reboot,

sudo reboot

and plug in your ssd.

Step 2: Partition the ssd and put a filesystem on it
Figure out the device name of you usb-ssd. There are different methods to do that.
You can use lsblk

sudo lsblk

or read dmesg

sudo dmesg

I think that your ssd will most likely have the device name “sda”.

Partition your ssd drive with fdisk

sudo fdisk /dev/sda

After creating a partition, leave with w (write and exit)

Assuming you have created the partition “sda1”, name it and create a filesystem on it.

sudo mkfs.ext4 -L oh_ssd /dev/sda1

Step 3: Copy the root system from the SD-card to the ssd
Be warned that copying a running system is considered to be “dirty” for some reasons. It works for me, your mileage may vary : -)

First mount the ssd

sudo mount /dev/sda1 /mnt

Check if everything’s fine: If you

ls /mnt

you should see the “lost+found” dir of your newly created ssd partition.

Stop openhab and copy the root system from the SD-Card to the ssd with tar. There are also other methods with rsync for example.

sudo systemctl stop openhab2

sudo tar cf - --one-file-system --exclude=/mnt/* --exclude=/proc/* --exclude=/lost+found/* --exclude=/sys/* --exclude=/media/* --exclude=/dev/* --exclude=/tmp/* --exclude=/boot/* --exclude=/run/* / | ( cd /mnt; sudo tar xfp -) 

Step 4: Adjust cmdline.txt and fstab

sudo nano /mnt/etc/fstab

Change the line of the root filesystem (second line in the example below) to the partition that you’ve created on your ssd. It is important that /boot must stay on the SD-Card (mmcblk0p1)

/dev/mmcblk0p1 /boot vfat defaults 0 2
/dev/sda1 / ext4 errors=remount-ro,noatime 0 1
tmpfs /tmp tmpfs defaults,nodev,nosuid 0 0

Ok, now you’re nearly set. Last thing is to tell the kernel where the root filesystem resides in future.

Edit /boot/cmdline.txt

sudo nano /boot/cmdline.txt

change the parameter root=/dev/mmcblk0p2 to your ssd partion (/dev/sda1)

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 elevator=deadline net.ifnames=0 root=/dev/sda1 rootfstype=ext4 rootwait

Step 5: Try it out
First double check all changes that you have done. If you’ve made an error, your system will likely get stuck at boot …
Than reboot.

sudo reboot

If you can relogin after some seconds, check if your really running on your ssd.

sudo df

_

Filesystem     1K-blocks    Used Available Use% Mounted on
udev               10240       0     10240   0% /dev
tmpfs             199164   12828    186336   7% /run
/dev/sda1       61407392 2244268  56020680   4% /
tmpfs             497908       0    497908   0% /dev/shm
tmpfs               5120       0      5120   0% /run/lock
tmpfs             497908       0    497908   0% /sys/fs/cgroup
tmpfs             497908      32    497876   1% /tmp
/dev/mmcblk0p1    130798   60150     70648  46% /boot
tmpfs              99584       0     99584   0% /run/user/1000

If the line with your root system (mounted on == “/”) reads “/dev/sda1”, then congrats, you’ve made it!

11 Likes

Great article!
That’s a nice addition in general and of course to openHABian. If you want to, I would happily accept a Pull Request to add this process to the openhabian-config menu as an optional step. :wink:

2 Likes

By openhabian setup you mean this will automatically move everything to the ssd?.. if so could you do that for the pi3 as well?!

Have done some scripting but I need to test it, not sure if I have time for it this weekend.

If the bash script works fine, I’ll try to integrate it into openhabian-setup.sh.
Maybe I’ll need some guidance …

does show the following at my RPi2:

NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0 29.8G  0 disk
mmcblk0     179:0    0   30G  0 disk
├─mmcblk0p1 179:1    0  128M  0 part /boot
└─mmcblk0p2 179:2    0 29.9G  0 part /

When I call the fdisk command, I get the following output:

Welcome to fdisk (util-linux 2.25.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x8c1f768e.

Command (m for help):

I quit using “w” getting the following output:

The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

But when I try to create the filesystem, I get the following error:

mke2fs 1.42.12 (29-Aug-2014)
The file /dev/sda1 does not exist and no size was specified.

Any ideas?

Thanks,
Boby

Seems not to be a noob tutorial, because it is assumed to do some things while being in the fdisk tool. For noobs like me: Here’s what I did for creating a partition:

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-62533295, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-62533295, default 62533295):

Created a new partition 1 of type 'Linux' and of size 29.8 GiB.

Command (m for help): w

Besides this: Great tutorial, thank you! Just moved my root fs within a few minutes. Thumbs ub!

Yes, you’re right, I didn’t describe the creation of the partion in detail, sorry for that.

The problem is that the disk can have already partions from its former life, so that you need to delete them before you can create one big (sda1) partion.

In the script to automate the process I’m using cfdisk, it has the command --delete to delete all partions (as far as I understand it), but I didn’t make progress with it the last few days.

I hope it fully works out for you now.

@ThomDietrich: I’ve integrated the process into openhabian-setup.sh now, see the PR

2 Likes

Merged! Thank you very much :wink:

Hi all,

Sorry to revive an old thread, but I’m trying to set up the file system on my SSD but I get the following message:

Can anyone help me with this? Does it mean my installation is on my SSD? This is a fresh install of OpenHABian.

sudo df:

Many thanks in advance

Hi,
sorry to hear that.

What does

cat /boot/cmdline.txt

say?

Hi Peter,

Thanks for your response.

The command shows the following:

I don’t understand a word but hope it helps :slight_smile:

Hi Craig,

that helps (at least a bit :-).

If that’s the cmdline.txt that one would get straight from setting up a new openhabian image, then “moye system root to usb” would not work for the time being.

You said that you did a fresh install of openhabian. Does that mean that you downloaded an openhabian-image and wrote that onto the SDcard via something like etcher or dd?

Are you using some kind of bootmanager like NOOBS or berryboot?

Hi Peter,

My fresh installation was done using Win32 Disk Imager to write the image to hear SD card. After a long while I could hen SSH to Openhabian.

On my previous installation I ran the ‘move root system to SSD’ option and it worked fine. I’ve tried a couple of times now with the same issue.

I only boot using the Openhabian image, no boot managers or anything like that. Really stumped now

Hi Craig,

it seems as if the reference to the root-partion for the bootloader has changed from an absolut reference (“root=/dev/mmcblk0p2”) to the usage of the partion uuid (root=PARTUUID=…).

I guess a quick fix for you would be to edit cmdline.txt (sudo nano /boot/cmdline.txt) and to replace the part

root=PARTUUID=3a958045-02
with
root=/dev/mmcblk0p2

Afterwards reboot and then it should work again.
Beware that there is a slight chance that the system is rendered unbootable after the change, so it would be wise to make a backup copy of the working cmdline.txt.
The type of the boot-partion is fat32, therefore it is easy accessible from a windows-machine.

I’ll try to make a fix for the issue so that it’s working out of the box again.

Peter

IMPORTANT ADDITION:
Please also check your /etc/fstab. If the root-partion is referenced there via partion uuid, that need to be changed also to /dev/mmcblk0p2 before using the function “move system root …”

Hey guys, the issue is actually not new, there was another user seeing the same thing. I didn’t have the time to fix this yet. @peter_juenger if you’d like throw a PR my way :wink:

Hi @ThomDietrich,

I’ve created a PR for that issue. Tested with new partuuid syntax and also old style dev / partion names, works fine here.

Hi @peter_juenger,

Please let me know when it is ready and I would be happy to test it.

Cheers,

Craig

Hi Peter,

The suggestion to edit cmdline.txt worked in that the file system was copied to SSD. After that however my connection times out on reboot :frowning:

My fstab looks like this…

changed to…

When the fix is in place on the openhabian-config menu please let me know and I would be happy to test it again.

Cheers

Merged. Please “Update” and try again :wink:

Thanks @peter_juenger for the excellent fix!