Boot Raspberry 4 OpenHAB From USB

This How To Boot Raspberry 4 OpenHAB From USB helps you to simply move openhabian booting from SD Card to a USB Stick, USB Drive, etc.

Obviously there are various reasons why you like to Boot Raspberry 4 OpenHAB From USB instead from SD Card. You find more of this reasoning in the Additional Information section.

Boot Raspberry 4 OpenHAB From USB

Boot Raspberry 4 OpenHAB From USB


My Setup Used For This How To Boot Raspberry 4 OpenHAB From USB

For this How To I leverage on an existing OpenHAB 3 installation running on openhabian. Further, I am leveraging on a high speed Corsair Flash Voyager GTX 512 GB USB stick that in various benchmarks evidenced a great performance and on a Raspberry Pi 4 8GB. Thats it.

How To Boot Raspberry 4 OpenHAB From  USB

The migration is not too difficult, but there are a couple of points that should be done in the right order. Following the below drives you thru 3 key phases. The first phase is to get ready. The following second phase is to migrate from the SD card to the USB drive / stick. The final phase is to let the Raspberry know the new boot partition and test the migration for success.

Getting Ready

If you have a frequent backup running, then you maybe like to skip the backup activity. However, if you do not have a backup of which you know it is working, then I strongly recommend you backup your full Raspberry before you execute any of the next steps. The backup can be done by for example a simple creation of an image of your current Raspberry 4 SD card.

If you have a proper backup created and for the worst case scenario ready, then let’s in a first step the system is up to date. To update it execute the below two commands. You maybe have also reasons to update the Raspberry firmware. The below video drives thru the whole scene as well, allowing you to see what happens before you execute

Update the software repository

sudo apt update

Run a full upgrade of the system

sudo apt full-upgrade

Jump to As Is Situation or continue with Optional Steps.

Optional steps
Only execute if you need the following two updates for the firmware.

(Optional) Update the Raspberry firmware

sudo rpi-update

(Optional) Double-check whether the eeprom is up to date


As Is Situation

Now understand the “as is situation” and make a couple of notes that are key for next steps. To do so, you like to execute a couple of commands.

Let’s understand which storage devices are plugged already into the Raspberry 4. Ideally, you have not yet plugged your USB stick or drive into the Raspberry. 

sudo lsblk
Find Out Which Devices Are Available

Find Out Which Devices Are Available

You most likely notice a device that is called something like mmcblk0. This most likely is your SD card. Now lets plug the USB stick or drive into the Raspberry and execute again the lsblk command.

sudo lsblk<br><br>
lsblk command including the USB Stick

lsblk command including the USB Stick

As you can see, in the second time, after plugging the USB stick, a new drive got visible. In this case its called sda. sda, sdb, (….), sdX typically are USB drives / sticks.

Not down two items (of course you adjust it for what were the outputs when you executed lsblk:

  • mmcblk0 -> SD Card -> mmcblk0p1 part boot and mmcblk0p2 part /
  • sda -> USB stick

Ensure your USB stick is empty. In any case, ensure you have a backup of all data stored on your USB stick! The following step will delete all data on your USB stick!

 sudo sfdisk --delete /dev/sda 

After execution of the above, you will notice that the USB stick has no partition anymore. You see this if you execute again lsblk.

Now… we are ready to rock!

Migrate Raspberry 4 OpenHAB From SD to USB

Rpi-clone is a fantastic tool to clone in a running system an SD card to a USB stick / drive. We however first should install it.&nbsp

git clone 
cd rpi-clone
sudo cp rpi-clone rpi-clone-setup /usr/local/sbin
sudo rpi-clone-setup -t $(hostname -s)

Once done we start to clone. Ensure that you replace sda by whatever sdX your USB drive is. Please double check and ensure you have backup. Selecting the wrong sdX will delete all your data. As seen in the pictures above, in this how to the right sdX is sda. The cloning might takes some time. The time depends on the disk size. Even it takes long, keep the SSH window or Terminal open as you will need it for the next step. If you do not keep it open, then you carry the risk of need for manual remount. 

openhabian@OpenHAB:~ $ cd ~/rpi-clone
openhabian@OpenHAB:~ $ sudo rpi-clone sda

Booted disk: mmcblk0 31.9GB                Destination disk: sda 512.1GB
Part      Size    FS     Label           Part   Size  FS  Label  
1 /boot   256.0M  fat32  --                                      
2 root     29.5G  ext4   rootfs                                  
== Initialize: IMAGE partition table - partition number mismatch: 2 -&gt; 0 ==
1 /boot               (50.0M used)   : MKFS  SYNC to sda1
2 root                (17.1G used)   : RESIZE  MKFS  SYNC to sda2
Run setup script       : no.
Verbose mode           : no.
** WARNING **          : All destination disk sda data will be overwritten!

Initialize and clone to the destination disk sda?  (yes/no): yes
Optional destination ext type file system label (16 chars max): OpenHAB

After successful clone, we check whether cloning was successful and source and destination partitions have the same layout. Same time we find out the partition IDs which is important for setting the target root partition. This target root partition should obviously be the one on the USB drive.

sudo lsblk -o name,mountpoint,size,partuuid
Find Out Partition IDs (PARTUUID) of the USB Boot Drive

Find Out Partition IDs (PARTUUID) of the USB Boot Drive

As you can see in the above picture, the partition ID’s (PARTUUID) of the USB Stick (sdaX) are different to the one’s from the SD card (mmcblk0X). You like to note down the partition IDs of the USB root partition which in the below picture is c6e1249c-02.You maybe ask how I as able to find out. In this case its very simple, since it is a clone, the partition size of the boot partition must be the same, hence 256M. Therefore the root partition must be the other one, sda2.

 Set New Root Partition To Be Booted On

You arrived now in the final phase. Since we successfully cloned the SD card to the USB stick the foundation is set. Now we need to tell the Raspberry which partition is the new root (sda2). To do so we edit the cmdline.txt file on the future USB Stick boot partition (sda1). Lets get it done!

First and if you have not already, mount the future USB Stick Boot partition. 

sudo mkdir /mnt/clone/boot
sudo mount /dev/sda1 /mnt/clone/boot

Once done we edit the cmdline.txt file and add the partition ID of the cloned USB Stick boot partition (PARTUUID) which was in the above c6e1249c-01.

sudo vi /mnt/clone/boot/cmdline.txt

Once done, it should look like something like this:

console=serial0,115200 console=tty1 root=PARTUUID=c6e1249c-02 rootfstype=ext4 rootwait

At this point, you are nearly done, but there is one more step required. You need to update fstab. If you have not already, mount the future USB Stick Root (sda2) partition. 

sudo mkdir /mnt/clone/root
sudo mount /dev/sda2 /mnt/clone/root

Next step, update fstab and change it accordingly.

sudo vi /mnt/clone/root/etc/fstab

After your changes it should look like something like this:

proc            /proc           proc    defaults          0       0
PARTUUID=c6e1249c-01  /boot           vfat    defaults,flush    0       2
PARTUUID=c6e1249c-02  /               ext4    defaults,noatime  0       1
# a swapfile is not a swap partition, no line here
#   use  dphys-swapfile swap[on|off]  for that

You are ready to test now! To do so execute:

sudo umount /mnt/clone/root
sudo umount /mnt/clone/boot<br>sudo shutdown -h now

After Execution of shutdown -h now

If you have a Raspberry 4, then remove the SD card to test whether your migration was successful. Wait a couple of minutes. Take of the USB-C cable that provides your Raspberry 4 with power. Once done, wait 30 seconds, plug the USB-C cable again to provide power and the Raspberry should start again.

If you’ve been successful, and if you selected a fast USB Stick / Drive, then you will notice an amazing speed improvement. I have not made any speed tests, but its amazing how much faster may raspberry with the brand-new Corsair Flash Voyager GTX 512 GB USB 3.1 Memory Stick Black.

If it did not work out for you, restore your backup and try again by carefully going thru each of the steps. Additionally you will find in the Additional Information section links to great resources that provide further and detailed background on the in the above executed commands. 

Additional Information

What speaks for leveraging on USB sticks

Having moved OpenHAB 3 openhabian from the SD card to a high performing USB stick like the Corsair Flash Voyager GTX 512 GB USB 3.1 Memory Stick Black provides a number of advantages:

  • Many Internet forums talk about issues with SD cards in the long run. SD cards are not made for applications that executes high amounts of read/write. Depending on the persistence you use for your OpenHAB, many read/write are necessary. This can be getting even worse if you have many smart devices and log a lot of data into your persistence
  • Leveraging on OpenHAB since 2014 and always using Raspberry for it, I learned quickly that SD cards in the long term are not reliable. Since about 2016 I am leveraging on Corsair Flash Voyager memory sticks since I learned by my experience that these sticks are fast and reliable. I never made the experience that one of these USB sticks made any problems. The opposite however I made with SD cards.
  • Its simple, but you get far more space for the same price with a USB stick as you get with a SD card
  • Also simple, the read / write speed of USB sticks (in combination with USB 3.x) is far higher then with SD cards

Certainly you will find in the internet plenty of other reasons why not leveraging on SD cards. Let me know if you have some other great reasons. I am happy to add them. 

Some Links For Researching And Learning on Additional Background For The Above