Skip to content

Instantly share code, notes, and snippets.

@JakubVanek
Last active September 13, 2020 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JakubVanek/bfc1485ecaa4379e95d37ca569229cba to your computer and use it in GitHub Desktop.
Save JakubVanek/bfc1485ecaa4379e95d37ca569229cba to your computer and use it in GitHub Desktop.
Bitfocus Companion cross-chroot for Raspberry Pi
## PART 1: get RPi image ready
# where the chroot will be
CHROOT=$(pwd)/chroot
# companion directory (will be bind-mounted to /companion)
COMPANION=$(pwd)/companion
# download latest Raspbian
wget https://downloads.raspberrypi.org/raspios_lite_armhf_latest -O raspbian.zip
unzip raspbian.zip # decompress the image
IMAGE=$(pwd)/*raspios*.img # find the inner image name (can be just replaced with absolute path to the image)
truncate -s +4G "$IMAGE" # make the image file larger
sudo losetup -f -P "$IMAGE" # bind it to a loop device
LOOPDEV=$(losetup -j "$IMAGE" | cut -d : -f 1) # find out which loop device it is
echo "RPi image bound to $LOOPDEV" # loopdev == /dev/loopN; can be just looked up in "losetup" output
# grow the main partition to full size
sudo parted "$LOOPDEV" resizepart 2 100% # or just resize this in "gparted /dev/loopN"
sudo e2fsck -f "${LOOPDEV}p2" # check before filesystem resize
sudo resize2fs "${LOOPDEV}p2" # resize filesystem
# make target directory
mkdir -p $CHROOT
# mount SD card filesystems
sudo mount "${LOOPDEV}p2" "$CHROOT"
sudo mount "${LOOPDEV}p1" "$CHROOT/boot"
sudo mkdir "$CHROOT/companion"
# bind-mount kernel filesystems
sudo mount --rbind /dev "$CHROOT/dev"
sudo mount --rbind /sys "$CHROOT/sys"
sudo mount --rbind /proc "$CHROOT/proc"
sudo mount --rbind /run "$CHROOT/run"
sudo mount --bind "$COMPANION" "$CHROOT/companion" # bind-mount companion folder
sudo mount --make-rslave "$CHROOT" # magic sauce for umount -R (recursive unmounting)
# copy hostnames + dns config
sudo cp /etc/hosts /etc/resolv.conf /etc/hostname "$CHROOT/etc/"
# install qemu-user-static on host (QEMU needs to be 32bit: https://bugs.launchpad.net/qemu/+bug/1805913)
sudo apt install qemu-user-static:i386 binfmt-support
# copy QEMU to image (this is now our "interpreter" of ARM binaries on x86-64)
sudo cp /usr/bin/qemu-arm-static "$CHROOT/usr/bin/"
# and finally, enter the chroot
sudo -i chroot "$CHROOT"
## PART 2: installing software in the RPi image
# this happens inside the chroot
# update all the stuff
apt update
apt full-upgrade
# install required packages (libjepg-turbo may not be required though)
apt install npm cmake libusb-1.0-0-dev libudev-dev git libjpeg62-turbo-dev
# install node installer + yarn
npm install -g n
npm install -g yarn
# install latest node 8.x
n 8.17.0
## PART 3: compiling Companion
cd /companion # fully cloned git repository expected in $COMPANION
./tools/yarn.sh # compile everything
yarn run rpidist # let's create a release zip (VERY SLOW! tries to compress the image very hard)
# alternatively transfer (or NFS/SMB/SSH/... mount) the whole directory to RPi and run it there with yarn; hopefully it works OK
## PART 4: cleaning up
exit # leave chroot
sudo umount -R "$CHROOT" # unmount all filesystems in the chroot directory
# NOTE: if the command above fails, try the --make-rslave trick again and then retry the unmounting:
sudo mount --make-rslave "$CHROOT"
sudo umount -R "$CHROOT"
sudo losetup -d "$LOOPDEV" # free the loop device
## PART 5: reentering the chroot
IMAGE=$(pwd)/*raspios*.img # find the image
sudo losetup -f -P "$IMAGE" # bind it to a loop device
LOOPDEV=$(losetup -j "$IMAGE" | cut -d : -f 1) # find out which loop device it is
# mount SD card filesystems
sudo mount "${LOOPDEV}p2" "$CHROOT"
sudo mount "${LOOPDEV}p1" "$CHROOT/boot"
# bind-mount kernel filesystems
sudo mount --rbind /dev "$CHROOT/dev"
sudo mount --rbind /sys "$CHROOT/sys"
sudo mount --rbind /proc "$CHROOT/proc"
sudo mount --rbind /run "$CHROOT/run"
# bind-mount companion folder
sudo mount --bind "$COMPANION" "$CHROOT/companion"
sudo mount --make-rslave "$CHROOT" # magic sauce for umount -R (recursive unmounting)
# and now enter the chroot
sudo -i chroot "$CHROOT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment