├── .gitignore ├── LICENSE ├── README.md ├── index.sh └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 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 | # nspawn-bootstrap 2 | 3 | Bootstrap a systemd-nspawn container with either an Arch, Ubuntu, or Debian distro. 4 | 5 | ```sh 6 | npm install -g nspawn-bootstrap 7 | ``` 8 | 9 | ## Usage 10 | 11 | ```sh 12 | nspawn-bootstrap ./ubuntu-16.04.img --ubuntu xenial --size 4GB 13 | ``` 14 | 15 | More options include 16 | 17 | ``` 18 | Usage: nspawn-bootstrap [options] 19 | 20 | --size 21 | --ubuntu 22 | --debian 23 | --arch 24 | 25 | Examples: 26 | 27 | nspawn-bootstrap --arch --size 4GB 28 | nspawn-bootstrap --ubuntu xenial --size 3GB 29 | nspawn-bootstrap --debian stable 30 | 31 | ``` 32 | 33 | ## License 34 | 35 | MIT 36 | -------------------------------------------------------------------------------- /index.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | 5 | FORCE=false 6 | NO_PARTS=false 7 | NO_CREATE=false 8 | IMAGE="" 9 | SIZE=${NSPAWN_BOOTSTRAP_IMAGE_SIZE:-4GB} 10 | 11 | while [ "$1" != "" ]; do 12 | case "$1" in 13 | --force) FORCE=true; shift ;; 14 | --size) SIZE="$2"; shift; shift ;; 15 | --ubuntu) UBUNTU="$2"; shift; shift ;; 16 | --debian) DEBIAN="$2"; shift; shift ;; 17 | --alpine) ALPINE=true; shift; ;; 18 | --arch) ARCH=true; shift; ;; 19 | --help) shift; ;; 20 | --no-partitions) NO_PARTS=true; shift ;; 21 | --mount) MNT="$2"; shift; shift ;; 22 | --no-create) NO_CREATE=true; shift ;; 23 | *) IMAGE="$1"; shift; ;; 24 | esac 25 | done 26 | 27 | if [ "$IMAGE" == "" ]; then 28 | echo "Usage: nspawn-bootstrap [options]" 29 | echo 30 | echo " --force" 31 | echo " --size " 32 | echo " --ubuntu " 33 | echo " --debian " 34 | echo " --arch" 35 | echo 36 | echo "Examples:" 37 | echo 38 | echo " nspawn-bootstrap --arch --size 4GB" 39 | echo " nspawn-bootstrap --ubuntu xenial --size 3GB" 40 | echo " nspawn-bootstrap --debian stable" 41 | echo 42 | exit 1 43 | fi 44 | 45 | SIZE=$(($(echo $SIZE | sed 's|[bB]||' | sed 's|[kK]|* 1024|' | sed 's|[mM]|* 1024 * 1024|' | sed 's|[gG]|* 1024 * 1024 * 1024|'))) 46 | 47 | required () { 48 | if [ "$(which $1 2>/dev/null)" == "" ]; then 49 | echo $1 is required 50 | exit 1 51 | fi 52 | } 53 | 54 | [ "$ARCH" != "" ] && required pacstrap 55 | [ "$UBUNTU" != "" ] && required debootstrap 56 | [ "$DEBIAN" != "" ] && required debootstrap 57 | 58 | if ! $NO_CREATE; then 59 | if $FORCE; then 60 | rm -f "$IMAGE" 61 | fi 62 | 63 | if [ -f "$IMAGE" ]; then 64 | echo $IMAGE already exists 65 | exit 1 66 | fi 67 | 68 | echo Allocating image ... 69 | fallocate -l "$SIZE" "$IMAGE" 70 | fi 71 | 72 | if ! $NO_PARTS; then 73 | echo Writing partition table ... 74 | printf 'n\n\n\n2048\n\na\nw\n' | fdisk "$IMAGE" -u >/dev/null 75 | fi 76 | 77 | echo Formatting to ext4 ... 78 | 79 | if $NO_PARTS; then 80 | DEV="$IMAGE" 81 | else 82 | DEV=$(sudo losetup -f --show "$IMAGE" --offset=$((2048 * 512))) 83 | fi 84 | 85 | if [ "$MNT" == "" ]; then 86 | MNT="$IMAGE.mnt" 87 | fi 88 | 89 | sudo mkfs.ext4 "$DEV" -q >/dev/null 90 | 91 | build () { 92 | mkdir -p "$MNT" 93 | sudo mount "$DEV" "$MNT" 94 | "$@" || ERR=$? 95 | sudo umount "$MNT" 96 | rmdir "$MNT" 97 | ! $NO_PART && sudo losetup -d "$DEV" 98 | [ "$ERR" != "" ] && rm -f "$IMAGE" && exit $ERR 99 | true 100 | } 101 | 102 | if [ "$ARCH" != "" ]; then 103 | echo Installing Arch ... 104 | build sudo pacstrap "$MNT" base 105 | elif [ "$UBUNTU" != "" ]; then 106 | echo Installing Ubuntu ... 107 | if [ "$(uname -m)" == "aarch64" ]; then 108 | build sudo debootstrap "$UBUNTU" "$MNT" http://ports.ubuntu.com/ 109 | else 110 | build sudo debootstrap "$UBUNTU" "$MNT" http://archive.ubuntu.com/ubuntu/ 111 | fi 112 | elif [ "$DEBIAN" != "" ]; then 113 | echo Installing Debian ... 114 | build sudo debootstrap "$DEBIAN" "$MNT" http://deb.debian.org/debian/ 115 | elif [ "$ALPINE" != "" ]; then 116 | echo Installing Alpine ... 117 | rm -rf /tmp/nspawn-bootstrap-alpine 118 | mkdir -p /tmp/nspawn-bootstrap-alpine 119 | wget https://dl-cdn.alpinelinux.org/alpine/v3.14/main/$(uname --processor)/apk-tools-static-2.12.7-r0.apk -O /tmp/nspawn-bootstrap-alpine/tar 120 | cd /tmp/nspawn-bootstrap-alpine 121 | tar zxf tar 122 | cd - 123 | build sudo /tmp/nspawn-bootstrap-alpine/sbin/apk.static --arch $(uname --processor) -X http://dl-cdn.alpinelinux.org/alpine/latest-stable/main/ -U --allow-untrusted --root "$MNT" --initdb add alpine-base 124 | rm -rf /tmp/nspawn-bootstrap-alpine 125 | else 126 | sudo losetup -d "$DEV" 127 | echo Done. Mount the first partition and install your OS. 128 | fi 129 | 130 | 131 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nspawn-bootstrap", 3 | "version": "1.6.3", 4 | "description": "Bootstrap a systemd-nspawn container with either an Arch, Ubuntu, or Debian distro", 5 | "dependencies": {}, 6 | "devDependencies": {}, 7 | "bin": { 8 | "nspawn-bootstrap": "./index.sh" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/mafintosh/nspawn-bootstrap.git" 13 | }, 14 | "author": "Mathias Buus (@mafintosh)", 15 | "license": "MIT", 16 | "bugs": { 17 | "url": "https://github.com/mafintosh/nspawn-bootstrap/issues" 18 | }, 19 | "homepage": "https://github.com/mafintosh/nspawn-bootstrap" 20 | } 21 | --------------------------------------------------------------------------------