tpotce/makeiso.sh

285 lines
9.4 KiB
Bash
Raw Normal View History

2014-11-28 17:02:20 +00:00
#!/bin/bash
2015-01-27 16:46:52 +00:00
2017-03-22 18:42:24 +00:00
# Set TERM, DIALOGRC
export TERM=linux
2014-11-28 17:02:20 +00:00
# Let's define some global vars
myBACKTITLE="T-Pot - ISO Creator"
2019-02-08 17:28:16 +00:00
#myMINIISOLINK="http://ftp.debian.org/debian/dists/testing/main/installer-amd64/current/images/netboot/mini.iso"
2019-02-13 16:09:23 +00:00
#myMINIISOLINK="https://d-i.debian.org/daily-images/amd64/daily/netboot/mini.iso"
# For stability reasons Debian Sid installation is built on a stable installer
2022-01-13 15:34:50 +00:00
### DEV
#myMINIISOLINK="http://ftp.debian.org/debian/dists/buster/main/installer-amd64/current/images/netboot/mini.iso"
myMINIISOLINK="http://ftp.debian.org/debian/dists/bullseye/main/installer-amd64/current/images/netboot/mini.iso"
#myMINIISOLINK="http://ftp.debian.org/debian/dists/bullseye/main/installer-arm64/current/images/netboot/mini.iso"
2019-01-28 20:52:55 +00:00
myMINIISO="mini.iso"
2015-12-08 14:47:39 +00:00
myTPOTISO="tpot.iso"
myTPOTDIR="tpotiso"
myTPOTSEED="iso/preseed/tpot.seed"
2019-08-02 17:35:56 +00:00
myPACKAGES="dialog genisoimage syslinux syslinux-utils pv rsync udisks2 xorriso"
myPFXFILE="iso/installer/keys/8021x.pfx"
myINSTALLERPATH="iso/installer/install.sh"
myNTPCONFFILE="iso/installer/ntp.conf"
2014-11-28 17:02:20 +00:00
myTMP="tmp"
myCONF_FILE="iso/installer/iso.conf"
myCONF_DEFAULT_FILE="iso/installer/iso.conf.dist"
2015-12-08 14:47:39 +00:00
# Got root?
myWHOAMI=$(whoami)
if [ "$myWHOAMI" != "root" ]
then
echo "Need to run as root ..."
sudo ./$0
2015-12-08 14:47:39 +00:00
exit
fi
# Let's check if all dependencies are met
myINST=""
for myDEPS in $myPACKAGES;
do
myOK=$(dpkg -s $myDEPS | grep ok | awk '{ print $3 }');
if [ "$myOK" != "ok" ]
then
myINST=$(echo $myINST $myDEPS)
fi
done
if [ "$myINST" != "" ]
then
apt-get update -y
for myDEPS in $myINST;
do
apt-get install $myDEPS -y
done
fi
2015-12-08 14:47:39 +00:00
# Let's clean up at the end or if something goes wrong ...
function fuCLEANUP {
rm -rf $myTMP $myTPOTDIR $myPFXFILE $myNTPCONFFILE $myCONF_FILE
2015-12-08 14:47:39 +00:00
if [ -f $myTPOTSEED.bak ];
then
mv $myTPOTSEED.bak $myTPOTSEED
fi
2014-11-28 17:02:20 +00:00
}
2015-12-08 14:47:39 +00:00
trap fuCLEANUP EXIT
# Let's create a function for validating an IPv4 address
function valid_ip()
{
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
# Let's ask if the user wants to run the script ...
2019-01-28 20:52:55 +00:00
dialog --backtitle "$myBACKTITLE" --title "[ Continue? ]" --yesno "\nDownload latest supported Debian Mini ISO and build the T-Pot Install Image." 8 50
2015-12-08 14:47:39 +00:00
mySTART=$?
if [ "$mySTART" = "1" ];
then
exit
fi
2014-11-28 17:02:20 +00:00
# Let's load the default config file
if [ -f $myCONF_DEFAULT_FILE ];
then
source $myCONF_DEFAULT_FILE
fi
2015-12-08 14:47:39 +00:00
# Let's ask the user for a proxy ...
while true;
do
dialog --backtitle "$myBACKTITLE" --title "[ Proxy Settings ]" --yesno "\nDo you want to configure a proxy?" 7 50
myCONF_PROXY_USE=$?
if [ "$myCONF_PROXY_USE" = "0" ]
2015-12-08 14:47:39 +00:00
then
myIPRESULT="false"
while [ "$myIPRESULT" = "false" ];
do
myCONF_PROXY_IP=$(dialog --backtitle "$myBACKTITLE" --no-cancel --title "Proxy IP?" --inputbox "" 7 50 "$myCONF_PROXY_IP" 3>&1 1>&2 2>&3 3>&-)
if valid_ip $myCONF_PROXY_IP; then myIPRESULT="true"; fi
2015-12-08 14:47:39 +00:00
done
myPORTRESULT="false"
while [ "$myPORTRESULT" = "false" ];
do
myCONF_PROXY_PORT=$(dialog --backtitle "$myBACKTITLE" --no-cancel --title "Proxy Port (i.e. 3128)?" --inputbox "" 7 50 "$myCONF_PROXY_PORT" 3>&1 1>&2 2>&3 3>&-)
if [[ $myCONF_PROXY_PORT =~ ^-?[0-9]+$ ]] && [ $myCONF_PROXY_PORT -gt 0 ] && [ $myCONF_PROXY_PORT -lt 65536 ]; then myPORTRESULT="true"; fi
2015-12-08 14:47:39 +00:00
done
sed -i.bak 's#d-i mirror/http/proxy.*#d-i mirror/http/proxy string http://'$myCONF_PROXY_IP':'$myCONF_PROXY_PORT'/#' $myTPOTSEED
2015-12-08 14:47:39 +00:00
break
else
myCONF_PROXY_IP=""
myCONF_PROXY_PORT=""
2015-12-08 14:47:39 +00:00
break
fi
done
# Let's ask the user for 802.1x data ...
while true;
do
dialog --backtitle "$myBACKTITLE" --title "[ Need 802.1x auth? ]" --yesno "\nDo you want to add a 802.1x host certificate?" 7 50
myCONF_PFX_USE=$?
if [ "$myCONF_PFX_USE" = "0" ]
2015-12-08 14:47:39 +00:00
then
myCONF_PFX_FILE=$(dialog --backtitle "$myBACKTITLE" --fselect "$myCONF_PFX_FILE" 15 50 3>&1 1>&2 2>&3 3>&-)
if [ -f "$myCONF_PFX_FILE" ]
2015-12-08 14:47:39 +00:00
then
cp $myCONF_PFX_FILE $myPFXFILE
2015-12-08 14:47:39 +00:00
dialog --backtitle "$myBACKTITLE" --title "[ Password protected? ]" --yesno "\nDoes the certificate need your password?" 7 50
myCONF_PFX_PW_USE=$?
if [ "$myCONF_PFX_PW_USE" = "0" ]
2015-12-08 14:47:39 +00:00
then
myCONF_PFX_PW=$(dialog --backtitle "$myBACKTITLE" --no-cancel --inputbox "Password?" 7 50 3>&1 1>&2 2>&3 3>&-)
else
myCONF_PFX_PW=""
2015-12-08 14:47:39 +00:00
fi
myCONF_PFX_HOST_ID=$(dialog --backtitle "$myBACKTITLE" --no-cancel --inputbox "Host ID?" 7 50 "$myCONF_PFX_HOST_ID" 3>&1 1>&2 2>&3 3>&-)
2015-12-08 14:47:39 +00:00
break
else
dialog --backtitle "$myBACKTITLE" --title "[ Try again! ]" --msgbox "\nThis is no regular file." 7 50;
fi
else
myCONF_PFX_FILE=""
myCONF_PFX_HOST_ID=""
myCONF_PFX_PW=""
2015-12-08 14:47:39 +00:00
break
fi
done
# Let's ask the user for a ntp server ...
while true;
do
dialog --backtitle "$myBACKTITLE" --title "[ NTP server? ]" --yesno "\nDo you want to configure a ntp server?" 7 50
myCONF_NTP_USE=$?
if [ "$myCONF_NTP_USE" = "0" ]
2015-12-08 14:47:39 +00:00
then
myIPRESULT="false"
while [ "$myIPRESULT" = "false" ];
do
myCONF_NTP_IP=$(dialog --backtitle "$myBACKTITLE" --no-cancel --title "NTP IP?" --inputbox "" 7 50 "$myCONF_NTP_IP" 3>&1 1>&2 2>&3 3>&-)
if valid_ip $myCONF_NTP_IP; then myIPRESULT="true"; fi
2015-12-08 14:47:39 +00:00
done
tee $myNTPCONFFILE <<EOF
2015-12-08 14:47:39 +00:00
driftfile /var/lib/ntp/ntp.drift
statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable
server $myCONF_NTP_IP
2015-12-08 14:47:39 +00:00
restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery
restrict 127.0.0.1
restrict ::1
EOF
break
else
myCONF_NTP_IP=""
2015-12-08 14:47:39 +00:00
break
fi
done
2014-11-28 17:02:20 +00:00
# Let's write the config file
2018-06-23 23:55:41 +00:00
if [ "$myCONF_PROXY_USE" == "0" ] || [ "$myCONF_PFX_USE" == "0" ] || [ "$myCONF_NTP_USE" == "0" ];
then
echo "# makeiso configuration file" > $myCONF_FILE
echo "myCONF_PROXY_USE=\"$myCONF_PROXY_USE\"" >> $myCONF_FILE
echo "myCONF_PROXY_IP=\"$myCONF_PROXY_IP\"" >> $myCONF_FILE
echo "myCONF_PROXY_PORT=\"$myCONF_PROXY_PORT\"" >> $myCONF_FILE
echo "myCONF_PFX_USE=\"$myCONF_PFX_USE\"" >> $myCONF_FILE
echo "myCONF_PFX_FILE=\"/root/installer/keys/8021x.pfx\"" >> $myCONF_FILE
echo "myCONF_PFX_PW_USE=\"$myCONF_PFX_PW_USE\"" >> $myCONF_FILE
echo "myCONF_PFX_PW=\"$myCONF_PFX_PW\"" >> $myCONF_FILE
echo "myCONF_PFX_HOST_ID=\"$myCONF_PFX_HOST_ID\"" >> $myCONF_FILE
echo "myCONF_NTP_USE=\"$myCONF_NTP_USE\"" >> $myCONF_FILE
echo "myCONF_NTP_IP=\"$myCONF_NTP_IP\"" >> $myCONF_FILE
echo "myCONF_NTP_CONF_FILE=\"/root/installer/ntp.conf\"" >> $myCONF_FILE
fi
2019-01-28 20:52:55 +00:00
# Let's download Debian Minimal ISO
if [ ! -f $myMINIISO ]
2015-12-08 14:47:39 +00:00
then
2019-01-28 20:52:55 +00:00
wget $myMINIISOLINK --progress=dot 2>&1 | awk '{print $7+0} fflush()' | dialog --backtitle "$myBACKTITLE" --title "[ Downloading Debian ... ]" --gauge "" 5 70;
echo 100 | dialog --backtitle "$myBACKTITLE" --title "[ Downloading Debian ... Done! ]" --gauge "" 5 70;
2015-12-08 14:47:39 +00:00
else
dialog --infobox "Using previously downloaded .iso ..." 3 50;
2014-11-28 17:02:20 +00:00
fi
# Let's loop mount it and copy all contents
2015-12-08 14:47:39 +00:00
mkdir -p $myTMP $myTPOTDIR
2019-01-28 20:52:55 +00:00
mount -o loop $myMINIISO $myTMP
rsync -a $myTMP/ $myTPOTDIR
2014-11-28 17:02:20 +00:00
umount $myTMP
# Let's modify initrd
gunzip $myTPOTDIR/initrd.gz
mkdir $myTPOTDIR/tmp
cd $myTPOTDIR/tmp
cpio --extract --make-directories --no-absolute-filenames < ../initrd
cd ..
rm initrd
cd ..
2014-11-28 17:02:20 +00:00
# Let's add the files for the automated install
mkdir -p $myTPOTDIR/tmp/opt/
cp iso/installer -R $myTPOTDIR/tmp/opt/
cp iso/isolinux/* $myTPOTDIR/
cp iso/preseed/tpot.seed $myTPOTDIR/tmp/preseed.cfg
# Let's create the new initrd
cd $myTPOTDIR/tmp
find . | cpio -H newc --create > ../initrd
cd ..
gzip initrd
rm -rf tmp
cd ..
2014-11-28 17:02:20 +00:00
# Let's create the new .iso
2015-12-08 14:47:39 +00:00
cd $myTPOTDIR
2019-05-02 16:59:36 +00:00
xorrisofs -gui -D -r -V "T-Pot" -cache-inodes -J -l -b isolinux.bin -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -o ../$myTPOTISO ../$myTPOTDIR 2>&1 | awk '{print $1+0} fflush()' | cut -f1 -d"." | dialog --backtitle "$myBACKTITLE" --title "[ Building T-Pot .iso ... ]" --gauge "" 5 70 0
2015-12-08 14:47:39 +00:00
echo 100 | dialog --backtitle "$myBACKTITLE" --title "[ Building T-Pot .iso ... Done! ]" --gauge "" 5 70
2014-11-28 17:02:20 +00:00
cd ..
2015-12-08 14:47:39 +00:00
isohybrid $myTPOTISO
sha256sum $myTPOTISO > tpot.sha256
2015-12-08 14:47:39 +00:00
# Let's write the image
while true;
do
dialog --backtitle "$myBACKTITLE" --yesno "\nWrite .iso to USB drive?" 7 50
myUSBCOPY=$?
if [ "$myUSBCOPY" = "0" ]
then
myTARGET=$(dialog --backtitle "$myBACKTITLE" --title "[ Select target device ... ]" --menu "" 16 40 10 $(lsblk -io NAME,SIZE -dnp) 3>&1 1>&2 2>&3 3>&-)
if [ "$myTARGET" != "" ]
then
dialog --backtitle "$myBACKTITLE" --yesno "\nWrite .iso to "$myTARGET"?" 7 50
myWRITE=$?
if [ "$myWRITE" = "0" ]
then
umount $myTARGET? 2>&1 || true
2016-03-08 14:34:37 +00:00
(pv -n "$myTPOTISO" | dd of="$myTARGET") 2>&1 | dialog --backtitle "$myBACKTITLE" --title "[ Writing .iso to target ... ]" --gauge "" 5 70 0
2015-12-08 14:47:39 +00:00
echo 100 | dialog --backtitle "$myBACKTITLE" --title "[ Writing .iso to target ... Done! ]" --gauge "" 5 70
udisksctl power-off -b $myTARGET 2>&1
2016-03-08 14:34:37 +00:00
break
2015-12-08 14:47:39 +00:00
fi
fi
else
break;
fi
done
2019-02-13 16:09:23 +00:00
dialog --clear
2014-11-28 17:02:20 +00:00
exit 0