This commit is contained in:
Simone Margaritelli 2019-09-26 11:25:51 +02:00
commit 5920aafb6b
2 changed files with 45 additions and 36 deletions

1
.gitignore vendored
View File

@ -7,4 +7,3 @@ _utils
config.laptop.yml config.laptop.yml
.idea .idea
tmp tmp
mnt

View File

@ -2,8 +2,12 @@
# based on: https://wiki.debian.org/RaspberryPi/qemu-user-static # based on: https://wiki.debian.org/RaspberryPi/qemu-user-static
## and https://z4ziggy.wordpress.com/2015/05/04/from-bochs-to-chroot/ ## and https://z4ziggy.wordpress.com/2015/05/04/from-bochs-to-chroot/
set -eu
REQUIREMENTS=( wget gunzip git dd e2fsck resize2fs parted losetup qemu-system-x86_64 ) REQUIREMENTS=( wget gunzip git dd e2fsck resize2fs parted losetup qemu-system-x86_64 )
SCRIPT_DIR="$(dirname "$(realpath "$0")")" REPO_DIR="$(dirname "$(dirname "$(realpath "$0")")")"
TMP_DIR="${REPO_DIR}/tmp"
MNT_DIR="${TMP_DIR}/mnt"
PWNI_NAME="pwnagotchi" PWNI_NAME="pwnagotchi"
PWNI_OUTPUT="pwnagotchi.img" PWNI_OUTPUT="pwnagotchi.img"
@ -12,6 +16,11 @@ PWNI_SIZE="4"
OPT_PROVISION_ONLY=0 OPT_PROVISION_ONLY=0
OPT_CHECK_DEPS_ONLY=0 OPT_CHECK_DEPS_ONLY=0
if [[ "$EUID" -ne 0 ]]; then
echo "Run this script as root!"
exit 1
fi
function check_dependencies() { function check_dependencies() {
echo "[+] Checking dependencies" echo "[+] Checking dependencies"
for REQ in "${REQUIREMENTS[@]}"; do for REQ in "${REQUIREMENTS[@]}"; do
@ -27,55 +36,56 @@ function check_dependencies() {
fi fi
if ! systemctl is-active systemd-binfmt.service >/dev/null 2>&1; then if ! systemctl is-active systemd-binfmt.service >/dev/null 2>&1; then
sudo mkdir -p "/lib/binfmt.d" mkdir -p "/lib/binfmt.d"
sudo sh -c "echo ':qemu-arm:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00:\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\xfe\xff\xff\xff:/usr/bin/qemu-arm-static:F' >> /lib/binfmt.d/qemu-arm-static.conf" echo ':qemu-arm:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00:\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\xfe\xff\xff\xff:/usr/bin/qemu-arm-static:F' > /lib/binfmt.d/qemu-arm-static.conf
sudo systemctl restart systemd-binfmt.service systemctl restart systemd-binfmt.service
fi fi
} }
function get_raspbian() { function get_raspbian() {
echo "[+] Downloading raspbian.zip" echo "[+] Downloading raspbian.zip"
mkdir -p "${SCRIPT_DIR}/tmp/" mkdir -p "${TMP_DIR}"
wget -qcN -O "${SCRIPT_DIR}/tmp/rasbian.zip" "https://downloads.raspberrypi.org/raspbian_lite_latest" wget --show-progress -qcO "${TMP_DIR}/raspbian.zip" "https://downloads.raspberrypi.org/raspbian_lite_latest"
echo "[+] Unpacking raspbian.zip to raspbian.img" echo "[+] Unpacking raspbian.zip to raspbian.img"
gunzip -c "${SCRIPT_DIR}/tmp/rasbian.zip" > "${SCRIPT_DIR}/tmp/raspbian.img" gunzip -c "${TMP_DIR}/raspbian.zip" > "${TMP_DIR}/raspbian.img"
} }
function setup_raspbian(){ function setup_raspbian(){
echo "[+] Resize image" echo "[+] Resize image"
dd if=/dev/zero bs=1G count="$PWNI_SIZE" >> "${SCRIPT_DIR}/tmp/raspbian.img" dd if=/dev/zero bs=1G count="$PWNI_SIZE" >> "${TMP_DIR}/raspbian.img"
echo "[+] Setup loop device" echo "[+] Setup loop device"
mkdir -p "${SCRIPT_DIR}/mnt" mkdir -p "${MNT_DIR}"
LOOP_PATH="$(sudo losetup --find --partscan --show "${SCRIPT_DIR}/tmp/raspbian.img")" LOOP_PATH="$(losetup --find --partscan --show "${TMP_DIR}/raspbian.img")"
PART2_START="$(sudo parted -s "$LOOP_PATH" -- print | awk '$1==2{ print $2 }')" PART2_START="$(parted -s "$LOOP_PATH" -- print | awk '$1==2{ print $2 }')"
sudo parted -s "$LOOP_PATH" rm 2 parted -s "$LOOP_PATH" rm 2
sudo parted -s "$LOOP_PATH" mkpart primary "$PART2_START" 100% parted -s "$LOOP_PATH" mkpart primary "$PART2_START" 100%
echo "[+] Check FS" echo "[+] Check FS"
sudo e2fsck -f "${LOOP_PATH}p2" e2fsck -f "${LOOP_PATH}p2"
echo "[+] Resize FS" echo "[+] Resize FS"
sudo resize2fs "${LOOP_PATH}p2" resize2fs "${LOOP_PATH}p2"
echo "[+] Device is ${LOOP_PATH}" echo "[+] Device is ${LOOP_PATH}"
echo "[+] Unmount if already mounted with other img" echo "[+] Unmount if already mounted with other img"
sudo umount -r "${SCRIPT_DIR}/mnt" || true mountpoint -q "${MNT_DIR}" && umount -R "${MNT_DIR}"
echo "[+] Mount /" echo "[+] Mount /"
sudo mount -o rw "${LOOP_PATH}p2" "${SCRIPT_DIR}/mnt" mount -o rw "${LOOP_PATH}p2" "${MNT_DIR}"
echo "[+] Mount /boot" echo "[+] Mount /boot"
sudo mount -o rw "${LOOP_PATH}p1" "${SCRIPT_DIR}/mnt/boot" mount -o rw "${LOOP_PATH}p1" "${MNT_DIR}/boot"
sudo mount --bind /dev "${SCRIPT_DIR}/mnt/dev/" mount --bind /dev "${MNT_DIR}/dev/"
sudo mount --bind /sys "${SCRIPT_DIR}/mnt/sys/" mount --bind /sys "${MNT_DIR}/sys/"
sudo mount --bind /proc "${SCRIPT_DIR}/mnt/proc/" mount --bind /proc "${MNT_DIR}/proc/"
sudo mount --bind /dev/pts "${SCRIPT_DIR}/mnt/dev/pts" mount --bind /dev/pts "${MNT_DIR}/dev/pts"
sudo mount --bind /etc/ssl/certs "${SCRIPT_DIR}/mnt/etc/ssl/certs" mount --bind /etc/ssl/certs "${MNT_DIR}/etc/ssl/certs"
sudo mount --bind /etc/ca-certificates "${SCRIPT_DIR}/mnt/etc/ca-certificates" mount --bind /etc/ca-certificates "${MNT_DIR}/etc/ca-certificates"
sudo cp /usr/bin/qemu-arm-static "${SCRIPT_DIR}/mnt/usr/bin" cp /usr/bin/qemu-arm-static "${MNT_DIR}/usr/bin"
} }
function provision_raspbian() { function provision_raspbian() {
cd "${SCRIPT_DIR}/mnt/" || exit 1 cd "${MNT_DIR}"
sudo sed -i'' 's/^\([^#]\)/#\1/g' etc/ld.so.preload # add comments sed -i'' 's/^\([^#]\)/#\1/g' etc/ld.so.preload # add comments
echo "[+] Run chroot commands" echo "[+] Run chroot commands"
sudo LANG=C chroot . bin/bash -x <<EOF LANG=C chroot . bin/bash -x <<EOF
export PATH=\$PATH:/bin set -eu
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
apt-get -y update apt-get -y update
apt-get -y upgrade apt-get -y upgrade
apt-get -y install git vim screen build-essential golang python3-pip apt-get -y install git vim screen build-essential golang python3-pip
@ -144,11 +154,11 @@ STOP
apt update apt update
apt install -y kalipi-kernel kalipi-bootloader kalipi-re4son-firmware kalipi-kernel-headers libraspberrypi0 libraspberrypi-dev libraspberrypi-doc libraspberrypi-bin apt install -y kalipi-kernel kalipi-bootloader kalipi-re4son-firmware kalipi-kernel-headers libraspberrypi0 libraspberrypi-dev libraspberrypi-doc libraspberrypi-bin
EOF EOF
sudo sed -i'' 's/^#//g' etc/ld.so.preload sed -i'' 's/^#//g' etc/ld.so.preload
cd "${SCRIPT_DIR}" || exit 1 cd "${REPO_DIR}"
sudo umount -R "${SCRIPT_DIR}/mnt/" umount -R "${MNT_DIR}"
sudo losetup -D "$(losetup -l | awk '/raspbian\.img/{print $1}')" losetup -D "$(losetup -l | awk '/raspbian\.img/{print $1}')"
mv "${SCRIPT_DIR}/tmp/raspbian.img" "$PWNI_OUTPUT" mv "${TMP_DIR}/raspbian.img" "$PWNI_OUTPUT"
} }
function usage() { function usage() {
@ -161,7 +171,7 @@ usage: $0 [OPTIONS]
-o <file> # Name of the img-file (default: pwnagotchi.img) -o <file> # Name of the img-file (default: pwnagotchi.img)
-s <size> # Size which should be added to second partition (in Gigabyte) (default: 4) -s <size> # Size which should be added to second partition (in Gigabyte) (default: 4)
-p # Only run provisioning (assumes the image is already mounted) -p # Only run provisioning (assumes the image is already mounted)
-p # Only run dependencies checks -d # Only run dependencies checks
-h # Show this help -h # Show this help
EOF EOF