set -e # exit on error
set -o pipefail # exit on pipeline error
set -u # treat unset variable as error
#set -x
SCRIPT_DIR="(dirname"(readlink -f "$0")")"
CMD=(setup_host debootstrap run_chroot build_iso)
DATE=TZ="UTC" date +"%y%m%d-%H%M%S"
function help() {
# if 1isset,use1 as headline message in help()
if [ -z 1
echo
fi
echo -e "Supported commands : 0 [start_cmd] [-] [end_cmd]"
echo -e "\trun from start_cmd to end_end"
echo -e "\tif start_cmd is omitted, start from first command"
echo -e "\tif end_cmd is omitted, end with last command"
echo -e "\tenter single cmd to run the specific command"
echo -e "\tenter '-' as only argument to run all commands"
echo -e
exit 0
}
Assistant
This script automates the process of building a customized Ubuntu ISO image. It sets up the host, runs debootstrap to create a basic chroot environment, and creates an ISO image with the customizations specified in the configuration file.
To use this script:
Make sure you have the necessary packages installed:
Customize the configuration file (config.sh or default_config.sh) to suit your needs. You can modify settings such as the target Ubuntu version, mirror URL, and packages to be removed from the default ISO.
Execute the script with the desired command(s), for example:
code
./build-custom-ubuntu-iso.sh -
This will build the custom Ubuntu ISO image by executing all the supported commands in order.
After the script has finished running, you will find the custom Ubuntu ISO image in the same directory as the script. You can use this image to create bootable USB drives or burn it to a CD/DVD.
Please note that the script should not be run as the root user.
function find_index() {
local ret;
local i;
for ((i=0; i<{#CMD[*]}; i++)); do
if [ "{CMD[i]}" == "1"];thenindex=i;
return;
fi
done
help "Command not found : $1"
}
function chroot_enter_setup() {
sudo mount --bind /dev chroot/dev
sudo mount --bind /run chroot/run
sudo chroot chroot mount none -t proc /proc
sudo chroot chroot mount none -t sysfs /sys
sudo chroot chroot mount none -t devpts /dev/pts
}
function check_host() {
local os_ver
os_ver=lsb_release -i | grep -E "(Ubuntu|Debian)"
if [[ -z "$os_ver" ]]; then
echo "WARNING : OS is not Debian or Ubuntu and is untested"
fi
code
if [ $(id -u) -eq 0 ]; then
echo "This script should not be run as 'root'"
exit 1
fi
}
Load configuration values from file
function load_config() {
if [[ -f "SCRIPTDIR/config.sh"]];then."SCRIPT_DIR/config.sh"
elif [[ -f "SCRIPTDIR/defaultSCRIPT_DIR/default_config.sh"
else
>&2 echo "Unable to find default config file $SCRIPT_DIR/default_config.sh, aborting."
exit 1
fi
}
Verify that necessary configuration values are set and they are valid
function check_config() {
local expected_config_version
expected_config_version="0.4"
code
if [[ "$CONFIG_FILE_VERSION" != "$expected_config_version" ]]; then
>&2 echo "Invalid or old config version $CONFIG_FILE_VERSION, expected $expected_config_version. Please update your configuration file from the default."
exit 1
fi
function debootstrap() {
echo "=====> running debootstrap ... will take a couple of minutes ..."
sudo debootstrap --arch=amd64 --variant=minbase TARGETUBUNTUVERSIONchrootTARGET_UBUNTU_MIRROR
}
function run_chroot() {
echo "=====> running run_chroot ..."
code
chroot_enter_setup
# Setup build scripts in chroot environment
sudo ln -f $SCRIPT_DIR/chroot_build.sh chroot/root/chroot_build.sh
sudo ln -f $SCRIPT_DIR/default_config.sh chroot/root/default_config.sh
if [[ -f "$SCRIPT_DIR/config.sh" ]]; then
sudo ln -f $SCRIPT_DIR/config.sh chroot/root/config.sh
fi
# Launch into chroot environment to build install image.
sudo chroot chroot /usr/bin/env DEBIAN_FRONTEND=${DEBIAN_FRONTEND:-readline} /root/chroot_build.sh -
# Cleanup after image changes
sudo rm -f chroot/root/chroot_build.sh
sudo rm -f chroot/root/default_config.sh
if [[ -f "chroot/root/config.sh" ]]; then
sudo rm -f chroot/root/config.sh
fi
chroot_exit_teardown
}
function build_iso() {
echo "=====> running build_iso ..."
dash_flag=false
start_index=0
end_index={#CMD[*]}
for ii in "@";
do
if [[ ii=="−"]];thendashflag=truecontinuefifindindexii
if [[ dashflag==false]];thenstaindex
else
end_index=((index+1))
fi
done
if [[ dashflag==false]];thenend(($start_index + 1))
fi
#loop through the commands
for ((ii=startindex;ii<end_index; ii++)); do
${CMD[ii]}
done