8 Star 10 Fork 2

anolis / KBuilder

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
create-liveiso.sh 11.14 KB
一键复制 编辑 原始数据 按行查看 历史
wangchuanguo 提交于 2023-09-02 14:31 . add tool of creating liveISO
#! /bin/bash
source ./livemedia.conf
######################
PRODUCT=`cat "$target"/etc/os-release | grep "^NAME" | cut -d \" -f2`
PRODUCT=`echo $PRODUCT|sed 's/ /-/g'|awk '{print tolower($0)}'`
if [ -z "$VERSION" ]; then
for file in /etc/{kos,anolis,system}-release; do
if [ -r "$file" ]; then
VERSION="$(sed 's/^[^0-9\]*\([0-9.]\+\).*$/\1/' "$file")"
break
fi
done
fi
if [ -z "$VERSION" ]; then
echo >&2 "warning: cannot autodetect OS version, using '$PRODUCT' as VERSION"
version=$PRODUCT
fi
echo "PRODUCT: $PRODUCT VERSION: $VERSION"
#######################################################################################################
CURDIR=$(cd "$(dirname ${BASH_SOURCE[0]})" && pwd)
cd $CURDIR
if [ -z "$PRODUCT" ] || [ -z "$VERSION" ] || [ -z "$RESULT_DIR" ] ;then
echo "Error: PRODUCT, VERSION, RESULT_DIR must be set!"
exit 1
fi
if [ -z "$BASEOS_REPO" ] || [ -z "$APPSTREAM_REPO" ] ;then
echo "Error: BASEOS_REPO,APPSTREAM_REPO must be set!"
exit 1
fi
valid_char_for_pkg_group='^[a-zA-Z0-9 _\.\+\-]\+$'
if [ ! -z "$ADD_PACKAGES" ]; then
if echo "$ADD_PACKAGES" | grep "$valid_char_for_pkg_group"; then
echo "Info: ADD_PACKAGES style checked"
else
echo "Error: ADD_PACKAGES contains incorrect data"
exit 2
fi
fi
if [ ! -z "$ADD_GROUPS" ]; then
if echo "$ADD_GROUPS" | grep "$valid_char_for_pkg_group"; then
echo "Info: ADD_GROUPS style checked"
else
echo "Error: ADD_GROUPS contains incorrect data"
exit 2
fi
fi
ADD_FILES=`echo "$ADD_FILES"|sed 's/ //g'`
valid_char_for_filetarget='^[a-zA-Z0-9 /_\.\+\-]\+$'
if [ ! -z "$ADD_FILES" ]; then
if echo "$ADD_FILES" | grep "$valid_char_for_filetarget"; then
if [ ! -e "$ADD_FILES" ];then
echo "Error: File to be added does not exist."
exit 3
fi
ADD_FILES=$(readlink -f $ADD_FILES)
else
echo "Error: ADD_FILES contains incorrect data, and valid chars are: upper and lower case letters numbers / - _ + ."
exit 3
fi
fi
ADD_FILES_TARGET=`echo "$ADD_FILES_TARGET"|sed 's/ //g'`
if [ ! -z "$ADD_FILES_TARGET" ]; then
if echo "$ADD_FILES_TARGET" | grep "$valid_char_for_filetarget"; then
echo " "
else
echo "Error: ADD_FILES_TARGET contains incorrect data, and valid chars are: upper and lower case letters numbers / - _ + ."
exit 4
fi
fi
if [[ "$ADD_FILES_TARGET" != "" && "$ADD_FILES" == "" ]];then
echo "ADD_FILES must have value when ADD_FILES_TARGET has value"
exit 5
fi
if [[ "$ADD_FILES" != "" && "$ADD_FILES_TARGET" == "" ]];then
echo "ADD_FILES_TARGET must have value when ADD_FILES has value, use /root by default."
ADD_FILES_TARGET="/"
fi
################################
function get_param()
{
echo "Parsing input parameters ..."
ret=0 #0 success, else error
while [ -n "$1" ]
do
case "$1" in
-u|--uefi)
#echo "create qcow2 image that use UEFI boot mode."
UEFI_FLAG=true
shift 1
;;
-s|--image-disk-size)
#echo "set the qcow2 image size. Only for autopart"
if grep '^[[:digit:]]*$' <<< "$2" > /dev/null; then
DISK_SIZE_OF_IMAGE=$2
else
echo "image disk size must be a number in megabytes."
return 1
fi
shift 2
;;
-h|--help|*)
usage
ret=1
break
;;
*)
ret=1
;;
esac
done
return $ret
}
function usage()
{
cat << EOF
Usage:
$0 [-u | --uefi]
$0 -h
Options:
-h Print this help
-u | --uefi Create live ISO that use UEFI boot mode, and LEGACY BIOS will be used by default
-s | --image-disk-size Image disk size that is 4000 megabytes as default(when no -s or --image-disk-size)
EOF
}
function uefi_firmware_check()
{
#we can make uefi-image at legacy-machine,but can not make legacy-image at uefi-machine
if [ -d /sys/firmware/efi ]; then
echo "your machine is UEFI, so the image to be created is must be UEFI"
UEFI_FLAG=true
fi
}
get_param $*
ret_get_param=$?
if [ $ret_get_param -ne 0 ]; then
#echo "input params have some error, return"
exit 3
fi
uefi_firmware_check
#######################
CUR_ARCH=`LANG=en_US.UTF-8 lscpu|grep "Architecture"|awk '{print $2}'`
KS_FILE="livemedia.ks"
PROJECT=$PRODUCT
PROJECT_ALL=$PRODUCT"-"$VERSION"-live"
PROJECT_ALL=`echo $PROJECT_ALL | sed 's/\./-/g'`
###check needed tools
echo "check whether all needed rpms had been installed..."
if test -z "$(rpm -qa lorax)"; then
echo "Error: lorax has not been installed, exec iso-tools-setup.sh firstly please!"
exit 1
fi
lorax_templates_name="lorax-templates-rhel"
if [ -f /etc/kos-release ]; then
lorax_templates_name="lorax-templates-anolis"
fi
if [ -f /etc/anolis-release ]; then
lorax_templates_name="lorax-templates-anolis"
fi
if test -z "$(rpm -qa $lorax_templates_name)"; then
echo "Error: $lorax_templates_name has not been installed, exec iso-tools-setup.sh firstly please!"
exit 1
fi
if test -z "$(rpm -qa lorax-lmc-virt)"; then
echo "Error: lorax-lmc-virt has not been installed, exec iso-tools-setup.sh firstly please!"
exit 1
fi
if test -z "$(rpm -qa isomd5sum)"; then
echo "Error: isomd5sum has not been installed, exec iso-tools-setup.sh firstly please!"
exit 1
fi
if test -z "$(rpm -qa genisoimage)"; then
echo "Error: genisoimage has not been installed, exec iso-tools-setup.sh firstly please!"
exit 1
fi
if [ $CUR_ARCH = "x86_64" ]; then
if test -z "$(rpm -qa syslinux)"; then
echo "Error: syslinux has not been installed, exec iso-tools-setup.sh firstly please!"
exit 1
fi
fi
######
ISO_NAME=""
if [ $CUR_ARCH = "x86_64" ]; then
BASEOS_REPO=`echo $BASEOS_REPO | sed 's/aarch64/x86_64/g'`
APPSTREAM_REPO=`echo $APPSTREAM_REPO | sed 's/aarch64/x86_64/g'`
if [ $UEFI_FLAG = true ]; then
RESULT_DIR=$RESULT_DIR"/uefi"
ISO_NAME=$PROJECT_ALL"-"$CUR_ARCH"-uefi.iso"
sed -i "s/^#grub2-efi.*cdboot/grub2-efi-x64-cdboot/g" $KS_FILE
sed -i "s/^#shim.*/shim-x64/g" $KS_FILE
sed -i "s/^#syslinux/syslinux/g" $KS_FILE
sed -i "s/^#memtest86+/memtest86+/g" $KS_FILE
else
RESULT_DIR=$RESULT_DIR"/legacy"
ISO_NAME=$PROJECT_ALL"-"$CUR_ARCH"-legacy.iso"
sed -i "s/^grub2-efi.*cdboot/#grub2-efi-x64-cdboot/g" $KS_FILE
sed -i "s/^shim.*/#shim-x64/g" $KS_FILE
sed -i "s/^#syslinux/syslinux/g" $KS_FILE
sed -i "s/^#memtest86+/memtest86+/g" $KS_FILE
fi
elif [ $CUR_ARCH = "aarch64" ]; then
BASEOS_REPO=`echo $BASEOS_REPO | sed 's/x86_64/aarch64/g'`
APPSTREAM_REPO=`echo $APPSTREAM_REPO | sed 's/x86_64/aarch64/g'`
if [ $UEFI_FLAG = true ]; then
RESULT_DIR=$RESULT_DIR"/uefi"
ISO_NAME=$PROJECT_ALL"-"$CUR_ARCH"-uefi.iso"
sed -i "s/^#grub2-efi.*cdboot/grub2-efi-aa64-cdboot/g" $KS_FILE
sed -i "s/^#shim.*/shim-aa64/g" $KS_FILE
sed -i "s/^syslinux/#syslinux/g" $KS_FILE
sed -i "s/^memtest86+/#memtest86+/g" $KS_FILE
else
RESULT_DIR=$RESULT_DIR"/legacy"
ISO_NAME=$PROJECT_ALL"-"$CUR_ARCH"-legacy.iso"
sed -i "s/^grub2-efi.*cdboot/#grub2-efi-aa64-cdboot/g" $KS_FILE
sed -i "s/^shim-aa64/#shim-aa64/g" $KS_FILE
sed -i "s/^#syslinux/syslinux/g" $KS_FILE
sed -i "s/^memtest86+/#memtest86+/g" $KS_FILE
fi
fi
sed -i "s#^url --url=.*#url --url=\"$BASEOS_REPO\"#g" $KS_FILE
sed -i "s#^repo --name=appstream --baseurl=.*#repo --name=appstream --baseurl=\"$APPSTREAM_REPO\"#g" $KS_FILE
#add other repos
sed -i '/###add otherrepos start###/,/###add otherrepos end###/{/###add otherrepos start###/!{/###add otherrepos end###/!d}}' $KS_FILE
for ((i=5; i>0; --i))
do
tmp="OTHER_REPO$i"
if [ -z "${!tmp}" ]; then
continue
fi
repostr="repo --name=${tmp} --baseurl='${!tmp}' "
sed -i "/###add otherrepos start###/a $repostr" $KS_FILE
done
#add pkgs && groups
sed -i '/###add packages start###/,/###add packages end###/{/###add packages start###/!{/###add packages end###/!d}}' $KS_FILE
if [ ! -z "$ADD_PACKAGES" ]; then
#del multi-space
ADD_PACKAGES=`echo $ADD_PACKAGES | sed 's/ \+/ /g'`
#del space at the begin or end of string
ADD_PACKAGES=`echo $ADD_PACKAGES | sed 's/^[ \\t]*//g' | sed 's/[ \\t]*$//g'`
#change space to \n
ADD_PACKAGES=`echo $ADD_PACKAGES | sed 's/ /\\\n/g'`
#echo "ADD_PACKAGES:$ADD_PACKAGES"
if [ ! -z "$ADD_PACKAGES" ]; then
sed -i "/###add packages start###/a $ADD_PACKAGES" $KS_FILE
fi
fi
if [ ! -z "$ADD_GROUPS" ]; then
#del multi-space
ADD_GROUPS=`echo $ADD_GROUPS | sed 's/ \+/ /g'`
#del space at the begin or end of string
ADD_GROUPS=`echo $ADD_GROUPS | sed 's/^[ \\t]*//g' | sed 's/[ \\t]*$//g'`
#change space to \n@
ADD_GROUPS=`echo $ADD_GROUPS | sed 's/ /\\\n@/g'`
#add @ for first group
if [ ! -z "$ADD_GROUPS" ]; then
ADD_GROUPS="@""$ADD_GROUPS"
fi
#echo "ADD_GROUPS:$ADD_GROUPS"
if [ ! -z "$ADD_GROUPS" ]; then
sed -i "/###add packages start###/a $ADD_GROUPS" $KS_FILE
fi
fi
#disable Gtk for hanging if installed GTK/GUI
sed -i "s/initialized = Gtk.init_check.*/initialized = 0/g" /usr/lib64/python3.6/site-packages/pyanaconda/exception.py
date_str=$(date "+%Y%m%d%H%M%S")
RESULT_DIR="$RESULT_DIR""/""$date_str"
cmd="livemedia-creator --ks $KS_FILE --no-virt --resultdir $RESULT_DIR --project $PROJECT --make-iso --iso-only --iso-name $ISO_NAME --releasever $VERSION --volid $PROJECT_ALL --title $PROJECT_ALL --image-size-align=$DISK_SIZE_OF_IMAGE --nomacboot --keep-image "
echo "runcmd:"$cmd
eval $cmd
if [[ "$ADD_FILES_TARGET" != "" && -f $CURDIR/$RESULT_DIR/$ISO_NAME ]];then
#if [ "$ADD_FILES_TARGET" != "" ]; then
cd $RESULT_DIR
mkdir newiso oldiso
mount $ISO_NAME oldiso
cp -r oldiso/* newiso/
rm -rf newiso/LiveOS/squashfs.img
unsquashfs -d tmp_squanshfs oldiso/LiveOS/squashfs.img
umount oldiso
mkdir tmp_rootfs
mount -rw tmp_squanshfs/LiveOS/rootfs.img tmp_rootfs
if [ -e "tmp_rootfs/$ADD_FILES_TARGET" ];then
if [ ! -d "tmp_rootfs/$ADD_FILES_TARGET" ];then
echo "Error: ADD_FILES_TARGET[$ADD_FILES_TARGET] is not a directory in squashfs.img!"
umount tmp_rootfs
exit 10
fi
else
echo "mkdir -p tmp_rootfs/$ADD_FILES_TARGET"
mkdir -p "tmp_rootfs/$ADD_FILES_TARGET"
fi
echo "copy file into squanshfs.img"
echo "cp [$ADD_FILES] [$ADD_FILES_TARGET]"
cp -r $ADD_FILES tmp_rootfs/$ADD_FILES_TARGET
if [ $? != 0 ];then
echo "Error: copy file into squashfs.img failed!"
umount tmp_rootfs
exit 10
fi
umount tmp_rootfs
mksquashfs tmp_squanshfs newiso/LiveOS/squashfs.img -comp xz #-Xbcj x86
if [ $? != 0 ];then
echo "Error: mksquashfs failed!"
exit 10
fi
mv $ISO_NAME "$ISO_NAME""_old"
cd newiso
if [ $CUR_ARCH = "x86_64" ]; then
mkisofs -o ../$ISO_NAME -b isolinux/isolinux.bin -c isolinux/boot.cat -boot-load-size 4 -boot-info-table -no-emul-boot -R -J -V $PROJECT_ALL -T ./
elif [ $CUR_ARCH = "aarch64" ]; then
mkisofs -o ../$ISO_NAME -eltorito-alt-boot -e images/efiboot.img -no-emul-boot -R -J -V $PROJECT_ALL -T ./
fi
cd ..
if [ $CUR_ARCH = "x86_64" ]; then
isohybrid $ISO_NAME
fi
implantisomd5 $ISO_NAME
fi
if [ -f $CURDIR/$RESULT_DIR/$ISO_NAME ]; then
echo "Your LiveISO is : [$CURDIR/$RESULT_DIR/$ISO_NAME]"
fi
Shell
1
https://gitee.com/anolis/kbuilder.git
git@gitee.com:anolis/kbuilder.git
anolis
kbuilder
KBuilder
master

搜索帮助