It has been visited a million+ times, so it has been useful to many people.
I was honored when the forum guys selected this post as "sticky", making it easy to find.
Recently, I updated the procedure to more reflect the changes in Raspbian.
Here is the link : https://www.raspberrypi.org/forums/viewtopic.php?f=29&t=44177
Enjoy!
If you like what you see, please support me by buying me a coffee: https://www.buymeacoffee.com/M9ouLVXBdw
To make sure this post does not get lost, I copied it over from the Raspberry Pi Forum,
This procedure will allow you to run Debian from a USB connected drive, instead of the SD card. (latest update: April 2020)
The RPi will still boot from the SD card, but in essence, will no longer write to it. The original idea was to keep the SD card from failing due to excessive writing to it. It will NOT protect you from disk corruption when you pull the power plug without first properly shutting down or halting the RPi. The chances are less likely with this setup, because the corruption happens during write operations to the SD card, but still.
Depending on your setup, this procedure can provide some overall speed improvements, but greatly removes the speed, size and reliability requirements for the SD card.
Starting with the 2017-04-10 kernel version, the Foundation changed to a more reliable way of using drive identifiers.
In more recent releases, PARTUUID=xxxx is now used in /boot/cmdline, and in /etc/fstab by default.
In Buster, there were some more changes, and from then on, you can use. It will be evident when you look at your cmdline and fstab files.
This method is not for NOOBS installations!
This procedure works with Jessie, Stretch and Buster. Newer versions may work but are not tested unless they are added to the list.
Using a USB Flash "stick"
If you're looking for a speed improvement, this procedure will most likely be a disappointment when you are done. The speed advantage, if at all, is at the theoretical very best, only 2x.
Using a "real" disk drive
Things change dramatically however when you use a "real" USB disc or even better, an SSD drive, although the maximum speed is still limited due to the architecture of the RPi hardware. I have just recently tested this procedure an RPi Model 4 using Buster light and that works well too.
If you have an RPi Model 3, you can also try out the latest method of displacing the SD card completely, and really boot from your USB drive. Have a look here, but remember, this is still under development at this very moment and may not work with all drives or in all situations.
https://www.raspberrypi.org/documentati ... des/msd.md It worked for me on a 3B+ and a (rotating) hard disk but there were some issues to go through because the automatic resize procedure for my disk did not work. You need to do this "by hand" but that is not part of this procedure.
If you have an RPi Model 4, you cannot boot directly from another disk (yet) like you can with the Model 3, so this is one way to allow you to run the model 4 from a different and even faster disk, using the USB 3 speeds.
You can also do this for an existing installation It does not need to be a new installation of Debian on your SD card, you can perform this procedure also on an already existing and running system. There is one caveat however, some applications seem to have the access to the SD card "hard-wired", in in those cases it will not work and you have to do this procedure before installing the app. It does not hurt to try though. Just be aware that copying of the data from an already running installation from the SD card to the new drive will take a bit longer due to the data in /var and others.
1. Installation
Power down your RPi or leave it powerless.
Connect your USB stick or drive to the USB slot of the RPI now. Only use one stick or drive for this procedure to avoid mistakes! If you have a real hard disk, it is even more important that you connect that to the USB port before you boot. Older model Pi's cannot handle the power surge when they are running, and will reboot abruptly and can damage the SD card.
After a reboot, check to see if your USB drive is recognized:Device 004 is the USB stick I'm using. The ID shows a unique manufacturer identifier.
Also check to see if the OS recognized the device: (you can also use the command dmesg | grep sda)Yes, the USB stick can be referenced as /dev/sda
There is no way of knowing what name the OS assigns to additional USB drives. It could also be /dev/sdb. To prepare for the eventuality of you adding another USB drive or stick, we are going to prepare the RPI for that now.
To prepare the drive to hold a file system, we are going to create one or more partitions, and then format them.
In most cases, the standard fdisk works fine. In some cases, you may have to use parted to get the right UUID information. You can also use gdisk. The instructions are the same.
Invoketo Creating Partition(s)You may get an error message because there could be a partition there already, formatted for Windows that was installed by default. We're going to re-partition and re-format the drive for Linux.
If there is a partition, enter d to delete the partion(s). It will say "Using (partition) 1", that's the default and OK.
Note
I'm now recommending that you pause for a moment, and consider to create two partitions as a minimum. If you have a large USB disk, you may even want more, and now is the time to do it. I will be creating two partitions on my stick. Partition 1 will hold the Raspbian filesystem, and partition 2 will hold my data. I like to keep those two separate as much as I can.
The first partition, for Raspian, can be any size you want (but larger than 2 GB). I'm going to create an 8GB partition, and use the rest for data. The size of the Raspian partition does matter, because the larger it is, the more room the "internal" file system on the card/stick has to move blocks around and optimize the "wear" leveling on these flash drives. If you have a "real" disk, this does not matter.
If you want more partitions, do as I do with the second, just keep track of the size. You may want to put that disk structure on paper first because backtracking is very difficult unless you are an expert.
Create the partitions:
Enter n to create a new partition, and select number 1:
Select the starting sector by hitting Return, then select +8G for the size 0f 8GByte. Now select the default filesystem ('Linux filesystem') by hitting Enter again.OK, now the second partition. I will use one that fills the rest of the disk. If you want more, use the default for the start, and give the size again, and continue until you are done.Now use v for verify to see if everything worked. If you don't like what you see, of if you changed your mind, or made a mistake, no sweat, just hit Ctrl-C and start again. If you are happy, hit w for write to make it permanent. You get one more chance to make up your mind.Phew, take a deep breath again, you need the oxygen for the next step.
Formatting the partition(s)
You need to format every partition you created in the previous step. After the partitioning, we now have more devices. device /dev/sda now has two children, called sda1 and sda2 or even more if you were (re)productive.
Do the same for the second partition :-L is for label, you can call the additional partitions anything you want.
Mounting the root partition
In order to use the new partitions, we need to mount them. Initially we're only going to mount the first partition that will hold the Raspian files (also called rootfs):Let's see what we've done so far:We made the 8GB drive /dev/sda1 available as /mnt for Raspian, so now we can start to use it.
We can copy (mirror) the Debian filesystem data currently on the SD card in /dev/root to the new drive. You can use dd, but that is a bit copy program and, because it also copies the empty space on the SD card, it is very, very slow.
It is better to use rsync that we already installed earlier.
Rsync is a lot faster, but it will still take some time to create a mirror image of the complete file system on the USB drive. We will copy everything from the root "/" to the just mounted drive /mnt (which is /dev/sda1 at the moment)
Because of the -v flag, you can see the files flashing by on the screen, so you can see the progress. You also get an appreciation about the complexity of a complete system.
On my Model B system it took only 5 minutes. Make sure this process does not get interrupted! If there are errors, have a look to see if it is something obvious, or just try the rsync again, or even the whole process. Don't continue unless this worked without errors.
Setting up the new boot procedure
We're now going to tell the system where to find the new filesystem when we are going to boot from the new drive.
The first file we need to change is /boot/cmdline.txt on the SD card.
Before we do that, we're going to create backup copies of that important file so we can easily switch back and forth to booting from the SD card or from the USB disk.This will hold the original boot instructions.
The contents of this file is one very long command string, and we need to replace the reference to the root partition on the SD card to the new USB drive.
Here is a copy of the original file on my SD card:We are going to change the PARTUUID.
Here's how you do that. I may oversimplify this a bit, but every drive, and in addition, every partition we just created now has a unique identifier. There is a UUID for the drive itself, and a PARTUUID for every partition. Here is where you can see that :(empty lines added for clarity)
For the cmdline.txt file in /boot, we're going to tell the system what partition to boot from, so we need the PARTUUID for the new drive for that. Instead of "root=PARTUUID", we now need to use from blkid: the PARTUUID=62576684-01 from /dev/sda1. First highlight that string with your mouse to copy it. Open the editorand past that into the open editor instead of the PARTUUID for root (without the "). Make sure that the one line still stays one line, or it may not boot. Save the file. Also save this new /boot/cmdline.txt file so we can switch back and forth later on.Check that you have three versions of this file before you move on. One .txt, the current version, one .sd the original one, and one .usb, the new one.
NOTE 1
To be on the safe side, there is one more command we could add to the configuration. During the power-up/boot sequence, it may take the USB drive a little longer to spin up to speed and be ready, than the OS expects. The default waiting period seems to be 2 seconds for USB devices.
Addto the end of the /boot/config.txt file.
NOTE 2
Depending on the power consumption of your stick or drive, and if you use an RPI that supports it, you can also add:in /boot/config.txt to let the RPI send more juice to the drive.
Mounting the new drive during boot
The next step is to actually mount the rootfs system during the boot process. This is handled by the file system during the later stages of the boot process itself. It uses the information located in the file /etc/fstab.
NOTE We are only going to change the data on the new drive, so we keep the SD card unchanged. This will make it easier to go back and forth with only changing the /boot/cmdline.txt file on the SD card.
It will show something like this:We will now change the reference to "/", which is the root filesystem and use the new USB drive instead. Change the PARTUUID for the line with "/" with the PARTUUID of the new drive (62576684-01)
We are ready to reboot the RPI.
It helps if you still have a console connected so you can see what is going on during the boot process. And hold your breath again! It will most likely work first time.
When it worked, and you could log in, check the correct root disk by issuing:it should report, which is the new drive.
Note that in some cases, depending on your new drive, the booting process may be a little longer than you are accustomed too.
If something goes wrong however, you need to look at the error messages and back-track carefully. This procedure really works, if you follow it carefully and not make short-cuts or changes. Make it work first, then change whatever you want later.
If it does work, and you have everything running on the USB stick or drive, you can also add the second or more partitions to /etc/fstab.
Adding the second Partition
If you now want to add the second partition on the USB drive, the one I designated for data, do as follows. First create a place holder to mount to.The last statement will set the ownership rights so you don't need to "sudo" to read/write to this drive. To make it permanently available, you can add one more line to /etc/fstab, like so :where xxxx is the PARTUUID from the partition you created.
Reboot, or use "sudo mount -a" and see if you can access that partition too (ls -al /mnt/my_data), it should only have a "lost+found" file.
If that also works, you can now update and upgrade Debian:And off you go...
Changing the process back to the SD card
If you ever run into problems booting from the USB drive, you can move the original /boot/cmdline.txt back by putting the SD card back into your PC, and use the Filemanager to copy the file /boot/cmdline.sd back to /boot/cmdline.txt. You don't need to do anything to the /etc/fstab file on either the SD card or the USB drive if you are going to boot from the SD card. (watch out: if you want to do this while the card is in your PC, do not use rename, because Windows may rename the file called cmdline.txt to cmdline.txt.txt !)
If your system still works with the USB stick, you can also make the changes to the cmdline files from within Debian (in /boot). You can also run a disk check in the USB drive when you have dismounted that drive and run from the SD card.
Note
One thing you need to keep track off is the difference in kernel versions between the boot code on the SD card and the file system on the USB drive when you apt-get upgrade. The original kernel software on the SD card may get out of synchronization with the newer boot files on the DOS partition. One way to fix that is to revert back to the SD card and also update that file system with apt-get upgrade.
This updated version/revision was made possible by the contributions of many users. Their posts are below, just be aware that many inputs or comments are now outdated, or hopefully integrated. It was the only way for me to keep new users from going through 12 pages (currently) of comments, discussions and other contributions.
Enjoy and stay tuned!
Paul
The RPi will still boot from the SD card, but in essence, will no longer write to it. The original idea was to keep the SD card from failing due to excessive writing to it. It will NOT protect you from disk corruption when you pull the power plug without first properly shutting down or halting the RPi. The chances are less likely with this setup, because the corruption happens during write operations to the SD card, but still.
Depending on your setup, this procedure can provide some overall speed improvements, but greatly removes the speed, size and reliability requirements for the SD card.
Starting with the 2017-04-10 kernel version, the Foundation changed to a more reliable way of using drive identifiers.
In more recent releases, PARTUUID=xxxx is now used in /boot/cmdline, and in /etc/fstab by default.
In Buster, there were some more changes, and from then on, you can use. It will be evident when you look at your cmdline and fstab files.
This method is not for NOOBS installations!
This procedure works with Jessie, Stretch and Buster. Newer versions may work but are not tested unless they are added to the list.
Using a USB Flash "stick"
If you're looking for a speed improvement, this procedure will most likely be a disappointment when you are done. The speed advantage, if at all, is at the theoretical very best, only 2x.
Using a "real" disk drive
Things change dramatically however when you use a "real" USB disc or even better, an SSD drive, although the maximum speed is still limited due to the architecture of the RPi hardware. I have just recently tested this procedure an RPi Model 4 using Buster light and that works well too.
If you have an RPi Model 3, you can also try out the latest method of displacing the SD card completely, and really boot from your USB drive. Have a look here, but remember, this is still under development at this very moment and may not work with all drives or in all situations.
https://www.raspberrypi.org/documentati ... des/msd.md It worked for me on a 3B+ and a (rotating) hard disk but there were some issues to go through because the automatic resize procedure for my disk did not work. You need to do this "by hand" but that is not part of this procedure.
If you have an RPi Model 4, you cannot boot directly from another disk (yet) like you can with the Model 3, so this is one way to allow you to run the model 4 from a different and even faster disk, using the USB 3 speeds.
You can also do this for an existing installation It does not need to be a new installation of Debian on your SD card, you can perform this procedure also on an already existing and running system. There is one caveat however, some applications seem to have the access to the SD card "hard-wired", in in those cases it will not work and you have to do this procedure before installing the app. It does not hurt to try though. Just be aware that copying of the data from an already running installation from the SD card to the new drive will take a bit longer due to the data in /var and others.
1. Installation
Power down your RPi or leave it powerless.
Connect your USB stick or drive to the USB slot of the RPI now. Only use one stick or drive for this procedure to avoid mistakes! If you have a real hard disk, it is even more important that you connect that to the USB port before you boot. Older model Pi's cannot handle the power surge when they are running, and will reboot abruptly and can damage the SD card.
After a reboot, check to see if your USB drive is recognized:Device 004 is the USB stick I'm using. The ID shows a unique manufacturer identifier.
Also check to see if the OS recognized the device: (you can also use the command dmesg | grep sda)Yes, the USB stick can be referenced as /dev/sda
There is no way of knowing what name the OS assigns to additional USB drives. It could also be /dev/sdb. To prepare for the eventuality of you adding another USB drive or stick, we are going to prepare the RPI for that now.
To prepare the drive to hold a file system, we are going to create one or more partitions, and then format them.
In most cases, the standard fdisk works fine. In some cases, you may have to use parted to get the right UUID information. You can also use gdisk. The instructions are the same.
Invoketo Creating Partition(s)You may get an error message because there could be a partition there already, formatted for Windows that was installed by default. We're going to re-partition and re-format the drive for Linux.
If there is a partition, enter d to delete the partion(s). It will say "Using (partition) 1", that's the default and OK.
Note
I'm now recommending that you pause for a moment, and consider to create two partitions as a minimum. If you have a large USB disk, you may even want more, and now is the time to do it. I will be creating two partitions on my stick. Partition 1 will hold the Raspbian filesystem, and partition 2 will hold my data. I like to keep those two separate as much as I can.
The first partition, for Raspian, can be any size you want (but larger than 2 GB). I'm going to create an 8GB partition, and use the rest for data. The size of the Raspian partition does matter, because the larger it is, the more room the "internal" file system on the card/stick has to move blocks around and optimize the "wear" leveling on these flash drives. If you have a "real" disk, this does not matter.
If you want more partitions, do as I do with the second, just keep track of the size. You may want to put that disk structure on paper first because backtracking is very difficult unless you are an expert.
Create the partitions:
Enter n to create a new partition, and select number 1:
Select the starting sector by hitting Return, then select +8G for the size 0f 8GByte. Now select the default filesystem ('Linux filesystem') by hitting Enter again.OK, now the second partition. I will use one that fills the rest of the disk. If you want more, use the default for the start, and give the size again, and continue until you are done.Now use v for verify to see if everything worked. If you don't like what you see, of if you changed your mind, or made a mistake, no sweat, just hit Ctrl-C and start again. If you are happy, hit w for write to make it permanent. You get one more chance to make up your mind.Phew, take a deep breath again, you need the oxygen for the next step.
Formatting the partition(s)
You need to format every partition you created in the previous step. After the partitioning, we now have more devices. device /dev/sda now has two children, called sda1 and sda2 or even more if you were (re)productive.
Do the same for the second partition :-L is for label, you can call the additional partitions anything you want.
Mounting the root partition
In order to use the new partitions, we need to mount them. Initially we're only going to mount the first partition that will hold the Raspian files (also called rootfs):Let's see what we've done so far:We made the 8GB drive /dev/sda1 available as /mnt for Raspian, so now we can start to use it.
We can copy (mirror) the Debian filesystem data currently on the SD card in /dev/root to the new drive. You can use dd, but that is a bit copy program and, because it also copies the empty space on the SD card, it is very, very slow.
It is better to use rsync that we already installed earlier.
Rsync is a lot faster, but it will still take some time to create a mirror image of the complete file system on the USB drive. We will copy everything from the root "/" to the just mounted drive /mnt (which is /dev/sda1 at the moment)
Because of the -v flag, you can see the files flashing by on the screen, so you can see the progress. You also get an appreciation about the complexity of a complete system.
On my Model B system it took only 5 minutes. Make sure this process does not get interrupted! If there are errors, have a look to see if it is something obvious, or just try the rsync again, or even the whole process. Don't continue unless this worked without errors.
Setting up the new boot procedure
We're now going to tell the system where to find the new filesystem when we are going to boot from the new drive.
The first file we need to change is /boot/cmdline.txt on the SD card.
Before we do that, we're going to create backup copies of that important file so we can easily switch back and forth to booting from the SD card or from the USB disk.This will hold the original boot instructions.
The contents of this file is one very long command string, and we need to replace the reference to the root partition on the SD card to the new USB drive.
Here is a copy of the original file on my SD card:We are going to change the PARTUUID.
Here's how you do that. I may oversimplify this a bit, but every drive, and in addition, every partition we just created now has a unique identifier. There is a UUID for the drive itself, and a PARTUUID for every partition. Here is where you can see that :(empty lines added for clarity)
For the cmdline.txt file in /boot, we're going to tell the system what partition to boot from, so we need the PARTUUID for the new drive for that. Instead of "root=PARTUUID", we now need to use from blkid: the PARTUUID=62576684-01 from /dev/sda1. First highlight that string with your mouse to copy it. Open the editorand past that into the open editor instead of the PARTUUID for root (without the "). Make sure that the one line still stays one line, or it may not boot. Save the file. Also save this new /boot/cmdline.txt file so we can switch back and forth later on.Check that you have three versions of this file before you move on. One .txt, the current version, one .sd the original one, and one .usb, the new one.
NOTE 1
To be on the safe side, there is one more command we could add to the configuration. During the power-up/boot sequence, it may take the USB drive a little longer to spin up to speed and be ready, than the OS expects. The default waiting period seems to be 2 seconds for USB devices.
Addto the end of the /boot/config.txt file.
NOTE 2
Depending on the power consumption of your stick or drive, and if you use an RPI that supports it, you can also add:in /boot/config.txt to let the RPI send more juice to the drive.
Mounting the new drive during boot
The next step is to actually mount the rootfs system during the boot process. This is handled by the file system during the later stages of the boot process itself. It uses the information located in the file /etc/fstab.
NOTE We are only going to change the data on the new drive, so we keep the SD card unchanged. This will make it easier to go back and forth with only changing the /boot/cmdline.txt file on the SD card.
It will show something like this:We will now change the reference to "/", which is the root filesystem and use the new USB drive instead. Change the PARTUUID for the line with "/" with the PARTUUID of the new drive (62576684-01)
We are ready to reboot the RPI.
It helps if you still have a console connected so you can see what is going on during the boot process. And hold your breath again! It will most likely work first time.
When it worked, and you could log in, check the correct root disk by issuing:it should report, which is the new drive.
Note that in some cases, depending on your new drive, the booting process may be a little longer than you are accustomed too.
If something goes wrong however, you need to look at the error messages and back-track carefully. This procedure really works, if you follow it carefully and not make short-cuts or changes. Make it work first, then change whatever you want later.
If it does work, and you have everything running on the USB stick or drive, you can also add the second or more partitions to /etc/fstab.
Adding the second Partition
If you now want to add the second partition on the USB drive, the one I designated for data, do as follows. First create a place holder to mount to.The last statement will set the ownership rights so you don't need to "sudo" to read/write to this drive. To make it permanently available, you can add one more line to /etc/fstab, like so :where xxxx is the PARTUUID from the partition you created.
Reboot, or use "sudo mount -a" and see if you can access that partition too (ls -al /mnt/my_data), it should only have a "lost+found" file.
If that also works, you can now update and upgrade Debian:And off you go...
Changing the process back to the SD card
If you ever run into problems booting from the USB drive, you can move the original /boot/cmdline.txt back by putting the SD card back into your PC, and use the Filemanager to copy the file /boot/cmdline.sd back to /boot/cmdline.txt. You don't need to do anything to the /etc/fstab file on either the SD card or the USB drive if you are going to boot from the SD card. (watch out: if you want to do this while the card is in your PC, do not use rename, because Windows may rename the file called cmdline.txt to cmdline.txt.txt !)
If your system still works with the USB stick, you can also make the changes to the cmdline files from within Debian (in /boot). You can also run a disk check in the USB drive when you have dismounted that drive and run from the SD card.
Note
One thing you need to keep track off is the difference in kernel versions between the boot code on the SD card and the file system on the USB drive when you apt-get upgrade. The original kernel software on the SD card may get out of synchronization with the newer boot files on the DOS partition. One way to fix that is to revert back to the SD card and also update that file system with apt-get upgrade.
This updated version/revision was made possible by the contributions of many users. Their posts are below, just be aware that many inputs or comments are now outdated, or hopefully integrated. It was the only way for me to keep new users from going through 12 pages (currently) of comments, discussions and other contributions.
Enjoy and stay tuned!
Paul
2 comments:
Post a Comment