From 295c96d56a099fd3094a9347c75aa13bdcaa8843 Mon Sep 17 00:00:00 2001 From: Arszilla Date: Sat, 20 Aug 2022 00:28:51 +0300 Subject: [PATCH] Update and fix bash syntax of archived scripts --- archived/efikamx.sh | 80 +++++++++++++------- archived/kalitap.sh | 138 +++++++++++++++++++++++----------- archived/luna.sh | 139 +++++++++++++++++++++++----------- archived/odroid-w-devkit.sh | 144 ++++++++++++++++++++++++------------ archived/odroid-w.sh | 138 +++++++++++++++++++++++----------- 5 files changed, 434 insertions(+), 205 deletions(-) diff --git a/archived/efikamx.sh b/archived/efikamx.sh index 29cf619..7ac1963 100755 --- a/archived/efikamx.sh +++ b/archived/efikamx.sh @@ -9,26 +9,31 @@ echo "This script is now deprecated" >&2 echo "The kernel is too old to run systemd" >&2 + sleep 5s set -e if [[ $EUID -ne 0 ]]; then - echo "This script must be run as root" - exit 1 + echo "This script must be run as root" + exit 1 + fi -if [[ $# -eq 0 ]] ; then +if [[ $# -eq 0 ]]; then echo "Please pass version number, e.g. $0 1.0.1" exit 0 + fi -basedir=`pwd`/efikamx-$1 +basedir=$(pwd)/efikamx-$1 # Custom hostname variable hostname=${2:-kali} + # Custom image file name variable - MUST NOT include .img at the end. imagename=${3:-kali-linux-$1-efikamx} + # Size of image in megabytes (Default is 7000=7GB) size=7000 @@ -38,10 +43,12 @@ machine=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1) # Make sure that the cross compiler can be found in the path before we do # anything else, that way the builds don't fail half way through. export CROSS_COMPILE=arm-linux-gnueabihf- -if [ $(compgen -c $CROSS_COMPILE | wc -l) -eq 0 ] ; then +if [ $(compgen -c $CROSS_COMPILE | wc -l) -eq 0 ]; then echo "Missing cross compiler. Set up PATH according to the README" exit 1 + fi + # Unset CROSS_COMPILE so that if there is any native compiling needed it doesn't # get cross compiled. unset CROSS_COMPILE @@ -64,6 +71,7 @@ extras="iceweasel wpasupplicant" export packages="${arm} ${base} ${services} ${extras}" export architecture="armhf" + # If you have your own preferred mirrors, set them here. # You may want to leave security.kali.org alone, but if you trust your local # mirror, feel free to change this as well. @@ -86,15 +94,15 @@ cp /usr/bin/qemu-arm-static kali-${architecture}/usr/bin/ LANG=C systemd-nspawn -M ${machine} -D kali-${architecture} /debootstrap/debootstrap --second-stage mkdir -p kali-${architecture}/etc/apt/ -cat << EOF > kali-${architecture}/etc/apt/sources.list + +cat <kali-${architecture}/etc/apt/sources.list deb http://${mirror}/kali moto main contrib non-free EOF - -echo "${hostname}" > kali-${architecture}/etc/hostname +echo "${hostname}" >kali-${architecture}/etc/hostname # So X doesn't complain, we add kali to hosts -cat << EOF > kali-${architecture}/etc/hosts +cat <kali-${architecture}/etc/hosts 127.0.0.1 ${hostname} localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet @@ -104,7 +112,8 @@ ff02::2 ip6-allrouters EOF mkdir -p kali-${architecture}/etc/network/ -cat << EOF > kali-${architecture}/etc/network/interfaces + +cat <kali-${architecture}/etc/network/interfaces auto lo iface lo inet loopback @@ -112,7 +121,7 @@ auto eth0 iface eth0 inet dhcp EOF -cat << EOF > kali-${architecture}/etc/resolv.conf +cat <kali-${architecture}/etc/resolv.conf nameserver 8.8.8.8 EOF @@ -124,12 +133,12 @@ export DEBIAN_FRONTEND=noninteractive #mount -o bind /dev/ kali-${architecture}/dev/ #mount -o bind /dev/pts kali-${architecture}/dev/pts -cat << EOF > kali-${architecture}/debconf.set +cat <kali-${architecture}/debconf.set console-common console-data/keymap/policy select Select keymap from full list console-common console-data/keymap/full select en-latin1-nodeadkeys EOF -cat << EOF > kali-${architecture}/third-stage +cat <kali-${architecture}/third-stage #!/bin/bash set -e dpkg-divert --add --local --divert /usr/sbin/invoke-rc.d.chroot --rename /usr/sbin/invoke-rc.d @@ -167,9 +176,10 @@ rm -f /third-stage EOF chmod 755 kali-${architecture}/third-stage + LANG=C systemd-nspawn -M ${machine} -D kali-${architecture} /third-stage -cat << EOF > kali-${architecture}/cleanup +cat <kali-${architecture}/cleanup #!/bin/bash rm -rf /root/.bash_history apt-get update @@ -183,6 +193,7 @@ rm -f /usr/bin/qemu* EOF chmod 755 kali-${architecture}/cleanup + LANG=C systemd-nspawn -M ${machine} -D kali-${architecture} /cleanup #umount kali-${architecture}/proc/sys/fs/binfmt_misc @@ -198,11 +209,11 @@ LANG=C systemd-nspawn -M ${machine} -D kali-${architecture} /cleanup echo 'T1:12345:respawn:/sbin/agetty 115200 ttymxc0 vt100' >> \ "${basedir}"/kali-${architecture}/etc/inittab -cat << EOF >> "${basedir}"/kali-${architecture}/etc/udev/links.conf +cat <>"${basedir}"/kali-${architecture}/etc/udev/links.conf M ttymxc0 c 5 1 EOF -cat << EOF >> "${basedir}"/kali-${architecture}/etc/securetty +cat <>"${basedir}"/kali-${architecture}/etc/securetty ttymxc0 EOF @@ -211,7 +222,7 @@ EOF # udev won't start and we have no devices, including keyboard/usb support. sed -i -e "s/2.6.3\[0-1\]/2.6.30/g" "${basedir}"/kali-${architecture}/etc/init.d/udev -cat << EOF > "${basedir}"/kali-${architecture}/etc/apt/sources.list +cat <"${basedir}"/kali-${architecture}/etc/apt/sources.list deb http://old.kali.org/kali moto main non-free contrib deb-src http://old.kali.org/kali moto main non-free contrib EOF @@ -222,19 +233,26 @@ EOF # Kernel section. If you want to use a custom kernel, or configuration, replace # them in this section. git clone --depth 1 https://github.com/genesi/linux-legacy "${basedir}"/kali-${architecture}/usr/src/kernel + cd "${basedir}"/kali-${architecture}/usr/src/kernel + touch .scmversion + export ARCH=arm export CROSS_COMPILE=arm-linux-gnueabihf- -patch -p1 --no-backup-if-mismatch < "${basedir}"/../patches/mac80211.patch + +patch -p1 --no-backup-if-mismatch <"${basedir}"/../patches/mac80211.patch + make mx51_efikamx_defconfig make -j $(grep -c processor /proc/cpuinfo) uImage modules make modules_install INSTALL_MOD_PATH="${basedir}"/kali-${architecture} + cp arch/arm/boot/uImage "${basedir}"/kali-${architecture}/boot + cd "${basedir}" # Create boot.txt file -cat << EOF > "${basedir}"/kali-${architecture}/boot/boot.script +cat <"${basedir}"/kali-${architecture}/boot/boot.script setenv ramdisk uInitrd; setenv kernel uImage; setenv bootargs console=tty1 root=/dev/mmcblk0p2 rootwait rootfstype=ext3 rw quiet; @@ -256,16 +274,21 @@ cd "${basedir}" # Create the disk and partition it echo "Creating image file for ${imagename}.img" + dd if=/dev/zero of="${basedir}"/${imagename}.img bs=1M count=${size} + parted ${imagename}.img --script -- mklabel msdos parted ${imagename}.img --script -- mkpart primary ext2 4096s 266239s parted ${imagename}.img --script -- mkpart primary ext3 266240s 100% # Set the partition variables -loopdevice=`losetup -f --show "${basedir}"/${imagename}.img` -device=`kpartx -va ${loopdevice} | sed 's/.*\(loop[0-9]\+\)p.*/\1/g' | head -1` +loopdevice=$(losetup -f --show "${basedir}"/${imagename}.img) + +device=$(kpartx -va ${loopdevice} | sed 's/.*\(loop[0-9]\+\)p.*/\1/g' | head -1) device="/dev/mapper/${device}" + bootp=${device}p1 + rootp=${device}p2 # Create file systems @@ -274,13 +297,15 @@ mkfs.ext3 ${rootp} # Create the dirs for the partitions and mount them mkdir -p "${basedir}"/root + mount ${rootp} "${basedir}"/root + mkdir -p "${basedir}"/root/boot + mount ${bootp} "${basedir}"/root/boot - # We do this down here to get rid of the build system's resolv.conf after running through the build. -cat << EOF > kali-${architecture}/etc/resolv.conf +cat <kali-${architecture}/etc/resolv.conf nameserver 8.8.8.8 EOF @@ -295,11 +320,12 @@ kpartx -dv ${loopdevice} losetup -d ${loopdevice} # Don't pixz on 32bit, there isn't enough memory to compress the images. -MACHINE_TYPE=`uname -m` +MACHINE_TYPE=$(uname -m) if [ ${MACHINE_TYPE} == 'x86_64' ]; then -echo "Compressing ${imagename}.img" -pixz "${basedir}"/${imagename}.img "${basedir}"/../${imagename}.img.xz -rm "${basedir}"/${imagename}.img + echo "Compressing ${imagename}.img" + pixz "${basedir}"/${imagename}.img "${basedir}"/../${imagename}.img.xz + rm "${basedir}"/${imagename}.img + fi # Clean up all the temporary build stuff and remove the directories. diff --git a/archived/kalitap.sh b/archived/kalitap.sh index b81c7ee..9363e5a 100755 --- a/archived/kalitap.sh +++ b/archived/kalitap.sh @@ -18,32 +18,47 @@ set -e if [ "$debug" = true ]; then exec > >(tee -a -i "${0%.*}.log") 2>&1 set -x + fi # Architecture architecture=${architecture:-"armhf"} + # Generate a random machine name to be used. -machine=$(tr -cd 'A-Za-z0-9' < /dev/urandom | head -c16 ; echo) +machine=$( + tr -cd 'A-Za-z0-9' ${work_dir}/eatmydata + 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 +cat >${work_dir}/usr/bin/dpkg < ${work_dir}/etc/apt/sources.list +cat <${work_dir}/etc/apt/sources.list deb ${mirror} ${suite} ${components//,/ } #deb-src ${mirror} ${suite} ${components//,/ } EOF -echo "${hostname}" > ${work_dir}/etc/hostname +echo "${hostname}" >${work_dir}/etc/hostname -cat << EOF > ${work_dir}/etc/hosts +cat <${work_dir}/etc/hosts 127.0.0.1 ${hostname} localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet @@ -176,7 +205,8 @@ ff02::2 ip6-allrouters EOF mkdir -p ${work_dir}/etc/network/ -cat << EOF > ${work_dir}/etc/network/interfaces + +cat <${work_dir}/etc/network/interfaces auto lo iface lo inet loopback @@ -191,7 +221,7 @@ iface usb0 inet static gateway 192.168.7.1 EOF -cat << EOF > ${work_dir}/etc/resolv.conf +cat <${work_dir}/etc/resolv.conf nameserver 8.8.8.8 EOF @@ -202,11 +232,12 @@ export MALLOC_CHECK_=0 # workaround for LP: #520465 # Enable the use of http proxy in third-stage in case it is enabled. if [ -n "$proxy_url" ]; then - echo "Acquire::http { Proxy \"$proxy_url\" };" > ${work_dir}/etc/apt/apt.conf.d/66proxy + echo "Acquire::http { Proxy \"$proxy_url\" };" >${work_dir}/etc/apt/apt.conf.d/66proxy + fi # Third stage -cat << EOF > ${work_dir}/third-stage +cat <${work_dir}/third-stage #!/bin/bash -e export DEBIAN_FRONTEND=noninteractive @@ -291,7 +322,7 @@ systemd-nspawn_exec /third-stage systemd-nspawn_exec dpkg-divert --remove --rename /usr/bin/dpkg # Clean system -systemd-nspawn_exec << 'EOF' +systemd-nspawn_exec <<'EOF' rm -f /0 rm -rf /bsp fc-cache -frs @@ -314,24 +345,26 @@ EOF if [ -n "$proxy_url" ]; then unset http_proxy rm -rf ${work_dir}/etc/apt/apt.conf.d/66proxy + fi # Mirror & suite replacement if [[ ! -z "${4}" || ! -z "${5}" ]]; then mirror=${4} suite=${5} + fi -cat << EOF >> ${work_dir}/etc/udev/links.conf +cat <>${work_dir}/etc/udev/links.conf M ttyO0 c 5 1 EOF -cat << EOF >> ${work_dir}/etc/securetty +cat <>${work_dir}/etc/securetty ttyO0 EOF # Define sources.list -cat << EOF > ${work_dir}/etc/apt/sources.list +cat <${work_dir}/etc/apt/sources.list deb ${mirror} ${suite} ${components//,/ } #deb-src ${mirror} ${suite} ${components//,/ } EOF @@ -343,9 +376,9 @@ git clone --depth 1 https://gitlab.com/kalilinux/packages/gcc-arm-linux-gnueabih # Get, compile and install kernel git clone --depth 1 https://github.com/wawtechnologies/linux-kernel-3.14.51-catchwire-kalitap.git ${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 < ${repo_dir}/patches/kali-wifi-injection-3.14.patch -patch -p1 --no-backup-if-mismatch < ${repo_dir}/patches/0001-wireless-carl9170-Enable-sniffer-mode-promisc-flag-t.patch +git rev-parse HEAD >${work_dir}/usr/src/kernel-at-commit +patch -p1 --no-backup-if-mismatch <${repo_dir}/patches/kali-wifi-injection-3.14.patch +patch -p1 --no-backup-if-mismatch <${repo_dir}/patches/0001-wireless-carl9170-Enable-sniffer-mode-promisc-flag-t.patch touch .scmversion export ARCH=arm export CROSS_COMPILE="${basedir}"/gcc-arm-linux-gnueabihf-4.7/bin/arm-linux-gnueabihf- @@ -363,7 +396,7 @@ cd "${basedir}" # systemd doesn't seem to be generating the fstab properly for some people, so # let's create one. The rootfs partition is added below via UUID after the image # is created since we won't know the UUID until then. -cat << EOF > ${work_dir}/etc/fstab +cat <${work_dir}/etc/fstab # proc /proc proc defaults 0 0 /dev/mmcblk0p1 /boot vfat defaults 0 2 @@ -388,7 +421,7 @@ install -m755 udp0srv/udp0srv ${work_dir}/usr/sbin cd "${basedir}" #u-boot kalitap specific overrides: -cat << EOF > ${work_dir}/boot/uEnv.txt +cat <${work_dir}/boot/uEnv.txt optargs="consoleblank=0 mem=1G rootwait fixrtc net.ifnames=0 rootwait" kernel_file=zImage initrd_file=initrd.img @@ -422,8 +455,8 @@ cd "${basedir}" # Calculate the space to create the image. 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)) +root_extra=$((${root_size} / 1024 / 1000 * 5 * 1024 / 5)) +raw_size=$(($((${free_space} * 1024)) + ${root_extra} + $((${bootsize} * 1024)) + 4096)) # Create the disk and partition it echo "Creating image file ${imagename}.img" @@ -434,8 +467,8 @@ parted -s -a minimal ${repo_dir}/${imagename}.img mkpart primary $fstype ${boots parted -s ${repo_dir}/${imagename}.img set 1 boot on # Set the partition variables -loopdevice=`losetup -f --show ${repo_dir}/${imagename}.img` -device=`kpartx -va ${loopdevice} | sed 's/.*\(loop[0-9]\+\)p.*/\1/g' | head -1` +loopdevice=$(losetup -f --show ${repo_dir}/${imagename}.img) +device=$(kpartx -va ${loopdevice} | sed 's/.*\(loop[0-9]\+\)p.*/\1/g' | head -1) sleep 5 device="/dev/mapper/${device}" bootp=${device}p1 @@ -445,9 +478,12 @@ rootp=${device}p2 mkfs.vfat -n BOOT -F 16 ${bootp} if [[ $fstype == ext4 ]]; then features="-O ^64bit,^metadata_csum" + elif [[ $fstype == ext3 ]]; then features="-O ^64bit" + fi + mkfs $features -t $fstype -L ROOTFS ${rootp} # Create the dirs for the partitions and mount them @@ -457,13 +493,13 @@ mkdir -p "${basedir}"/root/boot mount ${bootp} "${basedir}"/root/boot # We do this down here to get rid of the build system's resolv.conf after running through the build. -cat << EOF > ${work_dir}/etc/resolv.conf +cat <${work_dir}/etc/resolv.conf nameserver 8.8.8.8 EOF # Create an fstab so that we don't mount / read-only. UUID=$(blkid -s UUID -o value ${rootp}) -echo "UUID=$UUID / $fstype errors=remount-ro 0 1" >> ${work_dir}/etc/fstab +echo "UUID=$UUID / $fstype errors=remount-ro 0 1" >>${work_dir}/etc/fstab echo "Rsyncing rootfs into image file" rsync -HPavz -q ${work_dir}/ ${basedir}/root/ @@ -476,22 +512,31 @@ kpartx -dv ${loopdevice} losetup -d ${loopdevice} # Limite use cpu function -limit_cpu (){ - rand=$(tr -cd 'A-Za-z0-9' < /dev/urandom | head -c4 ; echo) # Randowm name group - cgcreate -g cpu:/cpulimit-${rand} # Name of group cpulimit - cgset -r cpu.shares=800 cpulimit-${rand} # Max 1024 +limit_cpu() { + rand=$( + tr -cd 'A-Za-z0-9' >(tee -a -i "${0%.*}.log") 2>&1 set -x + fi # Architecture architecture=${architecture:-"armhf"} + # Generate a random machine name to be used. -machine=$(tr -cd 'A-Za-z0-9' < /dev/urandom | head -c16 ; echo) +machine=$( + tr -cd 'A-Za-z0-9' ${work_dir}/eatmydata + 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 +cat >${work_dir}/usr/bin/dpkg < ${work_dir}/etc/apt/sources.list +cat <${work_dir}/etc/apt/sources.list deb ${mirror} ${suite} ${components//,/ } #deb-src ${mirror} ${suite} ${components//,/ } EOF # Set hostname -echo "${hostname}" > ${work_dir}/etc/hostname +echo "${hostname}" >${work_dir}/etc/hostname # So X doesn't complain, we add kali to hosts -cat << EOF > ${work_dir}/etc/hosts +cat <${work_dir}/etc/hosts 127.0.0.1 ${hostname} localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet @@ -178,12 +209,12 @@ ff02::2 ip6-allrouters EOF # Disable IPv6 -cat << EOF > ${work_dir}/etc/modprobe.d/ipv6.conf +cat <${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 +cat <${work_dir}/etc/network/interfaces auto lo iface lo inet loopback @@ -193,7 +224,7 @@ iface eth0 inet dhcp EOF # DNS server -echo "nameserver 8.8.8.8" > ${work_dir}/etc/resolv.conf +echo "nameserver 8.8.8.8" >${work_dir}/etc/resolv.conf # Copy directory bsp into build dir. cp -rp bsp ${work_dir} @@ -202,11 +233,12 @@ export MALLOC_CHECK_=0 # workaround for LP: #520465 # Enable the use of http proxy in third-stage in case it is enabled. if [ -n "$proxy_url" ]; then - echo "Acquire::http { Proxy \"$proxy_url\" };" > ${work_dir}/etc/apt/apt.conf.d/66proxy + echo "Acquire::http { Proxy \"$proxy_url\" };" >${work_dir}/etc/apt/apt.conf.d/66proxy + fi # Third stage -cat << EOF > ${work_dir}/third-stage +cat <${work_dir}/third-stage #!/bin/bash -e export DEBIAN_FRONTEND=noninteractive @@ -290,7 +322,7 @@ systemd-nspawn_exec /third-stage systemd-nspawn_exec dpkg-divert --remove --rename /usr/bin/dpkg # Clean system -systemd-nspawn_exec << 'EOF' +systemd-nspawn_exec <<'EOF' rm -f /0 rm -rf /bsp fc-cache -frs @@ -313,24 +345,26 @@ EOF if [ -n "$proxy_url" ]; then unset http_proxy rm -rf ${work_dir}/etc/apt/apt.conf.d/66proxy + fi # Mirror & suite replacement if [[ ! -z "${4}" || ! -z "${5}" ]]; then mirror=${4} suite=${5} + fi -cat << EOF >> ${work_dir}/etc/udev/links.conf +cat <>${work_dir}/etc/udev/links.conf M ttyO0 c 5 1 EOF -cat << EOF >> ${work_dir}/etc/securetty +cat <>${work_dir}/etc/securetty ttyO0 EOF # Define sources.list -cat << EOF > ${work_dir}/etc/apt/sources.list +cat <${work_dir}/etc/apt/sources.list deb ${mirror} ${suite} ${components//,/ } #deb-src ${mirror} ${suite} ${components//,/ } EOF @@ -338,7 +372,7 @@ EOF # Get, compile and install kernel git clone --depth 1 https://github.com/steev/luna-kernel ${work_dir}/usr/src/kernel cd ${work_dir}/usr/src/kernel -patch -p1 --no-backup-if-mismatch < ${repo_dir}/patches/mac80211.patch +patch -p1 --no-backup-if-mismatch <${repo_dir}/patches/mac80211.patch export ARCH=arm export CROSS_COMPILE=arm-linux-gnueabihf- cp ${repo_dir}/kernel-configs/luna.config .config @@ -350,7 +384,7 @@ cd "${basedir}" # systemd doesn't seem to be generating the fstab properly for some people, so # let's create one. We add the root partition below via UUID which we won't # know until after the image is created and partitioned. -cat << EOF > ${work_dir}/etc/fstab +cat <${work_dir}/etc/fstab # proc /proc proc defaults 0 0 /dev/mmcblk0p1 /boot vfat defaults 0 2 @@ -368,7 +402,7 @@ ln -s /usr/src/kernel source cd "${basedir}" #u-boot LUNA specific overrides: -cat << EOF > ${work_dir}/boot/uEnv.txt +cat <${work_dir}/boot/uEnv.txt fdtfile=am335x-luna.dtb kernel_file=zImage initrd_file=uInitrd @@ -388,8 +422,8 @@ EOF # Calculate the space to create the image. 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)) +root_extra=$((${root_size} / 1024 / 1000 * 5 * 1024 / 5)) +raw_size=$(($((${free_space} * 1024)) + ${root_extra} + $((${bootsize} * 1024)) + 4096)) # Create the disk and partition it echo "Creating image file ${imagename}.img" @@ -399,8 +433,8 @@ parted -s ${repo_dir}/${imagename}.img mkpart primary fat32 1MiB ${bootsize}MiB parted -s -a minimal ${repo_dir}/${imagename}.img mkpart primary $fstype ${bootsize}MiB 100% # Set the partition variables -loopdevice=`losetup -f --show ${repo_dir}/${imagename}.img` -device=`kpartx -va ${loopdevice} | sed 's/.*\(loop[0-9]\+\)p.*/\1/g' | head -1` +loopdevice=$(losetup -f --show ${repo_dir}/${imagename}.img) +device=$(kpartx -va ${loopdevice} | sed 's/.*\(loop[0-9]\+\)p.*/\1/g' | head -1) sleep 5 device="/dev/mapper/${device}" bootp=${device}p1 @@ -408,27 +442,32 @@ rootp=${device}p2 # Create file systems mkfs.vfat -n BOOT -F 16 ${bootp} + if [[ $fstype == ext4 ]]; then features="-O ^64bit,^metadata_csum" + elif [[ $fstype == ext3 ]]; then features="-O ^64bit" + fi + mkfs $features -t $fstype -L ROOTFS ${rootp} # Create the dirs for the partitions and mount them mkdir -p "${basedir}"/root mount ${rootp} "${basedir}"/root + mkdir -p $"${basedir}"/root/boot mount ${bootp} "${basedir}"/root/boot # We do this down here to get rid of the build system's resolv.conf after running through the build. -cat << EOF > ${work_dir}/etc/resolv.conf +cat <${work_dir}/etc/resolv.conf nameserver 8.8.8.8 EOF # Create an fstab so that we don't mount / read-only. UUID=$(blkid -s UUID -o value ${rootp}) -echo "UUID=$UUID / $fstype errors=remount-ro 0 1" >> ${work_dir}/etc/fstab +echo "UUID=$UUID / $fstype errors=remount-ro 0 1" >>${work_dir}/etc/fstab echo "Rsyncing rootfs into image file" rsync -HPavz -q ${work_dir}/ ${basedir}/root/ @@ -441,22 +480,31 @@ kpartx -dv ${loopdevice} losetup -d ${loopdevice} # Limite use cpu function -limit_cpu (){ - rand=$(tr -cd 'A-Za-z0-9' < /dev/urandom | head -c4 ; echo) # Randowm name group - cgcreate -g cpu:/cpulimit-${rand} # Name of group cpulimit - cgset -r cpu.shares=800 cpulimit-${rand} # Max 1024 +limit_cpu() { + rand=$( + tr -cd 'A-Za-z0-9' >(tee -a -i "${0%.*}.log") 2>&1 set -x + fi # Architecture architecture=${architecture:-"armel"} + # Generate a random machine name to be used. -machine=$(tr -cd 'A-Za-z0-9' < /dev/urandom | head -c16 ; echo) +machine=$( + tr -cd 'A-Za-z0-9' ${work_dir}/eatmydata + 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 +cat >${work_dir}/usr/bin/dpkg < ${work_dir}/etc/apt/sources.list +cat <${work_dir}/etc/apt/sources.list deb ${mirror} ${suite} ${components//,/ } #deb-src ${mirror} ${suite} ${components//,/ } EOF # Set hostname -echo "${hostname}" > ${work_dir}/etc/hostname +echo "${hostname}" >${work_dir}/etc/hostname # So X doesn't complain, we add kali to hosts -cat << EOF > ${work_dir}/etc/hosts +cat <${work_dir}/etc/hosts 127.0.0.1 ${hostname} localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet @@ -186,12 +217,12 @@ ff02::2 ip6-allrouters EOF # Disable IPv6 -cat << EOF > ${work_dir}/etc/modprobe.d/ipv6.conf +cat <${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 +cat <${work_dir}/etc/network/interfaces auto lo iface lo inet loopback @@ -201,7 +232,7 @@ iface eth0 inet dhcp EOF # DNS server -echo "nameserver 8.8.8.8" > ${work_dir}/etc/resolv.conf +echo "nameserver 8.8.8.8" >${work_dir}/etc/resolv.conf # Copy directory bsp into build dir. cp -rp bsp ${work_dir} @@ -210,11 +241,12 @@ export MALLOC_CHECK_=0 # workaround for LP: #520465 # Enable the use of http proxy in third-stage in case it is enabled. if [ -n "$proxy_url" ]; then - echo "Acquire::http { Proxy \"$proxy_url\" };" > ${work_dir}/etc/apt/apt.conf.d/66proxy + echo "Acquire::http { Proxy \"$proxy_url\" };" >${work_dir}/etc/apt/apt.conf.d/66proxy + fi # Third stage -cat << EOF > ${work_dir}/third-stage +cat <${work_dir}/third-stage #!/bin/bash -e export DEBIAN_FRONTEND=noninteractive @@ -333,7 +365,7 @@ systemd-nspawn_exec /third-stage systemd-nspawn_exec dpkg-divert --remove --rename /usr/bin/dpkg # Clean system -systemd-nspawn_exec << 'EOF' +systemd-nspawn_exec <<'EOF' rm -f /0 rm -rf /bsp fc-cache -frs @@ -356,16 +388,18 @@ EOF if [ -n "$proxy_url" ]; then unset http_proxy rm -rf ${work_dir}/etc/apt/apt.conf.d/66proxy + fi # Mirror & suite replacement if [[ ! -z "${4}" || ! -z "${5}" ]]; then mirror=${4} suite=${5} + fi # Define sources.list -cat << EOF > ${work_dir}/etc/apt/sources.list +cat <${work_dir}/etc/apt/sources.list deb ${mirror} ${suite} ${components//,/ } #deb-src ${mirror} ${suite} ${components//,/ } EOF @@ -373,32 +407,33 @@ EOF # systemd doesn't seem to be generating the fstab properly for some people, so # let's create one. Root partition is added below after the image file is created # because we add it via UUID. -cat << EOF > ${work_dir}/etc/fstab +cat <${work_dir}/etc/fstab # proc /proc proc defaults 0 0 /dev/mmcblk0p1 /boot vfat defaults 0 2 EOF # Create cmdline.txt file -cat << EOF > ${work_dir}/boot/cmdline.txt +cat <${work_dir}/boot/cmdline.txt dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 elevator=deadline root=/dev/mmcblk0p2 rootfstype=$fstype rootwait fbcon=map:10 net.ifnames=0 rw EOF # Copy a default config, with everything commented out so people find it when # they go to add something when they are following instructions on a website. cp ./bsp/firmware/rpi/config.txt ${work_dir}/boot/config.txt + # Remove repeat conditional filters [all] in config.txt sed -i "59,66d" ${work_dir}/boot/config.txt # Add needed bit for the lcd of the devkit. -cat << EOF >> ${work_dir}/boot/config.txt +cat <>${work_dir}/boot/config.txt # Use fbtft_device instead of a DT overlay dtparam=spi=on EOF # Create /etc/modules based on ODROID-W -cat << EOF > ${work_dir}/etc/modules +cat <${work_dir}/etc/modules # /etc/modules: kernel modules to load at boot time. # # This file contains the names of kernel modules that should be loaded @@ -411,18 +446,19 @@ fbtft_device name=adafruit22a rotate=90 EOF mkdir -p ${work_dir}/etc/modprobe.d/ -cat << EOF > ${work_dir}/etc/modprobe.d/fbtft_device.conf +cat <${work_dir}/etc/modprobe.d/fbtft_device.conf options fbtft_device name=adafruit22a rotate=90 EOF # Calculate the space to create the image. 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)) +root_extra=$((${root_size} / 1024 / 1000 * 5 * 1024 / 5)) +raw_size=$(($((${free_space} * 1024)) + ${root_extra} + $((${bootsize} * 1024)) + 4096)) # Create the disk and partition it echo "Creating image file ${imagename}.img" fallocate -l $(echo ${raw_size}Ki | numfmt --from=iec-i --to=si) ${repo_dir}/${imagename}.img + parted -s ${repo_dir}/${imagename}.img mklabel msdos parted -s ${repo_dir}/${imagename}.img mkpart primary fat32 1MiB ${bootsize}MiB parted -s -a minimal ${repo_dir}/${imagename}.img mkpart primary $fstype ${bootsize}MiB 100% @@ -436,25 +472,29 @@ rootp="${loopdevice}p2" mkfs.vfat -n BOOT -F 32 -v ${bootp} if [[ $fstype == ext4 ]]; then features="-O ^64bit,^metadata_csum" + elif [[ $fstype == ext3 ]]; then features="-O ^64bit" + fi + mkfs $features -t $fstype -L ROOTFS ${rootp} # Create the dirs for the partitions and mount them mkdir -p ${basedir}/root/ mount ${rootp} ${basedir}/root + mkdir -p ${basedir}/root/boot mount ${bootp} ${basedir}/root/boot # We do this down here to get rid of the build system's resolv.conf after running through the build. -cat << EOF > ${work_dir}/etc/resolv.conf +cat <${work_dir}/etc/resolv.conf nameserver 8.8.8.8 EOF # Create an fstab so that we don't mount / read-only. UUID=$(blkid -s UUID -o value ${rootp}) -echo "UUID=$UUID / $fstype errors=remount-ro 0 1" >> ${work_dir}/etc/fstab +echo "UUID=$UUID / $fstype errors=remount-ro 0 1" >>${work_dir}/etc/fstab echo "Rsyncing rootfs into image file" rsync -HPavz -q ${work_dir}/ ${basedir}/root/ @@ -467,22 +507,31 @@ kpartx -dv ${loopdevice} losetup -d ${loopdevice} # Limite use cpu function -limit_cpu (){ - rand=$(tr -cd 'A-Za-z0-9' < /dev/urandom | head -c4 ; echo) # Randowm name group - cgcreate -g cpu:/cpulimit-${rand} # Name of group cpulimit - cgset -r cpu.shares=800 cpulimit-${rand} # Max 1024 +limit_cpu() { + rand=$( + tr -cd 'A-Za-z0-9' >(tee -a -i "${0%.*}.log") 2>&1 set -x + fi # Architecture architecture=${architecture:-"armel"} + # Generate a random machine name to be used. -machine=$(tr -cd 'A-Za-z0-9' < /dev/urandom | head -c16 ; echo) +machine=$( + tr -cd 'A-Za-z0-9' ${work_dir}/eatmydata + 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 +cat >${work_dir}/usr/bin/dpkg < ${work_dir}/etc/apt/sources.list +cat <${work_dir}/etc/apt/sources.list deb ${mirror} ${suite} ${components//,/ } #deb-src ${mirror} ${suite} ${components//,/ } EOF # Set hostname -echo "${hostname}" > ${work_dir}/etc/hostname +echo "${hostname}" >${work_dir}/etc/hostname # So X doesn't complain, we add kali to hosts -cat << EOF > ${work_dir}/etc/hosts +cat <${work_dir}/etc/hosts 127.0.0.1 ${hostname} localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet @@ -185,12 +217,12 @@ ff02::2 ip6-allrouters EOF # Disable IPv6 -cat << EOF > ${work_dir}/etc/modprobe.d/ipv6.conf +cat <${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 +cat <${work_dir}/etc/network/interfaces auto lo iface lo inet loopback @@ -200,7 +232,7 @@ iface eth0 inet dhcp EOF # DNS server -echo "nameserver 8.8.8.8" > ${work_dir}/etc/resolv.conf +echo "nameserver 8.8.8.8" >${work_dir}/etc/resolv.conf # Copy directory bsp into build dir. cp -rp bsp ${work_dir} @@ -209,11 +241,12 @@ export MALLOC_CHECK_=0 # workaround for LP: #520465 # Enable the use of http proxy in third-stage in case it is enabled. if [ -n "$proxy_url" ]; then - echo "Acquire::http { Proxy \"$proxy_url\" };" > ${work_dir}/etc/apt/apt.conf.d/66proxy + echo "Acquire::http { Proxy \"$proxy_url\" };" >${work_dir}/etc/apt/apt.conf.d/66proxy + fi # Third stage -cat << EOF > ${work_dir}/third-stage +cat <${work_dir}/third-stage #!/bin/bash -e export DEBIAN_FRONTEND=noninteractive @@ -333,7 +366,7 @@ systemd-nspawn_exec /third-stage systemd-nspawn_exec dpkg-divert --remove --rename /usr/bin/dpkg # Clean system -systemd-nspawn_exec << 'EOF' +systemd-nspawn_exec <<'EOF' rm -f /0 rm -rf /bsp fc-cache -frs @@ -356,29 +389,31 @@ EOF if [ -n "$proxy_url" ]; then unset http_proxy rm -rf ${work_dir}/etc/apt/apt.conf.d/66proxy + fi # Mirror & suite replacement if [[ ! -z "${4}" || ! -z "${5}" ]]; then mirror=${4} suite=${5} + fi # Create cmdline.txt file -cat << EOF > ${work_dir}/boot/cmdline.txt +cat <${work_dir}/boot/cmdline.txt dwc_otg.fiq_fix_enable=2 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=$fstype rootwait rootflags=noload net.ifnames=0 EOF # systemd doesn't seem to be generating the fstab properly for some people, so # let's create one. -cat << EOF > ${work_dir}/etc/fstab +cat <${work_dir}/etc/fstab # proc /proc proc defaults 0 0 /dev/mmcblk0p1 /boot vfat defaults 0 2 EOF # Define sources.list -cat << EOF > ${work_dir}/etc/apt/sources.list +cat <${work_dir}/etc/apt/sources.list deb ${mirror} ${suite} ${components//,/ } #deb-src ${mirror} ${suite} ${components//,/ } EOF @@ -386,20 +421,21 @@ EOF # Copy a default config, with everything commented out so people find it when # they go to add something when they are following instructions on a website. cp ./bsp/firmware/rpi/config.txt ${work_dir}/boot/config.txt + # Remove repeat conditional filters [all] in config.txt sed -i "59,66d" ${work_dir}/boot/config.txt - cd "${basedir}" # Calculate the space to create the image. 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)) +root_extra=$((${root_size} / 1024 / 1000 * 5 * 1024 / 5)) +raw_size=$(($((${free_space} * 1024)) + ${root_extra} + $((${bootsize} * 1024)) + 4096)) # Create the disk and partition it echo "Creating image file ${imagename}.img" fallocate -l $(echo ${raw_size}Ki | numfmt --from=iec-i --to=si) ${repo_dir}/${imagename}.img + parted -s ${repo_dir}/${imagename}.img mklabel msdos parted -s ${repo_dir}/${imagename}.img mkpart primary fat32 1MiB ${bootsize}MiB parted -s -a minimal ${repo_dir}/${imagename}.img mkpart primary $fstype ${bootsize}MiB 100% @@ -413,25 +449,29 @@ rootp="${loopdevice}p2" mkfs.vfat -n BOOT -F 32 -v ${bootp} if [[ $fstype == ext4 ]]; then features="-O ^64bit,^metadata_csum" + elif [[ $fstype == ext3 ]]; then features="-O ^64bit" + fi + mkfs $features -t $fstype -L ROOTFS ${rootp} # Create the dirs for the partitions and mount them mkdir -p ${basedir}/root/ mount ${rootp} ${basedir}/root + mkdir -p ${basedir}/root/boot mount ${bootp} ${basedir}/root/boot # We do this down here to get rid of the build system's resolv.conf after running through the build. -cat << EOF > ${work_dir}/etc/resolv.conf +cat <${work_dir}/etc/resolv.conf nameserver 8.8.8.8 EOF # Create an fstab so that we don't mount / read-only. UUID=$(blkid -s UUID -o value ${rootp}) -echo "UUID=$UUID / $fstype errors=remount-ro 0 1" >> ${work_dir}/etc/fstab +echo "UUID=$UUID / $fstype errors=remount-ro 0 1" >>${work_dir}/etc/fstab echo "Rsyncing rootfs into image file" rsync -HPavz -q ${work_dir}/ ${basedir}/root/ @@ -444,22 +484,31 @@ kpartx -dv ${loopdevice} losetup -d ${loopdevice} # Limite use cpu function -limit_cpu (){ - rand=$(tr -cd 'A-Za-z0-9' < /dev/urandom | head -c4 ; echo) # Randowm name group - cgcreate -g cpu:/cpulimit-${rand} # Name of group cpulimit - cgset -r cpu.shares=800 cpulimit-${rand} # Max 1024 +limit_cpu() { + rand=$( + tr -cd 'A-Za-z0-9'