GithubHelp home page GithubHelp logo

ryankurte / docker-rpi-emu Goto Github PK

View Code? Open in Web Editor NEW
109.0 12.0 34.0 376 KB

QEMU and helper scripts in a docker container for emulating a raspberry pi environment

License: MIT License

Makefile 25.33% Shell 67.35% Dockerfile 7.32%

docker-rpi-emu's Introduction

Docker emulation environment for Raspberry Pi

Are you sick of long compile times on your Raspberry Pi?
How much time have you spent loading Raspbian images from raspberrypi.org and hand customising them?
Do you too wish you could run Raspberry Pi apps in a docker container against a persistant .img file inside xhyve on macOS? Then this is the project for you!

This project provides a dockerised (err, containerised) Qemu based emulated environment for the Raspberry Pi, useful for building Raspberry Pi based projects on x64 computers, and for customising Raspbian images for distribution.

Please note that this is very new. It works pretty well uner linux and OSX for emulation and for creating images to deploy, but YMMV.
It's also not a very good docker container, requiring priveledged mode to mount loopback adaptors and qemu on the docker host. All the scripts here can be run on native linux if you're that way inclined.

Check it out on Github or Dockerhub

Example

Usage

Note that your docker host machine must have qemu installed. Using Docker for Mac the host environment includes this as standard (OSX docker-machines however do not come with qemu), in Debian you will need to install the qemu and qemu-user-static packages.

From the Repo

To get started with an Emulated CLI:

  1. Run git clone [email protected]:ryankurte/docker-rpi-emu.git check out this repository
  2. Run cd docker-rpi-emu to change into the directory
  3. Run make run-emu to launch the emulated environment

This will bootstrap a Raspbian image from raspberrypi.org, build the docker image, and launch the emulated environment.

For examples of how to customise this, checkout the Makefile.

From Dockerhub

To get started with Docker, first pull the image with docker pull ryankurte/docker-rpi-emu.

Ensure you have a Raspbian image handy (and you may want to back this up, it will be modified by anything you do in the emulated environment), then run the following command.

docker run -it --rm --privileged=true -v IMAGE_LOCATION:/usr/rpi/images -w /usr/rpi ryankurte/docker-rpi-emu /bin/bash -c './run.sh images/IMAGE_NAME [COMMAND]'

Where IMAGE_LOCATION is the directory containing your Raspbian image to be mounted, IMAGE_NAME is the name of the image to be used, and [COMMAND] is the optional command to be executed (inside the image).

For example:

docker run -it --rm --privileged=true -v /Users/ryan/projects/docker-rpi-emu/images:/usr/rpi/images -w /usr/rpi ryankurte/docker-rpi-emu /bin/bash -c './run.sh images/2016-05-27-raspbian-jessie-lite.img /bin/bash'

Will mount the image directory /Users/ryan/projects/docker-rpi-emu/images and the image 2016-05-27-raspbian-jessie-lite.img then run the command /bin/bash in the emulated environment.

Components

The docker container includes the required Qemu components to support emulation. This must be launched in privileged mode to allow mounting of loopback devices.

The container also includes a set of scripts to streamline the loading/customization/launch/unloading of Qemu environments, which are installed into the /usr/rpi directory on the device.

Commands

Commands are installed into the /usr/rpi directory of the docker image.

./mount.sh IMAGE DIR identifies the partition sizes in the image and mounts the raspbian image to the location specified by DIR (both root and boot partitions).
./unmount.sh DIR unmounts both partitions mounted to DIR using the above script.
./qemu-setup.sh DIR adds Qemu components to the image at the mount point specified by DIR.
./qemu-cleanup.sh DIR removes Qemu components from the image at the mount point specified by DIR.
./qemu-launch.sh DIR runs Qemu instance from the directory specified by DIR
./run.sh IMAGE [COMMAND] wires all the above commands together to simplify launching an emulated environment with the provided IMAGE and optional COMMAND to execute.

If you have any questions, comments, or suggestions, feel free to open an issue or a pull request.

docker-rpi-emu's People

Contributors

ryankurte avatar show0k avatar simonlingoogle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

docker-rpi-emu's Issues

Temporary failure in name resolution

I am trying to clone some github repos. This worked fine on my home workstation but failing on my office workstation. I am unable to figure out how it is related to DNS. Thank you.

adi@adi:~/ryankurte-docker-qemu-rpi$ ls
2019-07-10-raspbian-buster-lite.img
adi@adi:~/ryankurte-docker-qemu-rpi$ docker run -it --rm --privileged=true -v /home/adi/ryankurte-docker-qemu-rpi:/usr/rpi/images -w /usr/rpi --dns 8.8.8.8 ryankurte/docker-rpi-emu /bin/bash
root@292bdbfae9f3:/usr/rpi# ./run.sh images/2019-07-10-raspbian-buster-lite.img 
Attempting to mount images/2019-07-10-raspbian-buster-lite.img to /media/rpi
Attached base loopback at: /dev/loop17
Located partitions: p1 (/boot) at 8192 and p2 (/) at 540672
Closed loopback /dev/loop17
Mounted to /media/rpi and /media/rpi/boot
Bootstrapping Qemu
root@292bdbfae9f3:/# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 26  bytes 3113 (3.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

root@292bdbfae9f3:/# ping www.google.com
ping: www.google.com: Temporary failure in name resolution
root@292bdbfae9f3:/# exit
exit
root@292bdbfae9f3:/usr/rpi# exit
exit
adi@adi:~/ryankurte-docker-qemu-rpi$ 

ps, top commands do not work inside the container

docker run -it --rm --privileged=true -v /home/user1/docker/my-raspberry/:/usr/rpi/images -w /usr/rpi docker-rpi-emu /bin/bash -c './run.sh images/4graspi.img /bin/bash'
Attempting to mount images/4graspi.img to /media/rpi
Attached base loopback at: /dev/loop0
Located partitions: p1 (/boot) at 8192 and p2 (/) at 137216
Closed loopback /dev/loop0
Mounted to /media/rpi and /media/rpi/boot
Bootstrapping Qemu
root@5f412aa3147b:/# ps
Error: /proc must be mounted
To mount /proc at boot you need an /etc/fstab line like:
proc /proc proc defaults
In the meantime, run "mount proc /proc -t proc"
root@5f412aa3147b:/# uname -a
Linux 5f412aa3147b 4.4.0-45-generic #66~14.04.1-Ubuntu SMP Wed Oct 19 15:05:38 UTC 2016 armv7l GNU/Linux
root@5f412aa3147b:/#

trying to run raspbian-stretch-lite

docker run -it --rm --privileged=true -v /Users/Marco/Downloads:/usr/rpi/images -w /usr/rpi ryankurte/docker-rpi-emu /bin/bash -c './run.sh images/2018-04-18-raspbian-stretch-lite.img /bin/bash'
Attempting to mount images/2018-04-18-raspbian-stretch-lite.img to /media/rpi
Attached base loopback at: /dev/loop0
Located partitions: p1 (/boot) at 8192 and p2 (/) at 98304
Closed loopback /dev/loop0
mount: /media/rpi/boot: overlapping loop device exists for /usr/rpi/images/2018-04-18-raspbian-stretch-lite.img.

networking issue

Hey,
I'm trying the project using the minibian image.

It works fine, however, it seem it has a networking issue. When I try to download stuff (wget or apt-get) it blocks. Also, the ping command has a strange output:

ping 8.8.8.8

PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
Unsupported ioctl: cmd=0x8906
64 bytes from 8.8.8.8: icmp_seq=1 ttl=37 time=0.513 ms
Unsupported ioctl: cmd=0x8906
64 bytes from 8.8.8.8: icmp_seq=2 ttl=37 time=0.879 ms
Unsupported ioctl: cmd=0x8906
64 bytes from 8.8.8.8: icmp_seq=3 ttl=37 time=0.851 ms

fdisk not found

Getting this error:
./mount.sh: line 40: fdisk: command not found

Can't run docker-rpi-emu on Ubuntu 17.10

I know this problem has been mentioned in #6 and #10 but I couldn't find in those issues a solution for me so I'm trying by opening a new one.

I can't run docker-rpi-emu on my Ubuntu Artful 17.10, nor with jessie or stretch images, all I get is the following error mesage:

$ docker run -it --rm --privileged=true -v $PWD/images:/usr/rpi/images -w /usr/rpi ryankurte/docker-rpi-emun/bash -c './run.sh images/2016-05-27-raspbian-jessie-lite.img'
Attempting to mount images/2016-05-27-raspbian-jessie-lite.img to /media/rpi
Attached base loopback at: /dev/loop0
Located partitions: p1 (/boot) at 8192 and p2 (/) at 137216
Closed loopback /dev/loop0
Mounted to /media/rpi and /media/rpi/boot
Bootstrapping Qemu
chroot: failed to run command 'bin/bash': No such file or directory

Wether I use the docker image from hub.docker.com or rebuild it from the github project.

Is there anything that's missing on my side?

I use the following docker version Docker version 1.13.1, build 092cba3

overlapping loop device exists

My versions:

Linux 5.0.0-15-generic #16pop0-Ubuntu SMP Wed May 15 14:41:25 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
Docker version 18.09.5, build e8ff056

I am using raspbian latest in a makefile that looks like this:

CWD=$(shell pwd)
RUN_ARGS=-it --rm --privileged=true -v $(CWD)/images:/usr/rpi/images -w /usr/rpi ryankurte/docker-rpi-emu
IMAGE=zerostomp.img

# Launch the docker image into an emulated session
emu: images/$(IMAGE)
	@echo "Launching interactive emulated session"
	@docker run $(RUN_ARGS) /bin/bash -c './run.sh images/$(IMAGE)'

# open host in docker
host: images/$(IMAGE)
	@docker run $(RUN_ARGS) /bin/bash

# Expand the image by a specified size
# TODO: implement expand script to detect partition sizes
expand: images/$(IMAGE)
	@echo "Expanding disk by 1G."
	@dd if=/dev/zero bs=1M count=1024 >> images/$(IMAGE)
	@docker run $(RUN_ARGS) ./expand.sh images/$(IMAGE) 1024

# download a zip of current raspbian-lite
images/raspbian_lite.zip:
	@echo "Getting zip of latest raspbian_lite."
	@mkdir images
	@wget https://downloads.raspberrypi.org/raspbian_lite_latest -O images/raspbian_lite.zip

# unzip image to images/$(IMAGE)
images/$(IMAGE): images/raspbian_lite.zip
	@echo "Extracting disk image for raspbian_lite."
	@unzip images/raspbian_lite.zip
	@mv *.img images/$(IMAGE)

It can do all the downloading/extracting, etc, but when I try to make emu I get this:

Extracting disk image for raspbian_lite.
Archive:  images/raspbian_lite.zip
  inflating: 2019-04-08-raspbian-stretch-lite.img  
Launching interactive emulated session
Attempting to mount images/zerostomp.img to /media/rpi
Attached base loopback at: /dev/loop0
Located partitions: p1 (/boot) at 8192 and p2 (/) at 98304
Closed loopback /dev/loop0
mount: /media/rpi/boot: overlapping loop device exists for /usr/rpi/images/zerostomp.img.
make: *** [Makefile:10: emu] Error 32

This appears to be related to this issue. If I am understanding the issue correctly, we just need a sizelimit on /boot in mount.sh:

offset=$((8192*512)),sizelimit=$((85405*512))

Should I make a PR?

expand rpi image

Hi,
I'm trying to expand an existing image (latest raspberry jessie), with these commands adapted from your makefile:

 docker run -it --rm --privileged=true -v $PATH_TO_IMAGES:/usr/rpi/images -w /usr/rpi ryankurte/docker-rpi-emu ./expand.sh images/2016-09-23-ra
spbian-jessie-lite.img 4096

[...output...]
The filesystem is already 322048 (4k) blocks long.  Nothing to do!

Essentially, I want to compile stuff in a fresh jessie-lite and than write it on SD in qemu, but it doesn't fit the image size as it is.

I'm on mac with Docker.app , version 1.12.1, build 6f9534c.

Thank you for the nice docker image!

Mount folder in container to host?

Is it possible to mount a folder inside the container to the host? I have tried countless times mounting folders using the -v flag in docker, but I have a strange suspicion that due to the emulation it is not possible.

Thoughts? Ideas?

Failed to launch the emulated environment: chroot: failed to run command 'bin/bash': Exec format error

I am having an issue with launching the emulated CLI. Could you please advise?

Thanks

Andrea

C02PW287G8WP:docker-rpi-emu andreacortis$ ls -al
total 64
drwxr-xr-x  13 andreacortis  staff   442 Aug 29 10:34 .
drwxr-xr-x  22 andreacortis  staff   748 Aug 29 09:33 ..
-rw-r--r--   1 andreacortis  staff    13 Aug 29 09:33 .dockerignore
drwxr-xr-x  13 andreacortis  staff   442 Aug 29 09:33 .git
-rw-r--r--   1 andreacortis  staff    18 Aug 29 09:33 .gitignore
-rw-r--r--   1 andreacortis  staff   205 Aug 29 09:33 .travis.yml
-rw-r--r--   1 andreacortis  staff   499 Aug 29 09:33 Dockerfile
-rw-r--r--   1 andreacortis  staff  1075 Aug 29 09:33 LICENSE.md
-rw-r--r--   1 andreacortis  staff  1920 Aug 29 09:33 Makefile
-rw-r--r--   1 andreacortis  staff  2182 Aug 29 09:33 Makefile.example
-rw-r--r--   1 andreacortis  staff  3958 Aug 29 09:33 README.md
drwxr-xr-x   4 andreacortis  staff   136 Aug 29 09:47 images
drwxr-xr-x   9 andreacortis  staff   306 Aug 29 09:33 scripts

C02PW287G8WP:docker-rpi-emu andreacortis$ make run-emu
Building base docker image
Sending build context to Docker daemon 452.6 kB
Step 1 : FROM ubuntu
 ---> bd3d4369aebc
Step 2 : MAINTAINER Ryan Kurte <[email protected]>
 ---> Using cache
 ---> 0197a892a4eb
Step 3 : LABEL Description "Qemu based emulation for raspberry pi using loopback images"
 ---> Using cache
 ---> 81e0d0be7787
Step 4 : RUN apt-get update
 ---> Using cache
 ---> 529db456397e
Step 5 : RUN apt-get install -y --allow-unauthenticated     qemu     qemu-user-static     binfmt-support     parted     vim
 ---> Using cache
 ---> eddbf8a1ee24
Step 6 : RUN apt-get clean
 ---> Using cache
 ---> 1f21c2566d2d
Step 7 : RUN rm -rf /var/lib/apt
 ---> Using cache
 ---> 900cff98a367
Step 8 : RUN mkdir -p /usr/rpi
 ---> Using cache
 ---> f872305d93d6
Step 9 : WORKDIR /usr/rpi
 ---> Using cache
 ---> 14ddc705be33
Step 10 : COPY scripts/* /usr/rpi/
 ---> Using cache
 ---> f3e80969b2dc
Successfully built f3e80969b2dc
Launching interactive emulated session
Attempting to mount images/2016-05-27-raspbian-jessie-lite.img to /media/rpi
Attached base loopback at: /dev/loop0
Located partitions: p1 (/boot) at 8192 and p2 (/) at 137216
Closed loopback /dev/loop0
Mounted to /media/rpi and /media/rpi/boot
Bootstrapping Qemu
chroot: failed to run command 'bin/bash': Exec format error
make: *** [run-emu] Error 126

Unable to reboot RPI image

I've installed docker-rbi-emu from Dockerhub onto Docker for Mac. I am execute the following command and I get the shell.

docker run -it --rm --privileged=true -v /Users/tk/projects/images:/usr/rpi/images -w /usr/rpi ryankurte/docker-rpi-emu /bin/bash -c './run.sh images/2017-11-29-raspbian-stretch-lite.img /bin/bash'

However I can't reboot.

Here is an example.
root@cffdf720a6ec:/# sudo reboot
sudo: unable to resolve host cffdf720a6ec
Failed to connect to bus: No such file or directory
Failed to talk to init daemon.

Another example
root@cffdf720a6ec:/# shutdown -r now
Failed to connect to bus: No such file or directory
Failed to talk to init daemon.

3rd example. This time I tried the login command instead of going straight to bash.
docker run -it --rm --privileged=true -v /Users/tk/projects/images:/usr/rpi/images -w /usr/rpi ryankurte/docker-rpi-emu /bin/bash -c './run.sh images/2017-11-29-raspbian-stretch-lite.img login'
Attempting to mount images/2017-11-29-raspbian-stretch-lite.img to /media/rpi
Attached base loopback at: /dev/loop0
Located partitions: p1 (/boot) at 8192 and p2 (/) at 94208
Closed loopback /dev/loop0
Mounted to /media/rpi and /media/rpi/boot
Bootstrapping Qemu

Pi is up and I login in as user pi.
df7b99443f3f login: pi
Password:
Last login: Wed Feb 21 20:20:00 UTC 2018 on UNKNOWN
Linux df7b99443f3f 4.9.60-linuxkit-aufs #1 SMP Mon Nov 6 16:00:12 UTC 2017 armv7l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.

Reboot attempted
pi@df7b99443f3f:~ $ sudo reboot
sudo: unable to resolve host df7b99443f3f
Failed to connect to bus: No such file or directory
Failed to talk to init daemon.

chroot failed on ubuntu

I got this error but I don't understand the problem:

chroot: failed to run command 'bin/bash': No such file or directory

On an Ubuntu 16.04 Machine

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.