Radix cross Linux Build System

Cross-platform build system is designed to build distributions of different operating systems for a set of target devices

39 Commits   2 Branches   2 Tags
     5         kx #!/bin/bash
     5         kx 
     5         kx CWD=`pwd`
     5         kx 
     5         kx program=`basename $0`
     5         kx sbindir=`cd $(dirname ${BASH_SOURCE[0]}) >/dev/null 2>&1 && pwd`
     5         kx 
     5         kx # 13 = permission denied (should be root)
     5         kx # 92 = Cannot create '/tmp/...' directory
     5         kx EXITSTATUS=0
     5         kx 
     5         kx ################################################################
     5         kx #
     5         kx # TMPDIR & signal handlers:
     5         kx #
     5         kx if [ -z "${DISTRO_NAME}" ] ; then
     5         kx   DISTRO_NAME='radix'
     5         kx fi
     5         kx 
     5         kx umask 022
     5         kx if [ ! -z "${TMPDIR}" ] ; then
     5         kx   export TMPDIR
     5         kx elif [ ! -d "${TMPDIR}" -o ! -w "${TMPDIR}" ] ; then
     5         kx   if [ -d "${HOME}/tmp" -a -w "${HOME}/tmp" ] ; then
     5         kx     export TMPDIR="${HOME}/tmp"
     5         kx   elif [ -d "/tmp" -a -w "/tmp" ] ; then
     5         kx     export TMPDIR="/tmp"
     5         kx   fi
     5         kx fi
     5         kx TMP=$(mkdir -p ${TMPDIR}/${DISTRO_NAME} && mktemp -d -p ${TMPDIR}/${DISTRO_NAME} $program.XXXXXXXX) || { echo "Cannot create '/tmp/...' directory" ; exit 92; }
     5         kx MNT=/mnt
     5         kx if [ ! -f /etc/system-installer ] ; then
     5         kx   if [ ! -z "${TMPDIR}" ] ; then
     5         kx     mkdir -p ${TMPDIR}/${DISTRO_NAME}/mnt
     5         kx     MNT=${TMPDIR}/${DISTRO_NAME}/mnt
     5         kx   else
     5         kx     mkdir -p /tmp/${DISTRO_NAME}/mnt
     5         kx     MNT=/tmp/${DISTRO_NAME}/mnt
     5         kx   fi
     5         kx fi
     5         kx 
     5         kx trap 'for dir in "`find ${MNT} -type d -mindepth 1 -maxdepth 1`" ; do \
     5         kx         umount ${dir} 2> /dev/null ; \
     5         kx       done ; \
     5         kx       rm -rf ${TMP} ; \
     5         kx       clear ; \
     5         kx       head -c 100 /dev/zero | tr "\0" "\n" ; \
     5         kx       echo "ERROR: $program - has been terminated." ; \
     5         kx       echo "" ; \
     5         kx       exit 1' INT TERM
     5         kx 
     5         kx #
     5         kx # Normal exit:
     5         kx #
     5         kx trap 'for dir in "`find ${MNT} -type d -mindepth 1 -maxdepth 1`" ; do \
     5         kx         umount ${dir} 2> /dev/null ; \
     5         kx       done ; \
     5         kx       rm -rf ${TMP} ; \
     5         kx       exit 0' EXIT
     5         kx #
     5         kx ################################################################
     5         kx 
     5         kx 
     5         kx DIALOG=
     5         kx export DIALOGRC=${CWD}/.dialogrc
     5         kx DDOUTPUT=${TMP}/dd-output.$$
     5         kx 
     5         kx VERBOSE=no
     5         kx 
     5         kx FDISK=`PATH=/sbin:/usr/sbin:$PATH which fdisk`
     5         kx if [ "`basename ${FDISK}`" != "fdisk" ] ; then
     5         kx   echo "ERROR: fdisk utility is not found in the system"
     5         kx   exit 1
     5         kx fi
     5         kx 
     5         kx PARTPROBE=`PATH=/sbin:/usr/sbin:$PATH which partprobe`
     5         kx if [ "`basename ${PARTPROBE}`" != "partprobe" ] ; then
     5         kx   PARTPROBE=
     5         kx fi
     5         kx 
     5         kx usage() {
     5         kx   cat <<EOF
     5         kx 
     5         kx Usage: ${program} [options] <DEVICE>
     5         kx 
     5         kx options:
     5         kx     -e, --efifs-image <efi32fs-image> - EFI filesystem image;
     5         kx     -r, --rootfs-image <ext4fs-image> - ROOT filesystem image;
     5         kx     -s, --spase <SIZE>                - SIZE reserved for other partitions,
     5         kx                                         for example, home file system.
     5         kx                                         Default SIZE=1G;
     5         kx     -v, --verbose                     - Enable verbose output;
     5         kx     -d, --dialog                      - Enable DIALOG to show progress;
     5         kx     -h, --help                        - Display this information.
     5         kx 
     5         kx DEVICE can be a regular file or block device such as /dev/sda, /dev/loop0, etc...
     5         kx 
     5         kx EOF
     5         kx }
     5         kx 
     5         kx check_current_user() {
     5         kx   msg=$1
     5         kx   if [ "$EUID" != "0" ] ; then
     5         kx     if [ "x${msg}" != "x" ] ; then
     5         kx       echo "ERROR: ${msg}"
     5         kx     else
     5         kx       echo "ERROR: ${program} - must be run by superuser"
     5         kx     fi
     5         kx     EXITSTATUS=13
     5         kx     exit $EXITSTATUS
     5         kx   fi
     5         kx }
     5         kx 
     5         kx 
     5         kx ################################################################
     5         kx #
     5         kx # show_progress() - print progress without DIALOG.
     5         kx #
     5         kx #   arguments:
     5         kx #   ---------
     5         kx #     $1 - input stream in following format:
     5         kx #
     5         kx #     XXX
     5         kx #     $PCT
     5         kx #     \n\
     5         kx #       $line
     5         kx #     XXX
     5         kx #
     5         kx #     where $PCT  - persentage  [0 ... 100],
     5         kx #           $line - message printed before progress line.
     5         kx #
     5         kx show_progress() {
     5         kx   local printed=no
     5         kx 
     5         kx   GAUGE_LENGHT=68
     5         kx 
     5         kx   local PCT=
     5         kx 
     5         kx   while read line ; do
     5         kx 
     5         kx     local message=
     5         kx     local lines=0
     5         kx     local lenght=0
     5         kx     local ctx=0
     5         kx     local i=0
     5         kx 
     5         kx     if [ "${line}" = "XXX" ] ; then
     5         kx       continue
     5         kx     fi
     5         kx     if [[ "${line}" =~ ^[0-9]*$ ]] ; then
     5         kx       PCT=${line}
     5         kx       continue
     5         kx     fi
     5         kx     while [ "${line:0:1}" = "n" ] ; do
     5         kx       line=${line:1}
     5         kx       message="${message}\n"
     5         kx       let 'lines += 1'
     5         kx     done
     5         kx 
     5         kx     message="${message}${line}"
     5         kx     let 'lines += 1'
     5         kx 
     5         kx     let 'lenght = 3 + GAUGE_LENGHT + 6'
     5         kx     let 'lines += 3'
     5         kx 
     5         kx     if [ "${printed}" = "yes" ] ; then
     5         kx       while [ ${i} -lt ${lines} ] ; do
     5         kx         # Return back and cleaning line by line:
     5         kx         printf "\r\033[K\033[1A"
     5         kx         let 'i +=1'
     5         kx       done
     5         kx       i=0
     5         kx     fi
     5         kx 
     5         kx     echo -e "${message}"
     5         kx     printf "\n  ["
     5         kx 
     5         kx     let 'ctx = GAUGE_LENGHT * PCT / 100'
     5         kx 
     5         kx     while [ $ctx -gt 0 ] ; do
     5         kx       printf "\u2588"
     5         kx       let 'ctx -= 1'
     5         kx       let 'i += 1'
     5         kx     done
     5         kx 
     5         kx     while [ $i -lt ${GAUGE_LENGHT} ] ; do
     5         kx       printf " "
     5         kx       let 'i += 1'
     5         kx     done
     5         kx 
     5         kx     printf "] %3d%%\n\n" $PCT
     5         kx 
     5         kx     printed=yes
     5         kx 
     5         kx   done < "${1:-/dev/stdin}"
     5         kx }
     5         kx 
     5         kx 
     5         kx ################################################################
     5         kx #
     5         kx # waitdd() - waiting for DD and display progress.
     5         kx #
     5         kx #   arguments:
     5         kx #   ---------
     5         kx #     $1 - PID
     5         kx #     $2 - DD output file
     5         kx #     $3 - Size of input stream
     5         kx #     $4 - Dialog title
     5         kx #
     5         kx waitdd() {
     5         kx 
     5         kx   local pid=$1
     5         kx   local ddout=$2
     5         kx   local isize=$3
     5         kx   local title=$4
     5         kx   local PCT=0
     5         kx 
     5         kx   ( while [ "x`ps -eo pid | grep $pid`" != "x" ] ; do
     5         kx       kill -USR1 "$pid" 2>/dev/null
     5         kx       line=`tail -n1 $ddout | grep "bytes"`
     5         kx       bytes=`echo "$line" | cut -f 1 -d ' '`
     5         kx       if [ "x$bytes" != "x" ] ; then
     5         kx         let "PCT = bytes * 100 / isize"
     5         kx       fi
     5         kx       cat << EOF
     5         kx XXX
     5         kx $PCT
     5         kx \n\
     5         kx   $line
     5         kx XXX
     5         kx EOF
     5         kx       usleep 300
     5         kx     done
     5         kx     if [ "x`ps -eo pid | grep $pid`" = "x" ] ; then
     5         kx       cat << EOF
     5         kx XXX
     5         kx 100
     5         kx \n\
     5         kx   Written $isize bytes
     5         kx XXX
     5         kx EOF
     5         kx     fi
     5         kx   ) | if [ "x${DIALOG}" = "x" ] ; then
     5         kx         show_progress
     5         kx       else
     5         kx         ${DIALOG} --colors \
     5         kx                   --backtitle "\Z7Radix\Zn \Z1cross\Zn\Z7 Linux\Zn" \
     5         kx                   --title " \Z0${title}\Zn " --gauge "\n" 8 74 0
     5         kx       fi
     5         kx }
     5         kx 
     5         kx 
     5         kx ################################################################
     5         kx # Disk Geometry:
     5         kx #
     5         kx bytes=
     5         kx sectors=
     5         kx unit_size=
     5         kx log_sector_size=
     5         kx phy_sector_size=
     5         kx alignment=4096
     5         kx format="512N"
     5         kx #
     5         kx #   Format | logical sector size | physical sector size
     5         kx #  --------+---------------------+----------------------
     5         kx #     512N |         512         |          512
     5         kx #  --------+---------------------+----------------------
     5         kx #     512E |         512         |         4096
     5         kx #  --------+---------------------+----------------------
     5         kx #    4096N |        4096         |         4096
     5         kx #  --------+---------------------+----------------------
     5         kx #
     5         kx 
     5         kx disk_size_in_bytes() {
     5         kx   disk=$1
     5         kx   echo "`LANG=en_US.UTF-8 ${FDISK} -l $disk`" | while read -r line ; do
     5         kx     found=`echo "$line" | grep "^Disk $disk"`
     5         kx     if [ "$found" != "" ] ; then
     5         kx       bytes=`echo $found | sed 's,.* \([0-9]*\) bytes.*,\1,'`
     5         kx       echo "$bytes"
     5         kx       break
     5         kx     fi
     5         kx   done
     5         kx }
     5         kx 
     5         kx disk_size_in_sectors() {
     5         kx   disk=$1
     5         kx   echo "`LANG=en_US.UTF-8 ${FDISK} -l $disk`" | while read -r line ; do
     5         kx     found=`echo "$line" | grep "^Disk $disk"`
     5         kx     if [ "$found" != "" ] ; then
     5         kx       sectors=`echo $found | sed 's,.* \([0-9]*\) sectors$,\1,'`
     5         kx       echo "$sectors"
     5         kx       break
     5         kx     fi
     5         kx   done
     5         kx }
     5         kx 
     5         kx disk_unit_size() {
     5         kx   disk=$1
     5         kx   echo "`LANG=en_US.UTF-8 ${FDISK} -l $disk`" | while read -r line ; do
     5         kx     found=`echo "$line" | grep "^Unit"`
     5         kx     if [ "$found" != "" ] ; then
     5         kx       unit_size=`echo $found | sed 's,.*= \([0-9]*\) bytes$,\1,'`
     5         kx       echo "$unit_size"
     5         kx       break
     5         kx     fi
     5         kx   done
     5         kx }
     5         kx 
     5         kx disk_log_sector_size() {
     5         kx   disk=$1
     5         kx   echo "`LANG=en_US.UTF-8 ${FDISK} -l $disk`" | while read -r line ; do
     5         kx     found=`echo "$line" | grep '^Sector size'`
     5         kx     if [ "$found" != "" ] ; then
     5         kx       log_sector_size=`echo $found | sed 's,.* \([0-9]*\) bytes / \([0-9]*\) bytes$,\1,'`
     5         kx       echo "$log_sector_size"
     5         kx       break
     5         kx     fi
     5         kx   done
     5         kx }
     5         kx 
     5         kx disk_phy_sector_size() {
     5         kx   disk=$1
     5         kx   echo "`LANG=en_US.UTF-8 ${FDISK} -l $disk`" | while read -r line ; do
     5         kx     found=`echo "$line" | grep '^Sector size'`
     5         kx     if [ "$found" != "" ] ; then
     5         kx       phy_sector_size=`echo $found | sed 's,.* \([0-9]*\) bytes / \([0-9]*\) bytes$,\2,'`
     5         kx       echo "$phy_sector_size"
     5         kx       break
     5         kx     fi
     5         kx   done
     5         kx }
     5         kx 
     5         kx disk_geometry() {
     5         kx   disk=$1
     5         kx 
     5         kx   bytes=`disk_size_in_bytes $disk`
     5         kx   sectors=`disk_size_in_sectors $disk`
     5         kx   unit_size=`disk_unit_size $disk`
     5         kx   log_sector_size=`disk_log_sector_size $disk`
     5         kx   phy_sector_size=`disk_phy_sector_size $disk`
     5         kx 
     5         kx   if [ $unit_size -eq $alignment ] ; then
     5         kx     # Copy of GPT at end of disk:
     5         kx     let "sectors = sectors - 5"
     5         kx   else
     5         kx     # Copy of GPT at end of disk:
     5         kx     let "sectors = sectors - 33"
     5         kx   fi
     5         kx 
     5         kx   if [ $phy_sector_size -eq $alignment ] ; then
     5         kx     if [ $log_sector_size -lt $phy_sector_size ] ; then
     5         kx       format="512E"
     5         kx     else
     5         kx       format="4096N"
     5         kx     fi
     5         kx   else
     5         kx     format="512N"
     5         kx   fi
     5         kx }
     5         kx #
     5         kx # End of Disk Geometry:
     5         kx ################################################################
     5         kx 
     5         kx 
     5         kx DEVICE=
     5         kx EFI32FS_IMAGE=
     5         kx EXT4FS_IMAGE=
     5         kx 
     5         kx efi32fs_size_in_bytes=
     5         kx ext4fs_size_in_bytes=
     5         kx 
     5         kx #
     5         kx # Size reserved for home file system:
     5         kx #
     5         kx space_size="1G"
     5         kx space_size_in_bytes=
     5         kx 
     5         kx disk_size_in_bytes=
     5         kx 
     5         kx #
     5         kx # Parse options:
     5         kx #
     5         kx while [ $# -ne 0 ] ; do
     5         kx   if [ "$1" = "-h" -o "$1" = "--help" ] ; then
     5         kx     usage
     5         kx     exit 0
     5         kx   elif [ "$1" = "-v" -o "$1" = "--verbose" ] ; then
     5         kx     VERBOSE=yes
     5         kx     shift 1
     5         kx   elif [ "$1" = "-d" -o "$1" = "--dialog" ] ; then
     5         kx     DIALOG=dialog
     5         kx     shift 1
     5         kx   elif [ "$1" = "-e" -o "$1" = "--efifs-image" ] ; then
     5         kx     if [ "x$2" = "x" ] ; then
     5         kx       echo "ERROR: EFI filesystem image is not specified"
     5         kx       exit 1
     5         kx     fi
     5         kx     EFI32FS_IMAGE="$2"
     5         kx     shift 2
     5         kx   elif [ "$1" = "-r" -o "$1" = "--rootfs-image" ] ; then
     5         kx     if [ "x$2" = "x" ] ; then
     5         kx       echo "ERROR: ROOT filesystem image is not specified"
     5         kx       exit 1
     5         kx     fi
     5         kx     EXT4FS_IMAGE="$2"
     5         kx     shift 2
     5         kx   elif [ "$1" = "-s" -o "$1" = "--space" ] ; then
     5         kx     if [ "$2" = "" ] ; then
     5         kx       echo "ERROR: Space size for additional partitions is not specified"
     5         kx       usage
     5         kx       exit 1
     5         kx     fi
     5         kx     space_size="$2"
     5         kx     if [ `echo "${space_size}" | grep '[Mm]'` ] ; then
     5         kx       size=`echo "${space_size}" | sed 's/^[ \t]*//;s/[ \t]*$//;s/[a-zA-Z]*//g'`
     5         kx       size=`echo "scale=1; $size * 1048576" | bc`
     5         kx       prec=`echo "${size}" | cut -f2 -d'.'` 
     5         kx       size=`echo "${size}" | cut -f1 -d'.'`
     5         kx       if [ $prec -gt 5 ] ; then
     5         kx         let "size = size + 1"
     5         kx       fi
     5         kx       space_size_in_bytes=$size
     5         kx     elif [ `echo "${space_size}" | grep '[Gg]'` ] ; then
     5         kx       size=`echo "${space_size}" | sed 's/^[ \t]*//;s/[ \t]*$//;s/[a-zA-Z]*//g'`
     5         kx       size=`echo "scale=1; $size * 1073741824" | bc`
     5         kx       prec=`echo "${size}" | cut -f2 -d'.'` 
     5         kx       size=`echo "${size}" | cut -f1 -d'.'`
     5         kx       if [ $prec -gt 5 ] ; then
     5         kx         let "size = size + 1"
     5         kx       fi
     5         kx       space_size_in_bytes=$size
     5         kx     else
     5         kx       space_size_in_bytes=`echo "${space_size}" | sed 's/^[ \t]*//;s/[ \t]*$//;s/[a-zA-Z]*//g'`
     5         kx     fi
     5         kx     if [ `echo "${space_size_in_bytes}" | grep "[^0-9]"` ] ; then
     5         kx       echo "ERROR: Wrong space size for additional partitions"
     5         kx       usage
     5         kx       exit 1
     5         kx     fi
     5         kx     shift 2
     5         kx   else
     5         kx     if [ $# -lt 1 ] ; then
     5         kx       echo "ERROR: Target DEVICE is not specified"
     5         kx       exit 1
     5         kx     fi
     5         kx     DEVICE="$1"
     5         kx     break
     5         kx   fi
     5         kx done
     5         kx 
     5         kx 
     5         kx if [ "x${DEVICE}" = "x" ] ; then
     5         kx   echo "ERROR: Target DEVICE is not specified"
     5         kx   exit 1
     5         kx fi
     5         kx 
     5         kx if [ "x${EFI32FS_IMAGE}" = "x" ] ; then
     5         kx   echo "ERROR: EFI filesystem image is not specified"
     5         kx   exit 1
     5         kx fi
     5         kx 
     5         kx if [ "x${EXT4FS_IMAGE}" = "x" ] ; then
     5         kx   echo "ERROR: ROOT filesystem image is not specified"
     5         kx   exit 1
     5         kx fi
     5         kx 
     5         kx #
     5         kx # Calculate space size in bytes if not specified in command line:
     5         kx #
     5         kx if [ "x${space_size_in_bytes}" = "x" ] ; then
     5         kx   if [ `echo "${space_size}" | grep '[Mm]'` ] ; then
     5         kx     size=`echo "${space_size}" | sed 's/^[ \t]*//;s/[ \t]*$//;s/[a-zA-Z]*//g'`
     5         kx     size=`echo "scale=1; $size * 1048576" | bc`
     5         kx     prec=`echo "${size}" | cut -f2 -d'.'` 
     5         kx     size=`echo "${size}" | cut -f1 -d'.'`
     5         kx     if [ $prec -gt 5 ] ; then
     5         kx       let "size = size + 1"
     5         kx     fi
     5         kx     space_size_in_bytes=$size
     5         kx   elif [ `echo "${space_size}" | grep '[Gg]'` ] ; then
     5         kx     size=`echo "${space_size}" | sed 's/^[ \t]*//;s/[ \t]*$//;s/[a-zA-Z]*//g'`
     5         kx     size=`echo "scale=1; $size * 1073741824" | bc`
     5         kx     prec=`echo "${size}" | cut -f2 -d'.'` 
     5         kx     size=`echo "${size}" | cut -f1 -d'.'`
     5         kx     if [ $prec -gt 5 ] ; then
     5         kx       let "size = size + 1"
     5         kx     fi
     5         kx     space_size_in_bytes=$size
     5         kx   else
     5         kx     space_size_in_bytes=`echo "${space_size}" | sed 's/^[ \t]*//;s/[ \t]*$//;s/[a-zA-Z]*//g'`
     5         kx   fi
     5         kx fi
     5         kx 
     5         kx efi32fs_size_in_bytes=$(stat -c%s "${EFI32FS_IMAGE}")
     5         kx ext4fs_size_in_bytes=$(stat -c%s "${EXT4FS_IMAGE}")
     5         kx 
     5         kx #
     5         kx # a multiple of 4096:
     5         kx #
     5         kx let "size = efi32fs_size_in_bytes / alignment * alignment"
     5         kx if [ ${size} -ne ${efi32fs_size_in_bytes} ] ; then
     5         kx   echo "ERROR: EFI filesystem image size must be a multiple of 4096"
     5         kx   exit 1
     5         kx fi
     5         kx 
     5         kx let "size  = ext4fs_size_in_bytes  / alignment * alignment"
     5         kx if [ ${size} -ne ${ext4fs_size_in_bytes} ] ; then
     5         kx   echo "ERROR: EFI filesystem image size must be a multiple of 4096"
     5         kx   exit 1
     5         kx fi
     5         kx 
     5         kx #
     5         kx # Calculate minimal size needed for input images:
     5         kx #
     5         kx disk_size_in_bytes=$(echo "${efi32fs_size_in_bytes} + ${ext4fs_size_in_bytes} + ${space_size_in_bytes}" | bc)
     5         kx disk_size_in_bytes=$(echo "${disk_size_in_bytes} + 512*2048 + 512*33" | bc)
     5         kx let "disk_size_in_bytes = disk_size_in_bytes / 512 * 512"
     5         kx 
     5         kx 
     5         kx if [ -b "${DEVICE}" ] ; then
     5         kx   check_current_user "${program} should be run by superuser: '${DEVICE}' is not a regular file"
     5         kx elif [ ! -f "${DEVICE}" ] ; then
     5         kx   if [ "x${DIALOG}" = "x" ] ; then
     5         kx     echo -e "\nCreating an empty disk image..."
     5         kx   fi
     5         kx   cnt=$(echo "${disk_size_in_bytes} / 512" | bc)
     5         kx   rm -f ${DDOUTPUT}
     5         kx   LANG=en_US.UTF-8 dd if=/dev/zero of=${DEVICE} of=disk bs=512 count=${cnt} >> ${DDOUTPUT} 2>&1 & \
     5         kx     waitdd $! ${DDOUTPUT} ${disk_size_in_bytes} "Create empty disk image"
     5         kx   rm -f ${DDOUTPUT}
     5         kx fi
     5         kx 
     5         kx 
     5         kx efi32fs_start_sector=2048
     5         kx efi32fs_end_sector=
     5         kx ext4fs_start_sector=
     5         kx ext4fs_end_sector=
     5         kx space_start_sector=
     5         kx space_end_sector=
     5         kx 
     5         kx #
     5         kx # https://uefi.org/specifications ->
     5         kx #  -> https://uefi.org/sites/default/files/resources/UEFI_Spec_2_9_2021_03_18.pdf
     5         kx #
     5         kx # If the block size is 512, the First Usable LBA must be greater than or equal to 34
     5         kx # (allowing 1 block for the Protective MBR, 1 block for the Partition Table Header, and
     5         kx # 32 blocks for the GPT Partition Entry Array); if the logical block size is 4096, the
     5         kx # First Useable LBA must be greater than or equal to 6 (allowing 1 block for the Protective
     5         kx # MBR, 1 block for the GPT Header, and 4 blocks for the GPT Partition Entry Array).
     5         kx #
     5         kx # Backup Partition Table requires 33 blocks in case block size is 512 bytes and 5 blocks
     5         kx # in case block size is 4096 bytes (Protective MBR has no copy at end of disk).
     5         kx #
     5         kx 
     5         kx #
     5         kx # Get size of disk:
     5         kx #
     5         kx disk_geometry ${DEVICE}
     5         kx 
     5         kx if [ "${VERBOSE}" = "yes" ] ; then
     5         kx   if [ "x${DIALOG}" != "x" ] ; then
     5         kx     clear
     5         kx   fi
     5         kx   echo ""
     5         kx   echo "Disk Geometry:"
     5         kx   echo "-------------"
     5         kx   echo "           bytes = $bytes"
     5         kx   if [ $unit_size -eq $alignment ] ; then
     5         kx     echo "         sectors = $sectors + 5"
     5         kx   else
     5         kx     echo "         sectors = $sectors + 33"
     5         kx   fi
     5         kx   echo "       unit_size = $unit_size"
     5         kx   echo " log_sector_size = $log_sector_size"
     5         kx   echo " phy_sector_size = $phy_sector_size"
     5         kx   echo "       alignment = $alignment"
     5         kx   echo "          format = $format"
     5         kx   echo ""
     5         kx fi
     5         kx 
     5         kx if [ ${disk_size_in_bytes} -gt ${bytes} ] ; then
     5         kx   echo "ERROR: Not enough space on '${DEVICE}' device"
     5         kx   exit 1
     5         kx fi
     5         kx 
     5         kx if [ $unit_size -eq $alignment ] ; then
     5         kx   efi32fs_start_sector=256
     5         kx fi
     5         kx 
     5         kx let "efi32fs_size_in_sectors = efi32fs_size_in_bytes / unit_size"
     5         kx let "ext4fs_size_in_sectors  = ext4fs_size_in_bytes  / unit_size"
     5         kx 
     5         kx #
     5         kx # Calculation of the initial and final sectors of partitions:
     5         kx #
     5         kx let "efi32fs_end_sector  = efi32fs_start_sector + ( efi32fs_size_in_bytes / unit_size - 1 )"
     5         kx let "ext4fs_start_sector = efi32fs_end_sector   + 1"
     5         kx let "ext4fs_end_sector   = ext4fs_start_sector  + ( ext4fs_size_in_bytes / unit_size - 1 )"
     5         kx 
     5         kx left=
     5         kx let "left = sectors - ext4fs_end_sector"
     5         kx need=
     5         kx let "need = space_size_in_bytes / unit_size"
     5         kx 
     5         kx if [ $left -lt $need ] ; then
     5         kx   echo "WARNING: Not enough space for additional partitions"
     5         kx fi
     5         kx 
     5         kx if [ ${need} -gt 0 ] ; then
     5         kx   let "space_start_sector = ext4fs_end_sector + 1"
     5         kx   let "space_end_sector   = sectors - 1"
     5         kx else
     5         kx   space_start_sector=-1
     5         kx   space_end_sector=-1
     5         kx fi
     5         kx 
     5         kx if [ "${VERBOSE}" = "yes" ] ; then
     5         kx   echo ""
     5         kx   echo "Disk Partioning:"
     5         kx   echo "---------------"
     5         kx   echo " efi32fs_size_in_bytes   = $efi32fs_size_in_bytes"
     5         kx   echo " ext4fs_size_in_bytes    = $ext4fs_size_in_bytes"
     5         kx   echo " space_size_in_bytes     = $space_size_in_bytes"
     5         kx   echo ""
     5         kx   echo " efi32fs_size_in_sectors = $efi32fs_size_in_sectors"
     5         kx   echo " efi32fs_start_sector    = $efi32fs_start_sector"
     5         kx   echo " efi32fs_end_sector      = $efi32fs_end_sector"
     5         kx   echo ""
     5         kx   echo " ext4fs_size_in_sectors  = $ext4fs_size_in_sectors"
     5         kx   echo " ext4fs_start_sector     = $ext4fs_start_sector"
     5         kx   echo " ext4fs_end_sector       = $ext4fs_end_sector"
     5         kx   echo ""
     5         kx   echo " space_start_sector      = $space_start_sector"
     5         kx   echo " space_end_sector        = $space_end_sector"
     5         kx   echo ""
     5         kx fi
     5         kx 
     5         kx 
     5         kx UEFI_TYPE=C12A7328-F81F-11D2-BA4B-00A0C93EC93B
     5         kx UEFI_UUID=eaf0eef1-f13a-7565-6669-203aefe0f0f2
     5         kx UEFI_NAME=uefi
     5         kx ROOT_TYPE=0FC63DAF-8483-4772-8E79-3D69D8477DE4
     5         kx ROOT_UUID=eaf0eef1-f13a-726F-6F74-203aefe0f0f2
     5         kx ROOT_NAME=root
     5         kx 
     5         kx 
     5         kx clean_disk() {
     5         kx   disk=$1
     5         kx   LANG=en_US.UTF-8 ${FDISK} --wipe=always --wipe-partition=always $disk 2>/dev/null 1>/dev/null <<EOF
     5         kx g
     5         kx w
     5         kx EOF
     5         kx }
     5         kx 
     5         kx #
     5         kx # NOTE: номер первого (и единственного раздела не запрашивается):
     5         kx #
     5         kx create_efi_pattition() {
     5         kx   disk=$1
     5         kx   LANG=en_US.UTF-8 ${FDISK} --wipe=always --wipe-partition=always $disk 2>/dev/null 1>/dev/null <<EOF
     5         kx n
     5         kx 1
     5         kx ${efi32fs_start_sector}
     5         kx ${efi32fs_end_sector}
     5         kx t
     5         kx ${UEFI_TYPE}
     5         kx x
     5         kx n
     5         kx ${UEFI_NAME}
     5         kx u
     5         kx ${UEFI_UUID}
     5         kx r
     5         kx w
     5         kx EOF
     5         kx }
     5         kx 
     5         kx create_root_pattition() {
     5         kx   disk=$1
     5         kx   LANG=en_US.UTF-8 ${FDISK} --wipe=always --wipe-partition=always $disk 2>/dev/null 1>/dev/null <<EOF
     5         kx n
     5         kx 2
     5         kx ${ext4fs_start_sector}
     5         kx ${ext4fs_end_sector}
     5         kx t
     5         kx 2
     5         kx ${ROOT_TYPE}
     5         kx x
     5         kx n
     5         kx 2
     5         kx ${ROOT_NAME}
     5         kx u
     5         kx 2
     5         kx ${ROOT_UUID}
     5         kx r
     5         kx w
     5         kx EOF
     5         kx }
     5         kx 
     5         kx #
     5         kx # Move the shell prompt to last line of Terminal:
     5         kx #
     5         kx if [ "x${DIALOG}" != "x" ] ; then
     5         kx   echo -en "\033[$(tput lines)B"
     5         kx fi
     5         kx 
     5         kx if [ "x${DIALOG}" = "x" ] ; then
     5         kx   echo -e "\nDisk partitioning..."
     5         kx else
     5         kx   ${DIALOG} --colors \
     5         kx             --backtitle "\Z7Radix\Zn \Z1cross\Zn\Z7 Linux\Zn" \
     5         kx             --title " \Z0Disk partitioning...\Zn " \
     5         kx             --infobox "\n Please wait for partitions have been created.\n" 5 74
     5         kx fi
     5         kx 
     5         kx clean_disk ${DEVICE}
     5         kx if [ "x${PARTPROBE}" != "x" ] ; then
     5         kx   ${PARTPROBE} ${DEVICE} 1>/dev/null 2>/dev/null
     5         kx fi
     5         kx 
     5         kx create_efi_pattition ${DEVICE}
     5         kx if [ "x${PARTPROBE}" != "x" ] ; then
     5         kx   ${PARTPROBE} ${DEVICE} 1>/dev/null 2>/dev/null
     5         kx fi
     5         kx 
     5         kx create_root_pattition ${DEVICE}
     5         kx if [ "x${PARTPROBE}" != "x" ] ; then
     5         kx   ${PARTPROBE} ${DEVICE} 1>/dev/null 2>/dev/null
     5         kx fi
     5         kx 
     5         kx #
     5         kx # Write EFI boot image:
     5         kx #
     5         kx if [ "x${DIALOG}" = "x" ] ; then
     5         kx   echo -e "\nRecording the '`basename ${EFI32FS_IMAGE}`' image..."
     5         kx fi
     5         kx rm -f ${DDOUTPUT}
     5         kx LANG=en_US.UTF-8 \
     5         kx   dd if=${EFI32FS_IMAGE} of=${DEVICE} \
     5         kx      bs=${unit_size} count=${efi32fs_size_in_sectors} \
     5         kx      seek=${efi32fs_start_sector} conv=notrunc >> ${DDOUTPUT} 2>&1 & \
     5         kx     waitdd $! ${DDOUTPUT} ${efi32fs_size_in_bytes} "EFI boot image recording..."
     5         kx rm -f ${DDOUTPUT}
     5         kx 
     5         kx sleep 1
     5         kx 
     5         kx #
     5         kx # Write EXT4 rootfs image:
     5         kx #
     5         kx if [ "x${DIALOG}" = "x" ] ; then
     5         kx   echo -e "\nRecording the '`basename ${EXT4FS_IMAGE}`' image..."
     5         kx fi
     5         kx rm -f ${DDOUTPUT}
     5         kx LANG=en_US.UTF-8 \
     5         kx   dd if=${EXT4FS_IMAGE} of=${DEVICE} \
     5         kx      bs=${unit_size} count=${ext4fs_size_in_sectors} \
     5         kx      seek=${ext4fs_start_sector} conv=notrunc >> ${DDOUTPUT} 2>&1 & \
     5         kx     waitdd $! ${DDOUTPUT} ${ext4fs_size_in_bytes} "ROOT filesystem image recording..."
     5         kx rm -f ${DDOUTPUT}
     5         kx 
     5         kx sleep 1
     5         kx 
     5         kx if [ "x${DIALOG}" = "x" ] ; then
     5         kx   echo -e "\nEFI boot disk is written.\n"
     5         kx else
     5         kx   ${DIALOG} --colors \
     5         kx             --backtitle "\Z7Radix\Zn \Z1cross\Zn\Z7 Linux\Zn" \
     5         kx             --title " \Z0EFI Boot Disk is Complete\Zn " \
     5         kx             --infobox "\n Boot disk is written.\n" 5 74
     5         kx fi