{"id":264,"date":"2015-12-30T10:09:47","date_gmt":"2015-12-30T08:09:47","guid":{"rendered":"https:\/\/www.kurokesu.com\/main\/?p=264"},"modified":"2016-09-01T20:32:06","modified_gmt":"2016-09-01T18:32:06","slug":"raspberry-pi-setup-for-power-safe-operation","status":"publish","type":"post","link":"https:\/\/www.kurokesu.com\/main\/2015\/12\/30\/raspberry-pi-setup-for-power-safe-operation\/","title":{"rendered":"Raspberry Pi &#8211; setup for power safe operation"},"content":{"rendered":"<h3><a href=\"https:\/\/www.kurokesu.com\/main\/wp-content\/uploads\/2015\/12\/IMG_5258_r_prw.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-225\" src=\"https:\/\/www.kurokesu.com\/main\/wp-content\/uploads\/2015\/12\/IMG_5258_r_prw-300x230.jpg\" alt=\"IMG_5258_r_prw\" width=\"300\" height=\"230\" srcset=\"https:\/\/www.kurokesu.com\/main\/wp-content\/uploads\/2015\/12\/IMG_5258_r_prw-300x230.jpg 300w, https:\/\/www.kurokesu.com\/main\/wp-content\/uploads\/2015\/12\/IMG_5258_r_prw.jpg 600w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>About<\/h3>\n<p style=\"text-align: justify;\">Raspberry Pi is pretty famous for eating SD cards. While this can be annoying when it acts as media player, it can be costly and time consuming operation to replace when installed in remote location. If we assume that SD card is branded and good quality, most dangerous thing that could happen is unexpected power down. To avoid corrupt filesystem I suggest mounting SD card on Read Only mode. This is not perfect, but after testing for a few days I propose this solution.<\/p>\n<p style=\"text-align: justify;\"><!--more--><\/p>\n<h3>Prepare<\/h3>\n<p style=\"text-align: justify;\">Prepare SD card. Format, flash latest Raspbian image and boot Raspberry. Find IP address and connect via SSH. You will need Internet connection to accomplish some steps.<\/p>\n<ul>\n<li>Format SD card with <a href=\"https:\/\/www.sdcard.org\/downloads\/formatter_4\/\">SD Formatter<\/a><\/li>\n<li>Download <a href=\"https:\/\/www.raspberrypi.org\/downloads\/raspbian\/\">Raspbian Jessie Lite<\/a> image (I used 4GB SD card)<\/li>\n<li>Flash downloaded image with <a href=\"http:\/\/sourceforge.net\/projects\/win32diskimager\/\">win32diskimager<\/a><\/li>\n<li>Connect LAN, and USB cables and power up Raspberry pi<\/li>\n<li>Find out IP address with <a href=\"http:\/\/www.advanced-ip-scanner.com\/\">Advanced ip scanner<\/a><\/li>\n<li>SSH to Raspbery Pi<\/li>\n<\/ul>\n<h3>Setup raspi-config<\/h3>\n<pre>$ sudo raspi-config<\/pre>\n<ul>\n<li>1 Expand Filesystem<\/li>\n<li>2 Change User Password<\/li>\n<li>5 Internatiolization options \/ I2 Chage Timezone \/ None of the above -&gt; <strong>UTC<\/strong><\/li>\n<li>9 Advanced Options \/ A2 Hostname -&gt; <strong>Kurokesu<\/strong><\/li>\n<li>4 Wait for Network at Boot -&gt; <strong>Fast Boot Without waiting for network connection<\/strong><\/li>\n<\/ul>\n<h3>Update OS<\/h3>\n<pre>$ sudo apt-get -y update\r\n$ sudo apt-get -y dist-upgrade\r\n$ sudo reboot<\/pre>\n<h3>Setup RO filesystem<\/h3>\n<h5>Disable swap<\/h5>\n<pre>$ sudo dphys-swapfile swapoff\r\n$ sudo dphys-swapfile uninstall\r\n$ sudo update-rc.d dphys-swapfile disable\r\n$ sudo systemctl disable systemd-readahead-collect\r\n$ sudo systemctl disable systemd-random-seed<\/pre>\n<h5>Replace rsyslog with syslog<\/h5>\n<pre>$ sudo apt-get -y install busybox-syslogd\r\n$ sudo dpkg --purge rsyslog<\/pre>\n<h5>Install UnionFS<\/h5>\n<pre>$ sudo apt-get -y install unionfs-fuse<\/pre>\n<pre class=\"brush: text;\">$ sudo nano \/usr\/local\/bin\/mount_unionfs\r\n#!\/bin\/sh\r\n[ -z \"$1\" ] &amp;&amp; exit 1 || DIR=$1\r\nROOT_MOUNT=$(grep -v \"^#\" \/etc\/fstab | awk '$2==\"\/\" {print substr($4,1,2)}')\r\nif [ \"$ROOT_MOUNT\" != \"ro\" ]; then\r\n\/bin\/mount --bind ${DIR}_org ${DIR}\r\nelse\r\n\/bin\/mount -t tmpfs ramdisk ${DIR}_rw\r\n\/usr\/bin\/unionfs-fuse -o cow,allow_other,suid,dev,nonempty ${DIR}_rw=RW:${DIR}_org=RO ${DIR}\r\nfi\r\n<\/pre>\n<pre class=\"brush: text;\">$ sudo chmod +x \/usr\/local\/bin\/mount_unionfs\r\n<\/pre>\n<h5>Edit boot script<\/h5>\n<pre>$ sudo nano \/boot\/cmdline.txt<\/pre>\n<pre>add ro:  ... elevator=deadline rootwait ro ...<\/pre>\n<h5>Edit fstab<\/h5>\n<pre class=\"brush: text;\">$ sudo nano \/etc\/fstab<\/pre>\n<pre class=\"brush: text;\">proc            \/proc           proc    defaults          0       0\r\n\/dev\/mmcblk0p1  \/boot           vfat    ro                0       2\r\n\/dev\/mmcblk0p2  \/               ext4    ro,noatime        0       1\r\nmount_unionfs   \/etc            fuse    defaults          0       0\r\nmount_unionfs   \/var            fuse    defaults          0       0\r\nnone            \/tmp            tmpfs   defaults          0       0<\/pre>\n<h5>Prepare directories<\/h5>\n<pre>$ sudo cp -al \/etc \/etc_org\r\n$ sudo mv \/var \/var_org\r\n$ sudo mkdir \/etc_rw\r\n$ sudo mkdir \/var \/var_rw\r\n<\/pre>\n<h5>Create script to mount filesystem in rw mode<\/h5>\n<pre>$ sudo nano \/home\/pi\/rw.sh<\/pre>\n<pre>#!\/bin\/bash\r\n\r\n# remount root rw\r\nmount -o remount,rw \/\r\n\r\n# prapare target paths\r\nmkdir -p \/chroot\r\nmkdir -p \/chroot\/{bin,boot,dev,etc,home,lib,opt,proc,root,run,sbin,sys,tmp,usr,var}\r\n\r\n# mount special filesystems\r\nmount -t proc proc \/chroot\/proc\r\nmount --rbind \/sys \/chroot\/sys\r\nmount --rbind \/dev \/chroot\/dev\r\n\r\n# bind rw directories\r\nfor f in {etc,var}; do mount --rbind \/${f}_org \/chroot\/$f; done<\/pre>\n<pre>$ sudo chmod +x \/home\/pi\/rw.sh\r\n$ sudo reboot<\/pre>\n<p style=\"text-align: justify;\">After completing all steps you should have safer system to work with. In order to make changes ensure power is stable and mount filesystem with script \/home\/pi\/rw.sh. Reboot after you made all changes.<\/p>\n<h5>Issue #1 reboot<\/h5>\n<p>There are some issues with reboot script and after normal reboot, Raspberry Pi hangs with last log entry<\/p>\n<pre>[ OK ] Reached target Shutdown.<\/pre>\n<p>We have read only file system, so it is safe to reboot not nicely with command:<\/p>\n<pre>sudo reboot -d -f -i<\/pre>\n<h5>Issue #2 crontab<\/h5>\n<p>Crontab is f*cked up after these modifications, so please make sure to implement your own. I ended with custom python script which is started from \/etc\/rc.local<\/p>\n","protected":false},"excerpt":{"rendered":"<p>About Raspberry Pi is pretty famous for eating SD cards. While this can be annoying when it acts as media player, it can be costly and time consuming operation to replace when installed in remote location. If we assume that SD card is branded and good quality, most dangerous thing that could happen is unexpected [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":225,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[157],"tags":[12,9,13],"class_list":["post-264","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-how-to","tag-raspberry-pi","tag-setup","tag-tips-and-tricks"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.kurokesu.com\/main\/wp-content\/uploads\/2015\/12\/IMG_5258_r_prw.jpg","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6VSmB-4g","_links":{"self":[{"href":"https:\/\/www.kurokesu.com\/main\/wp-json\/wp\/v2\/posts\/264","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kurokesu.com\/main\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kurokesu.com\/main\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kurokesu.com\/main\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kurokesu.com\/main\/wp-json\/wp\/v2\/comments?post=264"}],"version-history":[{"count":2,"href":"https:\/\/www.kurokesu.com\/main\/wp-json\/wp\/v2\/posts\/264\/revisions"}],"predecessor-version":[{"id":268,"href":"https:\/\/www.kurokesu.com\/main\/wp-json\/wp\/v2\/posts\/264\/revisions\/268"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kurokesu.com\/main\/wp-json\/wp\/v2\/media\/225"}],"wp:attachment":[{"href":"https:\/\/www.kurokesu.com\/main\/wp-json\/wp\/v2\/media?parent=264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kurokesu.com\/main\/wp-json\/wp\/v2\/categories?post=264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kurokesu.com\/main\/wp-json\/wp\/v2\/tags?post=264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}