|
| 1 | +Use [Armbian](https://www.armbian.com) to (automagically) compile Linux for your device, |
| 2 | +compile Bitcoin Core, Lightning and install all the other things you need, copy |
| 3 | +the blockchain and create an image for your SD card. |
| 4 | + |
| 5 | +## Ingredients |
| 6 | + |
| 7 | +* a board supported by Armbian. I suggest >= 16 GB eMMC storage and >= 2 GB of RAM |
| 8 | +* 1 microSD card >= 8 GB (only used for installation) |
| 9 | +* 1 microSD card >= 64 GB (stays in device, you can also use it during installation) |
| 10 | +* a computer (ideally >= 300 GB free space, >= 16 GB RAM) |
| 11 | +* a microSD card reader |
| 12 | + |
| 13 | +## Download and prune blockchain |
| 14 | + |
| 15 | +Download and install Bitcoin Core on your computer and wait for the full blockchain |
| 16 | +to sync. A few hints, if you open the Preferences (`Command` + `,` on macOS): |
| 17 | + |
| 18 | +* set "Size of database cache" to 1 GB less than your RAM (though no more than 10 GB). This makes things a lot faster. |
| 19 | +* click Open Configuration File and enter `prune=1` |
| 20 | +* if you have less than 200 GB of free disk space, use`prune=...` instead, with the amount in megabytes. Make it as large as possible, no less than 30000, but leave at least 50 GB free space. Unfortunately this does slow things down a bit. When you're done, you can reduce it all the way to 2 GB. |
| 21 | +* if you have an existing installation, make a copy of your bitcoin data directory (see below). Delete your wallet from the copy. If you don't have space for a fully copy, you can also put this copy on a USB drive. |
| 22 | + |
| 23 | +When it's done, open Help -> Debug Window. Click on the console tab and enter: `pruneblockchain 504500` |
| 24 | + |
| 25 | +This deletes all the blocks from the days before Lightning, about 20 GB at the time of writing. |
| 26 | + |
| 27 | +## Put blocks on microSD |
| 28 | + |
| 29 | +Use the 64 GB disk. If you only have one microSD card, you should perform this step |
| 30 | +after you finish [copying the bootable microSD image to eMMC](#Copy_microSD_card_to_device_eMMC). |
| 31 | + |
| 32 | +On a Mac, open disk utility. Find the SD card, click Erase and select exFAT. Open |
| 33 | +the disk in Finder and create a directory `blocks`. |
| 34 | + |
| 35 | +Find the Bitcoin data directory (`Library/Application Support/Bitcoin` on macOS) |
| 36 | +and open its `blocks` directory. Copy all files in `blocks` to this directory, |
| 37 | +except for the index folder. |
| 38 | + |
| 39 | +## Put rest of chain data in a shared folder |
| 40 | + |
| 41 | +Create a `shared` folder somewhere on your computer. Create a directory `blocks` |
| 42 | +inside of it, copy only the `blocks/index` folder to it. Also copy the `chainstate` |
| 43 | +directory to the `shared` folder. |
| 44 | + |
| 45 | +## Virtual Box |
| 46 | + |
| 47 | +Download [Virtual Box](https://www.virtualbox.org/wiki/Downloads), install it and |
| 48 | +when it asks, also install the guest extensions. The latter lets you share a folder |
| 49 | +between your computer and the VM. |
| 50 | + |
| 51 | +Armbian is picky about which Ubuntu version you use, so we'll use Ubuntu 18.04 Bionic |
| 52 | +both for the virtual machine as well as the device. If that doesn't work for some reason, |
| 53 | +the instructions below and all scripts most likely also work for Ubuntu 16.04 Xenial. |
| 54 | + |
| 55 | +If you already use Ubuntu 18.04 then of course you won't need the virtual machine, |
| 56 | +though if you run into strange errors, it might be worth trying. |
| 57 | + |
| 58 | +Download the [Ubuntu Server installer](https://www.ubuntu.com/download/server). |
| 59 | + |
| 60 | +Here's a good [step by step guide](https://github.com/bitcoin-core/docs/blob/master/gitian-building/gitian-building-create-vm-debian.md) |
| 61 | +for installing the VM, which some changes: |
| 62 | + |
| 63 | +* where it says "Debian", select "Ubuntu" |
| 64 | +* whenever you need a machine / user / disk name, enter "armbian" |
| 65 | +* give it as many CPU's as you have, but limit them to 90% so your machine doesn't freeze |
| 66 | +* give it at least 4 GB RAM, or 2 GB for every CPU you have, whichever is more |
| 67 | +* disk size: 50 GB should do |
| 68 | +* you can skip the Network Tab section, but |
| 69 | + * you should become familiar with SSH anyhow |
| 70 | + * Ubuntu doesn't enable SSH by default, so type `sudo apt-get install shh` after installation |
| 71 | +* the Ubuntu installer is pretty similar to the Debian one shown on that page (when in doubt, press enter) |
| 72 | + * it skips the root user stuff, so you just need to create a single password |
| 73 | + |
| 74 | +Go to the settings page of |
| 75 | +your virtual machine, to the Shared Folders tab. Click the + button, find the |
| 76 | +folder you just created, enter `shared` as the name and check the auto mount box. |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +Once the installation is complete, it should reboot the VM and you should see a |
| 81 | +login prompt. Use the password you entered earlier. |
| 82 | + |
| 83 | +Click on the VM window and then select Insert Guest Editions CD from the Devices menu. |
| 84 | + |
| 85 | +TODO: put most of the below in a script, split between prep and (re)build. |
| 86 | + |
| 87 | +Get Ubuntu up to date: |
| 88 | + |
| 89 | +``` |
| 90 | +sudo apt-get update |
| 91 | +sudo apt-get upgrade |
| 92 | +``` |
| 93 | + |
| 94 | +To install the Guest Editions: |
| 95 | + |
| 96 | +``` |
| 97 | +sudo apt-get install build-essential linux-headers-`uname -r` |
| 98 | +sudo /media/cdrom/./VBoxLinuxAdditions.run |
| 99 | +``` |
| 100 | + |
| 101 | +If for some reason after inserting guest editions `/media/cdrom` doesn't exist, try: |
| 102 | + |
| 103 | +``` |
| 104 | +sudo mkdir --p /media/cdrom |
| 105 | +sudo mount -t auto /dev/cdrom /media/cdrom/ |
| 106 | +cd /media/cdrom/ |
| 107 | +sudo sh VBoxLinuxAdditions.run |
| 108 | +``` |
| 109 | + |
| 110 | +Then reboot: `sudo reboot` |
| 111 | + |
| 112 | + |
| 113 | +Mount the shared drive with the correct permissions: |
| 114 | + |
| 115 | +```sh |
| 116 | +export USER_ID=`id -u` |
| 117 | +export GROUP_ID=`id -g` |
| 118 | +mkdir ~/shared |
| 119 | +sudo mount -t vboxsf -o umask=0022,gid=$GROUP_ID,uid=$USER_ID shared ~/shared |
| 120 | +``` |
| 121 | + |
| 122 | +Make sure everything is there: |
| 123 | + |
| 124 | +```sh |
| 125 | +ls ~/shared |
| 126 | +# blocks chainstate |
| 127 | +``` |
| 128 | + |
| 129 | +## Cross compile Bitcoin Core |
| 130 | + |
| 131 | +We need to cross-compile Bitcoin Core and C-Lightning, because it's too slow to |
| 132 | +do this during customize-image. In addition, we're compiling the master branch |
| 133 | +rather than the latest release tag, because we need ``-blocksdir`. |
| 134 | + |
| 135 | +```sh |
| 136 | +sudo apt-get install automake autotools-dev libtool g++-aarch64-linux-gnu \ |
| 137 | + g++-arm-linux-gnueabihf pkg-config ccache |
| 138 | + |
| 139 | +mkdir src |
| 140 | +git clone https://github.com/bitcoin/bitcoin.git src/bitcoin |
| 141 | + |
| 142 | +# TODO: Switch to release v0.17 tag once available |
| 143 | +# TODO: reuse install code between AWS and Armbian (with compile or fetch binary flag) |
| 144 | + |
| 145 | +pushd src/bitcoin |
| 146 | + pushd depends |
| 147 | + # TODO: check if 32 or 64 bit is required |
| 148 | + make HOST=arm-linux-gnueabihf NO_WALLET=1 NO_UPNP=1 NO_QT=1 -j5 |
| 149 | + # make HOST=aarch64-linux-gnu NO_WALLET=1 NO_UPNP=1 NO_QT=1 -j5 |
| 150 | + popd |
| 151 | + ./autogen.sh |
| 152 | + # TODO: check if 32 or 64 bit is required |
| 153 | + ./configure --disable-bench --disable-tests --prefix=$PWD/depends/arm-linux-gnueabihf --enable-glibc-back-compat --enable-reduce-exports LDFLAGS=-static-libstdc++ |
| 154 | + # ./configure --disable-bench --disable-tests --prefix=$PWD/depends/aarch64-linux-gnu --enable-glibc-back-compat --enable-reduce-exports LDFLAGS=-static-libstdc++ |
| 155 | + # TODO: get CPU count and memory |
| 156 | + make -j5 |
| 157 | +popd |
| 158 | +``` |
| 159 | + |
| 160 | +## Cross compile C-Lightning |
| 161 | + |
| 162 | +TODO: figure out how to cross compile |
| 163 | + |
| 164 | +## Armbian |
| 165 | + |
| 166 | +Clone the Armbian repo and the Matreon customization script: |
| 167 | + |
| 168 | +``` |
| 169 | +git clone --depth 1 https://github.com/armbian/build |
| 170 | +git clone https://github.com/Sjors/matreon.git |
| 171 | +``` |
| 172 | + |
| 173 | +Copy the Matreon custom build scripts to the right place: |
| 174 | + |
| 175 | +```sh |
| 176 | +mkdir -p build/userpatches/overlay/bin |
| 177 | +cp matreon/vendor/armbian/customize-image.sh build/userpatches |
| 178 | +cp matreon/vendor/armbian/lib.config build/userpatches |
| 179 | +``` |
| 180 | + |
| 181 | +Copy bitcoind to the right place: |
| 182 | + |
| 183 | +```sh |
| 184 | +cp src/bitcoin/src/bitcoind src/bitcoin/src/bitcoin-cli build/userpatches/overlay/bin |
| 185 | +``` |
| 186 | + |
| 187 | +Copy block index and chainstate: |
| 188 | + |
| 189 | +```sh |
| 190 | +cp -r ~/shared/blocks ~/build/userpatches/overlay |
| 191 | +cp -r ~/shared/chainstate ~/build/userpatches/overlay |
| 192 | +# Doesn't work, block and chainstate will be empty during the build: |
| 193 | +# mkdir ~/build/userpatches/overlay/blocks |
| 194 | +# mkdir ~/build/userpatches/overlay/chainstate |
| 195 | +# sudo mount --bind ~/shared/blocks ~/build/userpatches/overlay/blocks |
| 196 | +# sudo mount --bind ~/shared/chainstate ~/build/userpatches/overlay/chainstate |
| 197 | +``` |
| 198 | + |
| 199 | +Create an SSH key if you don't have one already and then copy `~/.ssh/id_rsa.pub` |
| 200 | +to the shared folder. If present, your pi will only be accessible via SSH using that |
| 201 | +key, whereas password login will only work if you have physical access to the device. |
| 202 | + |
| 203 | +### Start Armbian build |
| 204 | + |
| 205 | +```sh |
| 206 | +cd build |
| 207 | +./compile.sh RELEASE=bionic BUILD_DESKTOP=no KERNEL_ONLY=no KERNEL_CONFIGURE=no PRIVATE_CCACHE=yes |
| 208 | +``` |
| 209 | + |
| 210 | +After some initial work, it will ask you to select your board. Do so, and then sit |
| 211 | +back and wait... If all goes well, it should output something like: |
| 212 | + |
| 213 | +``` |
| 214 | +[ o.k. ] Writing U-boot bootloader [ /dev/loop1 ] |
| 215 | +[ o.k. ] Done building [ /home/armbian/build/output/images/Armbian_5.46_Nanopineoplus2_Ubuntu_bionic_next_4.14.48.img ] |
| 216 | +[ o.k. ] Runtime [ 30 min ] |
| 217 | +``` |
| 218 | + |
| 219 | +Move the resulting image to the shared folder so you can access it: |
| 220 | + |
| 221 | +```sh |
| 222 | +mv /home/armbian/build/output/images/Armbian*.img ~/shared |
| 223 | +``` |
| 224 | + |
| 225 | +You can shut the VM down now. |
| 226 | + |
| 227 | +## Prepare bootable microSD card |
| 228 | + |
| 229 | +Use [Etcher](https://etcher.io) to put the resulting `.img` file on the 8 GB SD card. |
| 230 | + |
| 231 | +The first time you login your user is `pi` and your password is `pi` (you'll be ask to pick a new one). |
| 232 | + |
| 233 | +If everything works, you can delete the VM if you like, but if you keep it around, |
| 234 | +the second time will be faster.I haven't worked out an upgrade mechanism yet. For |
| 235 | +the most part the device could just update itself. But for more complex changes, |
| 236 | +it might make more sense to build a new machine image from scratch and use it |
| 237 | +on a backup of your data. |
| 238 | + |
| 239 | +## Copy microSD card to device eMMC |
| 240 | + |
| 241 | +The device has eMMC storage which is faster than the microSD card, and you want |
| 242 | +to be able to replace this card with the 64 GB one that holds the blocks. |
| 243 | + |
| 244 | +To copy it over: |
| 245 | + |
| 246 | +```sh |
| 247 | +sudo nand-sata-install |
| 248 | +``` |
| 249 | + |
| 250 | +This powers off the device when its done. Eject the microSD card and insert the |
| 251 | +64 bit card. Now start the device again. |
| 252 | + |
| 253 | +## Check |
| 254 | + |
| 255 | +In your browser, go to http://192.168.0.100/ (enter the device IP address). |
| 256 | +You'll initially get a 500 error page (rather than nothing), but after a few minutes |
| 257 | +you Matreon page should appear! |
| 258 | + |
| 259 | +Try signing up as your own fan and make a 1 satoshi payment. |
| 260 | + |
| 261 | +## Your domain & HTTPS |
| 262 | + |
| 263 | +Only do this if you're comfortable with the whole world knowing your IP address. |
| 264 | +I'm working on Tor support as a more privacy friendly alternative. |
| 265 | + |
| 266 | +Your device needs to be visisble from the internet, so you have to forward port |
| 267 | +80 and 443 from your router to the device. In addition you need to forward port 9735 |
| 268 | +so your fans can connect to your Lightning node and pay you. Ideally you should |
| 269 | +also forward port 8883 so other Bitcoin nodes can connect to you. |
| 270 | + |
| 271 | +You then need to create an A Record in your domains DNS settings that points to |
| 272 | +your IP. |
| 273 | + |
| 274 | +Matreon will automatically obtain an HTTPS certificate for you if, during the |
| 275 | +steps above [TODO...] you entered a domain name, enabled https and provided an |
| 276 | +email address. As soon as it detects the A-Record, it will request the certificate |
| 277 | +and your domain should now work at https://... |
| 278 | + |
| 279 | +## Congrats |
| 280 | + |
| 281 | +If you pulled this off successfully, you now have the right skills to help the |
| 282 | +world verify that Bitcoin Core binaries are actually derived from the source code. |
| 283 | +Consider [contributing a Gitian build](https://github.com/bitcoin-core/docs/blob/master/gitian-building.md). |
0 commit comments