├── .gitignore ├── LICENSE ├── README.md ├── install.js ├── package.json └── run.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | sandbox.js 3 | raspbian 4 | raspberrypi/ 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # raspberry-pi-container 2 | 3 | A systemd-nspawn raspberry pi container. 4 | 5 | Uses qemu-arm-static to emulate arm7 on any Linux host 6 | 7 | ```sh 8 | npm install -g raspberry-pi-container 9 | raspberry-pi-container # drops you in a raspbian shell so you can compile / test things on arm 10 | ``` 11 | 12 | ## Dependencies 13 | 14 | Requires systemd-nspawn (Linux) and qemu-arm-static which can usually be installed by your package manager. 15 | 16 | Fell free to PR specific install instructions per platform. 17 | 18 | ## License 19 | 20 | MIT 21 | -------------------------------------------------------------------------------- /install.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const { spawn, exec } = require('child_process') 4 | const fs = require('fs') 5 | const path = require('path') 6 | const unzipper = require('unzipper') 7 | const get = require('simple-get') 8 | const pump = require('pump') 9 | const prettier = require('prettier-bytes') 10 | const diff = require('ansi-diff')() 11 | 12 | const raspbian = process.env.RASPBIAN || 'https://downloads.raspberrypi.org/raspbian_lite_latest' 13 | const dir = path.join(__dirname, 'raspbian') 14 | 15 | exec('which qemu-arm-static', function (err, stdout) { 16 | const qemu = !err && stdout && stdout.toString().trim() 17 | if (!qemu) throw new Error('qemu-arm-static is required') 18 | 19 | exec('which systemd-nspawn', function (err, stdout) { 20 | if (err || !stdout) throw new Error('systemd-nspawn is required') 21 | clear() 22 | get(raspbian, function (err, res) { 23 | if (err || res.statusCode !== 200) throw new Error('Could not download raspbian') 24 | 25 | const total = parseInt(res.headers['content-length'], 10) 26 | let read = 0 27 | 28 | res.on('data', function (data) { 29 | read += data.length 30 | write('Downloading latest raspbian: ' + prettier(read) + ' / ' + prettier(total) + '\n') 31 | }) 32 | 33 | pump(res, unzipper.Extract({ path: dir }), function (err) { 34 | if (err) throw err 35 | 36 | const img = fs.readdirSync(dir).filter(x => /\.img$/i.test(x)) 37 | write('Latest raspbian: ' + img + '\n') 38 | }) 39 | }) 40 | }) 41 | }) 42 | 43 | function clear () { 44 | if (fs.existsSync(dir) === false) { 45 | return 46 | } 47 | fs.readdirSync(dir).forEach(function (name) { 48 | try { 49 | fs.unlinkSync(path.join(dir, name)) 50 | } catch (err) {} 51 | }) 52 | } 53 | 54 | function write (msg) { 55 | process.stdout.write(diff.update(msg)) 56 | } 57 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "raspberry-pi-container", 3 | "version": "1.1.0", 4 | "description": "systemd-nspawn raspberry-pi container backed by qemu-arm-static", 5 | "main": "index.js", 6 | "bin": { 7 | "raspberry-pi-container": "./run.js" 8 | }, 9 | "dependencies": { 10 | "ansi-diff": "^1.1.1", 11 | "mount-img": "^1.2.0", 12 | "prettier-bytes": "^1.0.4", 13 | "pump": "^3.0.0", 14 | "simple-get": "^3.0.3", 15 | "unzipper": "^0.9.4" 16 | }, 17 | "devDependencies": {}, 18 | "repository": { 19 | "type": "git", 20 | "url": "https://github.com/mafintosh/raspberry-pi-container.git" 21 | }, 22 | "scripts": { 23 | "install": "node install.js" 24 | }, 25 | "author": "Mathias Buus (@mafintosh)", 26 | "license": "MIT", 27 | "bugs": { 28 | "url": "https://github.com/mafintosh/raspberry-pi-container/issues" 29 | }, 30 | "homepage": "https://github.com/mafintosh/raspberry-pi-container" 31 | } 32 | -------------------------------------------------------------------------------- /run.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const path = require('path') 4 | const fs = require('fs') 5 | const { execSync, spawnSync } = require('child_process') 6 | 7 | const raspbian = path.join(__dirname, 'raspbian') 8 | const mount = path.join(require.resolve('mount-img/package.json'), '../index.sh') 9 | const mnt = path.join(__dirname, 'raspberrypi') 10 | const img = path.join(raspbian, fs.readdirSync(raspbian).filter(x => /\.img$/.test(x))[0]) 11 | 12 | const qemu = execSync('which qemu-arm-static').toString().trim() 13 | 14 | if (fs.existsSync(mnt) === false) { 15 | fs.mkdirSync(mnt) 16 | } 17 | 18 | spawnSync(mount, ['-p', '2', '-f', img, mnt], { 19 | stdio: 'inherit' 20 | }) 21 | 22 | spawnSync('sudo', ['systemd-nspawn', '--bind', qemu, '-D', mnt].concat(process.argv.slice(2)), { 23 | stdio: 'inherit' 24 | }) 25 | 26 | spawnSync('sudo', ['umount', '-f', mnt], { 27 | stdio: 'inherit' 28 | }) 29 | --------------------------------------------------------------------------------