Skip to content

Instantly share code, notes, and snippets.

@filmaj
Last active August 23, 2019 13:36
Show Gist options
  • Save filmaj/7090793 to your computer and use it in GitHub Desktop.
Save filmaj/7090793 to your computer and use it in GitHub Desktop.
putting together an lxc for appium on ubuntu

ubuntu lxc

NOTES:

  • p1 referred to below is simply the name given to the LXC. You can change this if you want, just remember to always refer to the same container name :P
  • Only for real devices: make sure that the vendor ID you enter into the android udev rules file matches the device you want to be visible
  1. first, set up vagrant VM to run on a "public network". My Vagrantfile's only network config is:

     config.vm.network "public_network", :bridge => 'en0: Wi-Fi (AirPort)'
    
  2. get inside yo ubuntu vm

  3. (hardware devices only) connect device and run either lsusb or usb-devices - make sure your ubuntu host sees the device.

  4. sudo apt-get update && sudo apt-get -y dist-upgrade

  5. sudo apt-get install -y linux-image-current-generic

  6. reboot your ubuntu host!

  7. sudo apt-get install -y lxc

  8. sudo lxc-create -t ubuntu -n p1 this may take a while, beertimenao

  9. edit /etc/default/lxc. we want to create a weird private network so our mac can talk to the container. change a few lines so they look like this:

     # Leave USE_LXC_BRIDGE as "true" if you want to use lxcbr0 for your
     # containers.  Set to "false" if you'll use virbr0 or another existing
     # bridge, or mavlan to your host's NIC.
     USE_LXC_BRIDGE="true"
     # If you change the LXC_BRIDGE to something other than lxcbr0, then
     # you will also need to update your /etc/lxc/lxc.conf as well as the
     # configuration (/var/lib/lxc/<container>/config) for any containers
     # already created using the default config to reflect the new bridge
     # name.
     # If you have the dnsmasq daemon installed, you'll also have to update
     # /etc/dnsmasq.d/lxc and restart the system wide dnsmasq daemon.
     LXC_BRIDGE="lxcbr0"
     LXC_ADDR="10.0.150.1"
     LXC_NETMASK="255.255.255.0"
     LXC_NETWORK="10.0.150.0/24"
     LXC_DHCP_RANGE="10.0.150.2,10.0.150.254"
     LXC_DHCP_MAX="253"
    
  10. edit /var/lib/lxc/p1/config and uncomment the line #lxc.aa_profile = unconfined

  11. run ifconfig and note down eth1's IP: this is your VM's ip. mine is 10.0.1.136, so for all following instructions, replace my IP with yours :)

  12. sudo iptables -t nat -A POSTROUTING -o 10.0.1.136 -j MASQUERADE

  13. we might need to add a route to 10.0.150.0 inside the vm.. im not sure.. ask Daniel! :D

  14. lets quickly hop back to Mac-land. Run route -n add 10.0.150.0/24 10.0.1.136. So, basically, route our container IP to our VM IP.

  15. You should be able to ping 10.0.150.1 from mac land. If not.. ask Daniel :D

  16. (hardware devices only) sudo mkdir -p /var/lib/lxc/p1/rootfs/dev/bus/usb

  17. (hardware devices only) sudo mount --bind /dev/bus/usb /var/lib/lxc/p1/rootfs/dev/bus/usb

  18. sudo lxc-start -n p1 -d start the container in the background`

  19. sudo lxc-console -n p1 connect to the container, use ubuntu/ubuntu as user/pass. sudo pass inside containers is ubuntu.

  20. sudo apt-get update

  21. sudo apt-get install -y x11vnc git vim xvfb wget unzip build-essential openjdk-6-jdk python-software-properties usbutils a drink of your choice for getting this far!

  22. sudo apt-get -y install libfuse2

  23. cd /tmp

  24. sudo apt-get download fuse

  25. sudo dpkg-deb -x fuse_* .

  26. sudo dpkg-deb -e fuse_*

  27. sudo rm fuse_*.deb

  28. edit /tmp/DEBIAN/postinst so it looks like:

     #!/bin/bash
     exit 0
    
  29. sudo dpkg-deb -b . /fuse.deb

  30. sudo dpkg -i /fuse.deb

  31. sudo apt-get install -y ia32-libs

  32. (hardware devices only) you should see your device connected! run usb-devices

  33. sudo add-apt-repository ppa:chris-lea/node.js

  34. press enter

  35. sudo apt-get update

  36. sudo apt-get install -y nodejs all the nodez are mine`

  37. sudo npm install -g appium mocha

  38. cd /

  39. if you have the android sdk zip on your mac, copy it to a shared folder on your vm, or wget http://dl.google.com/android/adt/adt-bundle-linux-x86_64-20131030.zip -O adt.zip maybe a snack this time?

  40. unzip adt.zip -d /home/ubuntu

  41. mv /home/ubuntu/adt-bundle-linux-x86_64-20131030 /home/ubuntu/adt

  42. echo "export JAVA_HOME=/usr" >> ~/.bashrc

  43. echo "export ANDROID_HOME=/home/ubuntu/adt/sdk" >> ~/.bashrc

  44. echo "export PATH=${PATH}:/home/ubuntu/adt/sdk/tools:/home/ubuntu/adt/sdk/platform-tools" >> ~/.bashrc

  45. (hardware devices only) echo "SUBSYSTEM==\"usb\", ATTR{idVendor}==\"18d1\", MODE=\"0666\", GROUP=\"plugdev\"" > /etc/udev/rules.d/51-android.rules

  46. sudo chmod -R a+rwx /home/ubuntu/adt

  47. (hardware devices only) sudo chmod a+r /etc/udev/rules.d/51-android.rules

  48. source ~/.bashrc

  49. sudo /usr/local/adt/sdk/tools/android create avd --name 4.4 --force --target android-19 create an android 4.4 virtual device (emulator configuration) called 4.4 using the arm image. whether to use the x86 or the arm image here depends on the particular hardware running. macbooks can run the intel image. server hardware may not be able to. intel is preferred (generally faster) compared to arm. at this time android 4.4 did not have an intel img available.

  50. sudo /usr/local/adt/sdk/tools/emulator -avd 4.4 -noaudio -noskin -no-window -verbose -qemu -vnc :2 & starts the emulator

  51. you may get errors about missing keymaps. Note down the location that the emulator is complaining it cant find the keymaps. I had to create that directory. Then, I found this random github repo with a ton of keymaps. I created/copied over the necessary files that the emulator was complaining about from there. That seemed to work. shrug

  52. the emulator should be visible to adb, but not "online" yet, check by running adb devices. we can probably script something really simple that polls adb devices and checks until the emulator goes from offline to device.

  53. double check your container's IP: ifconfig. It should be 10.0.150.x.

  54. once the emulator is no longer offline, go back to mac-land, open up a vnc viewer (I use Chicken of the VNC), point it to 10.0.150.x:5902, and voila! VNC!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment