summary
I am trying to run the Android emulator that is included with Android Studio in an LXD container (for reproducibility, security, separation). The emulator runs, but freezes up regularly, and when it freezes, I am not able to stop the LXD container (lxc stop $VMNAME
), even after adding --force
to that command. The systemd-shutdown
process in the container cannot not be killed, even with -9
, and am forced to reboot the host. And when I reboot the host, it, too, never properly shuts down and I have to hold the power button down. Android Studio works fine in the LXD container as long as I don't start the Android emulator.
The host and guest OS are both Ubuntu 20.04. The host is running on a Dell XPS 13 9350.
The official docs (Configure hardware acceleration for the Android Emulator) say to install the packages qemu-kvm libvirt-bin ubuntu-vm-builder bridge-utils ia32-libs-multiarch
; however, many of these don't exist on Ubuntu 20.04.
How do I troubleshoot these issues?
what I tried
This is how I set up my LXD container:
- install container:
VMNAME=flutter3
lxc launch ubuntu:20.04 $VMNAME
lxc exec $VMNAME -- apt update && lxc exec $VMNAME -- apt upgrade -y
- create a file
lxc_x11.profile
which contains:
#from https://blog.simos.info/running-x11-software-in-lxd-containers/
config:
environment.DISPLAY: :0
environment.PULSE_SERVER: unix:/home/ubuntu/pulse-native
#nvidia.driver.capabilities: all
#nvidia.runtime: "true"
user.user-data: |
#cloud-config
runcmd:
- 'sed -i "s/; enable-shm = yes/enable-shm = no/g" /etc/pulse/client.conf'
packages:
- x11-apps
- mesa-utils
- pulseaudio
description: GUI LXD profile
devices:
PASocket1:
bind: container
connect: unix:/run/user/1000/pulse/native
listen: unix:/home/ubuntu/pulse-native
security.gid: "1000"
security.uid: "1000"
uid: "1000"
gid: "1000"
mode: "0777"
type: proxy
X0:
bind: container
connect: unix:@/tmp/.X11-unix/X0
listen: unix:@/tmp/.X11-unix/X0
security.gid: "1000"
security.uid: "1000"
type: proxy
mygpu:
type: gpu
name: x11
used_by: []
- add X11 support to container:
lxc profile create x11
cat lxc_x11.profile |lxc profile edit x11
lxc profile assign $VMNAME default,x11
lxc exec $VMNAME -- apt install -y x11-apps mesa-utils libxi6 pulseaudio pulseaudio-utils
lxc exec $VMNAME -- bash -c 'sed -i "s/; enable-shm = yes/enable-shm = no/g" /etc/pulse/client.conf'
lxc restart $VMNAME
lxc exec $VMNAME -- glxinfo -B
#test OpenGL accelleration: lxc exec $VMNAME -- sudo --user ubuntu glxgears
- install Flutter (from Getting started with Flutter on Ubuntu):
lxc exec $VMNAME -- sudo --u ubuntu -i
sudo snap install flutter --classic
sudo snap alias flutter.dart dart
sudo snap install android-studio --classic
android-studio # run through the first-install wizard accepting all the defaults, then exit
flutter config --android-studio-dir /snap/android-studio/current/android-studio
flutter doctor --android-licenses
flutter channel dev
flutter upgrade
flutter doctor
- enable KVM in LXC container and packages for virtual Android devices (source):
cat /proc/cpuinfo |grep -c vmx # should be more than 0
cat /sys/module/kvm_intel/parameters/nested # should be Y
virt-host-validate # should be PASS except IOMMU
lxc stop $VMNAME
lxc config device add $VMNAME kvm unix-char path=/dev/kvm
lxc start $VMNAME
lxc exec $VMNAME -- usermod -G kvm -a ubuntu
lxc exec $VMNAME -- ls -al /dev/kvm
lxc exec $VMNAME -- apt install -y libnss3 libxcomposite1 # to run Android in VM
- install packages for KVM that seemed to help Android emulator crashes:
lxc exec $VMNAME -- apt install -y qemu-kvm libvirt-clients libvirt-daemon-system bridge-utils virt-manager
lxc restart $VMNAME