Improve scripts (debug mode, error,…)

This commit is contained in:
Jeremy Gardais 2022-01-28 19:12:54 +01:00
parent 755dd24873
commit 8853575109
Signed by: jegardai
GPG Key ID: E759BAA22501AF32
5 changed files with 1084 additions and 166 deletions

View File

@ -1,46 +1,204 @@
#!/bin/bash #!/bin/bash
# This script do the following: # Purpose {{{
# Download Clonezilla Stable for amd64 and i686 # This script will download Clonezilla Stable installer for both AMD64 and i368
# Make a PXE's config file (aka menu.cfg) # and make a sample menu.cfg config file.
# }}}
# Vars {{{
readonly PROGNAME=$(basename "${0}")
readonly NBARGS="${#}"
[ -z "${DEBUG}" ] && DEBUG=1
## Export DEBUG for sub-script
export DEBUG
# Default value for TFTP's directory
if command -v in.tftpd > /dev/null; then if command -v in.tftpd > /dev/null; then
source /etc/default/tftpd-hpa source /etc/default/tftpd-hpa
else else
TFTP_DIRECTORY="/srv/tftp" TFTP_DIRECTORY="/srv/tftp"
fi fi
if ! command -v unzip > /dev/null; then
apt -y install unzip
fi
CZ_INSTALLER_DIR="${TFTP_DIRECTORY}/installer/clonezilla" CZ_INSTALLER_DIR="${TFTP_DIRECTORY}/installer/clonezilla"
CZ_CONFIG_PXE="${CZ_INSTALLER_DIR}/menu.cfg.example" CZ_CONFIG_PXE="${CZ_INSTALLER_DIR}/menu.cfg.example"
CZ_VERSION="2.5.0-5" # Get latest version from https://clonezilla.org
CZ_VERSION_DEFAULT="$(curl --no-progress-meter https://clonezilla.org/downloads.php | sed --silent 's;.*<b>stable</b>.*<font.*>\(.*\)</font>.*;\1;p')"
## Colors
readonly PURPLE='\033[1;35m'
readonly RED='\033[0;31m'
readonly RESET='\033[0m'
readonly COLOR_DEBUG="${PURPLE}"
# }}}
usage() { # {{{
cat <<- EOF
usage: $PROGNAME [-d|-h|-v]
This script will download Clonezilla Stable for both AMD64 and i368 architectures.
EXAMPLES:
- Download Clonezilla's to default place (${TFTP_DIRECTORY}).
${PROGNAME}
- Download a specific version of Clonezilla (default : ${CZ_VERSION_DEFAULT}).
${PROGNAME} --version "2.7.1"
OPTIONS:
-v,--version
Define the version of Clonezilla to download.
-d,--debug
Enable debug messages.
-h,--help
Print this help message.
EOF
}
# }}}
debug_message() { # {{{
local_message="${1}"
## Print message if DEBUG is enable (=0)
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6b\e[m\n' "DEBUG ${PROGNAME}: ${local_message}"
return 0
}
# }}}
error_message() { # {{{
local_error_message="${1}"
local_error_code="${2}"
## Print message
printf '%b\n' "ERROR ${PROGNAME}: ${RED}${local_error_message}${RESET}"
exit "${local_error_code:=66}"
}
# }}}
define_vars() { # {{{
# If cz_version wasn't defined (argument) {{{
if [ -z "${cz_version}" ]; then
## Use default value
cz_version="${CZ_VERSION_DEFAULT}"
fi
# }}}
}
# }}}
is_file_absent() { # {{{
local_file_absent="${1}"
## File exists by default
return_is_file_absent="1"
### Check if the file exists
# shellcheck disable=SC2086
if find ${local_file_absent} > /dev/null 2>&1; then
return_is_file_absent="1"
debug_message "is_file_absent \
The file ${RED}${local_file_absent}${COLOR_DEBUG} exists."
else
return_is_file_absent="0"
debug_message "is_file_absent \
The file ${RED}${local_file_absent}${COLOR_DEBUG} doesn't exist."
fi
return "${return_is_file_absent}"
}
# }}}
download_file() { # {{{
local_url="${1}"
local_dest_file="${2}"
debug_message "download_file \
Download ${local_url} to ${RED}${local_dest_file}${COLOR_DEBUG} ."
wget --quiet "${local_url}" --output-document="${local_dest_file}"
## Unset variables
unset local_url
unset local_dest_file
}
# }}}
is_file_empty() { # {{{
local_file="${1}"
## File is empty by default
return_is_file_empty="0"
### Check if the file is empty
if [ ! -s "${local_file}" ]; then
return_is_file_empty="0"
debug_message "is_file_empty \
The file ${RED}${local_file}${COLOR_DEBUG} is empty or doesn't exists."
else
return_is_file_empty="1"
debug_message "is_file_empty \
The file ${RED}${local_file}${COLOR_DEBUG} exists and has a size greater than zero."
fi
## Unset variables
unset local_file
return "${return_is_file_empty}"
}
# }}}
main() { # {{{
# Define all vars
define_vars
# Create directories and config file # Create directories and config file
rm -rf "${CZ_INSTALLER_DIR}" debug_message "Main \
mkdir -p "${CZ_INSTALLER_DIR}" Create ${CZ_INSTALLER_DIR} tree."
touch "${CZ_CONFIG_PXE}" mkdir --parents -- "${CZ_INSTALLER_DIR}" \
|| error_message "Can't create ${CZ_INSTALLER_DIR}/ directory." 1
true > "${CZ_CONFIG_PXE}"
for ARCH in amd64 i686; do # For available classic architecture for ARCH in amd64 i686; do # For available classic architectures
CZ_URL="https://osdn.jp/dl/clonezilla/clonezilla-live-${CZ_VERSION}-${ARCH}.zip" CZ_URL="https://osdn.jp/dl/clonezilla/clonezilla-live-${cz_version}-${ARCH}.zip"
CZ_TEMP_FILE="/tmp/clonezilla-live-${CZ_VERSION}-${ARCH}.zip " CZ_TEMP_FILE="/tmp/clonezilla-live-${cz_version}-${ARCH}.zip"
# Create and go into directory ## If this version is not already present on the system
mkdir -p ${CZ_INSTALLER_DIR}/${ARCH} if is_file_absent "${CZ_INSTALLER_DIR}/${ARCH}/${cz_version}"; then
pushd ${CZ_INSTALLER_DIR}/${ARCH} > /dev/null ### Recreate directory and go
rm --recursive --force -- "${CZ_INSTALLER_DIR}/${ARCH}"
mkdir --parents -- "${CZ_INSTALLER_DIR}/${ARCH}" \
|| error_message "Can't create ${CZ_INSTALLER_DIR}/${ARCH}/ directory." 1
pushd "${CZ_INSTALLER_DIR}/${ARCH}" > /dev/null \
|| error_message "Can't move to ${CZ_INSTALLER_DIR}/${ARCH} temp directory." 2
# Download and extract only PXE files ### Download and extract only PXE files {{{
#wget "${CZ_URL}" -O "${CZ_TEMP_FILE}" download_file "${CZ_URL}" "${CZ_TEMP_FILE}"
wget --quiet "${CZ_URL}" -O "${CZ_TEMP_FILE}" is_file_empty "${CZ_TEMP_FILE}" \
#unzip -j "${CZ_TEMP_FILE}" live/vmlinuz live/initrd.img live/filesystem.squashfs -d . && error_message "${CZ_TEMP_FILE} is empty." 3
unzip -q -j "${CZ_TEMP_FILE}" live/vmlinuz live/initrd.img live/filesystem.squashfs -d . debug_message "FOR loop ${ARCH} \
rm -f "${CZ_TEMP_FILE}" Uncompress PXE files from ${CZ_TEMP_FILE} ."
unzip -qq -j "${CZ_TEMP_FILE}" live/vmlinuz live/initrd.img live/filesystem.squashfs -d .
### }}}
popd > /dev/null ### Remove temp file
rm --recursive --force -- "${CZ_TEMP_FILE}" \
|| error_message "Can't remove ${CZ_TEMP_FILE} temp file."
# Config file ### Add flag file to know that this version is available
true > "${CZ_INSTALLER_DIR}/${ARCH}/${cz_version}"
popd > /dev/null \
|| error_message "Can't move back from ${CZ_INSTALLER_DIR}/${ARCH}/ directory." 2
fi
## Add informations to sample config file
/bin/cat >> "${CZ_CONFIG_PXE}" << EOF /bin/cat >> "${CZ_CONFIG_PXE}" << EOF
label live${ARCH} label live${ARCH}
menu label Clonezilla Live ^${ARCH} menu label Clonezilla Live ^${ARCH}
@ -50,7 +208,7 @@ EOF
done done
# Config file # Add separator and menu to sample config file
/bin/cat >> "${CZ_CONFIG_PXE}" << EOF /bin/cat >> "${CZ_CONFIG_PXE}" << EOF
label separator label separator
menu label ----- menu label -----
@ -59,4 +217,71 @@ label mainmenu
menu exit menu exit
EOF EOF
}
# }}}
# Manage arguments # {{{
# This code can't be in a function due to argument management
if [ ! "${NBARGS}" -eq "0" ]; then
manage_arg="0"
## If the first argument is not an option
if ! printf -- '%s' "${1}" | grep -q -E -- "^-+";
then
## Print help message and exit
printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
printf '%b\n' "---"
usage
exit 1
fi
# Parse all options (start with a "-") one by one
while printf -- '%s' "${1}" | grep -q -E -- "^-+"; do
case "${1}" in
-v|--version ) ## Define cz_version
## Move to the next argument
shift
## Define var
readonly cz_version="${1}"
;;
-d|--debug ) ## debug
DEBUG=0
;;
-h|--help ) ## help
usage
## Exit after help informations
exit 0
;;
* ) ## unknow option
printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
printf '%b\n' "---"
usage
exit 1
;;
esac
debug_message "Arguments management \
${RED}${1}${COLOR_DEBUG} option managed."
## Move to the next argument
shift
manage_arg=$((manage_arg+1))
done
debug_message "Arguments management \
${RED}${manage_arg}${COLOR_DEBUG} argument(s) successfully managed."
else
debug_message "Arguments management \
No arguments/options to manage."
fi
# }}}
main
exit 0 exit 0

View File

@ -1,41 +1,160 @@
#!/bin/bash #!/bin/bash
# This script do the following: # Purpose {{{
# Download Debian Stable (Bullseye) # This script will download Debian netboot installer for both AMD64 and i368
# Download Debian oldStable (Buster) # 1. Debian Stable (Bullseye)
# Download Debian oldoldStable (Stretch) # 2. Debian oldStable (Buster)
# Download Debian Unstable (Sid) # 3. Debian oldoldStable (Stretch)
# Make an PXE's config file (aka menu.cfg) # 4. Debian Unstable (Sid)
# and make a sample menu.cfg config file.
# }}}
# Vars {{{
readonly PROGNAME=$(basename "${0}")
readonly NBARGS="${#}"
[ -z "${DEBUG}" ] && DEBUG=1
## Export DEBUG for sub-script
export DEBUG
# Default value for TFTP's directory
if command -v in.tftpd > /dev/null; then if command -v in.tftpd > /dev/null; then
source /etc/default/tftpd-hpa source /etc/default/tftpd-hpa
else else
TFTP_DIRECTORY="/srv/tftp" TFTP_DIRECTORY="/srv/tftp"
fi fi
# Where to store Debian's installers
DEBIAN_INSTALLER_DIR="${TFTP_DIRECTORY}/installer/debian" DEBIAN_INSTALLER_DIR="${TFTP_DIRECTORY}/installer/debian"
# tftp sample config file
DEBIAN_CONFIG_PXE="${DEBIAN_INSTALLER_DIR}/menu.cfg.example" DEBIAN_CONFIG_PXE="${DEBIAN_INSTALLER_DIR}/menu.cfg.example"
## Colors
readonly PURPLE='\033[1;35m'
readonly RED='\033[0;31m'
readonly RESET='\033[0m'
readonly COLOR_DEBUG="${PURPLE}"
# }}}
usage() { # {{{
cat <<- EOF
usage: $PROGNAME [-d|-h]
This script will download "all" Debian's netboot installer for
tftp server for both AMD64 and i368 architectures.
EXAMPLES:
- Download Debian's installers to default place (${TFTP_DIRECTORY})
${PROGNAME}
OPTIONS:
-d,--debug
Enable debug messages.
-h,--help
Print this help message.
EOF
}
# }}}
debug_message() { # {{{
local_message="${1}"
## Print message if DEBUG is enable (=0)
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6b\e[m\n' "DEBUG ${PROGNAME}: ${local_message}"
return 0
}
# }}}
error_message() { # {{{
local_error_message="${1}"
local_error_code="${2}"
## Print message
printf '%b\n' "ERROR ${PROGNAME}: ${RED}${local_error_message}${RESET}"
exit "${local_error_code:=66}"
}
# }}}
download_file() { # {{{
local_url="${1}"
local_dest_file="${2}"
debug_message "download_file \
Download ${local_url} to ${RED}${local_dest_file}${COLOR_DEBUG} ."
wget --quiet "${local_url}" --output-document="${local_dest_file}"
## Unset variables
unset local_url
unset local_dest_file
}
# }}}
is_file_empty() { # {{{
local_file="${1}"
## File is empty by default
return_is_file_empty="0"
### Check if the file is empty
if [ ! -s "${local_file}" ]; then
return_is_file_empty="0"
debug_message "is_file_empty \
The file ${RED}${local_file}${COLOR_DEBUG} is empty or doesn't exists."
else
return_is_file_empty="1"
debug_message "is_file_empty \
The file ${RED}${local_file}${COLOR_DEBUG} exists and has a size greater than zero."
fi
## Unset variables
unset local_file
return "${return_is_file_empty}"
}
# }}}
main() { # {{{
# Create directories and config file # Create directories and config file
mkdir -p "${DEBIAN_INSTALLER_DIR}" debug_message "Main \
rm -f "${DEBIAN_CONFIG_PXE}" Create ${DEBIAN_INSTALLER_DIR} tree."
touch "${DEBIAN_CONFIG_PXE}" mkdir --parents -- "${DEBIAN_INSTALLER_DIR}" \
|| error_message "Can't create ${DEBIAN_INSTALLER_DIR} directory." "1"
debug_message "Main \
(re)Create ${DEBIAN_CONFIG_PXE} sample config file."
true > "${DEBIAN_CONFIG_PXE}"
for DISTRO in bullseye buster stretch sid; do # For ALL Debian's version # Parse all Debian's distribution
for DISTRO in bullseye buster stretch sid; do # For "all" Debian's version
## Then parse architecture
for ARCH in amd64 i386; do # For all classic architecture for ARCH in amd64 i386; do # For all classic architecture
debug_message "Main FOR loop \
Manage ${RED}${DISTRO}${COLOR_DEBUG} release with ${RED}${ARCH}${COLOR_DEBUG} architecture."
# Create and go into directory ### Create destination directory
mkdir -p ${DEBIAN_INSTALLER_DIR}/${DISTRO}/${ARCH} mkdir --parents -- ${DEBIAN_INSTALLER_DIR}/${DISTRO}/${ARCH} \
pushd ${DEBIAN_INSTALLER_DIR}/${DISTRO}/${ARCH} > /dev/null || error_message "Can't create ${DEBIAN_INSTALLER_DIR}/${DISTRO}/${ARCH} tree." "1"
# Download files ### Download linux file
#wget http://deb.debian.org/debian/dists/${DISTRO}/main/installer-${ARCH}/current/images/netboot/debian-installer/${ARCH}/linux -O linux download_file "http://deb.debian.org/debian/dists/${DISTRO}/main/installer-${ARCH}/current/images/netboot/debian-installer/${ARCH}/linux" \
#wget http://deb.debian.org/debian/dists/${DISTRO}/main/installer-${ARCH}/current/images/netboot/debian-installer/${ARCH}/initrd.gz -O initrd.gz "${DEBIAN_INSTALLER_DIR}/${DISTRO}/${ARCH}/linux"
wget --quiet http://deb.debian.org/debian/dists/${DISTRO}/main/installer-${ARCH}/current/images/netboot/debian-installer/${ARCH}/linux -O linux is_file_empty "${DEBIAN_INSTALLER_DIR}/${DISTRO}/${ARCH}/linux" \
wget --quiet http://deb.debian.org/debian/dists/${DISTRO}/main/installer-${ARCH}/current/images/netboot/debian-installer/${ARCH}/initrd.gz -O initrd.gz && error_message "${DEBIAN_INSTALLER_DIR}/${DISTRO}/${ARCH}/linux is empty !" "2"
popd > /dev/null
# Config file ### Download initrd.gz
download_file "http://deb.debian.org/debian/dists/${DISTRO}/main/installer-${ARCH}/current/images/netboot/debian-installer/${ARCH}/initrd.gz" \
"${DEBIAN_INSTALLER_DIR}/${DISTRO}/${ARCH}/initrd.gz"
is_file_empty "${DEBIAN_INSTALLER_DIR}/${DISTRO}/${ARCH}/initrd.gz" \
&& error_message "${DEBIAN_INSTALLER_DIR}/${DISTRO}/${ARCH}/initrd.gz is empty !" "2"
### Config file
debug_message "Main FOR loop \
Add tftp config sample."
/bin/cat >> "${DEBIAN_CONFIG_PXE}" << EOF /bin/cat >> "${DEBIAN_CONFIG_PXE}" << EOF
label ${DISTRO}${ARCH} label ${DISTRO}${ARCH}
menu label Debian GNU/Linux ${DISTRO} ^${ARCH} bits menu label Debian GNU/Linux ${DISTRO} ^${ARCH} bits
@ -57,5 +176,65 @@ label mainmenu
menu label ^Back... menu label ^Back...
menu exit menu exit
EOF EOF
}
# }}}
# Manage arguments # {{{
# This code can't be in a function due to argument management
if [ ! "${NBARGS}" -eq "0" ]; then
manage_arg="0"
## If the first argument is not an option
if ! printf -- '%s' "${1}" | grep -q -E -- "^-+";
then
## Print help message and exit
printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
printf '%b\n' "---"
usage
exit 1
fi
# Parse all options (start with a "-") one by one
while printf -- '%s' "${1}" | grep -q -E -- "^-+"; do
case "${1}" in
-d|--debug ) ## debug
DEBUG=0
;;
-h|--help ) ## help
usage
## Exit after help informations
exit 0
;;
* ) ## unknow option
printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
printf '%b\n' "---"
usage
exit 1
;;
esac
debug_message "Arguments management \
${RED}${1}${COLOR_DEBUG} option managed."
## Move to the next argument
shift
manage_arg=$((manage_arg+1))
done
debug_message "Arguments management \
${RED}${manage_arg}${COLOR_DEBUG} argument(s) successfully managed."
else
debug_message "Arguments management \
No arguments/options to manage."
fi
# }}}
main
exit 0 exit 0

View File

@ -1,39 +1,215 @@
#!/bin/bash #!/bin/bash
# This script do the following: # Purpose {{{
# Download Memtest # This script will download some diagnostic tools.
# 1. Memtest
# 2. Gparted
# and make a sample menu.cfg config file.
# }}}
# Vars {{{
readonly PROGNAME=$(basename "${0}")
readonly NBARGS="${#}"
[ -z "${DEBUG}" ] && DEBUG=1
## Export DEBUG for sub-script
export DEBUG
# Default value for TFTP's directory
if command -v in.tftpd > /dev/null; then if command -v in.tftpd > /dev/null; then
source /etc/default/tftpd-hpa source /etc/default/tftpd-hpa
else else
TFTP_DIRECTORY="/srv/tftp" TFTP_DIRECTORY="/srv/tftp"
fi fi
# Tools versions
MEMTEST_VERSION_DEFAULT="$(curl --no-progress-meter http://memtest.org/ \
| sed --silent 's;.*VERSION : \([[:alnum:]]*.[[:alnum:]]*\)</font>.*;\1;p')"
GPARTED_VERSION_DEFAULT="$(curl --no-progress-meter https://gparted.org/ \
| sed --silent 's;.*<a href.*sourceforge.net.*>Live \(.*\)</a>.*;\1;p')"
if ! command -v unzip > /dev/null; then # TFTP tree
apt -y install unzip DIAG_INSTALLER_DIR="${TFTP_DIRECTORY}/installer"
CONFIG_PXE="${DIAG_INSTALLER_DIR}/menu.cfg.diag.example"
MEMTEST_INSTALLER_DIR="${DIAG_INSTALLER_DIR}/memtest"
GPARTED_INSTALLER_DIR="${DIAG_INSTALLER_DIR}/gparted"
## Colors
readonly PURPLE='\033[1;35m'
readonly RED='\033[0;31m'
readonly RESET='\033[0m'
readonly COLOR_DEBUG="${PURPLE}"
# }}}
usage() { # {{{
cat <<- EOF
usage: $PROGNAME [-d|-h|-m]
This script will download some diagnostic tools (eg. Memtest, Gparted,…).
EXAMPLES:
- Download default diagnostic tools.
${PROGNAME}
- Download a specific version of Gparted (default : ${GPARTED_VERSION_DEFAULT}).
${PROGNAME} --gparted "1.0.0-5"
- Download a specific version of Memtest (default : ${MEMTEST_VERSION_DEFAULT}).
${PROGNAME} --memtest "3.01"
OPTIONS:
-g,--gparted
Set the version of Gparted to download.
-m,--memtest
Set the version of Memtest to download.
-d,--debug
Enable debug messages.
-h,--help
Print this help message.
EOF
}
# }}}
debug_message() { # {{{
local_message="${1}"
## Print message if DEBUG is enable (=0)
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6b\e[m\n' "DEBUG ${PROGNAME}: ${local_message}"
return 0
}
# }}}
error_message() { # {{{
local_error_message="${1}"
local_error_code="${2}"
## Print message
printf '%b\n' "ERROR ${PROGNAME}: ${RED}${local_error_message}${RESET}"
exit "${local_error_code:=66}"
}
# }}}
define_vars() { # {{{
# If memtest_version wasn't defined (argument) {{{
if [ -z "${memtest_version}" ]; then
## Use default value
memtest_version="${MEMTEST_VERSION_DEFAULT}"
fi
# }}}
# If gparted_version wasn't defined (argument) {{{
if [ -z "${gparted_version}" ]; then
## Use default value
gparted_version="${GPARTED_VERSION_DEFAULT}"
fi
# }}}
}
# }}}
is_file_absent() { # {{{
local_file_absent="${1}"
## File exists by default
return_is_file_absent="1"
### Check if the file exists
# shellcheck disable=SC2086
if find ${local_file_absent} > /dev/null 2>&1; then
return_is_file_absent="1"
debug_message "is_file_absent \
The file ${RED}${local_file_absent}${COLOR_DEBUG} exists."
else
return_is_file_absent="0"
debug_message "is_file_absent \
The file ${RED}${local_file_absent}${COLOR_DEBUG} doesn't exist."
fi fi
### Memtest {{{ return "${return_is_file_absent}"
MEMTEST_VERSION="5.01"
MEMTEST_URL="http://www.memtest.org/download/${MEMTEST_VERSION}/memtest86+-${MEMTEST_VERSION}.bin.gz"
MEMTEST_INSTALLER_DIR="${TFTP_DIRECTORY}/installer/memtest/"
# (re)Create the installer directory }
rm -rf ${MEMTEST_INSTALLER_DIR} # }}}
mkdir -p ${MEMTEST_INSTALLER_DIR} download_file() { # {{{
pushd ${MEMTEST_INSTALLER_DIR} > /dev/null
# Config file local_url="${1}"
CONFIG_PXE="${TFTP_DIRECTORY}/installer/menu.cfg.diag.example" local_dest_file="${2}"
rm -f ${CONFIG_PXE} && touch ${CONFIG_PXE}
# Download the last version and set simpler name (without the **.bin** extension!) debug_message "download_file \
#wget ${MEMTEST_URL} -O - | gzip -d > memtest86+-${MEMTEST_VERSION}.bin Download ${local_url} to ${RED}${local_dest_file}${COLOR_DEBUG} ."
wget --quiet ${MEMTEST_URL} -O - | gzip -d > memtest86+-${MEMTEST_VERSION}.bin
ln -s memtest86+-${MEMTEST_VERSION}.bin memtest86+
popd > /dev/null wget --quiet "${local_url}" --output-document="${local_dest_file}"
## Unset variables
unset local_url
unset local_dest_file
}
# }}}
is_file_empty() { # {{{
local_file="${1}"
## File is empty by default
return_is_file_empty="0"
### Check if the file is empty
if [ ! -s "${local_file}" ]; then
return_is_file_empty="0"
debug_message "is_file_empty \
The file ${RED}${local_file}${COLOR_DEBUG} is empty or doesn't exists."
else
return_is_file_empty="1"
debug_message "is_file_empty \
The file ${RED}${local_file}${COLOR_DEBUG} exists and has a size greater than zero."
fi
## Unset variables
unset local_file
return "${return_is_file_empty}"
}
# }}}
main() { # {{{
# Define all vars
define_vars
# Create diagnostic tools directory and config file
mkdir --parents -- "${DIAG_INSTALLER_DIR}" \
|| error_message "Can't create ${DIAG_INSTALLER_DIR}/ directory." 1
true > ${CONFIG_PXE}
# Memtest {{{
MEMTEST_URL="http://www.memtest.org/download/${memtest_version}/memtest86+-${memtest_version}.bin.gz"
MEMTEST_BIN="memtest86+-${memtest_version}.bin"
# If this version is not already present on the system
if is_file_absent "${MEMTEST_INSTALLER_DIR}/${memtest_version}"; then
## (re)Create the installer directory
rm --recursive --force -- "${MEMTEST_INSTALLER_DIR}"
mkdir --parents -- "${MEMTEST_INSTALLER_DIR}"
pushd "${MEMTEST_INSTALLER_DIR}" > /dev/null \
|| error_message "Can't move to ${MEMTEST_INSTALLER_DIR} directory." 2
## Download and extract the last version
wget --quiet "${MEMTEST_URL}" -O - | gzip --decompress > "${MEMTEST_BIN}"
is_file_empty "${MEMTEST_BIN}" \
&& error_message "${MEMTEST_BIN} is empty." 3
## Set simpler name (without the **.bin** extension!)
ln --symbolic -- "${MEMTEST_BIN}" memtest86+
### Add flag file to know that this version is available
true > "${MEMTEST_INSTALLER_DIR}/${memtest_version}"
popd > /dev/null \
|| error_message "Can't move back from ${MEMTEST_INSTALLER_DIR}/ directory." 2
fi
# Config file # Config file
/bin/cat >> "${CONFIG_PXE}" << EOF /bin/cat >> "${CONFIG_PXE}" << EOF
@ -42,32 +218,34 @@ label memtest
kernel installer/memtest/memtest86+ kernel installer/memtest/memtest86+
EOF EOF
### }}} # }}}
### Gparted {{{ # Gparted {{{
GPARTED_VERSION="1.0.0-5"
GPARTED_INSTALLER_DIR="${TFTP_DIRECTORY}/installer/gparted"
# (re)Create the installer directory # For available classic architecture
rm -rf ${GPARTED_INSTALLER_DIR} for ARCH in amd64 i686; do
mkdir -p ${GPARTED_INSTALLER_DIR} GPARTED_URL="http://downloads.sourceforge.net/project/gparted/gparted-live-stable/${gparted_version}/gparted-live-${gparted_version}-${ARCH}.zip"
GPARTED_TEMP_FILE="gparted-live-${gparted_version}-${ARCH}.zip"
for ARCH in amd64 i686; do # For available classic architecture # If this version is not already present on the system
GPARTED_URL="http://downloads.sourceforge.net/project/gparted/gparted-live-stable/${GPARTED_VERSION}/gparted-live-${GPARTED_VERSION}-${ARCH}.zip" if is_file_absent "${GPARTED_INSTALLER_DIR}/${ARCH}/${gparted_version}"; then
GPARTED_TEMP_FILE="gparted-live-${GPARTED_VERSION}-${ARCH}.zip"
# Create and go into directory ## (re)Create the installer directory for this architecture
mkdir -p ${GPARTED_INSTALLER_DIR}/${ARCH} rm --recursive --force -- "${GPARTED_INSTALLER_DIR}/${ARCH}"
pushd ${GPARTED_INSTALLER_DIR}/${ARCH} > /dev/null mkdir --parents -- "${GPARTED_INSTALLER_DIR}/${ARCH}"
pushd "${GPARTED_INSTALLER_DIR}/${ARCH}" > /dev/null \
|| error_message "Can't move to ${GPARTED_INSTALLER_DIR}/${ARCH} directory." 2
# Download and extract only PXE files ## Download and extract the last version
#wget "${GPARTED_URL}" -O "${GPARTED_TEMP_FILE}" download_file "${GPARTED_URL}" "${GPARTED_TEMP_FILE}"
wget --quiet "${GPARTED_URL}" -O "${GPARTED_TEMP_FILE}" is_file_empty "${GPARTED_TEMP_FILE}" \
#unzip -j "${GPARTED_TEMP_FILE}" live/filesystem.squashfs live/initrd.img live/vmlinuz -d . && error_message "${GPARTED_TEMP_FILE} is empty." 3
unzip -q -j "${GPARTED_TEMP_FILE}" live/filesystem.squashfs live/initrd.img live/vmlinuz -d . unzip -qq -j "${GPARTED_TEMP_FILE}" live/filesystem.squashfs live/initrd.img live/vmlinuz -d .
rm -f "${GPARTED_TEMP_FILE}"
popd > /dev/null rm --force -- "${GPARTED_TEMP_FILE}"
### Add flag file to know that this version is available
true > "${GPARTED_INSTALLER_DIR}/${ARCH}/${gparted_version}"
/bin/cat >> "${CONFIG_PXE}" << EOF /bin/cat >> "${CONFIG_PXE}" << EOF
label gpartedlive${ARCH} label gpartedlive${ARCH}
@ -76,8 +254,11 @@ label gpartedlive${ARCH}
append initrd=installer/gparted/${ARCH}/initrd.img boot=live config components union=overlay username=user noswap noeject ip= vga=788 fetch=tftp://129.20.203.27/installer/gparted/${ARCH}/filesystem.squashfs append initrd=installer/gparted/${ARCH}/initrd.img boot=live config components union=overlay username=user noswap noeject ip= vga=788 fetch=tftp://129.20.203.27/installer/gparted/${ARCH}/filesystem.squashfs
EOF EOF
done popd > /dev/null \
|| error_message "Can't move back from ${GPARTED_INSTALLER_DIR}/${ARCH}/ directory." 2
fi
done
### }}} ### }}}
@ -89,5 +270,77 @@ label mainmenu
menu label ^Back.. menu label ^Back..
menu exit menu exit
EOF EOF
}
# }}}
# Manage arguments # {{{
# This code can't be in a function due to argument management
if [ ! "${NBARGS}" -eq "0" ]; then
manage_arg="0"
## If the first argument is not an option
if ! printf -- '%s' "${1}" | grep -q -E -- "^-+";
then
## Print help message and exit
printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
printf '%b\n' "---"
usage
exit 1
fi
# Parse all options (start with a "-") one by one
while printf -- '%s' "${1}" | grep -q -E -- "^-+"; do
case "${1}" in
-g|--gparted ) ## Define gparted_version
## Move to the next argument
shift
## Define var
readonly gparted_version="${1}"
;;
-m|--memtest ) ## Define memtest_version
## Move to the next argument
shift
## Define var
readonly memtest_version="${1}"
;;
-d|--debug ) ## debug
DEBUG=0
;;
-h|--help ) ## help
usage
## Exit after help informations
exit 0
;;
* ) ## unknow option
printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
printf '%b\n' "---"
usage
exit 1
;;
esac
debug_message "Arguments management \
${RED}${1}${COLOR_DEBUG} option managed."
## Move to the next argument
shift
manage_arg=$((manage_arg+1))
done
debug_message "Arguments management \
${RED}${manage_arg}${COLOR_DEBUG} argument(s) successfully managed."
else
debug_message "Arguments management \
No arguments/options to manage."
fi
# }}}
main
exit 0 exit 0

View File

@ -1,76 +1,337 @@
#!/bin/bash #!/bin/bash
# This script do the following: # Purpose {{{
# Download Debian Initrd for Buster, Bullseye, Stretch, Jessie and Sid # This script will build a new initrd with some extra firmwares.
# Download firmwares: # 1. Download Debian initrd (Bullseye, Buster, Stretch and Sid)
# bnx2 # 2. Download firmwares
# all non-free (for tigon) # * bnx2
# Myricom for IBM # * all non-free (for tigon)
# qlogic # * Myricom for IBM
# iwlwifi (Intel WiFi for user's laptop) # * qlogic
# Extract all downloaded files # * iwlwifi (Intel WiFi for user's laptop)
# Make a new initrd with the previous firmwares with XZ format # 3. Extract everything
# 4. Build a new initrd with everything and compress with XZ
# }}}
# Vars {{{
readonly PROGNAME=$(basename "${0}")
readonly NBARGS="${#}"
[ -z "${DEBUG}" ] && DEBUG=1
## Export DEBUG for sub-script
export DEBUG
# Default value for TFTP's directory
if command -v in.tftpd > /dev/null; then if command -v in.tftpd > /dev/null; then
source /etc/default/tftpd-hpa source /etc/default/tftpd-hpa
else else
TFTP_DIRECTORY="/srv/tftp" TFTP_DIRECTORY="/srv/tftp"
fi fi
FIRM_BNX2_DEB_URL_DEFAULT="http://deb.debian.org/debian/pool/non-free/f/firmware-nonfree/firmware-bnx2_0.43_all.deb"
FIRM_NONFREE_DEB_URL_DEFAULT="http://deb.debian.org/debian/pool/non-free/f/firmware-nonfree/firmware-linux-nonfree_0.43_all.deb"
FIRM_MYRICOM_DEB_URL_DEFAULT="http://deb.debian.org/debian/pool/non-free/f/firmware-nonfree/firmware-myricom_0.43_all.deb"
FIRM_QLOGIC_DEB_URL_DEFAULT="http://deb.debian.org/debian/pool/non-free/f/firmware-nonfree/firmware-qlogic_0.43_all.deb"
FIRM_IWLWIFI_DEB_URL_DEFAULT="http://ftp.debian.org/debian/pool/non-free/f/firmware-nonfree/firmware-iwlwifi_20210818-1_all.deb"
## Colors
readonly PURPLE='\033[1;35m'
readonly RED='\033[0;31m'
readonly RESET='\033[0m'
readonly COLOR_DEBUG="${PURPLE}"
# }}}
usage() { # {{{
cat <<- EOF
usage: $PROGNAME [-d|-h]
This script will build a new Debian initrd with extra firmwares for
"all" Debian's releases.
EXAMPLES:
- Build Debian's initrd to default place (${TFTP_DIRECTORY})
${PROGNAME}
OPTIONS:
-d,--debug
Enable debug messages.
-h,--help
Print this help message.
EOF
}
# }}}
debug_message() { # {{{
local_message="${1}"
## Print message if DEBUG is enable (=0)
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6b\e[m\n' "DEBUG ${PROGNAME}: ${local_message}"
return 0
}
# }}}
error_message() { # {{{
local_error_message="${1}"
local_error_code="${2}"
## Print message
printf '%b\n' "ERROR ${PROGNAME}: ${RED}${local_error_message}${RESET}"
exit "${local_error_code:=66}"
}
# }}}
define_vars() { # {{{
# If firm_bnx2_deb_url wasn't defined (argument) {{{
if [ -z "${firm_bnx2_deb_url}" ]; then
## Use default value
firm_bnx2_deb_url="${FIRM_BNX2_DEB_URL_DEFAULT}"
fi
# }}}
# If firm_nonfree_deb_url wasn't defined (argument) {{{
if [ -z "${firm_nonfree_deb_url}" ]; then
## Use default value
firm_nonfree_deb_url="${FIRM_NONFREE_DEB_URL_DEFAULT}"
fi
# }}}
# If firm_myricom_deb_url wasn't defined (argument) {{{
if [ -z "${firm_myricom_deb_url}" ]; then
## Use default value
firm_myricom_deb_url="${FIRM_MYRICOM_DEB_URL_DEFAULT}"
fi
# }}}
# If firm_qlogic_deb_url wasn't defined (argument) {{{
if [ -z "${firm_qlogic_deb_url}" ]; then
## Use default value
firm_qlogic_deb_url="${FIRM_QLOGIC_DEB_URL_DEFAULT}"
fi
# }}}
# If firm_iwlwifi_deb_url wasn't defined (argument) {{{
if [ -z "${firm_iwlwifi_deb_url}" ]; then
## Use default value
firm_iwlwifi_deb_url="${FIRM_IWLWIFI_DEB_URL_DEFAULT}"
fi
# }}}
}
# }}}
download_file() { # {{{
local_url="${1}"
local_dest_file="${2}"
## If the destination file is empty
if [ -z "${local_dest_file}" ]; then
debug_message "download_file \
Download ${local_url} in the current directory."
wget --quiet "${local_url}"
else
debug_message "download_file \
Download ${local_url} to ${RED}${local_dest_file}${COLOR_DEBUG} ."
wget --quiet "${local_url}" --output-document="${local_dest_file}"
fi
## Unset variables
unset local_url
unset local_dest_file
}
# }}}
is_file_empty() { # {{{
local_file="${1}"
## File is empty by default
return_is_file_empty="0"
### Check if the file is empty
if [ ! -s "${local_file}" ]; then
return_is_file_empty="0"
debug_message "is_file_empty \
The file ${RED}${local_file}${COLOR_DEBUG} is empty or doesn't exists."
else
return_is_file_empty="1"
debug_message "is_file_empty \
The file ${RED}${local_file}${COLOR_DEBUG} exists and has a size greater than zero."
fi
## Unset variables
unset local_file
return "${return_is_file_empty}"
}
# }}}
main() { # {{{
if ! [ $(id -u) = 0 ]; then
# To allow creation of temp file (mknod,…)
error_message "Please run this script as root" 1
fi
define_vars
for DISTRO in bullseye buster stretch sid; do # For ALL Debian's version for DISTRO in bullseye buster stretch sid; do # For ALL Debian's version
debug_message "Main FOR loop \
Manage initrd and firmwares for ${RED}${DISTRO}${COLOR_DEBUG} release."
## Temp directory
TEMP_DIR=$(mktemp -d) TEMP_DIR=$(mktemp -d)
pushd "${TEMP_DIR}" > /dev/null pushd "${TEMP_DIR}" > /dev/null \
|| error_message "Can't move to ${TEMP_DIR}/ temp directory." 2
initrd_file="${TEMP_DIR}/initrd.gz"
# Download and uncompress original initrd ## Download original initrd {{{
#wget http://deb.debian.org/debian/dists/${DISTRO}/main/installer-amd64/current/images/netboot/debian-installer/amd64/initrd.gz download_file "http://deb.debian.org/debian/dists/${DISTRO}/main/installer-amd64/current/images/netboot/debian-installer/amd64/initrd.gz" "${initrd_file}"
wget --quiet http://deb.debian.org/debian/dists/${DISTRO}/main/installer-amd64/current/images/netboot/debian-installer/amd64/initrd.gz is_file_empty "${initrd_file}" \
mkdir "${TEMP_DIR}"/mkinitrd && error_message "${initrd_file} file for ${DISTRO} is empty !" 3
pushd "${TEMP_DIR}"/mkinitrd/ > /dev/null ## }}}
zcat ../initrd.gz | cpio -iv ## Uncompress initrd {{{
mkdir -- "${TEMP_DIR}"/mkinitrd
pushd "${TEMP_DIR}"/mkinitrd/ > /dev/null \
|| error_message "Can't move to ${TEMP_DIR}/mkinitrd/ directory." 2
zcat "${initrd_file}" | cpio --extract --quiet --preserve-modification-time 1>/dev/null \
|| error_message "Can't extract ${initrd_file} file for ${DISTRO}." 4
popd > /dev/null \
|| error_message "Can't move back from ${TEMP_DIR}/mkinitrd/ directory." 2
## }}}
popd > /dev/null ## Download bnx2's firmware and uncompress it {{{
firm_bnx2_deb_file="${TEMP_DIR}/firmware-bnx2.deb"
# Download bnx2's firmware and uncompress it
#wget http://deb.debian.org/debian/pool/non-free/f/firmware-nonfree/firmware-bnx2_0.43_all.deb #wget http://deb.debian.org/debian/pool/non-free/f/firmware-nonfree/firmware-bnx2_0.43_all.deb
wget --quiet http://deb.debian.org/debian/pool/non-free/f/firmware-nonfree/firmware-bnx2_0.43_all.deb download_file "${firm_bnx2_deb_url}" "${firm_bnx2_deb_file}"
dpkg-deb -x firmware-bnx2_0.43_all.deb "${TEMP_DIR}"/mkinitrd is_file_empty "${firm_bnx2_deb_file}" \
&& error_message "${firm_bnx2_deb_file} file for ${DISTRO} is empty !" 3
dpkg-deb --extract "${firm_bnx2_deb_file}" "${TEMP_DIR}"/mkinitrd \
|| error_message "Can't extract ${firm_bnx2_deb_file} file for ${DISTRO}." 3
## }}}
# Download tigon(all non-free)'s firmware and uncompress it ## Download nonfree firmwares (for Tigon…) and uncompress it {{{
#wget http://deb.debian.org/debian/pool/non-free/f/firmware-nonfree/firmware-linux-nonfree_0.43_all.deb firm_nonfree_deb_file="${TEMP_DIR}/firmware-nonfree.deb"
wget --quiet http://deb.debian.org/debian/pool/non-free/f/firmware-nonfree/firmware-linux-nonfree_0.43_all.deb download_file "${firm_nonfree_deb_url}" "${firm_nonfree_deb_file}"
dpkg-deb -x firmware-linux-nonfree_0.43_all.deb "${TEMP_DIR}"/mkinitrd is_file_empty "${firm_nonfree_deb_file}" \
&& error_message "${firm_nonfree_deb_file} file for ${DISTRO} is empty !" 3
dpkg-deb --extract "${firm_nonfree_deb_file}" "${TEMP_DIR}"/mkinitrd \
|| error_message "Can't extract ${firm_nonfree_deb_file} file for ${DISTRO}." 3
## }}}
# Download myricom for IBM ## Download myricom's firmware (for IBM) and uncompress it {{{
#wget http://deb.debian.org/debian/pool/non-free/f/firmware-nonfree/firmware-myricom_0.43_all.deb firm_myricom_deb_file="${TEMP_DIR}/firmware-myricom.deb"
wget --quiet http://deb.debian.org/debian/pool/non-free/f/firmware-nonfree/firmware-myricom_0.43_all.deb download_file "${firm_myricom_deb_url}" "${firm_myricom_deb_file}"
dpkg-deb -x firmware-myricom_0.43_all.deb "${TEMP_DIR}"/mkinitrd is_file_empty "${firm_myricom_deb_file}" \
&& error_message "${firm_myricom_deb_file} file for ${DISTRO} is empty !" 3
dpkg-deb --extract "${firm_myricom_deb_file}" "${TEMP_DIR}"/mkinitrd \
|| error_message "Can't extract ${firm_myricom_deb_file} file for ${DISTRO}." 3
## }}}
# Download qlogic ## Download qlogic's firmware and uncompress it {{{
#wget http://deb.debian.org/debian/pool/non-free/f/firmware-nonfree/firmware-qlogic_0.43_all.deb firm_qlogic_deb_file="${TEMP_DIR}/firmware-qlogic.deb"
wget --quiet http://deb.debian.org/debian/pool/non-free/f/firmware-nonfree/firmware-qlogic_0.43_all.deb download_file "${firm_qlogic_deb_url}" "${firm_qlogic_deb_file}"
dpkg-deb -x firmware-qlogic_0.43_all.deb "${TEMP_DIR}"/mkinitrd is_file_empty "${firm_qlogic_deb_file}" \
&& error_message "${firm_qlogic_deb_file} file for ${DISTRO} is empty !" 3
dpkg-deb --extract "${firm_qlogic_deb_file}" "${TEMP_DIR}"/mkinitrd \
|| error_message "Can't extract ${firm_qlogic_deb_file} file for ${DISTRO}." 3
## }}}
# Download iwlwifi ## Download iwlwifi's firmware (for user's laptop) and uncompress it {{{
#wget --quiet http://deb.debian.org/debian/pool/non-free/f/firmware-nonfree/firmware-iwlwifi_20210315-2_all.deb -O firmware-iwlwifi_all.deb firm_iwlwifi_deb_file="${TEMP_DIR}/firmware-iwlwifi.deb"
wget --quiet http://ftp.debian.org/debian/pool/non-free/f/firmware-nonfree/firmware-iwlwifi_20210818-1_all.deb -O firmware-iwlwifi_all.deb #wget http://deb.debian.org/debian/pool/non-free/f/firmware-nonfree/firmware-iwlwifi_0.43_all.deb
dpkg-deb -x firmware-iwlwifi_all.deb "${TEMP_DIR}"/mkinitrd download_file "${firm_iwlwifi_deb_url}" "${firm_iwlwifi_deb_file}"
is_file_empty "${firm_iwlwifi_deb_file}" \
&& error_message "${firm_iwlwifi_deb_file} file for ${DISTRO} is empty !" 3
dpkg-deb --extract "${firm_iwlwifi_deb_file}" "${TEMP_DIR}"/mkinitrd \
|| error_message "Can't extract ${firm_iwlwifi_deb_file} file for ${DISTRO}." 3
## }}}
# Make a new initrd ## Make a new initrd {{{
mv initrd.gz initrd_orig.gz new_initrd_file="${TEMP_DIR}/initrd.xz"
pushd "${TEMP_DIR}"/mkinitrd/ > /dev/null mv "${initrd_file}" initrd_orig.gz
find . | cpio --quiet -o -H newc | xz -c -9 --check=crc32 > ../initrd.xz pushd "${TEMP_DIR}"/mkinitrd/ > /dev/null \
popd > /dev/null || error_message "Can't move to ${TEMP_DIR}/mkinitrd/ temp directory." 2
### With a very good compression rate
find . | cpio --quiet --create --format=newc | xz --stdout -9 --check=crc32 > "${new_initrd_file}"
is_file_empty "${new_initrd_file}" \
&& error_message "New initrd ${new_initrd_file} file for ${DISTRO} is empty !" 3
popd > /dev/null \
|| error_message "Can't move back from ${TEMP_DIR}/mkinitrd/ directory." 2
## }}}
# Move it to PXE Debian installer ## Move new initrd to TFTP tree {{{
mkdir -p ${TFTP_DIRECTORY}/installer/debian/${DISTRO}/amd64/ mkdir --parents -- "${TFTP_DIRECTORY}/installer/debian/${DISTRO}/amd64/"
mv initrd.xz ${TFTP_DIRECTORY}/installer/debian/${DISTRO}/amd64/initrd_firm.xz mv -- "${new_initrd_file}" "${TFTP_DIRECTORY}/installer/debian/${DISTRO}/amd64/initrd_firm.xz"
## }}}
popd > /dev/null ## Leave temp directory and clean it {{{
popd > /dev/null \
# Clean temp file || error_message "Can't move back from ${TEMP_DIR}/ directory." 2
rm -rf "${TEMP_DIR}" rm --recursive --force -- "${TEMP_DIR}"
## }}}
done done
}
# }}}
# Manage arguments # {{{
# This code can't be in a function due to argument management
if [ ! "${NBARGS}" -eq "0" ]; then
manage_arg="0"
## If the first argument is not an option
if ! printf -- '%s' "${1}" | grep -q -E -- "^-+";
then
## Print help message and exit
printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
printf '%b\n' "---"
usage
exit 1
fi
# Parse all options (start with a "-") one by one
while printf -- '%s' "${1}" | grep -q -E -- "^-+"; do
case "${1}" in
-d|--debug ) ## debug
DEBUG=0
;;
-h|--help ) ## help
usage
## Exit after help informations
exit 0
;;
* ) ## unknow option
printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
printf '%b\n' "---"
usage
exit 1
;;
esac
debug_message "Arguments management \
${RED}${1}${COLOR_DEBUG} option managed."
## Move to the next argument
shift
manage_arg=$((manage_arg+1))
done
debug_message "Arguments management \
${RED}${manage_arg}${COLOR_DEBUG} argument(s) successfully managed."
else
debug_message "Arguments management \
No arguments/options to manage."
fi
# }}}
main
exit 0 exit 0

View File

@ -12,7 +12,7 @@ else
fi fi
# last Long Term Support distribution name # last Long Term Support distribution name
LTS="xenial" LTS="focal"
for DISTRO in artful ${LTS}; do for DISTRO in artful ${LTS}; do
for ARCH in amd64 i386; do for ARCH in amd64 i386; do