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