{"id":547,"date":"2014-12-30T17:40:45","date_gmt":"2014-12-30T16:40:45","guid":{"rendered":"http:\/\/www.moosth.net\/calabrothers\/?p=547"},"modified":"2015-05-09T00:07:08","modified_gmt":"2015-05-08T22:07:08","slug":"raspberry-qemu-bash-scripts-to-manage-virtual-and-sdcard-os-images","status":"publish","type":"post","link":"https:\/\/www.moosth.net\/calabrothers\/raspberry-qemu-bash-scripts-to-manage-virtual-and-sdcard-os-images\/","title":{"rendered":"Raspberry + QEMU &#8211; Bash scripts to manage Virtual and SDCard OS images"},"content":{"rendered":"<p><a href=\"http:\/\/www.moosth.net\/calabrothers\/wp-content\/uploads\/2014\/02\/Raspberry.png\"><img decoding=\"async\" data-attachment-id=\"483\" data-permalink=\"https:\/\/www.moosth.net\/calabrothers\/raspberry\/\" data-orig-file=\"https:\/\/www.moosth.net\/calabrothers\/wp-content\/uploads\/2014\/02\/Raspberry.png\" data-orig-size=\"1134,1367\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"Raspberry\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/www.moosth.net\/calabrothers\/wp-content\/uploads\/2014\/02\/Raspberry-849x1024.png\" class=\" wp-image-483 alignleft\" src=\"http:\/\/www.moosth.net\/calabrothers\/wp-content\/uploads\/2014\/02\/Raspberry-150x150.png\" alt=\"Raspberry\" width=\"160\" height=\"160\" \/><\/a><\/p>\n<h2>Introduction:<\/h2>\n<p>Raspberry Pi is a very popular development platform based on ARM processors.\u00a0Recently a fantastic IMU\/GPS combo shield was released for such platform by\u00a0<a title=\"EMLID Navio\" href=\"http:\/\/www.emlid.com\">EMLID<\/a>.\u00a0While source code of several components is available by their git-hub, additional\u00a0software components (for example <a title=\"ROS\" href=\"http:\/\/wiki.ros.org\/\">ROS<\/a>) could be interesting to develop your robotic application&#8230;\u00a0As first step, to get confident with the Raspberry environment, my personal suggestion\u00a0consists into running the Raspbian (the Raspberry operating system) through an\u00a0emulator, for example QEMU.<\/p>\n<h4>In this article, we are going to show how to download, setup and manage Raspbian images with Ubuntu and QEMU. For your convenience, all the steps are combined\u00a0into bash scripts. <strong>You may download freely and without any warranty.<\/strong><\/h4>\n<p>Because the robotic platform developed by our group is named <em>CalaTronics<\/em> you may find some reference to this name in the script logs \ud83d\ude42<\/p>\n<h2>1) Downloading the EMLID image of Raspbian: getRTRpi.sh<\/h2>\n<pre class=\"brush: bash; title: getRTRpi.sh; notranslate\" title=\"getRTRpi.sh\">\r\n\r\n#!\/bin\/bash\r\n\r\necho &quot;Downloading kernel-qemu&quot;\r\nwget http:\/\/xecdesign.com\/downloads\/linux-qemu\/kernel-qemu\r\n\r\necho &quot;Downloading image&quot;\r\nwget http:\/\/emlid.com\/files\/Raspbian-SEP-2014-RT-B+.img.tar.bz2 -c\r\n\r\necho &quot;Unzipping image&quot;\r\ntar jxvf Raspbian-SEP-2014-RT-B+.img.tar.bz2\r\n\r\necho &quot;Renaming image&quot;\r\nmv Raspbian-SEP-2014-RT-B+.img ccrpi.img\r\n\r\necho &quot;Cleaning up&quot;\r\nrm -f Raspbian-SEP-2014-RT-B+.img.tar.bz2\r\n\r\n<\/pre>\n<p>Once that this file is executed, you should find two new files named\u00a0<em>ccrpi.img<\/em> and\u00a0<em>kernel-qemu<\/em>.<\/p>\n<p><strong>Note that you may want to adjust some of the links in case are corrupted or updated.<\/strong><\/p>\n<h2>2) Initialize the image by adjusting some of the default params: initrpi.sh<\/h2>\n<pre class=\"brush: bash; title: initrpi.sh; notranslate\" title=\"initrpi.sh\">\r\n#!\/bin\/bash\r\necho -en &quot;\\ec&quot;\r\necho\r\necho &quot;CalaTronics Init RPi Firmware Manager&quot;\r\necho\r\n\r\nif &#x5B; -z $1 ];\r\nthen\r\n\techo\r\n\techo &quot;Use initrpi.sh file.img&quot;\r\n\techo\r\n\techo &quot;    file.img is the RPi image&quot;\r\n\techo\r\n\texit\r\nfi\t\r\n\r\nif &#x5B; ! -f $1 ];\r\nthen\r\n\techo &quot;Invalid image file.&quot;\r\n\techo\r\n\texit\r\nfi\r\n\r\nIMG_DEV=&quot;$1&quot;\r\nMEMORY=&quot;256&quot;\r\n\r\necho &quot;Selected Image : ${IMG_DEV}&quot;\r\n\r\necho &quot;Login\/Password should be pi\/raspberry&quot;\r\necho\r\necho &quot;a) \/etc\/ld.so.preload&quot;\r\necho &quot;   comment #\/usr\/lib\/arm-linux-gnueabihf\/libcofi_rpi.so&quot;\r\necho\r\necho &quot;b) \/etc\/udev\/rules.d\/90-qemu.rules&quot;\r\necho &quot;   KERNEL==\\&quot;sda\\&quot;, SYMLINK+=\\&quot;mmcblk0\\&quot;&quot;\r\necho &quot;   KERNEL==\\&quot;sda?\\&quot;, SYMLINK+=\\&quot;mmcblk0p%n\\&quot;&quot;\r\necho &quot;   KERNEL==\\&quot;sda2\\&quot;, SYMLINK+=\\&quot;root\\&quot;&quot;\r\necho\r\necho &quot;c) \/etc\/network\/interfaces&quot;\r\necho &quot;\t auto eth0&quot;\r\necho &quot;   iface eth0 inet static&quot;\r\necho &quot;         address 10.0.0.2&quot;\r\necho &quot;         network 10.0.0.0&quot;\r\necho &quot;         netmask 255.255.255.0&quot;\r\necho &quot;         broadcast 10.0.0.255&quot;\r\necho &quot;         gateway 10.0.0.1&quot;\r\n\r\necho\r\necho &quot;Running initial&quot;\r\necho\r\nsudo qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m ${MEMORY} -M versatilepb -no-reboot -serial stdio -append &quot;root=\/dev\/sda2 panic=1 rootfstype=ext4 rw init=\/bin\/bash&quot; -hda ${IMG_DEV} \r\n\r\necho\r\n<\/pre>\n<p>All the necessary steps and instructions required to adjust the default parameters will be displayed on the screen. Just do it&#8230;<\/p>\n<p>In case you are not familiar with these instructions, we are going to comment out a library link, change some udev options and fix the ethernet address to 10.0.0.2.<\/p>\n<h2>3) Manage the images: ccrpi.sh<\/h2>\n<p>One important, sometime required, step during development is to make periodic updates.\u00a0You may consider to develop on your emulator or directly on the Raspberry&#8230; it is your choice. But how to keep this two deviced (virtual\/real) synchronized?<\/p>\n<p>A possible solution is to run the SD card image of the Raspberry through the emulator. Then each modification will be done on the SD card and the Raspberry will use the fresh image.<\/p>\n<p>Another possibility is to work without SD card and flash it when necessary (for example using dd).<\/p>\n<p>We made a simple script which will provide you all these options: the ccrpi.sh.<\/p>\n<pre class=\"brush: bash; title: ccrpi.sh; notranslate\" title=\"ccrpi.sh\">\r\n#!\/bin\/bash\r\necho -en &quot;\\ec&quot;\r\necho\r\necho &quot;CalaTronics RPi Firmware Manager&quot;\r\necho\r\n\r\nif &#x5B; -z $1 ];\r\nthen\r\n\techo\r\n\techo &quot;Use ccrpi.sh file.img &#x5B;\/dev\/sdX]&quot;\r\n\techo\r\n\techo &quot;    file.img is the RPi image&quot;\r\n\techo &quot;    \/dev\/sdX is the (optional) device to perform firmware operations&quot;\r\n\techo\r\n\texit\r\nfi\t\r\n\r\nif &#x5B; ! -f $1 ];\r\nthen\r\n\techo &quot;Invalid image file.&quot;\r\n\techo\r\n\texit\r\nfi\r\n\r\nIMG_DEV=&quot;$1&quot;\r\nMEMORY=&quot;256&quot;\r\nHOST_IP=&quot;10.0.0.1&quot;\r\nHOST_NET=&quot;10.0.0.0\/8&quot;\r\nHOST_PORT=&quot;2222&quot;\r\nGUEST_PORT=&quot;22&quot;\r\nGUEST_IP=&quot;10.0.0.2&quot;\r\n\r\necho &quot;Selected Image : ${IMG_DEV}&quot;\r\n\r\nif &#x5B; -z $2 ];\r\nthen\r\n\techo\r\n\techo &quot;Pure simulation mode active&quot;\r\n\techo &quot;Running CalaTronics SD (Firmware) &#x5B;${HOST_IP}:${HOST_PORT}-&gt;${GUEST_IP}:${GUEST_PORT}]&quot;\r\n\techo\r\n\tsudo qemu-system-arm \\\r\n\t\t-kernel kernel-qemu -cpu arm1176 -m ${MEMORY} -M versatilepb -no-reboot -serial stdio \\\r\n\t\t-append &quot;root=\/dev\/sda2 panic=1 rootfstype=ext4 rw&quot; -hda ${IMG_DEV} -net nic -net user,net=${HOST_NET},host=${HOST_IP},hostfwd=tcp:127.0.0.1:${HOST_PORT}-${GUEST_IP}:${GUEST_PORT}\r\n        exit\r\nfi\t\r\n\r\necho &quot;Device: ${SD_CARD} SELECTED&quot;\r\n\r\necho &quot;Device: $2&quot;\r\nif &#x5B;&#x5B; $2 =~ (\/dev\/.+) ]]; then\r\n\techo &quot;Device: $1 PATH OK&quot;\r\nelse\r\n\techo &quot;Device: $1 PATH INVALID&quot;\r\n\techo &quot;Use ccrpi.sh \/dev\/sdX&quot;\r\n\texit;\r\nfi\r\n\r\nif &#x5B;&#x5B; $2 =~ .*&#x5B;^0-9]$ ]]; then\r\n\techo &quot;Device: $1 DEVICE OK&quot;\r\nelse\r\n\techo &quot;Device: $1 DEVICE INVALID&quot;\r\n\techo &quot;Use ccrpi.sh \/dev\/sdX&quot;\r\n\texit;\r\nfi\r\n\r\nSD_CARD=&quot;$2&quot;\r\n\r\necho\r\necho &quot;Check SD Card: ${SD_CARD}&quot;\r\n\r\nsudo fdisk -l ${SD_CARD} | grep ${SD_CARD}\r\n\r\necho\r\necho &quot;Choosing the wrong HD could cause data loss.&quot;\r\necho\r\nread -p &quot;Continue (y\/n)? &quot; choice\r\ncase &quot;$choice&quot; in\r\n  y|Y ) ;;\r\n  n|N ) echo &quot;If the device is wrong, select a new one.&quot;\r\n\texit;;\r\n    * ) echo &quot;No option was selected, aborting.&quot;\r\n\texit;;\r\nesac\r\n\r\necho\r\necho &quot;Select the operation:&quot;\r\necho\r\necho &quot;1) IMG -&gt; SD  (Flash)&quot;\r\necho &quot;2) SD  -&gt; IMG (Backup)&quot;\r\necho &quot;3) Run    IMG (Simulator)&quot;\r\necho &quot;4) Run    SD  (Firmware)&quot;\r\n\r\necho\r\nread -p &quot;?&gt; &quot; choice\r\necho\r\ncase &quot;$choice&quot; in\r\n  1 )\r\n\tif &#x5B; -f ${IMG_DEV} ];\r\n\tthen\r\n\t\techo &quot;Flashing CalaTronics Firmware&quot;\r\n\t\techo\r\n\t\techo &quot;&gt; dd bs=1M if=${IMG_DEV} of=${SD_CARD}&quot;\r\n\t\tsudo    dd bs=1M if=${IMG_DEV} of=${SD_CARD}\r\n\telse\r\n\t\techo &quot;ERROR: CalaTronics Firmware not available offline (${IMG_DEV}).&quot;\r\n\tfi\r\n\t;;\r\n  2 )\r\n\techo &quot;Saving   CalaTronics Firmware&quot;\r\n\techo\r\n\techo &quot;&gt; dd bs=1M if=${SD_CARD} of=${IMG_DEV}&quot;\r\n\tsudo    dd bs=1M if=${SD_CARD} of=${IMG_DEV}\r\n\t;;\r\n  3 )\r\n\techo &quot;Running CalaTronics IMG (Simulator) &#x5B;${HOST_IP}:${HOST_PORT}-&gt;${GUEST_IP}:${GUEST_PORT}]&quot;\r\n\techo\r\n\tsudo qemu-system-arm \\\r\n\t\t-kernel kernel-qemu -cpu arm1176 -m ${MEMORY} -M versatilepb -no-reboot -serial stdio \\\r\n\t\t-append &quot;root=\/dev\/sda2 panic=1 rootfstype=ext4 rw&quot; -hda ${IMG_DEV} -net nic -net user,net=${HOST_NET},host=${HOST_IP},hostfwd=tcp:127.0.0.1:${HOST_PORT}-${GUEST_IP}:${GUEST_PORT}\r\n\t;;\r\n  4 ) \techo &quot;Running CalaTronics SD (Firmware) &#x5B;${HOST_IP}:${HOST_PORT}-&gt;${GUEST_IP}:${GUEST_PORT}]&quot;\r\n\techo\r\n\tsudo qemu-system-arm \\\r\n\t\t-kernel kernel-qemu -cpu arm1176 -m ${MEMORY} -M versatilepb -no-reboot -serial stdio \\\r\n\t\t-append &quot;root=\/dev\/sda2 panic=1 rootfstype=ext4 rw&quot; -hda ${SD_CARD} -net nic -net user,net=${HOST_NET},host=${HOST_IP},hostfwd=tcp:127.0.0.1:${HOST_PORT}-${GUEST_IP}:${GUEST_PORT}\r\n\t;;\r\n  * ) \techo &quot;No option was selected, aborting.&quot;\r\n\texit;;\r\nesac\r\necho\r\n\r\n<\/pre>\n<p>If you already perform an initialization through the previous scripts, you\u00a0may consider to flash the SD card with the image (you have in your PC).<\/p>\n<h2>4)\u00a0Connecting to the Emulator!<\/h2>\n<p>Once you run the image with the simulator (by NOT passing the device option to the ccrpi.sh or by selecting the mode 3\/4) you may want to connect to the system using ssh&#8230;<\/p>\n<pre class=\"brush: bash; title: ssh connection; notranslate\" title=\"ssh connection\">\r\n#!\/bin\/bash\r\necho -en &quot;\\ec&quot;\r\necho\r\necho &quot;CalaTronics RPi Firmware SSH Connection&quot;\r\necho\r\n\r\necho &quot;Login:    pi&quot;\r\necho &quot;Password: raspberry&quot;\r\n\r\nHOST_PORT=&quot;2222&quot;\r\n\r\necho\r\nssh -lpi 127.0.0.1 -p${HOST_PORT}\r\n<\/pre>\n<p>Note that the selected SSH host is localhost while the port is 2222. This port forward rule is the result of the ccrpi.sh script options.<\/p>\n<h2>5)\u00a0Enjoy your Raspberry Emulator!<\/h2>\n<p>Hope you enjoyed this fantastic journey into emulators, images and bash scripts&#8230; Now you are ready for a new challenge&#8230; <a title=\"ROS on Raspberry PI\" href=\"http:\/\/wiki.ros.org\/ROSberryPi\/Installing%20ROS%20Indigo%20on%20Raspberry%20Pi\">install ROS<\/a>??<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: Raspberry Pi is a very popular development platform based on ARM processors.\u00a0Recently a fantastic IMU\/GPS combo shield was released for such platform by\u00a0EMLID.\u00a0While source code of several components is available by their git-hub, additional\u00a0software components (for example ROS) could be interesting to develop your robotic application&#8230;\u00a0As first step, to get confident with the Raspberry [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":483,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[2,20],"tags":[26,25,21],"class_list":["post-547","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news","category-software","tag-bash","tag-qemu","tag-raspberry"],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3QYke-8P","jetpack_featured_media_url":"https:\/\/www.moosth.net\/calabrothers\/wp-content\/uploads\/2014\/02\/Raspberry.png","_links":{"self":[{"href":"https:\/\/www.moosth.net\/calabrothers\/wp-json\/wp\/v2\/posts\/547","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.moosth.net\/calabrothers\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.moosth.net\/calabrothers\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.moosth.net\/calabrothers\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.moosth.net\/calabrothers\/wp-json\/wp\/v2\/comments?post=547"}],"version-history":[{"count":25,"href":"https:\/\/www.moosth.net\/calabrothers\/wp-json\/wp\/v2\/posts\/547\/revisions"}],"predecessor-version":[{"id":577,"href":"https:\/\/www.moosth.net\/calabrothers\/wp-json\/wp\/v2\/posts\/547\/revisions\/577"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.moosth.net\/calabrothers\/wp-json\/wp\/v2\/media\/483"}],"wp:attachment":[{"href":"https:\/\/www.moosth.net\/calabrothers\/wp-json\/wp\/v2\/media?parent=547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.moosth.net\/calabrothers\/wp-json\/wp\/v2\/categories?post=547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.moosth.net\/calabrothers\/wp-json\/wp\/v2\/tags?post=547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}