{"id":133,"date":"2014-10-25T17:24:57","date_gmt":"2014-10-26T00:24:57","guid":{"rendered":"https:\/\/www.falatic.com\/?p=133"},"modified":"2014-10-25T18:59:31","modified_gmt":"2014-10-26T01:59:31","slug":"a-quick-guide-to-running-a-raspberry-pi-from-a-hard-drive","status":"publish","type":"post","link":"https:\/\/www.falatic.com\/index.php\/133\/a-quick-guide-to-running-a-raspberry-pi-from-a-hard-drive","title":{"rendered":"A quick guide to running a Raspberry Pi from a hard drive"},"content":{"rendered":"<p>The Raspberry Pi is a fun if\u00a0limited little single-board computer (were that it had more than one core). That said, running off an SD card\u00a0is\u00a0not exactly optimal, with write speeds being the greatest handicap.\u00a0<em><a href=\"http:\/\/elinux.org\/RPi_SD_cards\" target=\"_blank\">This is even worse with micro SD cards<\/a>.<\/em><\/p>\n<p>While SD cards with &#8220;Ultra&#8221; and &#8220;Extreme&#8221; abound,\u00a0their claim to fame is speed while writing very\u00a0large sequential files (some DSLR cameras can push &gt; 40 MB\/s of sustained writes in raw video\/photography modes). When it comes to small random writes (e.g., the 4K range) the\u00a0write speed drops precipitously.\u00a0The maximum bandwidth of the SD card slot is in the 20-25 MB\/s range, and finding a card which performs well over the full typical range of writes is difficult (I&#8217;ve seen cards that do better at small writes but max out at 15 MB\/s). The USB slot can manage 30 MB\/s and a small laptop HDD or even an SSD is a very cheap way to provide\u00a0a serious speed bump across the range!<\/p>\n<p><span style=\"text-decoration: underline;\">Prerequisites:<\/span><\/p>\n<ul>\n<li>A powered USB 2.0 hub can be useful. I personally have a <a href=\"http:\/\/plugable.com\/products\/usb2-hub-ag7\" target=\"_blank\">Plugable\u00a07 port hub<\/a>\u00a0which has worked quite well for me (<a href=\"http:\/\/www.raspberrypi.org\/forums\/viewtopic.php?f=46&amp;t=58452\" target=\"_blank\">it was widely recommended and doesn&#8217;t <em>backpower<\/em> through the USB ports<\/a>, an important consideration). A powered hub is useful for other things as well, particularly if you add other peripherals (you can easily power the RPi itself as well as\u00a0Wifi, keyboard and mouse, bluetooth, storage devices\u00a0and other\u00a0things). It may be possible to use only\u00a0the internal RPi USB\u00a0ports, but it depends on the drive you use and the total power available.\u00a0<em>Note that USB 3.0 hubs\u00a0may or may not work and won&#8217;t\u00a0add a speed benefit.<\/em><\/li>\n<li>A laptop-size hard drive &#8211; this will consume less power and can often be powered straight from a USB port. Got a small\u00a0Western Digital\u00a0Passport? That&#8217;ll work! If you&#8217;re repurposing a drive, I had good luck with <a href=\"http:\/\/www.amazon.com\/Optimized-Tool-Free-Inateck-External-Enclosure\/dp\/B00KYF1LLI\/\" target=\"_blank\">the Inatek case<\/a>\u00a0(it&#8217;s handy for other projects and was pretty cheap). Whatever you use, it needn&#8217;t be state of the art since this is still I\/O bound.<\/li>\n<li>A working installation of Raspian on an SD card in a Pi. <strong>This example\u00a0assumes a default two partition setup on the card (boot and root filesystems)<\/strong>.<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline;\">Process:<\/span><\/p>\n<p>(Adapted &#8211; with important corrections! &#8211; from\u00a0<a href=\"http:\/\/raspberrypihobbyist.blogspot.com\/2013\/07\/running-from-external-hard-drive.html\" target=\"_blank\">another site<\/a>)<\/p>\n<p>List the current partitions on all attached drives:<\/p>\n<pre>sudo fdisk -l<\/pre>\n<p>We should see the original SD Card, e.g.:<\/p>\n<p style=\"padding-left: 30px;\">Disk \/dev\/mmcblk0: 15.9 GB, 15931539456 bytes<br \/>\nDevice Boot Start End Blocks Id System<br \/>\n\/dev\/mmcblk0p1 8192 122879 57344 c W95 FAT32 (LBA)<br \/>\n\/dev\/mmcblk0p2 122880 31116287 15496704 83 Linux<\/p>\n<p>As well as the external drive, e.g.:<\/p>\n<p style=\"padding-left: 30px;\">Disk \/dev\/sda: 160.0 GB, 160041885696 bytes<br \/>\nDevice Boot Start End Blocks Id System<br \/>\n<em>(Since my drive is unformatted we see no partitions present.)<\/em><\/p>\n<p>Use fdisk to partition the new HDD<\/p>\n<pre>sudo fdisk \/dev\/sda<\/pre>\n<ul>\n<li style=\"padding-left: 30px;\">Use &#8216;d&#8217; repeatedly to delete unwanted partitions (typ. all of them)<\/li>\n<li style=\"padding-left: 30px;\">Use &#8216;w&#8217; to sync and commit the change.<\/li>\n<li style=\"padding-left: 30px;\">Create the root partition: n -&gt; p -&gt; 1 -&gt; (default) -&gt; +64G<\/li>\n<li style=\"padding-left: 30px;\">Create the swap partition: n -&gt; p -&gt; 2 -&gt; (default) -&gt; +4G<\/li>\n<li style=\"padding-left: 30px;\">Set the swap partition type: t -&gt; 2 -&gt; 82<\/li>\n<li style=\"padding-left: 30px;\">Create the ext partition: n -&gt; e -&gt; 3 -&gt; (default) -&gt; (default)<\/li>\n<li style=\"padding-left: 30px;\">Create a spare partition: n -&gt; l -&gt; (default) -&gt; +32G<\/li>\n<li style=\"padding-left: 30px;\">Write, sync and quit: w<\/li>\n<\/ul>\n<p>Check that things were created OK:<\/p>\n<pre>sudo fdisk -l<\/pre>\n<p>Run &#8216;top&#8217; (or <em>free -m<\/em>) (note that swap is currently\u00a0a 100 MB\u00a0on-disk swapfile. Also my main memory is well-short of the 512 MB you&#8217;d expect because I&#8217;ve reserved 128 MB for\u00a0video memory in\u00a0<em>\/boot\/config.txt<\/em>\u00a0(gpu_mem=128):<\/p>\n<p style=\"padding-left: 30px;\"><em>KiB Mem: 382832 total, 82508 used, 300324 free, 11488 buffers<\/em><br \/>\n<em> KiB Swap: <strong>102396<\/strong> total, 0 used, 102396 free, 42804 cached<\/em><\/p>\n<p>For the new root partition, just clone it and size it (you might prefer to do the cloning offline (e.g., using <em>gparted<\/em>)):<\/p>\n<pre>sudo dd if=\/dev\/mmcblk0p2 of=\/dev\/sda1 bs=32M conv=noerror,sync<\/pre>\n<p><em>It&#8217;s\u00a0pretty slow! 14.5 MB\/s on mine. My SD card is fast but the alternating read-then-write means you&#8217;ll be doing this at half the optimal read bandwidth of your card.<\/em><\/p>\n<pre>sudo e2fsck -f \/dev\/sda1<\/pre>\n<p>Press &#8216;y&#8217; if fixes are required (e.g., &#8220;Free blocks count wrong&#8221; is typical)<\/p>\n<pre>sudo resize2fs \/dev\/sda1<\/pre>\n<p>Initialize the new swap partition:<\/p>\n<pre>sudo mkswap \/dev\/sda2<\/pre>\n<p>Initialize, mount and configure a spare partition\u00a0(optional):<\/p>\n<pre>sudo mkfs.ext4 \/dev\/sda5\r\nsudo mkdir \/mnt\/spare\r\nsudo mount \/dev\/sda5 \/spare\r\nsudo chown -R pi:pi \/spare<\/pre>\n<p>Mount the new rootfs and configure swap and fstab:<\/p>\n<pre>sudo mount \/dev\/sda1 \/mnt\r\nsudo vi \/mnt\/etc\/fstab\r\n<em><strong>    # Edit the root paritition: \/dev\/mmcblk0p2 --&gt; \/dev\/sda1\r\n    # Because we'll be running on an external drive the 'noatime'\r\n    # SD card wear optimization may be optional now<\/strong><\/em>\r\n        \/dev\/sda1 \/ ext4 defaults,noatime 0 1<strong><em>\r\n    # Add the swap partition:\r\n<\/em><\/strong>        \/dev\/sda2 none swap sw 0 0<strong><em>\r\n    # Add the spare partition:\r\n<\/em><\/strong>        \/dev\/sda5 \/spare ext4 errors=remount-ro 0 1<\/pre>\n<p>Configure the boot file:<\/p>\n<pre>sudo cp \/boot\/cmdline.txt \/boot\/cmdline.orig\r\nsudo vi \/boot\/cmdline.txt\r\n    <strong><em># Update: root=\/dev\/mmcblk0p2 --&gt; root=\/dev\/sda1<\/em><\/strong><\/pre>\n<p>Sync and reboot:<\/p>\n<pre>sudo sync\r\nsudo reboot<\/pre>\n<p><strong>At this point we are using the new drive for the root fs!<\/strong><\/p>\n<p>Problems booting?\u00a0You can mount the boot partition in Linux or Windows to edit files like <em>cmdline.txt<\/em>\u00a0when debugging:<\/p>\n<ul>\n<li>Adding\u00a0bootdelay \/ rootdelay parameters to the cmdline.txt file can help if the HDD is\u00a0taking too long to spin up and times out<\/li>\n<li>You can\u00a0edit\/rename the cmdline.txt file back to what it was originally, allowing you to\u00a0run from the SD card alone once again<\/li>\n<\/ul>\n<p>Now, remove the old <em>\/var\/swap<\/em>-based\u00a0swapfile\u00a0(if present):<\/p>\n<pre>sudo dphys-swapfile swapoff\r\nsudo dphys-swapfile uninstall\r\nsudo reboot<\/pre>\n<p>Take a look at the output of &#8216;top&#8217; (or <em>free -m<\/em>) again. Note the new, larger swapfile:<\/p>\n<p style=\"padding-left: 30px;\"><em>KiB Mem: 382832 total, 84376 used, 298456 free, 12132 buffers<\/em><br \/>\n<em> KiB Swap: <strong>4194300<\/strong> total, 0 used, 4194300 free, 42552 cached<\/em><\/p>\n<p>Speed tests I ran showed read\/write\u00a0performance in the 28-29 MB\/s range, which is about as good as you&#8217;re going to get (uncompressed) over a\u00a0USB 2.0 link.<\/p>\n<p>More thoughts:<\/p>\n<ul>\n<li>The boot partition is miniscule and has a small impact on speed (and only at boot-time). You could clone the boot partition+MBR to a very small SD card and save the original SD card for other uses (process is outside the scope of this document).<\/li>\n<li>Use a <a href=\"https:\/\/www.adafruit.com\/product\/1569\" target=\"_blank\">short micro SD adapter<\/a> with a regular model B to contain the boot\u00a0partition without having a long lever projecting from the device.<\/li>\n<\/ul>\n<!-- wpsso rrssb get buttons: buttons on archive option not enabled -->\n","protected":false},"excerpt":{"rendered":"<p>The Raspberry Pi is a fun if\u00a0limited little single-board computer (were that it had more than one core). That said, running off an SD card\u00a0is\u00a0not exactly optimal, with write speeds <a href=\"https:\/\/www.falatic.com\/index.php\/133\/a-quick-guide-to-running-a-raspberry-pi-from-a-hard-drive\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":"","_links_to":"","_links_to_target":""},"categories":[85],"tags":[115,112,114,113],"class_list":["entry","author-marty","post-133","post","type-post","status-publish","format-standard","category-electronics","tag-external","tag-raspberry-pi","tag-rootfs","tag-rpi"],"_links":{"self":[{"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/posts\/133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/comments?post=133"}],"version-history":[{"count":0,"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/posts\/133\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/media?parent=133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/categories?post=133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/tags?post=133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}