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.
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.
- Insert the USB stick into your Raspberry Pi
- Determine the ID of the key with dmesg
- Create two partitions with fdisk (or cfdisk If you want a minimalist interface). You may have to delete the old ones if necessary.
Do not forget to save ! 🙂
- Format partitions in ext4 with mkfs.ext4
- You can now mount these partitions :
- 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
- 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.
mount - bind / /tmp/sd_root /.
If you look at the contents of /tmp/sd_root, you have your main partition :
- 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 /.
- If necessary to copy the directory to the partition for users :
rsync - avh home/pi / / tmp/usb_home /.
- 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
/dev/sda1 / ext4 defaults,noatime 0 1 /dev/sda2/home ext4 defaults,noatime 0 1
- 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 🙂
[Update] Remember to create the directories/boot and/tmp on the new partition.