Raspberry Pi – Use a USB stick for your partitions

We all know : SD cards have a number of limited entries. In addition the Raspberry Pi SD card reader is fairly fragile and can windows be damaged. So a possible solution, store your partitions on a USB key or an external drive.

img_3160_usb-logo

Since I do the tutorials I have of remaking a good thirty times my SD card and break 2 adapters for micro-SD cards (yet I'm usually pretty careful). Therefore side life it should decrease fairly quickly even if a margin !

I propose today as an example to create on a 8 GB USB key a 6 GB file system and 2 GB partition to manage workspaces users (the /Home). All from your Pi Raspberry.

  1. Insert the USB stick into your Raspberry Pi
  2. Determine the ID of the key with dmesg

    Résultat de dmesg : chez moi la clé est sur /dev/sda

    Result of dmesg : for me the key is/dev/sdaChez me the key is on/dev/sda

  3. Create two partitions with fdisk (or cfdisk If you want a minimalist interface). You may have to delete the old ones if necessary.
    Nous voici avec une clé USB contenant 2 partitions ext4

    Here we are with a USB drive containing 2 ext4 partitions

    Do not forget to save ! 🙂

  4. Format partitions in ext4 with mkfs.ext4
    mkfs.ext4/dev/sda1
    mkfs.ext4/dev/sda2
  5. You can now mount these partitions :
    mkdir/tmp/usb_root
    mkdir/tmp/usb_home
    mount/dev/sda1/tmp/usb_root
    mount/dev/sda2/tmp/usb_home
  6. Now that you have your two partitions, need to copy all the files from the SD card to the 6 GB partition. For this we will pass by the utility rsync who will be faster than DD.
    apt - get install rsync
  7. Now we have to duplicate the primary partition. Only problem, This partition is used and thus likely to be amended. We will therefore raise the score by linking to the primary partition with the option –bind.
    mkdir/tmp/sd_root
    mount - bind / /tmp/sd_root /.

    If you look at the contents of /tmp/sd_root, you have your main partition :

    Votre partition principale après la commande mount --bind

    Your primary partition after the mount command –bind

  8. Now launch the copy. But it is necessary to exclude the directories you want to not duplicate : /Home (It will be our second partition), /tmp (We work in) and /boot (who will be on our key) :
    cd /
    rsync - avh - exclude ' home/pi'-exclude ' tmp /'-exclude ' boot /' / tmp/sd_root / / tmp/usb_root /.
  9. If necessary to copy the directory to the partition for users :
    rsync - avh home/pi / / tmp/usb_home /.
  10. Change the partitions to mount at startup. It goes in the file /etc/fstab. For us it will be /tmp/usb_root/etc/fstab. Replace the line :
    /dev/mmcblk0p2  /               ext4 defaults,noatime  0       1

    by

    /dev/sda1  /               ext4 defaults,noatime  0       1
    /dev/sda2/home ext4 defaults,noatime  0       1
  11. Last step, change the SD card to boot from the USB stick. Edit the /boot/cmdline.txt file and replace with/dev/sda1. You should have the following line :
    dwc_otg.lpm_enable = 0 console = ttyAMA0, 115200 kgdboc = ttyAMA0, 115200 console = tty1 root =/dev/sda1 rootfstype = ext4 elevator = deadline rootwait logo.nologo

Vous pouvez redémarrer la machine et profiter du système sur la clé USB 🙂

Thanks to for his Article on the subject

[Update] Remember to create the directories/boot and/tmp on the new partition.

Like this article ? Bear with me on patreon!

You may also like...