    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Building and Running Software on an Ubuntu Phone</title>
        <link>https://members.accu.org/index.php/articles/2158</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + Overload Journal #129 - October 2015</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c354/">o129</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-354/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+354/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Building and Running Software on an Ubuntu Phone</h1>
<p><strong>Author:</strong>&nbsp;Martin Moene</p>
<p>
<strong>Date:</strong> 03 October 2015 22:20:50 +01:00 or Sat, 03 October 2015 22:20:50 +01:00</p>
<p><strong>Summary:</strong>&nbsp;Many of us has a smartphone. Alan Griffiths shows us how to hack an Ubuntu Touch phone.</p>
<p><strong>Body:</strong>&nbsp;<p>It has often been said that nowadays we carry around more computing power in our pocket than NASA used to put men on the moon. It is likely true but, so far, that computing power has not been readily available for general purpose computing. All that is changing. Iâ€™ve been working with Canonical on a project that is included in their â€˜Ubuntu Touchâ€™ phone operating system.</p>

<p>Weâ€™ve seen Linux on phones before (Googleâ€™s Android has been very successful) but one thing different about Ubuntu Touch is that it uses a large part of the same software stack as youâ€™ll find on desktops and servers. Canonical are working on â€˜convergenceâ€™ where the same software can provide either a phone or desktop experience depending on the connected hardware. Full â€˜convergenceâ€™ isnâ€™t here yet but you can already accomplish serious computing tasks like building and running software entirely on the phone.</p>

<p>This is quite a cool concept and I thought I should share the experience.</p>

<p>The first thing you need is a suitable phone. Iâ€™ve a growing collection owned by Canonical (plus one I own myself â€“ I got sucked into the coolness of carrying a general purpose computer around). You can find a list of the supported hardware at <a href="https://wiki.ubuntu.com/Touch/Devices">https://wiki.ubuntu.com/Touch/Devices</a> â€“ there youâ€™ll see that there are phones supported by Canonical as well as some community ports. The following works on Nexus 4 (mako), Nexus 10 (manta), BQ Aquaris E4.5 Ubuntu Edition (krillin) and Meizu MX4 Ubuntu Edition (arale) and I expect only small adjustments to be needed for other phones (like specifying an image server if the phone is not supported on the Canonical server).</p>

<p>I work on Ubuntu Desktop which means the desktop tools I mention below are readily available. On Ubuntu you can install these with:</p>

<pre class="programlisting">
  sudo apt-get install ubuntu-device-flash</pre>

<p>On other systems youâ€™ll have to work out the equivalent incantations. (They are in the packages android-tools-adb and ubuntu-device-flash.)</p>

<p>One final warning: if you simply want to develop Apps for the Ubuntu phone, this isnâ€™t the best approach (and there is plenty of guidance at <a href="http://www.ubuntu.com/phone/developers">http://www.ubuntu.com/phone/developers</a>). What follows will instead help you with setting up a general purpose development environment on the phone.</p>

<h2>Installing an OS image</h2>

<p>Depending on what you want to work on you can, of course, skip this section and use the production image that came with a commercial Ubuntu Touch phone. If youâ€™re re-purposing a phone that has Android on it, or you want access to one of the pre-release versions, then you need to write or update the OS image. (I am working on parts of the software that runs the phone and fall into the latter category.)</p>

<p>Note that the tools and commands that follow can completely replace the existing OS and user data. Use backup appropriately!</p>

<p>In this section, and the following one, we assume thereâ€™s a USB connection to your phone and that the phone is in â€˜developer modeâ€™. Essentially, you need to go into <strong>Settings</strong> &gt; <strong>About this phone</strong> &gt; <strong>Developer mode</strong>. For security reasons connecting to the phone like this requires you to have a lock code set and for the phone to be unlocked with the screen on.</p>

<p>The channels providing OS images for various uses are listed here: <a href="https://developer.ubuntu.com/en/start/ubuntu-for-devices/image-channels/">https://developer.ubuntu.com/en/start/ubuntu-for-devices/image-channels/</a>.</p>

<p>The tool used to manage installing the OS is ubuntu-device-flash. For example, to change the OS on a phone with an existing Ubuntu Touch installation:</p>

<pre class="programlisting">
  ubuntu-device-flash touch â€“channel=ubuntu-touch/devel/ubuntu</pre>

<p>If you want a clean installation (or to overwrite Andrioid) you need to get the phone into recovery mode (typically, power on with both volume buttons pressed) and add <code>â€“bootstrap</code> to the above command. NB you will lose any user data.</p>

<p>The ubuntu-device-flash touch tool  does have decent <code>â€“help</code> options. For example, <code>ubuntu-device-flash query â€“help</code>.</p>

<p>After installation, start the phone up, go through the setup and tutorial setting a security code and connecting to your wifi.</p>

<h2>Getting ssh working</h2>

<p>In this section, like the previous one, we assume thereâ€™s a USB connection to your phone and that the phone is in â€˜developer modeâ€™.</p>

<p>The instructions here are copied from <a href="http://askubuntu.com/questions/348714/how-can-i-access-my-ubuntu-phone-over-ssh">http://askubuntu.com/questions/348714/how-can-i-access-my-ubuntu-phone-over-ssh</a> for convenience. These enable ssh and push your public key to the default â€˜phabletâ€™ account.</p>

<pre class="programlisting">
adb shell android-gadget-service enable ssh
adb shell mkdir /home/phablet/.ssh
adb push ~/.ssh/id_rsa.pub /home/phablet/.ssh/authorized_keys
adb shell chown -R phablet.phablet /home/phablet/.ssh
adb shell chmod 700 /home/phablet/.ssh
adb shell chmod 600 /home/phablet/.ssh/authorized_keys</pre>

<p>Next, assuming your phone is connected to your LAN find the phoneâ€™s IP address:</p>

<pre class="programlisting">
  adb shell ip addr show wlan0|grep inet</pre>

<p>(You can also find the IP address through <strong>System Settings</strong> &gt; <strong>Wi-Fi</strong> &gt; â€˜<code>&gt;</code>â€™next to network name.)</p>

<p>From now on you donâ€™t need the USB connection and can connect over your network:</p>

<pre class="programlisting">
  ssh phablet@&lt;IP from above command&gt;</pre>

<p>Note that the ssh service isnâ€™t started automatically so, if you restart the phone, it may need starting again. You donâ€™t need a USB connection to do this, just type <code>android-gadget-service enable ssh</code> in the terminal app.</p>

<h2>Creating a chroot to work in</h2>

<p>There are a couple of reasons to do development work in a chroot. Firstly the system partition is almost full, so installing the tools you want would likely overfill it. Secondly, the system image isnâ€™t managed using debian packages, but by downloading deltas. If you make changes to the system file system, things will break. Also, If you later re-flash the OS image while preserving user data you donâ€™t lose the chroot.</p>

<p>The following is pulled together from email discussions and steals ideas from a number of developers. I especially mention Michal Sawicz who offered the original of the script in Listing 1 (he deserves all the credit for the good bits but no blame for the bad bits as Iâ€™ve tweaked it since).</p>

<p>Iâ€™ve just explained that using apt on the phone makes it incompatible with the OTA updates â€“ but Iâ€™m going to take a shortcut and install debootstrap temporarily. I hope this returns the system to its original state â€“ it hasnâ€™t broken on me yet. It is definitely safe if you donâ€™t allow OTA updates.</p>

<p>Now in the ssh session you started at the end of the last section:</p>

<pre class="programlisting">
 sudo mount -oremount,rw /
 sudo apt-get install debootstrap
 sudo mount -o remount,dev /home
 sudo debootstrap vivid /home/phablet/chroots/vivid
 sudo apt-get purge debootstrap
 sudo mount -o remount,ro /</pre>

<p>For convenience, set up a matching user account in the chroot. Listing 1 has a useful â€œchâ€ script that makes setting up the and using the chroot simpler.</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
#!/bin/bash

usage() {
  [ -n &quot;$2&quot; ] &amp;&amp; ( echo $2; echo )
  echo &quot;Usage:&quot;
  echo &quot;  sudo $0 [OPTIONS] CHROOT_PATH&quot;
  echo
  echo &quot;  Options:&quot;
  echo &quot;    -h|--help  displays this help message&quot;
  echo &quot;    -r|--root  chroot as root, not current user&quot;
  exit $1
}

ARGS=$(getopt -o r,h --long &quot;root,help&quot; -n &quot;$0&quot; -- &quot;$@&quot;);

if [ $? -ne 0 ];
then
  usage 3
fi

eval set -- &quot;$ARGS&quot;;

while true; do
  case &quot;$1&quot; in
    -h|--help)
      usage
      ;;
    -r|--root)
      shift;
      USERSPEC=0:0
      TARGET_UID=0
      ;;
    --)
      shift;
      break;
      ;;
  esac
done

setup_chroot() {
  # $1 is chroot path
  # $2 is user id

  # add current user in the chroot if doesn't exist
  chroot $1 id $2 &amp;&gt; /dev/null || useradd -R $1 -u $2 -m phablet

  # make the prompt hint when chrooted
  echo `id -un ${TARGET_UID:-$2}`@`basename $1` &gt; $1/etc/debian_chroot

  mount -t proc none $1/proc
  mount -t sysfs none $1/sys
  mount --bind /dev $1/dev
  mount --bind /usr/lib/locale/ $1/usr/lib/locale/
  mount --bind $HOME $1/$HOME

  cleanup() {
    umount $1/proc $1/sys $1/dev $1/$HOME
      $1/usr/lib/locale/
  }
  trap &quot;cleanup $1&quot; EXIT
}

[ `id -u` == 0 ] || usage 2 &quot;This script must be ran as root.&quot;
[ -d  &quot;$1&quot; ] || usage 1 &quot;You need to pass a valid chroot path as argument.&quot;

CHROOT=`readlink -f $1` || exit 2
shift
BASE_UID=`id -u $( logname )`
BASE_GID=`id -g $( logname )`

setup_chroot $CHROOT $BASE_UID

chroot --userspec ${USERSPEC:-$BASE_UID:$BASE_GID} $CHROOT $@
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 1</td>
	</tr>
</table>

<p>To add the â€œphabletâ€ user to sudoers in the chroot. In the ssh session:</p>

<pre class="programlisting">
  sudo ~/bin/ch --root chroots/vivid/</pre>

<p>Now, youâ€™re root in the chroot and type:</p>

<pre class="programlisting">
  sudo adduser phablet sudo
  passwd phablet</pre>

<p>This set up the chroot â€œphabletâ€ account to be able to use sudo. Now end the chroot root session and return to the ssh shell:</p>

<pre class="programlisting">
  ^D</pre>

<h2>Building some software</h2>

<p>From the ssh session you get into the â€œphabletâ€ account inside the chroot:</p>

<pre class="programlisting">
  sudo ~/bin/ch chroots/vivid/</pre>

<p>Youâ€™ll use this command every time you want to switch into the chroot for development.</p>

<p>Phone development uses an â€˜overlay ppaâ€™ on top of vivid (a.k.a. 15.04) so you will want to add this to the chroot. This will continue into next year when it will switch to a 16.04 base. The following steps add the overlay:</p>

<pre class="programlisting">
  sudo apt-get install software-properties-common
  sudo add-apt-repository ppa:ci-train-ppa-service/stable-phone-overlay
  sudo apt-get update</pre>

<p>Still as phablet within the chroot you should install your favourite build tools. Here are the ones I use for Mir:</p>

<pre class="programlisting">
  sudo apt-get install cmake g++ make bzr</pre>

<p>Now, one ought to be able to set up building a project as normal. I use the Mir project on which I work as an example but you should vary as appropriate for your own needs. The relevant instructions for Mir are at <a href="http://unity.ubuntu.com/mir/building_source_for_pc.html">http://unity.ubuntu.com/mir/building_source_for_pc.html</a>. Sill as phablet in the chroot:</p>

<pre class="programlisting">
  cd
  bzr branch lp:mir mir
  cd mir/</pre>
  
<p>Now the next step tells you to &lt;CodeComment&gt;sudo apt-get install devscripts equivs cmake&lt;/CodeComment&gt; but <code>equivs</code> isnâ€™t available in the phone archive so the incantation doesnâ€™t work. So instead of that command (and the one that follows), use what is in Listing 2.</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
sudo apt-get install cmake-data pkg-config debhelper doxygen xsltproc graphviz libboost-dev libboost-date-time-dev libboost-program-options-dev libboost-system-dev libboost-filesystem-dev protobuf-compiler libdrm-dev libegl1-mesa-dev libgles2-mesa-dev libgbm-dev libglm-dev libprotobuf-dev pkg-config android-headers libhardware-dev libandroid-properties-dev libgoogle-glog-dev liblttng-ust-dev libxkbcommon-dev libumockdev-dev umockdev libudev-dev libgtest-dev google-mock valgrind libglib2.0-dev libfreetype6-dev libevdev-dev uuid-dev python3 dh-python glmark2-es2-mir
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 2</td>
	</tr>
</table>

<p>Then resume the steps to build the project:</p>

<pre class="programlisting">
  mkdir build
  cd build
  cmake ..
  make -j2 all ptest</pre>
  
<p>Depending on the phone you're using you may be able to start more processes (e.g. -j4), but at this stage you should have built Mir and run the basic test suites. A few of the tests may fail as a side effect of running in the chroot: donâ€™t worry about them. You can also install Mir in the chroot as normal:</p>

<pre class="programlisting">
  sudo make install</pre>

<p>Then exit the chroot for the top-level ssh shell:</p>

<pre class="programlisting">
  ^D</pre>

<h2>Running the software</h2>

<p>Of course, installing software in a chroot is pretty limited (see the test failures mentioned above) but the following instructions for running the Mir â€œperformance testâ€ is an example of what you can do to work around this.</p>

<p>The performance test uses glmark and that is hard coded to find its data at /usr/share/glmark2. So that can work we once again make the file system writeable. I think adding an empty directory that can used for a â€œmount --bindâ€ is unlikely to cause problems (but it is a risk you may wish to avoid by removing it after use). Once again from the top-level ssh shell:</p>

<pre class="programlisting">
  sudo mount -oremount,rw /
  sudo mkdir /usr/share/glmark2
  sudo mount -o remount,ro /
  sudo mount --bind ~/chroots/vivid/usr/share/  glmark2//usr/share/glmark2</pre>

<p>Now, stop the graphics stack running the normal phone interface so that Mir can have sole access:</p>

<pre class="programlisting">
  sudo stop lightdm</pre>

<p>Now run the â€œperformance testâ€:</p>

<pre class="programlisting">
  export MIR_CLIENT_PLATFORM_PATH=~/chroots/vivid/usr/local/lib/mir/client-platform/
  export MIR_SERVER_PLATFORM_PATH=~/chroots/vivid/usr/local/lib/mir/server-platform/
  export PATH=~/chroots/vivid/usr/bin:~/chroots/vivid/usr/local/bin:$PATH
  export LD_LIBRARY_PATH=~/chroots/vivid/usr/local/lib/
  mirbacklight
  mir_performance_tests
</pre>

<p>Finally, start the graphics stack running the normal phone interface again.</p>

<pre class="programlisting">
  sudo start lightdm</pre>

<h2>Conclusion</h2>

<p>The above example shows how to go about working on Mir. Like most open source projects, thatâ€™s entirely possible for you to do. If youâ€™re not interested in doing that I hope you will use these notes as a starting point for your own ideas. The possibilities will become ever more interesting as the Ubuntu Touch platform matures.</p>

<h2>Acknowledgement</h2>
<p>Thanks to Kevin Gunn, Alexandros Frantzis and the Overload team for their feedback, which improved the article.</p>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
