#!/bin/sh # # PiLC bootstrap # # Copyright 2016-2018 Michael Buesch # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # basedir="$(dirname "$0")" [ "$(echo "$basedir" | cut -c1)" = '/' ] || basedir="$PWD/$basedir" # The repository root is basedir. basedir="$basedir/.." MAIN_MIRROR="http://mirrordirector.raspbian.org/raspbian/" DEFAULT_SUITE=stretch RPIGPIO_VERSION="0.6.3" RPIGPIO_FILE="RPi.GPIO-$RPIGPIO_VERSION.tar.gz" RPIGPIO_MIRROR="https://netcologne.dl.sourceforge.net/project/raspberry-gpio-python/$RPIGPIO_FILE" RPIGPIO_SHA256="9366ff36104a39368759929e71f0d8ad6a88553497b3064cbc40f4248806cc19" SPIDEV_VERSION="3.2" SPIDEV_FILE="spidev-$SPIDEV_VERSION.tar.gz" SPIDEV_MIRROR="https://pypi.python.org/packages/36/83/73748b6e1819b57d8e1df8090200195cdae33aaa22a49a91ded16785eedd/$SPIDEV_FILE" SPIDEV_SHA256="09d2b5122f0dd79910713a11f9a0020f71537224bf829916def4fffc0ea59456" PPL_VERSION="0.1.1" PPL_FILE="ppl_v$PPL_VERSION.zip" PPL_MIRROR="./libs/pixtend/v1/ppl/$PPL_FILE" PPL_SHA256="103edcdbc377f8b478fcbc89117cbad143500c611cb714568f55513cece220d4" PPL2_VERSION="0.1.3" PPL2_FILE="pplv2_v$PPL2_VERSION.zip" PPL2_MIRROR="./libs/pixtend/v2/pplv2/$PPL2_FILE" PPL2_SHA256="cab6e7cd9062ffbf81a8f570ea0cad663addd8fe22e31cb75e887ae89e425651" info() { echo "--- $*" } error() { echo "=== ERROR: $*" >&2 } warning() { echo "=== WARNING: $*" >&2 } die() { error "$*" exit 1 } # print the first of its arguments. first() { echo "$1" } # print the last of its arguments. last() { while [ $# -gt 1 ]; do shift; done echo "$1" } # $1=program_name have_program() { which "$1" >/dev/null 2>&1 } # $1=program_name, ($2=description) assert_program() { local bin="$1" local desc="$2" [ -n "$desc" ] || desc="$bin" have_program "$bin" || die "$bin not found. Please install $desc." } term_signal() { die "Terminating signal received" } cleanup() { info "Cleaning up..." for mp in "$mp_shm" "$mp_proc_binfmt_misc" "$mp_proc" "$mp_sys" "$mp_bootimgfile" "$mp_rootimgfile"; do [ -n "$mp" -a -d "$mp" ] &&\ umount -l "$mp" >/dev/null 2>&1 done for mp in "$mp_bootimgfile" "$mp_rootimgfile"; do [ -n "$mp" -a -d "$mp" ] &&\ rmdir "$mp" >/dev/null 2>&1 done } write_image() { local image="$1" local dev="$2" if have_program blkdiscard; then info "Discarding $dev ..." blkdiscard "$dev" ||\ error "blkdiscard failed." else warning "Skipping discard. blkdiscard not installed." fi info "Writing $image to $dev ..." [ -b "$dev" ] || die "$dev is not a block device" mount | grep -q "$dev" && die "$dev is mounted. Refusing to write to it!" dd if="$image" of="$dev" bs=32M status=progress ||\ die "Failed to write image." } boot_config_file() { cat < "$opt_target_dir/usr/sbin/policy-rc.d" ||\ die "Failed to create policy-rc.d" chmod 755 "$opt_target_dir/usr/sbin/policy-rc.d" ||\ die "Failed to chmod policy-rc.d" info "Cleaning tmp..." rm -rf "$opt_target_dir"/tmp/* # Copy qemu. local qemu_bin="$opt_target_dir/$opt_qemu" if ! [ -x "$qemu_bin" ]; then info "Copying qemu binary from '$opt_qemu' to '$qemu_bin'..." mkdir -p "$(dirname "$qemu_bin")" ||\ die "Failed to make qemu base directory." cp "$opt_qemu" "$qemu_bin" ||\ die "Failed to copy qemu binary." fi info "Copying PiLC bootstrap script..." cp "$basedir/pilc/pilc-bootstrap.sh" "$opt_target_dir/" ||\ die "Failed to copy bootstrap script." info "Checking out awlsim..." local awlsim_dir="$opt_target_dir/tmp/awlsim" local checkout_dir="$awlsim_dir/src" rm -rf "$awlsim_dir" mkdir -p "$awlsim_dir" || die "mkdir failed" git clone --no-checkout "$basedir/.git" "$checkout_dir" ||\ die "Failed to clone" ( cd "$checkout_dir" ||\ die "Failed to cd" git checkout "$opt_branch" ||\ die "Failed to check out branch." git submodule update --init submodules/pyprofibus ||\ die "Failed to pull pyprofibus submodule" rm -r .git submodules/pyprofibus/.git ||\ die "Failed to remove .git directory." mv submodules/pyprofibus .. ||\ die "Failed to move pyprofibus submodule." ) || die # Fetch packages download "$opt_target_dir/tmp/$RPIGPIO_FILE" "$RPIGPIO_MIRROR" "$RPIGPIO_SHA256" download "$opt_target_dir/tmp/$SPIDEV_FILE" "$SPIDEV_MIRROR" "$SPIDEV_SHA256" download "$opt_target_dir/tmp/$PPL_FILE" "$PPL_MIRROR" "$PPL_SHA256" download "$opt_target_dir/tmp/$PPL2_FILE" "$PPL2_MIRROR" "$PPL2_SHA256" # Second stage will mount a few filesystems. # Keep track to umount them in cleanup. mp_proc="$opt_target_dir/proc" mp_proc_binfmt_misc="$opt_target_dir/proc/sys/fs/binfmt_misc" mp_sys="$opt_target_dir/sys" mp_shm="$opt_target_dir/dev/shm" } pilc_bootstrap_second_stage() { info "Running second stage ($opt_arch)..." [ -x /pilc-bootstrap.sh ] ||\ die "Second stage does not contain the bootstrap script." # Set up environment. export LC_ALL=C export LANGUAGE=C export LANG=C if [ "$opt_rpiver" = "1" -o "$opt_rpiver" = "0" ]; then info "Optimizing for RPi 1.x, zero(w) or later" local march="armv6kz" elif [ "$opt_rpiver" = "2" ]; then info "Optimizing for RPi 2.x or later" local march="armv7-a" else info "Optimizing for RPi 3.x or later" local march="armv8-a" fi export CFLAGS="-O3 -march=$march -mfpu=vfp -mfloat-abi=hard -pipe" export CXXFLAGS="$CFLAGS" # export CC=clang LINKCC=clang LDSHARED="clang -shared" CXX=clang++ # debootstrap second stage. if [ $opt_skip_debootstrap2 -eq 0 ]; then info "Running debootstrap second stage..." /debootstrap/debootstrap --verbose --second-stage ||\ die "Debootstrap second stage failed." fi info "Mounting /proc..." mkdir -p /proc ||\ die "Failed to create /proc mountpoint." mount -t proc proc /proc ||\ die "Mounting /proc failed." info "Mounting /sys..." mkdir -p /sys ||\ die "Failed to create /sys mountpoint." mount -t sysfs sysfs /sys ||\ die "Mounting /sys failed." info "Mounting /dev/shm..." mkdir -p /dev/shm ||\ die "Failed to create /dev/shm mountpoint." mount -t tmpfs tmpfs /dev/shm ||\ die "Mounting /dev/shm failed." info "Writing apt configuration..." cat > /etc/apt/sources.list < /etc/apt/apt.conf.d/99no-translations ||\ die "Failed to set apt.conf.d" info "Creating /etc/fstab" mkdir -p /config ||\ die "Failed to create /config" cat > /etc/fstab < /etc/hostname ||\ die "Failed to set hostname" printf 'PiLC GNU/Linux (based on Raspbian) \\n \\l\n\n' > /etc/issue ||\ die "Failed to create /etc/issue" printf 'PiLC GNU/Linux (based on Raspbian)\n' > /etc/issue.net ||\ die "Failed to create /etc/issue.net" sed -i -e 's|PRETTY_NAME=.*|PRETTY_NAME="PiLC"|' \ /etc/os-release ||\ die "Failed to set os-release PRETTY_NAME." sed -i -e 's|NAME=.*|NAME="PiLC"|' \ /etc/os-release ||\ die "Failed to set os-release NAME." sed -i -e 's|ID=.*|ID=pilc|' \ /etc/os-release ||\ die "Failed to set os-release ID." sed -i -e 's|ID_LIKE=.*|ID_LIKE=raspbian|' \ /etc/os-release ||\ die "Failed to set os-release ID_LIKE." sed -i -e 's|HOME_URL=.*|HOME_URL="https://bues.ch/a/pilc"|' \ /etc/os-release ||\ die "Failed to set os-release HOME_URL." sed -i -e 's|SUPPORT_URL=.*|SUPPORT_URL="https://bues.ch/a/pilc"|' \ /etc/os-release ||\ die "Failed to set os-release SUPPORT_URL." sed -i -e 's|BUG_REPORT_URL=.*|BUG_REPORT_URL="https://bues.ch/a/pilc"|' \ /etc/os-release ||\ die "Failed to set os-release BUG_REPORT_URL." info "Updating packages..." cat < /etc/ssh/sshd_not_to_be_run ||\ die "Failed to create /etc/ssh/sshd_not_to_be_run" echo 1 > /etc/ssh/ssh_create_keys ||\ die "Failed to create /etc/ssh/ssh_create_keys" info "Creating /etc/rc.local..." cat > /etc/rc.local < /sys/class/gpio/export /bin/echo in > /sys/class/gpio/gpio\${i}/direction done # Add /dev/ttyS0 link for convenience. if ! [ -e /dev/ttyS0 ]; then /bin/ln -s /dev/ttyAMA0 /dev/ttyS0 fi exit 0 EOF [ $? -eq 0 ] || die "Failed to create /etc/rc.local" chmod 755 /etc/rc.local || die "Failed to chmod /etc/rc.local" info "Creating /etc/modules-load.d/i2c.conf..." cat > /etc/modules-load.d/i2c.conf < "/etc/sudoers.d/00-pi" ||\ die "Failed to create /etc/sudoers.d/00-pi" info "Initializing home directory..." mkdir -p /home/pi/.vim || die "Failed to mkdir /home/pi/.vim" cat > /home/pi/.vim/vimrc < /home/pi/.tmux.conf <\ /etc/systemd/system/awlsim-server.service ||\ die "Failed to create awlsim-server.service" systemctl enable awlsim-server.service ||\ die "Failed to enable awlsim-server-service" ) || die info "Building pyprofibus..." ( cd /tmp/awlsim/pyprofibus ||\ die "Failed to cd" debuild -uc -us -b -d || die "debuild failed" info "Built pyprofibus files:" ls .. || die "Failed to list results" info "Installing pyprofibus..." dpkg -i ../python3-pyprofibus_*.deb ||\ die "Failed to install python3-pyprofibus" dpkg -i ../pypy-pyprofibus_*.deb ||\ die "Failed to install pypy-pyprofibus" dpkg -i ../profisniff_*.deb ||\ die "Failed to install profisniff" dpkg -i ../gsdparser_*.deb ||\ die "Failed to install gsdparser" # Copy debs rm -rf /home/pi/deb/pyprofibus mkdir -p /home/pi/deb/pyprofibus ||\ die "mkdir /home/pi/deb/pyprofibus failed" cp ../*pyprofibus*.deb ../*pyprofibus*.buildinfo ../*pyprofibus*.changes \ ../profisniff_*.deb ../gsdparser_*.deb \ /home/pi/deb/pyprofibus/ ||\ die "Failed to copy pyprofibus debs" ) || die rm -r /tmp/awlsim ||\ die "Failed to remove awlsim checkout." info "Configuring network..." cat > /etc/network/interfaces.d/lo < /etc/network/interfaces.d/enx < /etc/network/interfaces.d/eth < /etc/network/interfaces.d/eth$i < /etc/network/interfaces.d/wlan0 < "$opt_target_dir/boot/cmdline.txt" < "$opt_target_dir/boot/config.txt" ||\ die "Failed to create /boot/config.txt" local img="$(first "$opt_target_dir/boot/"vmlinuz-*-rpi)" if [ -e "$img" ]; then mv "$img" "$opt_target_dir/boot/kernel.img" ||\ die "Failed to create kernel.img" fi local img="$(first "$opt_target_dir/boot/"initrd.img-*-rpi)" if [ -e "$img" ]; then mv "$img" "$opt_target_dir/boot/initrd.img" ||\ die "Failed to create initrd.img" fi local img="$(first "$opt_target_dir/boot/"vmlinuz-*-rpi2)" if [ -e "$img" ]; then mv "$img" "$opt_target_dir/boot/kernel7.img" ||\ die "Failed to create kernel7.img" fi local img="$(first "$opt_target_dir/boot/"initrd.img-*-rpi2)" if [ -e "$img" ]; then mv "$img" "$opt_target_dir/boot/initrd7.img" ||\ die "Failed to create initrd7.img" fi # Prepare image paths. local imgfile="${opt_target_dir}${opt_imgsuffix}.img" local imgfile_zip="${imgfile}.7z" local bootimgfile="${imgfile}.boot" mp_bootimgfile="${bootimgfile}.mp" local rootimgfile="${imgfile}.root" mp_rootimgfile="${rootimgfile}.mp" rm -f "$imgfile" "$imgfile_zip" "$rootimgfile" "$bootimgfile" rmdir "$mp_bootimgfile" "$mp_rootimgfile" 2>/dev/null # Create images. if [ "$opt_img" -ne 0 ]; then info "Creating boot image..." mkfs.vfat -F 32 -i 7771B0BB -n boot -C "$bootimgfile" \ $(expr \( 64 \* 1024 \) - \( 4 \* 1024 \) ) ||\ die "Failed to create boot partition file system." mkdir "$mp_bootimgfile" ||\ die "Failed to make boot partition mount point." mount -o loop "$bootimgfile" "$mp_bootimgfile" ||\ die "Failed to mount boot partition." rsync -aHAX --inplace \ "$opt_target_dir/boot/" "$mp_bootimgfile/" ||\ die "Failed to copy boot files." umount "$mp_bootimgfile" ||\ die "Failed to umount boot partition." rmdir "$mp_bootimgfile" ||\ die "Failed to remove boot partition mount point." info "Creating root image..." mkfs.ext4 "$rootimgfile" $(expr \( 4000 - 64 \) \* 1024 ) ||\ die "Failed to create root filesystem." mkdir "$mp_rootimgfile" ||\ die "Failed to make root partition mount point." mount -o loop "$rootimgfile" "$mp_rootimgfile" ||\ die "Failed to mount root partition." rsync -aHAX --inplace \ --exclude='boot/*' \ --exclude='proc/*' \ --exclude='sys/*' \ --exclude='dev/shm/*' \ --exclude='tmp/*' \ --exclude="$(basename "$opt_qemu")" \ "$opt_target_dir/" "$mp_rootimgfile/" ||\ die "Failed to copy root files." umount "$mp_rootimgfile" ||\ die "Failed to umount root partition." rmdir "$mp_rootimgfile" ||\ die "Failed to remove root partition mount point." info "Creating image '$imgfile'..." dd if=/dev/zero of="$imgfile" bs=1M count=4000 conv=sparse ||\ die "Failed to create image file." parted "$imgfile" <