##// END OF EJS Templates
.
Unknown -
r372:5134a4dae9e0
parent child
Show More
@@ -1,210 +1,214
1 1 #
2 2 # Setup RPi2/3 config and cmdline
3 3 #
4 4
5 5 # Load utility functions
6 6 . ./functions.sh
7 7
8 8 if [ "$BUILD_KERNEL" = true ] ; then
9 9 if [ -n "$RPI_FIRMWARE_DIR" ] && [ -d "$RPI_FIRMWARE_DIR" ] ; then
10 10 # Install boot binaries from local directory
11 11 cp "${RPI_FIRMWARE_DIR}"/boot/bootcode.bin "${BOOT_DIR}"/bootcode.bin
12 12 cp "${RPI_FIRMWARE_DIR}"/boot/fixup.dat "${BOOT_DIR}"/fixup.dat
13 13 cp "${RPI_FIRMWARE_DIR}"/boot/fixup_cd.dat "${BOOT_DIR}"/fixup_cd.dat
14 14 cp "${RPI_FIRMWARE_DIR}"/boot/fixup_x.dat "${BOOT_DIR}"/fixup_x.dat
15 15 cp "${RPI_FIRMWARE_DIR}"/boot/start.elf "${BOOT_DIR}"/start.elf
16 16 cp "${RPI_FIRMWARE_DIR}"/boot/start_cd.elf "${BOOT_DIR}"/start_cd.elf
17 17 cp "${RPI_FIRMWARE_DIR}"/boot/start_x.elf "${BOOT_DIR}"/start_x.elf
18 18 else
19 19 # Create temporary directory for boot binaries
20 20 temp_dir=$(as_nobody mktemp -d)
21 21
22 22 # Install latest boot binaries from raspberry/firmware github
23 23 as_nobody wget -q -O "${temp_dir}/bootcode.bin" "${FIRMWARE_URL}/bootcode.bin"
24 24 as_nobody wget -q -O "${temp_dir}/fixup.dat" "${FIRMWARE_URL}/fixup.dat"
25 25 as_nobody wget -q -O "${temp_dir}/fixup_cd.dat" "${FIRMWARE_URL}/fixup_cd.dat"
26 26 as_nobody wget -q -O "${temp_dir}/fixup_x.dat" "${FIRMWARE_URL}/fixup_x.dat"
27 27 as_nobody wget -q -O "${temp_dir}/start.elf" "${FIRMWARE_URL}/start.elf"
28 28 as_nobody wget -q -O "${temp_dir}/start_cd.elf" "${FIRMWARE_URL}/start_cd.elf"
29 29 as_nobody wget -q -O "${temp_dir}/start_x.elf" "${FIRMWARE_URL}/start_x.elf"
30 30
31 31 # Move downloaded boot binaries
32 32 mv "${temp_dir}/"* "${BOOT_DIR}/"
33 33
34 34 # Remove temporary directory for boot binaries
35 35 rm -fr "${temp_dir}"
36 36
37 37 # Set permissions of the boot binaries
38 38 chown -R root:root "${BOOT_DIR}"
39 39 chmod -R 600 "${BOOT_DIR}"
40 40 fi
41 41 fi
42 42
43 43 # Setup firmware boot cmdline
44 44 if [ "$ENABLE_UBOOTUSB" = true ] ; then
45 45 CMDLINE="dwc_otg.lpm_enable=0 root=/dev/sda2 rootfstype=ext4 rootflags=commit=100,data=writeback elevator=deadline rootwait console=tty1"
46 46 else
47 47 if [ "$ENABLE_SPLITFS" = true ] ; then
48 48 CMDLINE="dwc_otg.lpm_enable=0 root=/dev/sda1 rootfstype=ext4 rootflags=commit=100,data=writeback elevator=deadline rootwait console=tty1"
49 49 else
50 50 CMDLINE="dwc_otg.lpm_enable=0 root=/dev/mmcblk0p2 rootfstype=ext4 rootflags=commit=100,data=writeback elevator=deadline rootwait console=tty1"
51 51 fi
52 52 fi
53 53
54 54
55 55
56 56 # Add encrypted root partition to cmdline.txt
57 57 if [ "$ENABLE_CRYPTFS" = true ] ; then
58 58 if [ "$ENABLE_SPLITFS" = true ] ; then
59 59 CMDLINE=$(echo "${CMDLINE}" | sed "s/sda1/mapper\/${CRYPTFS_MAPPING} cryptdevice=\/dev\/sda1:${CRYPTFS_MAPPING}/")
60 60 else
61 61 if [ "$ENABLE_UBOOTUSB" = true ] ; then
62 62 CMDLINE=$(echo "${CMDLINE}" | sed "s/sda2/mapper\/${CRYPTFS_MAPPING} cryptdevice=\/dev\/sda2:${CRYPTFS_MAPPING}/")
63 63 else
64 64 CMDLINE=$(echo "${CMDLINE}" | sed "s/mmcblk0p2/mapper\/${CRYPTFS_MAPPING} cryptdevice=\/dev\/mmcblk0p2:${CRYPTFS_MAPPING}/")
65 65 fi
66 66 fi
67 67 fi
68 68
69 69 # Add serial console support
70 70 if [ "$ENABLE_CONSOLE" = true ] ; then
71 71 CMDLINE="${CMDLINE} console=ttyAMA0,115200 kgdboc=ttyAMA0,115200"
72 72 fi
73 73
74 74 # Remove IPv6 networking support
75 75 if [ "$ENABLE_IPV6" = false ] ; then
76 76 CMDLINE="${CMDLINE} ipv6.disable=1"
77 77 fi
78 78
79 79 # Automatically assign predictable network interface names
80 80 if [ "$ENABLE_IFNAMES" = false ] ; then
81 81 CMDLINE="${CMDLINE} net.ifnames=0"
82 82 else
83 83 CMDLINE="${CMDLINE} net.ifnames=1"
84 84 fi
85 85
86 86 # Set init to systemd if required by Debian release
87 87 if [ "$RELEASE" = "stretch" ] || [ "$RELEASE" = "buster" ] ; then
88 88 CMDLINE="${CMDLINE} init=/bin/systemd"
89 89 fi
90 90
91 91 # Install firmware boot cmdline
92 92 echo "${CMDLINE}" > "${BOOT_DIR}/cmdline.txt"
93 93
94 94 # Install firmware config
95 95 install_readonly files/boot/config.txt "${BOOT_DIR}/config.txt"
96 96
97 97 # Setup minimal GPU memory allocation size: 16MB (no X)
98 98 if [ "$ENABLE_MINGPU" = true ] ; then
99 99 echo "gpu_mem=16" >> "${BOOT_DIR}/config.txt"
100 100 fi
101 101
102 102 # Setup boot with initramfs
103 103 if [ "$ENABLE_INITRAMFS" = true ] ; then
104 104 echo "initramfs initramfs-${KERNEL_VERSION} followkernel" >> "${BOOT_DIR}/config.txt"
105 105 fi
106 106
107 107 # Disable RPi3 Bluetooth and restore ttyAMA0 serial device
108 108 if [ "$RPI_MODEL" = 3 ] || [ "$RPI_MODEL" = 3P ] || [ "$RPI_MODEL" = 3P ]; then
109 109 if [ "$ENABLE_CONSOLE" = true ] && [ "$ENABLE_UBOOT" = false ] ; then
110 110 echo "dtoverlay=pi3-disable-bt" >> "${BOOT_DIR}/config.txt"
111 111 echo "enable_uart=1" >> "${BOOT_DIR}/config.txt"
112 112 else
113 113 # Create temporary directory for Bluetooth sources
114 114 temp_dir=$(as_nobody mktemp -d)
115 115
116 116 # Fetch Bluetooth sources
117 117 as_nobody git -C "${temp_dir}" clone "${BLUETOOTH_URL}"
118 118
119 119 # Copy downloaded sources
120 120 mv "${temp_dir}/pi-bluetooth" "${R}/tmp/"
121 121
122 # Raspberry-sys-mod package for /dev/serial device needed by bluetooth service
123 wget -O "${R}/tmp/pi-bluetooth/99-com.rules" https://raw.githubusercontent.com/RPi-Distro/raspberrypi-sys-mods/master/etc.armhf/udev/rules.d/99-com.rules
124
125 # Bluetooth firmware from arch aur https://aur.archlinux.org/packages/pi-bluetooth/
126 wget -O "${R}/tmp/pi-bluetooth/LICENCE.broadcom_bcm43xx" https://aur.archlinux.org/cgit/aur.git/plain/LICENCE.broadcom_bcm43xx?h=pi-bluetooth
127 wget -O "${R}/tmp/pi-bluetooth/BCM43430A1.hcd" https://aur.archlinux.org/cgit/aur.git/plain/BCM43430A1.hcd?h=pi-bluetooth
128
122 129 # Set permissions
123 130 chown -R root:root "${R}/tmp/pi-bluetooth"
124 131
125 132 # Install files to chroot
126 133 # Install tools
127 134 install_readonly "${R}/tmp/pi-bluetooth/usr/bin/btuart" "${R}/usr/bin/btuart"
128 135 install_readonly "${R}/tmp/pi-bluetooth/usr/bin/bthelper" "${R}/usr/bin/bthelper"
129 136
130 137 # Install bluetooth udev rule
131 138 install_readonly "${R}/tmp/pi-bluetooth/lib/udev/rules.d/90-pi-bluetooth.rules" "${LIB_DIR}/udev/rules.d/90-pi-bluetooth.rules"
132 139 #aur
133 140 #install_readonly "${R}/tmp/pi-bluetooth/50-bluetooth-hci-auto-poweron.rules" "${ETC_DIR}/udev/rules.d/50-bluetooth-hci-auto-poweron.rules"
134 141
135 142 # Install Firmware Flash file and apropiate licence
136 143 mkdir "${ETC_DIR}/firmware/"
137 144
138 #aur https://aur.archlinux.org/packages/pi-bluetooth/
145 # Install firmware and licence
139 146 #install_readonly "${R}/tmp/pi-bluetooth/LICENCE.broadcom_bcm43xx" "${ETC_DIR}/firmware/LICENCE.broadcom_bcm43xx"
140 147 #install_readonly "${R}/tmp/pi-bluetooth/BCM43430A1.hcd" "${ETC_DIR}/firmware/BCM43430A1.hcd"
141 wget -O "${R}/tmp/pi-bluetooth/LICENCE.broadcom_bcm43xx" https://aur.archlinux.org/cgit/aur.git/plain/LICENCE.broadcom_bcm43xx?h=pi-bluetooth
142 wget -O "${R}/tmp/pi-bluetooth/BCM43430A1.hcd" https://aur.archlinux.org/cgit/aur.git/plain/BCM43430A1.hcd?h=pi-bluetooth
143 148 install_readonly "${R}/tmp/pi-bluetooth/LICENCE.broadcom_bcm43xx" "${ETC_DIR}/firmware/LICENCE.broadcom_bcm43xx"
144 149 install_readonly "${R}/tmp/pi-bluetooth/BCM43430A1.hcd" "${ETC_DIR}/firmware/LICENCE.broadcom_bcm43xx"
145 150
146 151 # Install systemd service for bluetooth
147 152 #install_readonly "${R}/tmp/pi-bluetooth/brcm43438.service" "${ETC_DIR}/systemd/system/brcm43438.service"
148 153 install_readonly "${R}/tmp/pi-bluetooth/debian/pi-bluetooth.bthelper@.service" "${ETC_DIR}/systemd/system/pi-bluetooth.bthelper@.service"
149 154 install_readonly "${R}/tmp/pi-bluetooth/debian/pi-bluetooth.hciuart.service" "${ETC_DIR}/systemd/system/pi-bluetooth.hciuart.service"
150 155
151 # Raspberry-sys-mod package
152 wget -O "${R}/tmp/pi-bluetooth/99-com.rules" https://raw.githubusercontent.com/RPi-Distro/raspberrypi-sys-mods/master/etc.armhf/udev/rules.d/99-com.rules
156
153 157 install_readonly "${R}/tmp/pi-bluetooth/99-com.rules" "${ETC_DIR}/udev/rules.d/99-com.rules"
154 158
155 159 # Remove temporary directory
156 160 rm -fr "${temp_dir}"
157 161
158 162 # Get /dev/serial back for compability
159 163
160 164
161 165 fi
162 166 fi
163 167
164 168 # Create firmware configuration and cmdline symlinks
165 169 ln -sf firmware/config.txt "${R}/boot/config.txt"
166 170 ln -sf firmware/cmdline.txt "${R}/boot/cmdline.txt"
167 171
168 172 # Install and setup kernel modules to load at boot
169 173 mkdir -p "${LIB_DIR}/modules-load.d/"
170 174 install_readonly files/modules/rpi2.conf "${LIB_DIR}/modules-load.d/rpi2.conf"
171 175
172 176 # Load hardware random module at boot
173 177 if [ "$ENABLE_HWRANDOM" = true ] && [ "$BUILD_KERNEL" = false ] ; then
174 178 sed -i "s/^# bcm2708_rng/bcm2708_rng/" "${LIB_DIR}/modules-load.d/rpi2.conf"
175 179 fi
176 180
177 181 # Load sound module at boot
178 182 if [ "$ENABLE_SOUND" = true ] ; then
179 183 sed -i "s/^# snd_bcm2835/snd_bcm2835/" "${LIB_DIR}/modules-load.d/rpi2.conf"
180 184 else
181 185 echo "dtparam=audio=off" >> "${BOOT_DIR}/config.txt"
182 186 fi
183 187
184 188 # Enable I2C interface
185 189 if [ "$ENABLE_I2C" = true ] ; then
186 190 echo "dtparam=i2c_arm=on" >> "${BOOT_DIR}/config.txt"
187 191 sed -i "s/^# i2c-bcm2708/i2c-bcm2708/" "${LIB_DIR}/modules-load.d/rpi2.conf"
188 192 sed -i "s/^# i2c-dev/i2c-dev/" "${LIB_DIR}/modules-load.d/rpi2.conf"
189 193 fi
190 194
191 195 # Enable SPI interface
192 196 if [ "$ENABLE_SPI" = true ] ; then
193 197 echo "dtparam=spi=on" >> "${BOOT_DIR}/config.txt"
194 198 echo "spi-bcm2708" >> "${LIB_DIR}/modules-load.d/rpi2.conf"
195 199 if [ "$RPI_MODEL" = 3 ] || [ "$RPI_MODEL" = 3P ]; then
196 200 sed -i "s/spi-bcm2708/spi-bcm2835/" "${LIB_DIR}/modules-load.d/rpi2.conf"
197 201 fi
198 202 fi
199 203
200 204 # Disable RPi2/3 under-voltage warnings
201 205 if [ -n "$DISABLE_UNDERVOLT_WARNINGS" ] ; then
202 206 echo "avoid_warnings=${DISABLE_UNDERVOLT_WARNINGS}" >> "${BOOT_DIR}/config.txt"
203 207 fi
204 208
205 209 # Install kernel modules blacklist
206 210 mkdir -p "${ETC_DIR}/modprobe.d/"
207 211 install_readonly files/modules/raspi-blacklist.conf "${ETC_DIR}/modprobe.d/raspi-blacklist.conf"
208 212
209 213 # Install sysctl.d configuration files
210 214 install_readonly files/sysctl.d/81-rpi-vm.conf "${ETC_DIR}/sysctl.d/81-rpi-vm.conf"
@@ -1,831 +1,829
1 1 #!/bin/bash
2 2 ########################################################################
3 3 # rpi23-gen-image.sh 2015-2017
4 4 #
5 5 # Advanced Debian "stretch" and "buster" bootstrap script for RPi2/3
6 6 #
7 7 # This program is free software; you can redistribute it and/or
8 8 # modify it under the terms of the GNU General Public License
9 9 # as published by the Free Software Foundation; either version 2
10 10 # of the License, or (at your option) any later version.
11 11 #
12 12 # Copyright (C) 2015 Jan Wagner <mail@jwagner.eu>
13 13 #
14 14 # Big thanks for patches and enhancements by 20+ github contributors!
15 15 ########################################################################
16 16
17 17 # Are we running as root?
18 18 if [ "$(id -u)" -ne "0" ] ; then
19 19 echo "error: this script must be executed with root privileges!"
20 20 exit 1
21 21 fi
22 22
23 23 # Check if ./functions.sh script exists
24 24 if [ ! -r "./functions.sh" ] ; then
25 25 echo "error: './functions.sh' required script not found!"
26 26 exit 1
27 27 fi
28 28
29 29 # Load utility functions
30 30 . ./functions.sh
31 31
32 32 # Load parameters from configuration template file
33 33 if [ -n "$CONFIG_TEMPLATE" ] ; then
34 34 use_template
35 35 fi
36 36
37 37 # Introduce settings
38 38 set -e
39 39 echo -n -e "\n#\n# RPi2/3 Bootstrap Settings\n#\n"
40 40 set -x
41 41
42 42 # Raspberry Pi model configuration
43 43 export RPI_MODEL=${RPI_MODEL:=2}
44 44
45 45 # Debian release
46 46 export RELEASE=${RELEASE:=buster}
47 47
48 48 #Kernel Branch
49 49 export KERNEL_BRANCH=${KERNEL_BRANCH:=""}
50 50
51 51 # URLs
52 52 KERNEL_URL=${KERNEL_URL:=https://github.com/raspberrypi/linux}
53 53 FIRMWARE_URL=${FIRMWARE_URL:=https://github.com/raspberrypi/firmware/raw/master/boot}
54 54 WLAN_FIRMWARE_URL=${WLAN_FIRMWARE_URL:=https://github.com/RPi-Distro/firmware-nonfree/raw/master/brcm}
55 55 COLLABORA_URL=${COLLABORA_URL:=https://repositories.collabora.co.uk/debian}
56 56 FBTURBO_URL=${FBTURBO_URL:=https://github.com/ssvb/xf86-video-fbturbo.git}
57 57 UBOOT_URL=${UBOOT_URL:=https://git.denx.de/u-boot.git}
58 58 VIDEOCORE_URL=${VIDEOCORE_URL:=https://github.com/raspberrypi/userland}
59 59 #BIS= Kernel has KVM and zswap enabled
60 60 RPI3_64_BIS_KERNEL_URL=${RPI3_64_BIS_KERNEL_URL:=https://github.com/sakaki-/bcmrpi3-kernel-bis/releases/download/4.14.80.20181113/bcmrpi3-kernel-bis-4.14.80.20181113.tar.xz}
61 61 #default bcmrpi3_defconfig target kernel
62 62 RPI3_64_DEF_KERNEL_URL=${RPI3_64_DEF_KERNEL_URL:=https://github.com/sakaki-/bcmrpi3-kernel/releases/download/4.14.80.20181113/bcmrpi3-kernel-4.14.80.20181113.tar.xz}
63 63 #enhanced kernel
64 64 RPI3_64_KERNEL_URL=${RPI3_64_KERNEL_URL:=$RPI3_64_BIS_KERNEL_URL}
65 #https://aur.archlinux.org/packages/pi-bluetooth/
66 #BLUETOOTH_URL=${BLUETOOTH_URL:=https://aur.archlinux.org/pi-bluetooth.git}
67 65 BLUETOOTH_URL=${BLUETOOTH_URL:=https://github.com/RPi-Distro/pi-bluetooth.git}
68 66
69 67 # Build directories
70 68 BASEDIR=${BASEDIR:=$(pwd)/images/${RELEASE}}
71 69 BUILDDIR="${BASEDIR}/build"
72 70
73 71 # Prepare date string for default image file name
74 72 DATE="$(date +%Y-%m-%d)"
75 73 if [ -z "$KERNEL_BRANCH" ] ; then
76 74 IMAGE_NAME=${IMAGE_NAME:=${BASEDIR}/${DATE}-${KERNEL_ARCH}-CURRENT-rpi${RPI_MODEL}-${RELEASE}-${RELEASE_ARCH}}
77 75 else
78 76 IMAGE_NAME=${IMAGE_NAME:=${BASEDIR}/${DATE}-${KERNEL_ARCH}-${KERNEL_BRANCH}-rpi${RPI_MODEL}-${RELEASE}-${RELEASE_ARCH}}
79 77 fi
80 78
81 79 # Chroot directories
82 80 R="${BUILDDIR}/chroot"
83 81 ETC_DIR="${R}/etc"
84 82 LIB_DIR="${R}/lib"
85 83 BOOT_DIR="${R}/boot/firmware"
86 84 KERNEL_DIR="${R}/usr/src/linux"
87 85 WLAN_FIRMWARE_DIR="${LIB_DIR}/firmware/brcm"
88 86
89 87 # Firmware directory: Blank if download from github
90 88 RPI_FIRMWARE_DIR=${RPI_FIRMWARE_DIR:=""}
91 89 # General settings
92 90 SET_ARCH=${SET_ARCH:=32}
93 91 HOSTNAME=${HOSTNAME:=rpi${RPI_MODEL}-${RELEASE}}
94 92 PASSWORD=${PASSWORD:=raspberry}
95 93 USER_PASSWORD=${USER_PASSWORD:=raspberry}
96 94 DEFLOCAL=${DEFLOCAL:="en_US.UTF-8"}
97 95 TIMEZONE=${TIMEZONE:="Europe/Berlin"}
98 96 EXPANDROOT=${EXPANDROOT:=true}
99 97
100 98 # Keyboard settings
101 99 XKB_MODEL=${XKB_MODEL:=""}
102 100 XKB_LAYOUT=${XKB_LAYOUT:=""}
103 101 XKB_VARIANT=${XKB_VARIANT:=""}
104 102 XKB_OPTIONS=${XKB_OPTIONS:=""}
105 103
106 104 # Network settings (DHCP)
107 105 ENABLE_DHCP=${ENABLE_DHCP:=true}
108 106
109 107 # Network settings (static)
110 108 NET_ADDRESS=${NET_ADDRESS:=""}
111 109 NET_GATEWAY=${NET_GATEWAY:=""}
112 110 NET_DNS_1=${NET_DNS_1:=""}
113 111 NET_DNS_2=${NET_DNS_2:=""}
114 112 NET_DNS_DOMAINS=${NET_DNS_DOMAINS:=""}
115 113 NET_NTP_1=${NET_NTP_1:=""}
116 114 NET_NTP_2=${NET_NTP_2:=""}
117 115
118 116 # APT settings
119 117 APT_PROXY=${APT_PROXY:=""}
120 118 APT_SERVER=${APT_SERVER:="ftp.debian.org"}
121 119
122 120 # Feature settings
123 121 ENABLE_CONSOLE=${ENABLE_CONSOLE:=true}
124 122 ENABLE_I2C=${ENABLE_I2C:=false}
125 123 ENABLE_SPI=${ENABLE_SPI:=false}
126 124 ENABLE_IPV6=${ENABLE_IPV6:=true}
127 125 ENABLE_SSHD=${ENABLE_SSHD:=true}
128 126 ENABLE_NONFREE=${ENABLE_NONFREE:=false}
129 127 ENABLE_WIRELESS=${ENABLE_WIRELESS:=false}
130 128 ENABLE_SOUND=${ENABLE_SOUND:=true}
131 129 ENABLE_DBUS=${ENABLE_DBUS:=true}
132 130 ENABLE_HWRANDOM=${ENABLE_HWRANDOM:=true}
133 131 ENABLE_MINGPU=${ENABLE_MINGPU:=false}
134 132 ENABLE_XORG=${ENABLE_XORG:=false}
135 133 ENABLE_WM=${ENABLE_WM:=""}
136 134 ENABLE_RSYSLOG=${ENABLE_RSYSLOG:=true}
137 135 ENABLE_USER=${ENABLE_USER:=true}
138 136 USER_NAME=${USER_NAME:="pi"}
139 137 ENABLE_ROOT=${ENABLE_ROOT:=false}
140 138 ENABLE_QEMU=${ENABLE_QEMU:=false}
141 139 ENABLE_SYSVINIT=${ENABLE_SYSVINIT:=false}
142 140
143 141 # SSH settings
144 142 SSH_ENABLE_ROOT=${SSH_ENABLE_ROOT:=false}
145 143 SSH_DISABLE_PASSWORD_AUTH=${SSH_DISABLE_PASSWORD_AUTH:=false}
146 144 SSH_LIMIT_USERS=${SSH_LIMIT_USERS:=false}
147 145 SSH_ROOT_PUB_KEY=${SSH_ROOT_PUB_KEY:=""}
148 146 SSH_USER_PUB_KEY=${SSH_USER_PUB_KEY:=""}
149 147
150 148 # Advanced settings
151 149 ENABLE_MINBASE=${ENABLE_MINBASE:=false}
152 150 ENABLE_REDUCE=${ENABLE_REDUCE:=false}
153 151 ENABLE_UBOOT=${ENABLE_UBOOT:=false}
154 152 UBOOTSRC_DIR=${UBOOTSRC_DIR:=""}
155 153 ENABLE_UBOOTUSB=${ENABLE_UBOOTUSB=false}
156 154 ENABLE_FBTURBO=${ENABLE_FBTURBO:=false}
157 155 ENABLE_VIDEOCORE=${ENABLE_VIDEOCORE:=true}
158 156 VIDEOCORESRC_DIR=${VIDEOCORESRC_DIR:=""}
159 157 FBTURBOSRC_DIR=${FBTURBOSRC_DIR:=""}
160 158 ENABLE_HARDNET=${ENABLE_HARDNET:=false}
161 159 ENABLE_IPTABLES=${ENABLE_IPTABLES:=false}
162 160 ENABLE_SPLITFS=${ENABLE_SPLITFS:=false}
163 161 ENABLE_INITRAMFS=${ENABLE_INITRAMFS:=false}
164 162 ENABLE_IFNAMES=${ENABLE_IFNAMES:=true}
165 163 DISABLE_UNDERVOLT_WARNINGS=${DISABLE_UNDERVOLT_WARNINGS:=}
166 164
167 165 # Kernel compilation settings
168 166 BUILD_KERNEL=${BUILD_KERNEL:=true}
169 167 KERNEL_REDUCE=${KERNEL_REDUCE:=false}
170 168 KERNEL_THREADS=${KERNEL_THREADS:=1}
171 169 KERNEL_HEADERS=${KERNEL_HEADERS:=true}
172 170 KERNEL_MENUCONFIG=${KERNEL_MENUCONFIG:=false}
173 171 KERNEL_REMOVESRC=${KERNEL_REMOVESRC:=true}
174 172 KERNEL_OLDDEFCONFIG=${KERNEL_OLDDEFCONFIG:=false}
175 173 KERNEL_CCACHE=${KERNEL_CCACHE:=false}
176 174 KERNEL_ZSWAP=${KERNEL_ZSWAP:=false}
177 175 KERNEL_VIRT=${KERNEL_VIRT:=false}
178 176 KERNEL_BPF=${KERNEL_BPF:=true}
179 177
180 178 # Kernel compilation from source directory settings
181 179 KERNELSRC_DIR=${KERNELSRC_DIR:=""}
182 180 KERNELSRC_CLEAN=${KERNELSRC_CLEAN:=false}
183 181 KERNELSRC_CONFIG=${KERNELSRC_CONFIG:=true}
184 182 KERNELSRC_PREBUILT=${KERNELSRC_PREBUILT:=false}
185 183
186 184 # Reduce disk usage settings
187 185 REDUCE_APT=${REDUCE_APT:=true}
188 186 REDUCE_DOC=${REDUCE_DOC:=true}
189 187 REDUCE_MAN=${REDUCE_MAN:=true}
190 188 REDUCE_VIM=${REDUCE_VIM:=false}
191 189 REDUCE_BASH=${REDUCE_BASH:=false}
192 190 REDUCE_HWDB=${REDUCE_HWDB:=true}
193 191 REDUCE_SSHD=${REDUCE_SSHD:=true}
194 192 REDUCE_LOCALE=${REDUCE_LOCALE:=true}
195 193
196 194 # Encrypted filesystem settings
197 195 ENABLE_CRYPTFS=${ENABLE_CRYPTFS:=false}
198 196 CRYPTFS_PASSWORD=${CRYPTFS_PASSWORD:=""}
199 197 CRYPTFS_MAPPING=${CRYPTFS_MAPPING:="secure"}
200 198 CRYPTFS_CIPHER=${CRYPTFS_CIPHER:="aes-xts-plain64:sha512"}
201 199 CRYPTFS_XTSKEYSIZE=${CRYPTFS_XTSKEYSIZE:=512}
202 200
203 201 # Chroot scripts directory
204 202 CHROOT_SCRIPTS=${CHROOT_SCRIPTS:=""}
205 203
206 204 # Packages required in the chroot build environment
207 205 APT_INCLUDES=${APT_INCLUDES:=""}
208 206 APT_INCLUDES="${APT_INCLUDES},apt-transport-https,apt-utils,ca-certificates,debian-archive-keyring,dialog,sudo,systemd,sysvinit-utils,locales,keyboard-configuration,console-setup"
209 207
210 208 #Packages to exclude from chroot build environment
211 209 APT_EXCLUDES=${APT_EXCLUDES:=""}
212 210
213 211 # Packages required for bootstrapping
214 212 REQUIRED_PACKAGES="debootstrap debian-archive-keyring qemu-user-static binfmt-support dosfstools rsync bmap-tools whois git bc psmisc dbus sudo netselect-apt"
215 213 MISSING_PACKAGES=""
216 214
217 215 # Packages installed for c/c++ build environment in chroot (keep empty)
218 216 COMPILER_PACKAGES=""
219 217
220 218 #If init and systemd-sysv are wanted e.g. halt/reboot/shutdown scripts
221 219 if [ "$ENABLE_SYSVINIT" = false ] ; then
222 220 APT_EXCLUDES="--exclude=${APT_EXCLUDES},init,systemd-sysv"
223 221 fi
224 222
225 223 #Check if apt-cacher-ng has its default port open on and set APT_PROXY
226 224 if [ -n "$(lsof -i :3142)" ] ; then
227 225 HTTP_PROXY=http://127.0.0.1:3142/
228 226 fi
229 227
230 228 #ipinfo=$(curl ipinfo.io | grep country )
231 229 #grep -o '\"[^"]*\"' $ipinfo | tr -d '"'
232 230 #grep -Po '"country":.*?[^\\]",' $(curl ipinfo.io | grep country )
233 231 #sed -i "s,http:,https:,g" "${ETC_DIR}/apt/sources.list"
234 232 #autconfigure best apt server to not spam ftp.debian.org
235 233 #rm files/apt/sources.list
236 234 #netselect-apt does not know buster yet
237 235 if [ "$RELEASE" = "buster" ] ; then
238 236 RLS=testing
239 237 else
240 238 RLS="$RELEASE"
241 239 fi
242 240
243 241 if [ -f "$(pwd)/files/apt/sources.list" ] ; then
244 242 rm "$(pwd)/files/apt/sources.list"
245 243 fi
246 244
247 245 if [ "$ENABLE_NONFREE" = true ] ; then
248 246 netselect-apt --arch "$RELEASE_ARCH" --tests 10 --sources --nonfree --outfile "$(pwd)/files/apt/sources.list" -d "$RLS"
249 247 else
250 248 netselect-apt --arch "$RELEASE_ARCH" --tests 10 --sources --outfile "$(pwd)/files/apt/sources.list" -d "$RLS"
251 249 fi
252 250
253 251 #sed and cut the result string so we can use it as APT_SERVER
254 252 APT_SERVER=$(grep -m 1 http files/apt/sources.list | sed "s|http://| |g" | cut -d ' ' -f 3)
255 253 APT_SERVER=${APT_SERVER::-1}
256 254
257 255 #make script easier and more stable to use with convenient setup switch. Just setup SET_ARCH and RPI_MODEL and your good to go!
258 256 if [ -n "$SET_ARCH" ] ; then
259 257 echo "Setting Architecture specific settings"
260 258 ##################################
261 259 # 64 bit config
262 260 ##################################
263 261 if [ "$SET_ARCH" = 64 ] ; then
264 262 echo "64 bit mode selected - Setting up enviroment"
265 263 # 64 bit depended settings
266 264 QEMU_BINARY=${QEMU_BINARY:=/usr/bin/qemu-aarch64-static}
267 265 KERNEL_ARCH=${KERNEL_ARCH:=arm64}
268 266 KERNEL_BIN_IMAGE=${KERNEL_BIN_IMAGE:="Image"}
269 267
270 268 if [ "$RPI_MODEL" = 3 ] || [ "$RPI_MODEL" = 3P ] ; then
271 269 REQUIRED_PACKAGES="${REQUIRED_PACKAGES} crossbuild-essential-arm64"
272 270 KERNEL_DEFCONFIG=${KERNEL_DEFCONFIG:=bcmrpi3_defconfig}
273 271 RELEASE_ARCH=${RELEASE_ARCH:=arm64}
274 272 KERNEL_IMAGE=${KERNEL_IMAGE:=kernel8.img}
275 273 CROSS_COMPILE=${CROSS_COMPILE:=aarch64-linux-gnu-}
276 274 else
277 275 echo "error: Only Raspberry PI 3 and 3B+ support 64bit"
278 276 exit 1
279 277 fi
280 278 fi
281 279
282 280 ##################################
283 281 # 32 bit config
284 282 ##################################
285 283 if [ "$SET_ARCH" = 32 ] ; then
286 284 echo "32 bit mode selected - Setting up enviroment"
287 285 #General 32bit configuration
288 286 QEMU_BINARY=${QEMU_BINARY:=/usr/bin/qemu-arm-static}
289 287 KERNEL_ARCH=${KERNEL_ARCH:=arm}
290 288 KERNEL_BIN_IMAGE=${KERNEL_BIN_IMAGE:="zImage"}
291 289
292 290 #Raspberry setting grouped by board compability
293 291 if [ "$RPI_MODEL" = 0 ] || [ "$RPI_MODEL" = 1 ] || [ "$RPI_MODEL" = 1P ] ; then
294 292 echo "Setting settings for bcm2835 Raspberry PI boards"
295 293 REQUIRED_PACKAGES="${REQUIRED_PACKAGES} crossbuild-essential-armel"
296 294 KERNEL_DEFCONFIG=${KERNEL_DEFCONFIG:=bcmrpi_defconfig}
297 295 RELEASE_ARCH=${RELEASE_ARCH:=armel}
298 296 KERNEL_IMAGE=${KERNEL_IMAGE:=kernel.img}
299 297 CROSS_COMPILE=${CROSS_COMPILE:=arm-linux-gnueabi-}
300 298 fi
301 299 if [ "$RPI_MODEL" = 2 ] || [ "$RPI_MODEL" = 3 ] || [ "$RPI_MODEL" = 3P ] ; then
302 300 echo "Setting settings for bcm2837 Raspberry PI boards"
303 301 REQUIRED_PACKAGES="${REQUIRED_PACKAGES} crossbuild-essential-armhf"
304 302 KERNEL_DEFCONFIG=${KERNEL_DEFCONFIG:=bcm2709_defconfig}
305 303 RELEASE_ARCH=${RELEASE_ARCH:=armhf}
306 304 KERNEL_IMAGE=${KERNEL_IMAGE:=kernel7.img}
307 305 CROSS_COMPILE=${CROSS_COMPILE:=arm-linux-gnueabihf-}
308 306 fi
309 307 fi
310 308 #SET_ARCH not set
311 309 else
312 310 echo "error: Please set '32' or '64' as value for SET_ARCH"
313 311 exit 1
314 312 fi
315 313
316 314 #Device specific configuration
317 315 echo "Select DTB-File"
318 316 case "$RPI_MODEL" in
319 317 0)
320 318 DTB_FILE=${DTB_FILE:=bcm2708-rpi-0-w.dtb}
321 319 UBOOT_CONFIG=${UBOOT_CONFIG:=rpi_defconfig}
322 320 ;;
323 321 1)
324 322 DTB_FILE=${DTB_FILE:=bcm2708-rpi-b.dtb}
325 323 UBOOT_CONFIG=${UBOOT_CONFIG:=rpi_defconfig}
326 324 ;;
327 325 1P)
328 326 DTB_FILE=${DTB_FILE:=bcm2708-rpi-b-plus.dtb}
329 327 UBOOT_CONFIG=${UBOOT_CONFIG:=rpi_defconfig}
330 328 ;;
331 329 2)
332 330 DTB_FILE=${DTB_FILE:=bcm2709-rpi-2-b.dtb}
333 331 UBOOT_CONFIG=${UBOOT_CONFIG:=rpi_2_defconfig}
334 332 ;;
335 333 3)
336 334 DTB_FILE=${DTB_FILE:=bcm2710-rpi-3-b.dtb}
337 335 UBOOT_CONFIG=${UBOOT_CONFIG:=rpi_3_defconfig}
338 336 ;;
339 337 3P)
340 338 DTB_FILE=${DTB_FILE:=bcm2710-rpi-3-b.dtb}
341 339 UBOOT_CONFIG=${UBOOT_CONFIG:=rpi_3_defconfig}
342 340 ;;
343 341 *)
344 342 echo "error: Raspberry Pi model $RPI_MODEL is not supported!"
345 343 exit 1
346 344 ;;
347 345 esac
348 346 echo "$DTB_FILE selected"
349 347
350 348 #DEBUG off
351 349 set +x
352 350
353 351 # Check if the internal wireless interface is supported by the RPi model
354 352 if [ "$ENABLE_WIRELESS" = true ] ; then
355 353 if [ "$RPI_MODEL" = 1 ] || [ "$RPI_MODEL" = 1P ] || [ "$RPI_MODEL" = 2 ] ; then
356 354 echo "error: The selected Raspberry Pi model has no internal wireless interface"
357 355 exit 1
358 356 else
359 357 echo "Raspberry Pi $RPI_MODEL has WIFI support"
360 358 fi
361 359 fi
362 360
363 361 # Check if DISABLE_UNDERVOLT_WARNINGS parameter value is supported
364 362 if [ -n "$DISABLE_UNDERVOLT_WARNINGS" ] ; then
365 363 if [ "$DISABLE_UNDERVOLT_WARNINGS" != 1 ] && [ "$DISABLE_UNDERVOLT_WARNINGS" != 2 ] ; then
366 364 echo "error: DISABLE_UNDERVOLT_WARNINGS=${DISABLE_UNDERVOLT_WARNINGS} is not supported"
367 365 exit 1
368 366 fi
369 367 fi
370 368
371 369 if [ "$ENABLE_VIDEOCORE" = true ] ; then
372 370 REQUIRED_PACKAGES="${REQUIRED_PACKAGES} cmake"
373 371 fi
374 372
375 373 # Add libncurses5 to enable kernel menuconfig
376 374 if [ "$KERNEL_MENUCONFIG" = true ] ; then
377 375 REQUIRED_PACKAGES="${REQUIRED_PACKAGES} libncurses-dev"
378 376 fi
379 377
380 378 # Add ccache compiler cache for (faster) kernel cross (re)compilation
381 379 if [ "$KERNEL_CCACHE" = true ] ; then
382 380 REQUIRED_PACKAGES="${REQUIRED_PACKAGES} ccache"
383 381 fi
384 382
385 383 # Add cryptsetup package to enable filesystem encryption
386 384 if [ "$ENABLE_CRYPTFS" = true ] && [ "$BUILD_KERNEL" = true ] ; then
387 385 REQUIRED_PACKAGES="${REQUIRED_PACKAGES} cryptsetup"
388 386 APT_INCLUDES="${APT_INCLUDES},cryptsetup,busybox,console-setup"
389 387
390 388 if [ -z "$CRYPTFS_PASSWORD" ] ; then
391 389 echo "error: no password defined (CRYPTFS_PASSWORD)!"
392 390 exit 1
393 391 fi
394 392 ENABLE_INITRAMFS=true
395 393 fi
396 394
397 395 # Add initramfs generation tools
398 396 if [ "$ENABLE_INITRAMFS" = true ] && [ "$BUILD_KERNEL" = true ] ; then
399 397 APT_INCLUDES="${APT_INCLUDES},initramfs-tools"
400 398 fi
401 399
402 400 # Add device-tree-compiler required for building the U-Boot bootloader
403 401 if [ "$ENABLE_UBOOT" = true ] ; then
404 402 APT_INCLUDES="${APT_INCLUDES},device-tree-compiler,bison,flex,bc"
405 403 else
406 404 if [ "$ENABLE_UBOOTUSB" = true ] ; then
407 405 echo "error: Enabling UBOOTUSB requires u-boot to be enabled"
408 406 exit 1
409 407 fi
410 408 fi
411 409
412 410 # Check if root SSH (v2) public key file exists
413 411 if [ -n "$SSH_ROOT_PUB_KEY" ] ; then
414 412 if [ ! -f "$SSH_ROOT_PUB_KEY" ] ; then
415 413 echo "error: '$SSH_ROOT_PUB_KEY' specified SSH public key file not found (SSH_ROOT_PUB_KEY)!"
416 414 exit 1
417 415 fi
418 416 fi
419 417
420 418 # Check if $USER_NAME SSH (v2) public key file exists
421 419 if [ -n "$SSH_USER_PUB_KEY" ] ; then
422 420 if [ ! -f "$SSH_USER_PUB_KEY" ] ; then
423 421 echo "error: '$SSH_USER_PUB_KEY' specified SSH public key file not found (SSH_USER_PUB_KEY)!"
424 422 exit 1
425 423 fi
426 424 fi
427 425
428 426 # Check if all required packages are installed on the build system
429 427 for package in $REQUIRED_PACKAGES ; do
430 428 if [ "$(dpkg-query -W -f='${Status}' $package)" != "install ok installed" ] ; then
431 429 MISSING_PACKAGES="${MISSING_PACKAGES} $package"
432 430 fi
433 431 done
434 432
435 433 # If there are missing packages ask confirmation for install, or exit
436 434 if [ -n "$MISSING_PACKAGES" ] ; then
437 435 echo "the following packages needed by this script are not installed:"
438 436 echo "$MISSING_PACKAGES"
439 437
440 438 printf "\ndo you want to install the missing packages right now? [y/n] "
441 439 read -r confirm
442 440 [ "$confirm" != "y" ] && exit 1
443 441
444 442 # Make sure all missing required packages are installed
445 443 apt-get -qq -y install "${MISSING_PACKAGES}"
446 444 fi
447 445
448 446 # Check if ./bootstrap.d directory exists
449 447 if [ ! -d "./bootstrap.d/" ] ; then
450 448 echo "error: './bootstrap.d' required directory not found!"
451 449 exit 1
452 450 fi
453 451
454 452 # Check if ./files directory exists
455 453 if [ ! -d "./files/" ] ; then
456 454 echo "error: './files' required directory not found!"
457 455 exit 1
458 456 fi
459 457
460 458 # Check if specified KERNELSRC_DIR directory exists
461 459 if [ -n "$KERNELSRC_DIR" ] && [ ! -d "$KERNELSRC_DIR" ] ; then
462 460 echo "error: '${KERNELSRC_DIR}' specified directory not found (KERNELSRC_DIR)!"
463 461 exit 1
464 462 fi
465 463
466 464 # Check if specified UBOOTSRC_DIR directory exists
467 465 if [ -n "$UBOOTSRC_DIR" ] && [ ! -d "$UBOOTSRC_DIR" ] ; then
468 466 echo "error: '${UBOOTSRC_DIR}' specified directory not found (UBOOTSRC_DIR)!"
469 467 exit 1
470 468 fi
471 469
472 470 # Check if specified VIDEOCORESRC_DIR directory exists
473 471 if [ -n "$VIDEOCORESRC_DIR" ] && [ ! -d "$VIDEOCORESRC_DIR" ] ; then
474 472 echo "error: '${VIDEOCORESRC_DIR}' specified directory not found (VIDEOCORESRC_DIR)!"
475 473 exit 1
476 474 fi
477 475
478 476 # Check if specified FBTURBOSRC_DIR directory exists
479 477 if [ -n "$FBTURBOSRC_DIR" ] && [ ! -d "$FBTURBOSRC_DIR" ] ; then
480 478 echo "error: '${FBTURBOSRC_DIR}' specified directory not found (FBTURBOSRC_DIR)!"
481 479 exit 1
482 480 fi
483 481
484 482 # Check if specified CHROOT_SCRIPTS directory exists
485 483 if [ -n "$CHROOT_SCRIPTS" ] && [ ! -d "$CHROOT_SCRIPTS" ] ; then
486 484 echo "error: ${CHROOT_SCRIPTS} specified directory not found (CHROOT_SCRIPTS)!"
487 485 exit 1
488 486 fi
489 487
490 488 # Check if specified device mapping already exists (will be used by cryptsetup)
491 489 if [ -r "/dev/mapping/${CRYPTFS_MAPPING}" ] ; then
492 490 echo "error: mapping /dev/mapping/${CRYPTFS_MAPPING} already exists, not proceeding"
493 491 exit 1
494 492 fi
495 493
496 494 # Don't clobber an old build
497 495 if [ -e "$BUILDDIR" ] ; then
498 496 echo "error: directory ${BUILDDIR} already exists, not proceeding"
499 497 exit 1
500 498 fi
501 499
502 500 # Setup chroot directory
503 501 mkdir -p "${R}"
504 502
505 503 # Check if build directory has enough of free disk space >512MB
506 504 if [ "$(df --output=avail "${BUILDDIR}" | sed "1d")" -le "524288" ] ; then
507 505 echo "error: ${BUILDDIR} not enough space left to generate the output image!"
508 506 exit 1
509 507 fi
510 508
511 509 set -x
512 510
513 511 # Call "cleanup" function on various signals and errors
514 512 trap cleanup 0 1 2 3 6
515 513
516 514 # Add required packages for the minbase installation
517 515 if [ "$ENABLE_MINBASE" = true ] ; then
518 516 APT_INCLUDES="${APT_INCLUDES},vim-tiny,netbase,net-tools,ifupdown"
519 517 fi
520 518
521 519 # Add parted package, required to get partprobe utility
522 520 if [ "$EXPANDROOT" = true ] ; then
523 521 APT_INCLUDES="${APT_INCLUDES},parted"
524 522 fi
525 523
526 524 # Add dbus package, recommended if using systemd
527 525 if [ "$ENABLE_DBUS" = true ] ; then
528 526 APT_INCLUDES="${APT_INCLUDES},dbus"
529 527 fi
530 528
531 529 # Add iptables IPv4/IPv6 package
532 530 if [ "$ENABLE_IPTABLES" = true ] ; then
533 531 APT_INCLUDES="${APT_INCLUDES},iptables,iptables-persistent"
534 532 fi
535 533
536 534 # Add openssh server package
537 535 if [ "$ENABLE_SSHD" = true ] ; then
538 536 APT_INCLUDES="${APT_INCLUDES},openssh-server"
539 537 fi
540 538
541 539 # Add alsa-utils package
542 540 if [ "$ENABLE_SOUND" = true ] ; then
543 541 APT_INCLUDES="${APT_INCLUDES},alsa-utils"
544 542 fi
545 543
546 544 # Add rng-tools package
547 545 if [ "$ENABLE_HWRANDOM" = true ] ; then
548 546 APT_INCLUDES="${APT_INCLUDES},rng-tools"
549 547 fi
550 548
551 549 # Add fbturbo video driver
552 550 if [ "$ENABLE_FBTURBO" = true ] ; then
553 551 # Enable xorg package dependencies
554 552 ENABLE_XORG=true
555 553 fi
556 554
557 555 # Add user defined window manager package
558 556 if [ -n "$ENABLE_WM" ] ; then
559 557 APT_INCLUDES="${APT_INCLUDES},${ENABLE_WM}"
560 558
561 559 # Enable xorg package dependencies
562 560 ENABLE_XORG=true
563 561 fi
564 562
565 563 # Add xorg package
566 564 if [ "$ENABLE_XORG" = true ] ; then
567 565 APT_INCLUDES="${APT_INCLUDES},xorg,dbus-x11"
568 566 fi
569 567
570 568 # Replace selected packages with smaller clones
571 569 if [ "$ENABLE_REDUCE" = true ] ; then
572 570 # Add levee package instead of vim-tiny
573 571 if [ "$REDUCE_VIM" = true ] ; then
574 572 APT_INCLUDES="$(echo ${APT_INCLUDES} | sed "s/vim-tiny/levee/")"
575 573 fi
576 574
577 575 # Add dropbear package instead of openssh-server
578 576 if [ "$REDUCE_SSHD" = true ] ; then
579 577 APT_INCLUDES="$(echo "${APT_INCLUDES}" | sed "s/openssh-server/dropbear/")"
580 578 fi
581 579 fi
582 580
583 581 # Configure kernel sources if no KERNELSRC_DIR
584 582 if [ "$BUILD_KERNEL" = true ] && [ -z "$KERNELSRC_DIR" ] ; then
585 583 KERNELSRC_CONFIG=true
586 584 fi
587 585
588 586 # Configure reduced kernel
589 587 if [ "$KERNEL_REDUCE" = true ] ; then
590 588 KERNELSRC_CONFIG=false
591 589 fi
592 590
593 591 # Configure qemu compatible kernel
594 592 if [ "$ENABLE_QEMU" = true ] ; then
595 593 DTB_FILE=vexpress-v2p-ca15_a7.dtb
596 594 UBOOT_CONFIG=vexpress_ca15_tc2_defconfig
597 595 KERNEL_DEFCONFIG="vexpress_defconfig"
598 596 if [ "$KERNEL_MENUCONFIG" = false ] ; then
599 597 KERNEL_OLDDEFCONFIG=true
600 598 fi
601 599 fi
602 600
603 601 # Execute bootstrap scripts
604 602 for SCRIPT in bootstrap.d/*.sh; do
605 603 head -n 3 "$SCRIPT"
606 604 . "$SCRIPT"
607 605 done
608 606
609 607 ## Execute custom bootstrap scripts
610 608 if [ -d "custom.d" ] ; then
611 609 for SCRIPT in custom.d/*.sh; do
612 610 . "$SCRIPT"
613 611 done
614 612 fi
615 613
616 614 # Execute custom scripts inside the chroot
617 615 if [ -n "$CHROOT_SCRIPTS" ] && [ -d "$CHROOT_SCRIPTS" ] ; then
618 616 cp -r "${CHROOT_SCRIPTS}" "${R}/chroot_scripts"
619 617 chroot_exec /bin/bash -x <<'EOF'
620 618 for SCRIPT in /chroot_scripts/* ; do
621 619 if [ -f $SCRIPT -a -x $SCRIPT ] ; then
622 620 $SCRIPT
623 621 fi
624 622 done
625 623 EOF
626 624 rm -rf "${R}/chroot_scripts"
627 625 fi
628 626
629 627 # Remove c/c++ build environment from the chroot
630 628 chroot_remove_cc
631 629
632 630 # Generate required machine-id
633 631 MACHINE_ID=$(dbus-uuidgen)
634 632 echo -n "${MACHINE_ID}" > "${R}/var/lib/dbus/machine-id"
635 633 echo -n "${MACHINE_ID}" > "${ETC_DIR}/machine-id"
636 634
637 635 # APT Cleanup
638 636 chroot_exec apt-get -y clean
639 637 chroot_exec apt-get -y autoclean
640 638 chroot_exec apt-get -y autoremove
641 639
642 640 # Unmount mounted filesystems
643 641 umount -l "${R}/proc"
644 642 umount -l "${R}/sys"
645 643
646 644 # Clean up directories
647 645 rm -rf "${R}/run/*"
648 646 rm -rf "${R}/tmp/*"
649 647
650 648 # Clean up files
651 649 rm -f "${ETC_DIR}/ssh/ssh_host_*"
652 650 rm -f "${ETC_DIR}/dropbear/dropbear_*"
653 651 rm -f "${ETC_DIR}/apt/sources.list.save"
654 652 rm -f "${ETC_DIR}/resolvconf/resolv.conf.d/original"
655 653 rm -f "${ETC_DIR}/*-"
656 654 rm -f "${ETC_DIR}/apt/apt.conf.d/10proxy"
657 655 rm -f "${ETC_DIR}/resolv.conf"
658 656 rm -f "${R}/root/.bash_history"
659 657 rm -f "${R}/var/lib/urandom/random-seed"
660 658 rm -f "${R}/initrd.img"
661 659 rm -f "${R}/vmlinuz"
662 660 rm -f "${R}${QEMU_BINARY}"
663 661
664 662 if [ "$ENABLE_QEMU" = true ] ; then
665 663 # Setup QEMU directory
666 664 mkdir "${BASEDIR}/qemu"
667 665
668 666 # Copy kernel image to QEMU directory
669 667 install_readonly "${BOOT_DIR}/${KERNEL_IMAGE}" "${BASEDIR}/qemu/${KERNEL_IMAGE}"
670 668
671 669 # Copy kernel config to QEMU directory
672 670 install_readonly "${R}/boot/config-${KERNEL_VERSION}" "${BASEDIR}/qemu/config-${KERNEL_VERSION}"
673 671
674 672 # Copy kernel dtbs to QEMU directory
675 673 for dtb in "${BOOT_DIR}/"*.dtb ; do
676 674 if [ -f "${dtb}" ] ; then
677 675 install_readonly "${dtb}" "${BASEDIR}/qemu/"
678 676 fi
679 677 done
680 678
681 679 # Copy kernel overlays to QEMU directory
682 680 if [ -d "${BOOT_DIR}/overlays" ] ; then
683 681 # Setup overlays dtbs directory
684 682 mkdir "${BASEDIR}/qemu/overlays"
685 683
686 684 for dtb in "${BOOT_DIR}/overlays/"*.dtb ; do
687 685 if [ -f "${dtb}" ] ; then
688 686 install_readonly "${dtb}" "${BASEDIR}/qemu/overlays/"
689 687 fi
690 688 done
691 689 fi
692 690
693 691 # Copy u-boot files to QEMU directory
694 692 if [ "$ENABLE_UBOOT" = true ] ; then
695 693 if [ -f "${BOOT_DIR}/u-boot.bin" ] ; then
696 694 install_readonly "${BOOT_DIR}/u-boot.bin" "${BASEDIR}/qemu/u-boot.bin"
697 695 fi
698 696 if [ -f "${BOOT_DIR}/uboot.mkimage" ] ; then
699 697 install_readonly "${BOOT_DIR}/uboot.mkimage" "${BASEDIR}/qemu/uboot.mkimage"
700 698 fi
701 699 if [ -f "${BOOT_DIR}/boot.scr" ] ; then
702 700 install_readonly "${BOOT_DIR}/boot.scr" "${BASEDIR}/qemu/boot.scr"
703 701 fi
704 702 fi
705 703
706 704 # Copy initramfs to QEMU directory
707 705 if [ -f "${BOOT_DIR}/initramfs-${KERNEL_VERSION}" ] ; then
708 706 install_readonly "${BOOT_DIR}/initramfs-${KERNEL_VERSION}" "${BASEDIR}/qemu/initramfs-${KERNEL_VERSION}"
709 707 fi
710 708 fi
711 709
712 710 # Calculate size of the chroot directory in KB
713 711 CHROOT_SIZE=$(expr "$(du -s "${R}" | awk '{ print $1 }')")
714 712
715 713 # Calculate the amount of needed 512 Byte sectors
716 714 TABLE_SECTORS=$(expr 1 \* 1024 \* 1024 \/ 512)
717 715 FRMW_SECTORS=$(expr 64 \* 1024 \* 1024 \/ 512)
718 716 ROOT_OFFSET=$(expr "${TABLE_SECTORS}" + "${FRMW_SECTORS}")
719 717
720 718 # The root partition is EXT4
721 719 # This means more space than the actual used space of the chroot is used.
722 720 # As overhead for journaling and reserved blocks 35% are added.
723 721 ROOT_SECTORS=$(expr "$(expr "${CHROOT_SIZE}" + "${CHROOT_SIZE}" \/ 100 \* 35)" \* 1024 \/ 512)
724 722
725 723 # Calculate required image size in 512 Byte sectors
726 724 IMAGE_SECTORS=$(expr "${TABLE_SECTORS}" + "${FRMW_SECTORS}" + "${ROOT_SECTORS}")
727 725
728 726 # Prepare image file
729 727 if [ "$ENABLE_SPLITFS" = true ] ; then
730 728 dd if=/dev/zero of="$IMAGE_NAME-frmw.img" bs=512 count="${TABLE_SECTORS}"
731 729 dd if=/dev/zero of="$IMAGE_NAME-frmw.img" bs=512 count=0 seek="${FRMW_SECTORS}"
732 730 dd if=/dev/zero of="$IMAGE_NAME-root.img" bs=512 count="${TABLE_SECTORS}"
733 731 dd if=/dev/zero of="$IMAGE_NAME-root.img" bs=512 count=0 seek="${ROOT_SECTORS}"
734 732
735 733 # Write firmware/boot partition tables
736 734 sfdisk -q -L -uS -f "$IMAGE_NAME-frmw.img" 2> /dev/null <<EOM
737 735 ${TABLE_SECTORS},${FRMW_SECTORS},c,*
738 736 EOM
739 737
740 738 # Write root partition table
741 739 sfdisk -q -L -uS -f "$IMAGE_NAME-root.img" 2> /dev/null <<EOM
742 740 ${TABLE_SECTORS},${ROOT_SECTORS},83
743 741 EOM
744 742
745 743 # Setup temporary loop devices
746 744 FRMW_LOOP="$(losetup -o 1M --sizelimit 64M -f --show "$IMAGE_NAME"-frmw.img)"
747 745 ROOT_LOOP="$(losetup -o 1M -f --show "$IMAGE_NAME"-root.img)"
748 746 else # ENABLE_SPLITFS=false
749 747 dd if=/dev/zero of="$IMAGE_NAME.img" bs=512 count="${TABLE_SECTORS}"
750 748 dd if=/dev/zero of="$IMAGE_NAME.img" bs=512 count=0 seek="${IMAGE_SECTORS}"
751 749
752 750 # Write partition table
753 751 sfdisk -q -L -uS -f "$IMAGE_NAME.img" 2> /dev/null <<EOM
754 752 ${TABLE_SECTORS},${FRMW_SECTORS},c,*
755 753 ${ROOT_OFFSET},${ROOT_SECTORS},83
756 754 EOM
757 755
758 756 # Setup temporary loop devices
759 757 FRMW_LOOP="$(losetup -o 1M --sizelimit 64M -f --show "$IMAGE_NAME".img)"
760 758 ROOT_LOOP="$(losetup -o 65M -f --show "$IMAGE_NAME".img)"
761 759 fi
762 760
763 761 if [ "$ENABLE_CRYPTFS" = true ] ; then
764 762 # Create dummy ext4 fs
765 763 mkfs.ext4 "$ROOT_LOOP"
766 764
767 765 # Setup password keyfile
768 766 touch .password
769 767 chmod 600 .password
770 768 echo -n ${CRYPTFS_PASSWORD} > .password
771 769
772 770 # Initialize encrypted partition
773 771 echo "YES" | cryptsetup luksFormat "${ROOT_LOOP}" -c "${CRYPTFS_CIPHER}" -s "${CRYPTFS_XTSKEYSIZE}" .password
774 772
775 773 # Open encrypted partition and setup mapping
776 774 cryptsetup luksOpen "${ROOT_LOOP}" -d .password "${CRYPTFS_MAPPING}"
777 775
778 776 # Secure delete password keyfile
779 777 shred -zu .password
780 778
781 779 # Update temporary loop device
782 780 ROOT_LOOP="/dev/mapper/${CRYPTFS_MAPPING}"
783 781
784 782 # Wipe encrypted partition (encryption cipher is used for randomness)
785 783 dd if=/dev/zero of="${ROOT_LOOP}" bs=512 count="$(blockdev --getsz "${ROOT_LOOP}")"
786 784 fi
787 785
788 786 # Build filesystems
789 787 mkfs.vfat "$FRMW_LOOP"
790 788 mkfs.ext4 "$ROOT_LOOP"
791 789
792 790 # Mount the temporary loop devices
793 791 mkdir -p "$BUILDDIR/mount"
794 792 mount "$ROOT_LOOP" "$BUILDDIR/mount"
795 793
796 794 mkdir -p "$BUILDDIR/mount/boot/firmware"
797 795 mount "$FRMW_LOOP" "$BUILDDIR/mount/boot/firmware"
798 796
799 797 # Copy all files from the chroot to the loop device mount point directory
800 798 rsync -a "${R}/" "$BUILDDIR/mount/"
801 799
802 800 # Unmount all temporary loop devices and mount points
803 801 cleanup
804 802
805 803 # Create block map file(s) of image(s)
806 804 if [ "$ENABLE_SPLITFS" = true ] ; then
807 805 # Create block map files for "bmaptool"
808 806 bmaptool create -o "$IMAGE_NAME-frmw.bmap" "$IMAGE_NAME-frmw.img"
809 807 bmaptool create -o "$IMAGE_NAME-root.bmap" "$IMAGE_NAME-root.img"
810 808
811 809 # Image was successfully created
812 810 echo "$IMAGE_NAME-frmw.img ($(expr \( "${TABLE_SECTORS}" + "${FRMW_SECTORS}" \) \* 512 \/ 1024 \/ 1024)M)" ": successfully created"
813 811 echo "$IMAGE_NAME-root.img ($(expr \( "${TABLE_SECTORS}" + "${ROOT_SECTORS}" \) \* 512 \/ 1024 \/ 1024)M)" ": successfully created"
814 812 else
815 813 # Create block map file for "bmaptool"
816 814 bmaptool create -o "$IMAGE_NAME.bmap" "$IMAGE_NAME.img"
817 815
818 816 # Image was successfully created
819 817 echo "$IMAGE_NAME.img ($(expr \( "${TABLE_SECTORS}" + "${FRMW_SECTORS}" + "${ROOT_SECTORS}" \) \* 512 \/ 1024 \/ 1024)M)" ": successfully created"
820 818
821 819 # Create qemu qcow2 image
822 820 if [ "$ENABLE_QEMU" = true ] ; then
823 821 QEMU_IMAGE=${QEMU_IMAGE:=${BASEDIR}/qemu/${DATE}-${KERNEL_ARCH}-CURRENT-rpi${RPI_MODEL}-${RELEASE}-${RELEASE_ARCH}}
824 822 QEMU_SIZE=16G
825 823
826 824 qemu-img convert -f raw -O qcow2 "$IMAGE_NAME".img "$QEMU_IMAGE".qcow2
827 825 qemu-img resize "$QEMU_IMAGE".qcow2 $QEMU_SIZE
828 826
829 827 echo "$QEMU_IMAGE.qcow2 ($QEMU_SIZE)" ": successfully created"
830 828 fi
831 829 fi
General Comments 0
Vous devez vous connecter pour laisser un commentaire. Se connecter maintenant