2021-09-16 06:42:05 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
#
|
|
|
|
|
# Kali Linux ARM build-script for Utilite Pro
|
|
|
|
|
# https://gitlab.com/kalilinux/build-scripts/kali-arm
|
|
|
|
|
#
|
|
|
|
|
# This is a community script - you will need to generate your own image to use
|
|
|
|
|
# More information: https://www.kali.org/docs/arm/utilite-pro/
|
|
|
|
|
#
|
2013-10-29 16:45:51 +02:00
|
|
|
|
2021-09-16 06:42:05 +01:00
|
|
|
# Stop on error
|
|
|
|
|
set -e
|
2013-12-02 23:45:09 -04:00
|
|
|
|
2020-07-21 22:32:15 -05:00
|
|
|
# Uncomment to activate debug
|
|
|
|
|
# debug=true
|
2018-06-22 18:44:57 -05:00
|
|
|
|
2020-07-21 22:32:15 -05:00
|
|
|
if [ "$debug" = true ]; then
|
|
|
|
|
exec > >(tee -a -i "${0%.*}.log") 2>&1
|
|
|
|
|
set -x
|
2013-10-29 16:45:51 +02:00
|
|
|
fi
|
|
|
|
|
|
2020-07-21 22:32:15 -05:00
|
|
|
# Architecture
|
|
|
|
|
architecture=${architecture:-"armhf"}
|
2021-09-16 06:58:38 +01:00
|
|
|
# Generate a random machine name to be used
|
2020-07-21 22:32:15 -05:00
|
|
|
machine=$(tr -cd 'A-Za-z0-9' < /dev/urandom | head -c16 ; echo)
|
2018-06-30 17:03:58 -05:00
|
|
|
# Custom hostname variable
|
2018-07-06 18:03:37 -05:00
|
|
|
hostname=${2:-kali}
|
2021-09-16 06:58:38 +01:00
|
|
|
# Custom image file name variable - MUST NOT include .img at the end
|
2021-09-19 10:11:39 +01:00
|
|
|
image_name=${3:-kali-linux-$1-utilite-pro}
|
2020-07-21 22:32:15 -05:00
|
|
|
# Suite to use, valid options are:
|
2018-07-06 18:03:37 -05:00
|
|
|
# kali-rolling, kali-dev, kali-bleeding-edge, kali-dev-only, kali-experimental, kali-last-snapshot
|
2020-07-21 22:32:15 -05:00
|
|
|
suite=${suite:-"kali-rolling"}
|
|
|
|
|
# Free space rootfs in MiB
|
|
|
|
|
free_space="300"
|
|
|
|
|
# /boot partition in MiB
|
|
|
|
|
bootsize="128"
|
2020-07-26 14:38:21 -05:00
|
|
|
# Select compression, xz or none
|
|
|
|
|
compress="xz"
|
2021-09-16 16:02:54 +01:00
|
|
|
# Choose filesystem format to format (ext3 or ext4)
|
2020-07-26 14:38:21 -05:00
|
|
|
fstype="ext3"
|
2021-09-16 06:58:38 +01:00
|
|
|
# If you have your own preferred mirrors, set them here
|
2020-08-10 16:55:26 +02:00
|
|
|
mirror=${mirror:-"http://http.kali.org/kali"}
|
2021-09-16 16:02:54 +01:00
|
|
|
# GitLab URL Kali repository
|
2020-07-21 22:32:15 -05:00
|
|
|
kaligit="https://gitlab.com/kalilinux"
|
2021-09-16 16:02:54 +01:00
|
|
|
# GitHub raw URL
|
2020-07-21 22:32:15 -05:00
|
|
|
githubraw="https://raw.githubusercontent.com"
|
2018-06-30 17:03:58 -05:00
|
|
|
|
2021-09-16 06:58:38 +01:00
|
|
|
# Check EUID=0 you can run any binary as root
|
2020-07-21 22:32:15 -05:00
|
|
|
if [[ $EUID -ne 0 ]]; then
|
2021-09-16 16:02:54 +01:00
|
|
|
echo "This script must be run as root or have super user permissions" >&2
|
|
|
|
|
echo "Use: sudo $0 ${1:-2.0} ${2:-kali}" >&2
|
2020-07-21 22:32:15 -05:00
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Pass version number
|
|
|
|
|
if [[ $# -eq 0 ]] ; then
|
2021-09-16 16:02:54 +01:00
|
|
|
echo "Please pass version number, e.g. $0 2.0, and (if you want) a hostname, default is kali" >&2
|
2020-07-21 22:32:15 -05:00
|
|
|
exit 0
|
|
|
|
|
fi
|
|
|
|
|
|
2021-09-16 06:58:38 +01:00
|
|
|
# Check exist bsp directory
|
2020-07-21 22:32:15 -05:00
|
|
|
if [ ! -e "bsp" ]; then
|
2021-09-16 16:02:54 +01:00
|
|
|
echo "Error: missing bsp directory structure" >&2
|
|
|
|
|
echo "Please clone the full repository ${kaligit}/build-scripts/kali-arm" >&2
|
2020-07-21 22:32:15 -05:00
|
|
|
exit 255
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Current directory
|
|
|
|
|
current_dir="$(pwd)"
|
|
|
|
|
# Base directory
|
2021-09-19 10:11:39 +01:00
|
|
|
base_dir=${current_dir}/utilite-"$1"
|
2020-07-21 22:32:15 -05:00
|
|
|
# Working directory
|
2021-09-19 10:11:39 +01:00
|
|
|
work_dir="${base_dir}/kali-${architecture}"
|
2020-07-21 22:32:15 -05:00
|
|
|
|
|
|
|
|
# Check directory build
|
2021-09-19 10:11:39 +01:00
|
|
|
if [ -e "${base_dir}" ]; then
|
|
|
|
|
echo "${base_dir} directory exists, will not continue" >&2
|
2020-07-21 22:32:15 -05:00
|
|
|
exit 1
|
|
|
|
|
elif [[ ${current_dir} =~ [[:space:]] ]]; then
|
2021-09-16 16:02:54 +01:00
|
|
|
echo "The directory "\"${current_dir}"\" contains whitespace. Not supported." >&2
|
2020-07-21 22:32:15 -05:00
|
|
|
exit 1
|
|
|
|
|
else
|
2021-09-19 10:11:39 +01:00
|
|
|
echo "The base_dir thinks it is: ${base_dir}"
|
|
|
|
|
mkdir -p ${base_dir}
|
2014-05-15 11:06:22 -05:00
|
|
|
fi
|
2013-12-08 13:12:45 -04:00
|
|
|
|
2020-07-21 22:32:15 -05:00
|
|
|
components="main,contrib,non-free"
|
2020-04-28 19:35:50 -05:00
|
|
|
arm="kali-linux-arm ntpdate"
|
|
|
|
|
base="apt-transport-https apt-utils bash-completion console-setup dialog e2fsprogs ifupdown initramfs-tools inxi iw man-db mlocate netcat-traditional net-tools parted pciutils psmisc rfkill screen tmux unrar usbutils vim wget whiptail zerofree"
|
|
|
|
|
desktop="kali-desktop-xfce kali-root-login xserver-xorg-video-fbdev xfonts-terminus xinput"
|
2020-06-17 15:09:54 -05:00
|
|
|
tools="kali-linux-default"
|
2020-04-28 19:35:50 -05:00
|
|
|
services="apache2 atftpd"
|
2020-04-29 19:16:19 -05:00
|
|
|
extras="alsa-utils bc bison bluez bluez-firmware kali-linux-core libnss-systemd libssl-dev triggerhappy"
|
2018-12-18 23:04:10 -06:00
|
|
|
|
2013-12-08 13:12:45 -04:00
|
|
|
|
2019-06-17 02:00:54 -05:00
|
|
|
packages="${arm} ${base} ${services}"
|
2013-10-29 16:45:51 +02:00
|
|
|
|
2021-09-16 06:58:38 +01:00
|
|
|
# Automatic configuration to use an http proxy, such as apt-cacher-ng
|
|
|
|
|
# You can turn off automatic settings by uncommenting apt_cacher=off
|
2020-07-21 22:32:15 -05:00
|
|
|
# apt_cacher=off
|
2021-09-16 06:58:38 +01:00
|
|
|
# By default the proxy settings are local, but you can define an external proxy
|
2020-07-21 22:32:15 -05:00
|
|
|
# proxy_url="http://external.intranet.local"
|
|
|
|
|
apt_cacher=${apt_cacher:-"$(lsof -i :3142|cut -d ' ' -f3 | uniq | sed '/^\s*$/d')"}
|
|
|
|
|
if [ -n "$proxy_url" ]; then
|
|
|
|
|
export http_proxy=$proxy_url
|
|
|
|
|
elif [ "$apt_cacher" = "apt-cacher-ng" ] ; then
|
|
|
|
|
if [ -z "$proxy_url" ]; then
|
|
|
|
|
proxy_url=${proxy_url:-"http://127.0.0.1:3142/"}
|
|
|
|
|
export http_proxy=$proxy_url
|
|
|
|
|
fi
|
|
|
|
|
fi
|
2013-10-29 16:45:51 +02:00
|
|
|
|
2020-08-02 00:14:21 -05:00
|
|
|
# Detect architecture
|
|
|
|
|
if [[ "${architecture}" == "arm64" ]]; then
|
|
|
|
|
qemu_bin="/usr/bin/qemu-aarch64-static"
|
|
|
|
|
lib_arch="aarch64-linux-gnu"
|
|
|
|
|
elif [[ "${architecture}" == "armhf" ]]; then
|
|
|
|
|
qemu_bin="/usr/bin/qemu-arm-static"
|
|
|
|
|
lib_arch="arm-linux-gnueabihf"
|
|
|
|
|
elif [[ "${architecture}" == "armel" ]]; then
|
|
|
|
|
qemu_bin="/usr/bin/qemu-arm-static"
|
|
|
|
|
lib_arch="arm-linux-gnueabi"
|
|
|
|
|
fi
|
|
|
|
|
|
2021-09-16 06:58:38 +01:00
|
|
|
# create the rootfs - not much to modify here, except maybe throw in some more packages if you want
|
2020-08-02 00:14:21 -05:00
|
|
|
eatmydata debootstrap --foreign --keyring=/usr/share/keyrings/kali-archive-keyring.gpg --include=kali-archive-keyring,eatmydata \
|
2020-07-21 22:32:15 -05:00
|
|
|
--components=${components} --arch ${architecture} ${suite} ${work_dir} http://http.kali.org/kali
|
2013-10-29 16:45:51 +02:00
|
|
|
|
2021-09-16 06:58:38 +01:00
|
|
|
# systemd-nspawn environment
|
2020-07-21 22:32:15 -05:00
|
|
|
systemd-nspawn_exec(){
|
|
|
|
|
LANG=C systemd-nspawn -q --bind-ro ${qemu_bin} -M ${machine} -D ${work_dir} "$@"
|
|
|
|
|
}
|
2019-04-29 23:30:19 -05:00
|
|
|
|
2021-09-16 06:58:38 +01:00
|
|
|
# We need to manually extract eatmydata to use it for the second stage
|
2020-08-02 00:14:21 -05:00
|
|
|
for archive in ${work_dir}/var/cache/apt/archives/*eatmydata*.deb; do
|
|
|
|
|
dpkg-deb --fsys-tarfile "$archive" > ${work_dir}/eatmydata
|
|
|
|
|
tar -xkf ${work_dir}/eatmydata -C ${work_dir}
|
|
|
|
|
rm -f ${work_dir}/eatmydata
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
# Prepare dpkg to use eatmydata
|
|
|
|
|
systemd-nspawn_exec dpkg-divert --divert /usr/bin/dpkg-eatmydata --rename --add /usr/bin/dpkg
|
|
|
|
|
|
|
|
|
|
cat > ${work_dir}/usr/bin/dpkg << EOF
|
|
|
|
|
#!/bin/sh
|
|
|
|
|
if [ -e /usr/lib/${lib_arch}/libeatmydata.so ]; then
|
|
|
|
|
[ -n "\${LD_PRELOAD}" ] && LD_PRELOAD="\$LD_PRELOAD:"
|
|
|
|
|
LD_PRELOAD="\$LD_PRELOAD\$so"
|
|
|
|
|
fi
|
|
|
|
|
for so in /usr/lib/${lib_arch}/libeatmydata.so; do
|
|
|
|
|
[ -n "\$LD_PRELOAD" ] && LD_PRELOAD="\$LD_PRELOAD:"
|
|
|
|
|
LD_PRELOAD="\$LD_PRELOAD\$so"
|
|
|
|
|
done
|
|
|
|
|
export LD_PRELOAD
|
|
|
|
|
exec "\$0-eatmydata" --force-unsafe-io "\$@"
|
|
|
|
|
EOF
|
2021-09-17 10:42:05 +01:00
|
|
|
chmod 0755 ${work_dir}/usr/bin/dpkg
|
2020-08-02 00:14:21 -05:00
|
|
|
|
2020-07-21 22:32:15 -05:00
|
|
|
# debootstrap second stage
|
2020-08-02 00:14:21 -05:00
|
|
|
systemd-nspawn_exec eatmydata /debootstrap/debootstrap --second-stage
|
2013-10-29 16:45:51 +02:00
|
|
|
|
2020-07-21 22:32:15 -05:00
|
|
|
cat << EOF > ${work_dir}/etc/apt/sources.list
|
|
|
|
|
deb ${mirror} ${suite} ${components//,/ }
|
|
|
|
|
#deb-src ${mirror} ${suite} ${components//,/ }
|
2013-10-29 16:45:51 +02:00
|
|
|
EOF
|
|
|
|
|
|
|
|
|
|
# Set hostname
|
2020-07-21 22:32:15 -05:00
|
|
|
echo "${hostname}" > ${work_dir}/etc/hostname
|
2013-10-29 16:45:51 +02:00
|
|
|
|
2020-07-21 22:32:15 -05:00
|
|
|
# So X doesn't complain, we add kali to hosts
|
|
|
|
|
cat << EOF > ${work_dir}/etc/hosts
|
2018-07-06 18:03:37 -05:00
|
|
|
127.0.0.1 ${hostname} localhost
|
2013-10-29 16:45:51 +02:00
|
|
|
::1 localhost ip6-localhost ip6-loopback
|
|
|
|
|
fe00::0 ip6-localnet
|
|
|
|
|
ff00::0 ip6-mcastprefix
|
|
|
|
|
ff02::1 ip6-allnodes
|
|
|
|
|
ff02::2 ip6-allrouters
|
|
|
|
|
EOF
|
|
|
|
|
|
2020-07-21 22:32:15 -05:00
|
|
|
# Disable IPv6
|
|
|
|
|
cat << EOF > ${work_dir}/etc/modprobe.d/ipv6.conf
|
|
|
|
|
# Don't load ipv6 by default
|
|
|
|
|
alias net-pf-10 off
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
|
|
cat << EOF > ${work_dir}/etc/network/interfaces
|
2013-10-29 16:45:51 +02:00
|
|
|
auto lo
|
|
|
|
|
iface lo inet loopback
|
|
|
|
|
|
|
|
|
|
auto eth0
|
2020-05-01 15:51:07 -05:00
|
|
|
allow-hotplug eth0
|
2013-10-29 16:45:51 +02:00
|
|
|
iface eth0 inet dhcp
|
|
|
|
|
EOF
|
|
|
|
|
|
2020-07-21 22:32:15 -05:00
|
|
|
# DNS server
|
2021-09-18 05:48:20 +01:00
|
|
|
echo "nameserver ${nameserver}" > "${work_dir}"/etc/resolv.conf
|
2013-10-29 16:45:51 +02:00
|
|
|
|
2021-09-16 06:58:38 +01:00
|
|
|
# Copy directory bsp into build dir
|
2020-07-21 22:32:15 -05:00
|
|
|
cp -rp bsp ${work_dir}
|
2013-10-29 16:45:51 +02:00
|
|
|
|
2020-07-21 22:32:15 -05:00
|
|
|
export MALLOC_CHECK_=0 # workaround for LP: #520465
|
2013-10-29 16:45:51 +02:00
|
|
|
|
2021-09-16 06:58:38 +01:00
|
|
|
# Enable the use of http proxy in third-stage in case it is enabled
|
2020-07-21 22:32:15 -05:00
|
|
|
if [ -n "$proxy_url" ]; then
|
|
|
|
|
echo "Acquire::http { Proxy \"$proxy_url\" };" > ${work_dir}/etc/apt/apt.conf.d/66proxy
|
|
|
|
|
fi
|
2018-07-11 23:28:59 -05:00
|
|
|
|
2020-07-21 22:32:15 -05:00
|
|
|
# Third stage
|
|
|
|
|
cat << EOF > ${work_dir}/third-stage
|
|
|
|
|
#!/bin/bash -e
|
|
|
|
|
export DEBIAN_FRONTEND=noninteractive
|
2013-10-29 16:45:51 +02:00
|
|
|
|
2020-08-02 00:14:21 -05:00
|
|
|
eatmydata apt-get update
|
2013-10-29 16:45:51 +02:00
|
|
|
|
2020-08-02 00:14:21 -05:00
|
|
|
eatmydata apt-get -y install binutils ca-certificates console-common git initramfs-tools less locales nano u-boot-tools
|
2020-05-07 15:17:10 -05:00
|
|
|
|
2021-09-16 06:58:38 +01:00
|
|
|
# Create kali user with kali password... but first, we need to manually make some groups because they don't yet exist..
|
|
|
|
|
# This mirrors what we have on a pre-installed VM, until the script works properly to allow end users to set up their own... user
|
2020-05-07 15:17:10 -05:00
|
|
|
# However we leave off floppy, because who a) still uses them, and b) attaches them to an SBC!?
|
2021-09-16 06:58:38 +01:00
|
|
|
# And since a lot of these have serial devices of some sort, dialout is added as well
|
2020-05-07 15:17:10 -05:00
|
|
|
# scanner, lpadmin and bluetooth have to be added manually because they don't
|
2021-09-16 06:58:38 +01:00
|
|
|
# yet exist in /etc/group at this point
|
2020-05-07 15:17:10 -05:00
|
|
|
groupadd -r -g 118 bluetooth
|
|
|
|
|
groupadd -r -g 113 lpadmin
|
|
|
|
|
groupadd -r -g 122 scanner
|
|
|
|
|
groupadd -g 1000 kali
|
|
|
|
|
|
|
|
|
|
useradd -m -u 1000 -g 1000 -G sudo,audio,bluetooth,cdrom,dialout,dip,lpadmin,netdev,plugdev,scanner,video,kali -s /bin/bash kali
|
|
|
|
|
echo "kali:kali" | chpasswd
|
|
|
|
|
|
2020-07-27 18:47:11 -05:00
|
|
|
aptops="--allow-change-held-packages -o dpkg::options::=--force-confnew -o Acquire::Retries=3"
|
2020-07-21 22:32:15 -05:00
|
|
|
|
2018-12-18 23:04:10 -06:00
|
|
|
# This looks weird, but we do it twice because every so often, there's a failure to download from the mirror
|
2021-09-16 06:58:38 +01:00
|
|
|
# So to workaround it, we attempt to install them twice
|
2020-08-02 00:14:21 -05:00
|
|
|
eatmydata apt-get install -y \$aptops ${packages} || eatmydata apt-get --yes --fix-broken install
|
|
|
|
|
eatmydata apt-get install -y \$aptops ${packages} || eatmydata apt-get --yes --fix-broken install
|
|
|
|
|
eatmydata apt-get install -y \$aptops ${desktop} ${extras} ${tools} || eatmydata apt-get --yes --fix-broken install
|
|
|
|
|
eatmydata apt-get install -y \$aptops ${desktop} ${extras} ${tools} || eatmydata apt-get --yes --fix-broken install
|
|
|
|
|
eatmydata apt-get install -y \$aptops --autoremove systemd-timesyncd || eatmydata apt-get --yes --fix-broken install
|
|
|
|
|
eatmydata apt-get dist-upgrade -y \$aptops
|
2020-07-21 22:32:15 -05:00
|
|
|
|
2020-08-02 00:14:21 -05:00
|
|
|
eatmydata apt-get -y --allow-change-held-packages --purge autoremove
|
2020-07-21 22:32:15 -05:00
|
|
|
|
|
|
|
|
# Linux console/Keyboard configuration
|
|
|
|
|
echo 'console-common console-data/keymap/policy select Select keymap from full list' | debconf-set-selections
|
|
|
|
|
echo 'console-common console-data/keymap/full select en-latin1-nodeadkeys' | debconf-set-selections
|
|
|
|
|
|
|
|
|
|
# Copy all services
|
|
|
|
|
install -m644 /bsp/services/all/*.service /etc/systemd/system/
|
2018-07-11 23:28:59 -05:00
|
|
|
|
2019-02-12 17:56:53 -06:00
|
|
|
# Regenerated the shared-mime-info database on the first boot
|
2021-09-16 06:58:38 +01:00
|
|
|
# since it fails to do so properly in a chroot
|
2019-02-12 17:56:53 -06:00
|
|
|
systemctl enable smi-hack
|
|
|
|
|
|
2018-07-11 23:28:59 -05:00
|
|
|
# Generate SSH host keys on first run
|
|
|
|
|
systemctl enable regenerate_ssh_host_keys
|
2020-07-21 22:32:15 -05:00
|
|
|
# Enable sshd
|
2019-02-12 17:56:53 -06:00
|
|
|
systemctl enable ssh
|
2018-07-11 23:28:59 -05:00
|
|
|
|
2020-08-08 20:38:29 -05:00
|
|
|
# Allow users to use NM over ssh
|
|
|
|
|
install -m644 /bsp/polkit/10-NetworkManager.pkla /var/lib/polkit-1/localauthority/50-local.d
|
|
|
|
|
|
2020-07-21 22:32:15 -05:00
|
|
|
cd /root
|
|
|
|
|
apt download -o APT::Sandbox::User=root ca-certificates 2>/dev/null
|
|
|
|
|
|
2018-07-11 23:28:59 -05:00
|
|
|
# Copy over the default bashrc
|
2020-07-21 22:32:15 -05:00
|
|
|
cp /etc/skel/.bashrc /root/.bashrc
|
2018-07-11 23:28:59 -05:00
|
|
|
|
2020-07-21 22:32:15 -05:00
|
|
|
# Set a REGDOMAIN. This needs to be done or wireless doesn't work correctly on the RPi 3B+
|
|
|
|
|
sed -i -e 's/REGDOM.*/REGDOMAIN=00/g' /etc/default/crda
|
|
|
|
|
|
|
|
|
|
# Enable serial console access
|
|
|
|
|
echo "T1:23:respawn:/sbin/agetty -L ttymxc3 115200 vt100" >> /etc/inittab
|
|
|
|
|
|
|
|
|
|
# Try and make the console a bit nicer
|
2021-09-16 06:58:38 +01:00
|
|
|
# Set the terminus font for a bit nicer display
|
2020-07-21 22:32:15 -05:00
|
|
|
sed -i -e 's/FONTFACE=.*/FONTFACE="Terminus"/' /etc/default/console-setup
|
|
|
|
|
sed -i -e 's/FONTSIZE=.*/FONTSIZE="6x12"/' /etc/default/console-setup
|
2020-05-07 16:57:01 -05:00
|
|
|
|
2018-07-11 23:28:59 -05:00
|
|
|
# Fix startup time from 5 minutes to 15 secs on raise interface wlan0
|
2019-02-12 17:56:53 -06:00
|
|
|
sed -i 's/^TimeoutStartSec=5min/TimeoutStartSec=15/g' "/usr/lib/systemd/system/networking.service"
|
2020-08-02 00:14:21 -05:00
|
|
|
|
|
|
|
|
rm -f /usr/bin/dpkg
|
2013-10-29 16:45:51 +02:00
|
|
|
EOF
|
|
|
|
|
|
2020-07-21 22:32:15 -05:00
|
|
|
# Run third stage
|
2021-09-17 10:42:05 +01:00
|
|
|
chmod 0755 ${work_dir}/third-stage
|
2020-07-21 22:32:15 -05:00
|
|
|
systemd-nspawn_exec /third-stage
|
2013-10-29 16:45:51 +02:00
|
|
|
|
2020-08-02 00:14:21 -05:00
|
|
|
# Clean up eatmydata
|
|
|
|
|
systemd-nspawn_exec dpkg-divert --remove --rename /usr/bin/dpkg
|
|
|
|
|
|
2020-07-21 22:32:15 -05:00
|
|
|
# Clean system
|
2020-08-10 16:55:26 +02:00
|
|
|
systemd-nspawn_exec << 'EOF'
|
2013-10-29 16:45:51 +02:00
|
|
|
rm -f /0
|
2020-07-21 22:32:15 -05:00
|
|
|
rm -rf /bsp
|
|
|
|
|
fc-cache -frs
|
|
|
|
|
rm -rf /tmp/*
|
|
|
|
|
rm -rf /etc/*-
|
|
|
|
|
rm -rf /hs_err*
|
|
|
|
|
rm -rf /userland
|
|
|
|
|
rm -rf /opt/vc/src
|
|
|
|
|
rm -f /etc/ssh/ssh_host_*
|
2020-07-26 17:40:50 -05:00
|
|
|
rm -rf /var/lib/dpkg/*-old
|
2020-07-21 22:32:15 -05:00
|
|
|
rm -rf /var/lib/apt/lists/*
|
|
|
|
|
rm -rf /var/cache/apt/*.bin
|
|
|
|
|
rm -rf /var/cache/apt/archives/*
|
|
|
|
|
rm -rf /var/cache/debconf/*.data-old
|
2020-08-10 16:55:26 +02:00
|
|
|
for logs in $(find /var/log -type f); do > $logs; done
|
2020-07-21 22:32:15 -05:00
|
|
|
history -c
|
2013-10-29 16:45:51 +02:00
|
|
|
EOF
|
|
|
|
|
|
2021-09-16 06:58:38 +01:00
|
|
|
# Disable the use of http proxy in case it is enabled
|
2020-07-21 22:32:15 -05:00
|
|
|
if [ -n "$proxy_url" ]; then
|
|
|
|
|
unset http_proxy
|
|
|
|
|
rm -rf ${work_dir}/etc/apt/apt.conf.d/66proxy
|
|
|
|
|
fi
|
2013-10-29 16:45:51 +02:00
|
|
|
|
2020-08-10 16:55:26 +02:00
|
|
|
# Mirror & suite replacement
|
|
|
|
|
if [[ ! -z "${4}" || ! -z "${5}" ]]; then
|
|
|
|
|
mirror=${4}
|
|
|
|
|
suite=${5}
|
|
|
|
|
fi
|
|
|
|
|
|
2020-07-21 22:32:15 -05:00
|
|
|
cat << EOF >> ${work_dir}/etc/udev/links.conf
|
2013-10-29 16:45:51 +02:00
|
|
|
M ttymxc3 c 5 1
|
|
|
|
|
EOF
|
|
|
|
|
|
2020-07-21 22:32:15 -05:00
|
|
|
cat << EOF >> ${work_dir}/etc/securetty
|
2013-10-29 16:45:51 +02:00
|
|
|
ttymxc3
|
|
|
|
|
EOF
|
|
|
|
|
|
2020-07-26 14:38:21 -05:00
|
|
|
# Mirror replacement
|
2020-07-27 18:47:11 -05:00
|
|
|
if [[ ! -z "${@:5}" || "$suite" != "kali-rolling" ]]; then
|
2020-07-26 14:38:21 -05:00
|
|
|
mirror=${@:5}
|
2020-07-27 19:56:25 -05:00
|
|
|
[ ! -z "${@:5}" ] || mirror="http://http.kali.org/kali"
|
2020-07-27 18:47:11 -05:00
|
|
|
[ "$suite" != "kali-rolling" ] && suite=kali-rolling
|
2020-07-26 14:38:21 -05:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Define sources.list
|
2020-07-21 22:32:15 -05:00
|
|
|
cat << EOF > ${work_dir}/etc/apt/sources.list
|
2020-07-26 14:38:21 -05:00
|
|
|
deb ${mirror} ${suite} ${components//,/ }
|
|
|
|
|
#deb-src ${mirror} ${suite} ${components//,/ }
|
2014-09-17 09:19:15 -05:00
|
|
|
EOF
|
|
|
|
|
|
2020-07-21 22:32:15 -05:00
|
|
|
# systemd doesn't seem to be generating the fstab properly for some people, so
|
|
|
|
|
# let's create one. We leave the root partition off and add it below after
|
2021-09-16 06:58:38 +01:00
|
|
|
# we create the partition in the image so we can use the UUID
|
2020-07-21 22:32:15 -05:00
|
|
|
cat << EOF > ${work_dir}/etc/fstab
|
|
|
|
|
# <file system> <mount point> <type> <options> <dump> <pass>
|
|
|
|
|
proc /proc proc defaults 0 0
|
|
|
|
|
/dev/mmcblk0p1 /boot vfat defaults 0 2
|
|
|
|
|
EOF
|
2018-07-11 23:28:59 -05:00
|
|
|
|
2021-09-19 10:11:39 +01:00
|
|
|
cd ${base_dir}
|
2021-09-16 06:58:38 +01:00
|
|
|
# Clone a cross compiler to use instead of the Kali one due to kernel age
|
2019-04-30 05:27:16 -05:00
|
|
|
git clone --depth 1 https://gitlab.com/kalilinux/packages/gcc-arm-linux-gnueabihf-4-7.git gcc-arm-linux-gnueabihf-4.7
|
2018-07-11 23:28:59 -05:00
|
|
|
|
2014-03-26 02:36:32 -04:00
|
|
|
# Kernel section. If you want to use a custom kernel, or configuration, replace
|
2021-09-16 06:58:38 +01:00
|
|
|
# them in this section
|
2020-07-21 22:32:15 -05:00
|
|
|
git clone --branch utilite/devel --depth 1 https://github.com/utilite-computer/linux-kernel ${work_dir}/usr/src/kernel
|
|
|
|
|
cd ${work_dir}/usr/src/kernel
|
|
|
|
|
git rev-parse HEAD > ${work_dir}/usr/src/kernel-at-commit
|
|
|
|
|
patch -p1 --no-backup-if-mismatch < ${current_dir}/patches/mac80211.patch
|
2021-09-16 06:58:38 +01:00
|
|
|
# Needed for issues with hdmi being inited already in u-boot
|
2020-07-21 22:32:15 -05:00
|
|
|
patch -p1 --no-backup-if-mismatch < ${current_dir}/patches/f922b0d.patch
|
|
|
|
|
patch -p1 --no-backup-if-mismatch < ${current_dir}/patches/0001-wireless-carl9170-Enable-sniffer-mode-promisc-flag-t.patch
|
2015-08-10 22:17:08 -05:00
|
|
|
# This patch is necessary for older revisions of the Utilite so leave the patch
|
|
|
|
|
# and comment in the repo to know why this is here. Should be fixed by a u-boot
|
2021-09-16 06:58:38 +01:00
|
|
|
# upgrade but CompuLab haven't released it yet, so leave it here for now
|
2020-07-21 22:32:15 -05:00
|
|
|
#patch -p1 --no-backup-if-mismatch < ${current_dir}/patches/31727b0.patch
|
|
|
|
|
cp ${current_dir}/kernel-configs/utilite-3.10.config .config
|
|
|
|
|
cp ${current_dir}/kernel-configs/utilite-3.10.config ${work_dir}/usr/src/utilite-3.10.config
|
2013-10-29 16:45:51 +02:00
|
|
|
touch .scmversion
|
|
|
|
|
export ARCH=arm
|
2021-09-19 10:11:39 +01:00
|
|
|
export CROSS_COMPILE="${base_dir}"/gcc-arm-linux-gnueabihf-4.7/bin/arm-linux-gnueabihf-
|
2015-08-10 22:17:08 -05:00
|
|
|
make -j $(grep -c processor /proc/cpuinfo)
|
2020-07-21 22:32:15 -05:00
|
|
|
make modules_install INSTALL_MOD_PATH=${work_dir}
|
|
|
|
|
cp arch/arm/boot/zImage ${work_dir}/boot/zImage-cm-fx6
|
|
|
|
|
cp arch/arm/boot/dts/imx6q-sbc-fx6m.dtb ${work_dir}/boot/imx6q-sbc-fx6m.dtb
|
2015-08-10 22:17:08 -05:00
|
|
|
make mrproper
|
|
|
|
|
cp ../utilite-3.10.config .config
|
2021-09-19 10:11:39 +01:00
|
|
|
cd "${base_dir}"
|
2013-10-29 16:45:51 +02:00
|
|
|
|
2017-09-20 13:27:28 -05:00
|
|
|
# Fix up the symlink for building external modules
|
|
|
|
|
# kernver is used so we don't need to keep track of what the current compiled
|
|
|
|
|
# version is
|
2020-07-21 22:32:15 -05:00
|
|
|
kernver=$(ls ${work_dir}/lib/modules/)
|
|
|
|
|
cd ${work_dir}/lib/modules/${kernver}
|
2017-09-20 13:27:28 -05:00
|
|
|
rm build
|
|
|
|
|
rm source
|
|
|
|
|
ln -s /usr/src/kernel build
|
|
|
|
|
ln -s /usr/src/kernel source
|
2021-09-19 10:11:39 +01:00
|
|
|
cd "${base_dir}"
|
2017-09-20 13:27:28 -05:00
|
|
|
|
2015-08-10 22:17:08 -05:00
|
|
|
# Create a file to set up our u-boot environment
|
2020-07-21 22:32:15 -05:00
|
|
|
cat << EOF > ${work_dir}/boot/boot.txt
|
2015-08-10 22:17:08 -05:00
|
|
|
setenv mmcdev 2
|
2020-07-26 17:40:50 -05:00
|
|
|
setenv bootargs 'earlyprintk console=ttymxc3,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=$fstype rw rootwait net.ifnames=0'
|
2015-08-10 22:17:08 -05:00
|
|
|
setenv loadaddr 0x10800000
|
|
|
|
|
setenv fdtaddr 0x15000000
|
|
|
|
|
setenv bootm_low 0x15000000
|
|
|
|
|
setenv zimage zImage-cm-fx6
|
|
|
|
|
setenv dtb imx6q-sbc-fx6m.dtb
|
|
|
|
|
#setenv kernel uImage-cm-fx6
|
|
|
|
|
|
|
|
|
|
load mmc \${mmcdev}:1 \${loadaddr} \${zimage}
|
|
|
|
|
load mmc \${mmcdev}:1 \${fdtaddr} \${dtb}
|
|
|
|
|
bootz \${loadaddr} - \${fdtaddr}
|
|
|
|
|
#load mmc \${mmcdev}:1 \${loadaddr} \${kernel}
|
|
|
|
|
#bootm \${loadaddr}
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
|
|
# And generate the boot.scr
|
2020-07-21 22:32:15 -05:00
|
|
|
mkimage -A arm -T script -C none -d ${work_dir}/boot/boot.txt ${work_dir}/boot/boot.scr
|
2015-08-10 22:17:08 -05:00
|
|
|
|
2021-09-19 10:11:39 +01:00
|
|
|
cd "${base_dir}"
|
2013-10-29 16:45:51 +02:00
|
|
|
|
2021-09-16 06:58:38 +01:00
|
|
|
# Calculate the space to create the image
|
2020-07-24 13:39:50 -05:00
|
|
|
root_size=$(du -s -B1 ${work_dir} --exclude=${work_dir}/boot | cut -f1)
|
|
|
|
|
root_extra=$((${root_size}/1024/1000*5*1024/5))
|
|
|
|
|
raw_size=$(($((${free_space}*1024))+${root_extra}+$((${bootsize}*1024))+4096))
|
2018-07-06 18:03:37 -05:00
|
|
|
|
|
|
|
|
# Create the disk and partition it
|
2021-09-19 10:11:39 +01:00
|
|
|
echo "Creating image file ${image_name}.img"
|
|
|
|
|
fallocate -l $(echo ${raw_size}Ki | numfmt --from=iec-i --to=si) ${current_dir}/${image_name}.img
|
|
|
|
|
parted -s ${current_dir}/${image_name}.img mklabel msdos
|
|
|
|
|
parted -s ${current_dir}/${image_name}.img mkpart primary fat32 1MiB ${bootsize}MiB
|
|
|
|
|
parted -s -a minimal ${current_dir}/${image_name}.img mkpart primary $fstype ${bootsize}MiB 100%
|
2018-07-06 18:03:37 -05:00
|
|
|
|
|
|
|
|
# Set the partition variables
|
2021-09-19 10:11:39 +01:00
|
|
|
loopdevice=`losetup -f --show ${current_dir}/${image_name}.img`
|
2018-07-06 18:03:37 -05:00
|
|
|
device=`kpartx -va ${loopdevice} | sed 's/.*\(loop[0-9]\+\)p.*/\1/g' | head -1`
|
|
|
|
|
sleep 5
|
|
|
|
|
device="/dev/mapper/${device}"
|
|
|
|
|
bootp=${device}p1
|
|
|
|
|
rootp=${device}p2
|
|
|
|
|
|
|
|
|
|
# Create file systems
|
2020-07-21 22:32:15 -05:00
|
|
|
mkfs.vfat -n BOOT ${bootp}
|
2020-05-07 15:19:48 -05:00
|
|
|
# The utilite uses an older kernel, and newer mkfs tools add extras to ext3, so
|
2021-09-16 06:58:38 +01:00
|
|
|
# we disable them otherwise there will be a kernel panic at boot
|
2020-07-26 14:38:21 -05:00
|
|
|
if [[ $fstype == ext4 ]]; then
|
|
|
|
|
features="-O ^64bit,^metadata_csum"
|
|
|
|
|
elif [[ $fstype == ext3 ]]; then
|
|
|
|
|
features="-O ^64bit"
|
|
|
|
|
fi
|
|
|
|
|
mkfs $features -t $fstype -L ROOTFS ${rootp}
|
2018-07-06 18:03:37 -05:00
|
|
|
|
|
|
|
|
# Create the dirs for the partitions and mount them
|
2021-09-19 10:11:39 +01:00
|
|
|
mkdir -p "${base_dir}"/root
|
|
|
|
|
mount ${rootp} "${base_dir}"/root
|
|
|
|
|
mkdir -p "${base_dir}"/root/boot
|
|
|
|
|
mount ${bootp} "${base_dir}"/root/boot
|
2015-08-10 22:17:08 -05:00
|
|
|
|
2021-09-16 06:58:38 +01:00
|
|
|
# We do this down here to get rid of the build system's resolv.conf after running through the build
|
2021-09-18 05:48:20 +01:00
|
|
|
echo "nameserver ${nameserver}" > "${work_dir}"/etc/resolv.conf
|
2018-07-06 18:03:37 -05:00
|
|
|
|
2021-09-16 06:58:38 +01:00
|
|
|
# Create an fstab so that we don't mount / read-only
|
2020-06-25 17:42:35 -05:00
|
|
|
UUID=$(blkid -s UUID -o value ${rootp})
|
2020-07-26 17:40:50 -05:00
|
|
|
echo "UUID=$UUID / $fstype errors=remount-ro 0 1" >> ${work_dir}/etc/fstab
|
2020-06-25 17:42:35 -05:00
|
|
|
|
2018-07-06 18:03:37 -05:00
|
|
|
echo "Rsyncing rootfs into image file"
|
2021-09-19 10:11:39 +01:00
|
|
|
rsync -HPavz -q ${work_dir}/ ${base_dir}/root/
|
2017-09-20 13:27:28 -05:00
|
|
|
|
2013-10-29 16:45:51 +02:00
|
|
|
# Unmount partitions
|
2018-07-06 18:03:37 -05:00
|
|
|
sync
|
|
|
|
|
umount ${bootp}
|
|
|
|
|
umount ${rootp}
|
|
|
|
|
kpartx -dv ${loopdevice}
|
|
|
|
|
losetup -d ${loopdevice}
|
2013-10-29 16:45:51 +02:00
|
|
|
|
2021-09-16 16:02:54 +01:00
|
|
|
# Limit CPU function
|
2020-07-26 14:38:21 -05:00
|
|
|
limit_cpu (){
|
2021-09-16 16:02:54 +01:00
|
|
|
rand=$(tr -cd 'A-Za-z0-9' < /dev/urandom | head -c4 ; echo) # Random name group
|
2020-07-26 14:38:21 -05:00
|
|
|
cgcreate -g cpu:/cpulimit-${rand} # Name of group cpulimit
|
2020-07-21 22:32:15 -05:00
|
|
|
cgset -r cpu.shares=800 cpulimit-${rand} # Max 1024
|
|
|
|
|
cgset -r cpu.cfs_quota_us=80000 cpulimit-${rand} # Max 100000
|
2020-07-26 14:38:21 -05:00
|
|
|
# Retry command
|
|
|
|
|
local n=1; local max=5; local delay=2
|
|
|
|
|
while true; do
|
|
|
|
|
cgexec -g cpu:cpulimit-${rand} "$@" && break || {
|
|
|
|
|
if [[ $n -lt $max ]]; then
|
|
|
|
|
((n++))
|
|
|
|
|
echo -e "\e[31m Command failed. Attempt $n/$max \033[0m"
|
|
|
|
|
sleep $delay;
|
|
|
|
|
else
|
|
|
|
|
echo "The command has failed after $n attempts."
|
|
|
|
|
break
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
done
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if [ $compress = xz ]; then
|
|
|
|
|
if [ $(arch) == 'x86_64' ]; then
|
2021-09-19 10:11:39 +01:00
|
|
|
echo "Compressing ${image_name}.img"
|
2020-07-27 18:47:11 -05:00
|
|
|
[ $(nproc) \< 3 ] || cpu_cores=3 # cpu_cores = Number of cores to use
|
2021-09-19 10:11:39 +01:00
|
|
|
limit_cpu pixz -p ${cpu_cores:-2} ${current_dir}/${image_name}.img # -p Nº cpu cores use
|
|
|
|
|
chmod 0644 ${current_dir}/${image_name}.img.xz
|
2020-07-26 14:38:21 -05:00
|
|
|
fi
|
|
|
|
|
else
|
2021-09-19 10:11:39 +01:00
|
|
|
chmod 0644 ${current_dir}/${image_name}.img
|
2014-03-07 21:19:17 -06:00
|
|
|
fi
|
2018-06-30 17:03:58 -05:00
|
|
|
|
2021-09-16 06:58:38 +01:00
|
|
|
# Clean up all the temporary build stuff and remove the directories
|
|
|
|
|
# Comment this out to keep things around if you want to see what may have gone wrong
|
2018-06-30 17:03:58 -05:00
|
|
|
echo "Removing temporary build files"
|
2021-09-19 10:11:39 +01:00
|
|
|
rm -rf "${base_dir}"
|