12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106 |
- #!/usr/bin/bash
- # Copyright (C) 2017-2018 Daniel Tartavel-jeannot <contact@librepc.com>
- # Jean-Baptiste Biernacki <j.biernacki@free.fr>
- # Copyright (C) 2020 Neal Gompa <ngompa13@gmail.com>
- #
- # This program is free software; you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation; either version 2 of the License,dnf or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
- #Change the local to the most global
- export LC_ALL=C
- function WaitToContinue() {
- read -p "Break... Press [ENTER] to continue." GARBAGE
- }
- function title() {
- echo -e "\e[1;32m${1}\e[0m"
- }
- function info() {
- echo -e "\e[36m${1}\e[0m"
- }
- function warning() {
- echo -e "\e[1;35m${1}\e[0m"
- }
- function error() {
- echo -e "\e[1;31m${1}\e[0m"
- }
- function help() {
- echo -e "${CMDNAME} [option] [size of image in Go]"
- echo -e "Options:"
- echo -e "--all create ready to burn image of Mageia ${MAGEIA_VERSION}"
- echo -e "--clean Clean all (suppress all) to make a new image"
- echo -e "--size size of image default: 7Go"
- echo -e "--build-path Path to the build directory of the image of Mageia ${MAGEIA_VERSION}"
- echo -e "--target target system (for now rpi, odroid)"
- echo -e "--target-version version of the target (0, 1, 2, 3 for rpi, 3 or 4 for odroid)"
- echo -e "--config Path to config files"
- echo -e "--bootfs filesystem of boot partition (ext4 or vfat) default: ext4"
- echo -e "--nonfree activate nonfree repos"
- echo -e "--tainted activate tainted repos"
- echo -e "\nBuild levels:"
- echo -e "--create-chroot Create the chroot directory"
- echo -e "--install-basesystem install base system"
- echo -e "--add-urpmimedia add mirrors for urpmi"
- echo -e "--chroot chroot to arm directory and launch packages installation"
- echo -e "--create-image Create the image of Mageia ${MAGEIA_VERSION}"
- echo "--config Path to config files (rpi1 rpi2 rpi3 rpi3+ xu4)"
- echo -e "\nFor image size, make sure it fit on physical support. (Default size is 7 Go)"
- }
- function verify_disk_space()
- {
- title "Verifying if there is enough space on disk to make the image"
- DISK_SPACE=$(/usr/bin/df -BG --output=avail "${INSTALL_PATH}" | sed '1d;s/[^0-9]//g')
- info "Free disk space: ${DISK_SPACE}G"
- if [ ${DISK_SPACE} -lt ${IMAGE_SIZE} ]; then
- warning "image size is greater than disk space"
- info "correct the problem and relaunch the script with parameter --create-rpi-image"
- info "${0} --size ${IMAGE_SIZE} --create-rpi-image"
- return 1
- fi
- return 0
- }
- # creation of install path and copy of qemu ( installing it if not yet installed )
- function createchroot()
- {
- title "Making chroot"
- if ! [ -f /usr/share/distribution-gpg-keys/mageia/RPM-GPG-KEY-Mageia ]; then
- title "distribution-gpg-keys package not present : installing distribution-gpg-keys"
- #/usr/bin/dnf --assumeyes --setopt=install_weak_deps=False install distribution-gpg-keys
- installpkg "distribution-gpg-keys" "--setopt=install_weak_deps=False"
- if [ ${?} -ne 0 ]; then
- error "line ${LINENO} can't install distribution-gpg-keys : exiting"
- exit ${ERR_1}
- fi
- fi
- if ! [ -f /usr/bin/qemu-arm-static ]; then
- title "Qemu package not present : installing qemu packages"
- #/usr/bin/dnf --assumeyes --setopt=install_weak_deps=False install qemu-user-static
- installpkg "qemu-user-static" "--setopt=install_weak_deps=False"
- if [ ${?} -ne 0 ]; then
- error "line ${LINENO} can't install qemu-user-static : exiting"
- exit ${ERR_1}
- fi
- fi
- # Starting qemu service if not started
- /usr/bin/systemctl is-active systemd-binfmt.service
- if [ ${?} -ne 0 ]; then
- title "Starting systemd-binfmt.service"
- /usr/bin/systemctl start systemd-binfmt.service
- if [ ${?} -ne 0 ]; then
- error "line ${LINENO} can't start qemu-user-static : exiting"
- exit ${ERR_1}
- fi
- else
- title "Restarting systemd-binfmt.service"
- /usr/bin/systemctl restart systemd-binfmt.service
- if [ ${?} -ne 0 ]; then
- error "line ${LINENO} can't start qemu-user-static : exiting"
- exit ${ERR_1}
- fi
- fi
- return 0
- }
- # enabling extra Mageia repositories
- function genusemirroroptions()
- {
- DNF_MIRROROPTS="--config=${DNF_CONFIGPATH} --forcearch=${ARM_VERSION} --nogpgcheck --releasever=${MAGEIA_VERSION}"
- if [ ! -z "${MIRROR}" ]; then
- DNF_MIRROROPTS="${DNF_MIRROROPTS} --disablerepo=* --repofrompath=mgarel,${MIRROR}/media/core/release/ --enablerepo=mgarel"
- if [ "${MAGEIA_VERSION}" != "cauldron" ]; then
- DNF_MIRROROPTS="${DNF_MIRROROPTS} --repofrompath=mgaup,${MIRROR}/media/core/updates/ --enablerepo=mgaup"
- fi
- if [ ${NONFREE} -eq 1 ]; then
- DNF_MIRROROPTS="${DNF_MIRROROPTS} --repofrompath=mgarel-nonfree,${MIRROR}/media/nonfree/release/ --enablerepo=mgarel-nonfree "
- if [ "${MAGEIA_VERSION}" != "cauldron" ]; then
- DNF_MIRROROPTS="${DNF_MIRROROPTS} --repofrompath=mgaup-nonfree,${MIRROR}/media/nonfree/updates/ --enablerepo=mgaup-nonfree"
- fi
- fi
- if [ ${TAINTED} -eq 1 ]; then
- DNF_MIRROROPTS="${DNF_MIRROROPTS} --repofrompath=mgarel-tainted,${MIRROR}/media/tainted/release/ --enablerepo=mgarel-tainted "
- if [ "${MAGEIA_VERSION}" != "cauldron" ]; then
- DNF_MIRROROPTS="${DNF_MIRROROPTS} --repofrompath=mgaup-tainted,${MIRROR}/media/tainted/updates/ --enablerepo=mgaup-tainted"
- fi
- fi
- fi
- export DNF_MIRROROPTS
- return 0
- }
- # enabling extra Mageia repositories
- function enableextrarepos()
- {
- extrasect_baseprefix="mageia"
- # if [ "${MAGEIA_VERSION}" = "cauldron" ]; then
- # extrasect_baseprefix="cauldron"
- # extrasect_updatesprefix=""
- # else
- # extrasect_baseprefix="mageia"
- extrasect_updatesprefix="updates"
- # fi
- if [ ${NONFREE} -eq 1 ]; then
- title "activating non-free repos"
- DNFPARAM=--installroot="${BUILD_PATH}" config-manager --set-enabled "${extrasect_baseprefix}-${ARM_VERSION}-nonfree"
- if [ "${MAGEIA_VERSION}" != "cauldron" ]; then
- DNFPARAM=$DNFPARAM" --set-enabled ${extrasect_updatesprefix}-${ARM_VERSION}-nonfree"
- fi
- /usr/bin/dnf $DNFPARAM
- err=${?}
- if [ ${err} -ne 0 ]; then
- error "line ${LINENO} error ${err} - can't activate nonfree repositories : exiting"
- exit ${ERR_1}
- fi
- fi
- if [ ${TAINTED} -eq 1 ]; then
- title "activating tainted repos"
- DNFPARAM=--installroot="${BUILD_PATH}" config-manager --set-enabled "${extrasect_baseprefix}-${ARM_VERSION}-tainted"
- if [ "${MAGEIA_VERSION}" != "cauldron" ]; then
- DNFPARAM=$DNFPARAM" --set-enabled ${extrasect_updatesprefix}-${ARM_VERSION}-tainted"
- fi
- /usr/bin/dnf $DNFPARAM
- err=${?}
- if [ ${err} -ne 0 ]; then
- error "line ${LINENO} error ${err} - can't activate tainted repositories : exiting"
- exit ${ERR_1}
- fi
- fi
- return 0
- }
- # adding Mageia urpmi repositories
- function addurpmimedia()
- {
- title "Creating media ${MIRROR}"
- info "Removing old media"
- /sbin/urpmi.removemedia --urpmi-root "${BUILD_PATH}" -a
- info "Adding media"
- if [ ! -z "${MIRROR}" ] ; then
- /sbin/urpmi.addmedia --urpmi-root "${BUILD_PATH}" --distrib "${MIRROR}"
- else
- info "MIRROR variable not set, using mirrorlist."
- /sbin/urpmi.addmedia --urpmi-root "${BUILD_PATH}" --distrib --mirrorlist "http://mirrors.mageia.org/api/mageia.${MAGEIA_VERSION}.${ARM_VERSION}.list"
- fi
- err=${?}
- if [ ${err} -ne 0 ]; then
- error "line ${LINENO} error ${err} - can't add medias from ${MIRROR} : exiting"
- exit ${ERR_1}
- fi
- if [ ${NONFREE} -eq 1 ]; then
- title "activating non-free repos"
- /sbin/urpmi.update --urpmi-root "${BUILD_PATH}" --no-ignore Nonfree\ Release Nonfree\ Updates
- err=${?}
- if [ ${err} -ne 0 ]; then
- error "line ${LINENO} error ${err} - can't activate medias nonfree : exiting"
- exit ${ERR_1}
- fi
- fi
- if [ ${TAINTED} -eq 1 ]; then
- title "activating tainted repos"
- /sbin/urpmi.update --urpmi-root "${BUILD_PATH}" --no-ignore Tainted\ Release Tainted\ Updates
- err=${?}
- if [ ${err} -ne 0 ]; then
- error "line ${LINENO} error ${err} - can't activate medias tainted : exiting"
- exit ${ERR_1}
- fi
- fi
- return 0
- }
- function installbasesystem()
- {
- # Create Build path
- if ! [ -d "${BUILD_PATH}" ]; then
- warning "Build path ( ${BUILD_PATH} ) does not exist, do you want to create it ? [Y|n] "
- read yn
- if [ -z ${yn} ] || [ ${yn} = "Y" ] || [ ${yn} = "y" ]; then
- title "Creating ${BUILD_PATH}"
- /usr/bin/mkdir -p "${BUILD_PATH}/usr/bin" "${BUILD_PATH}/usr/lib/binfmt.d"
- if [ ${?} -ne 0 ]; then
- error "line ${LINENO} can't create ${BUILD_PATH} : exiting"
- exit ${ERR_1}
- fi
- else
- exit ${ERR_1}
- fi
- else
- info "Build path exists"
- fi
- # Workaround mga#26044.
- info "Preparing root filesystem tree"
- mkdir -p "${BUILD_PATH}/usr/bin"
- mkdir -p "${BUILD_PATH}/usr/sbin"
- mkdir -p "${BUILD_PATH}/usr/lib"
- ln -sr "${BUILD_PATH}/usr/bin" "${BUILD_PATH}/bin"
- ln -sr "${BUILD_PATH}/usr/sbin" "${BUILD_PATH}/sbin"
- ln -sr "${BUILD_PATH}/usr/lib" "${BUILD_PATH}/lib"
- if [ "${ARM_VERSION}" = "aarch64" ]; then
- mkdir -p "${BUILD_PATH}/usr/lib64"
- ln -sr "${BUILD_PATH}/usr/lib64" "${BUILD_PATH}/lib64"
- fi
- title "installing basesystem"
- #/usr/bin/dnf --installroot="${BUILD_PATH}" ${DNF_MIRROROPTS} --assumeyes install shadow-utils basesystem-minimal
- installpkg "shadow-utils basesystem-minimal"
- if [ ${?} -ne 0 ]; then
- error "line ${LINENO} error installing shadow-utils or basesystem-minimal : exiting"
- exit ${ERR_1}
- fi
- #/usr/bin/dnf --installroot="${BUILD_PATH}" ${DNF_MIRROROPTS} --assumeyes install basesystem-minimal
- #if [ ${?} -ne 0 ]; then
- # error "line ${LINENO} error installing basesystem-minimal : exiting"
- # exit ${ERR_1}
- #fi
- if [ ${INSTALL_METHOD} = "urpmi" ]; then
- installpkg "urpmi locales"
- else
- installpkg "dnf dnf-plugins-core locales"
- fi
- #/usr/bin/dnf --installroot="${BUILD_PATH}" ${DNF_MIRROROPTS} --assumeyes install dnf dnf-plugins-core locales
- #if [ ${?} -ne 0 ]; then
- # error "line ${LINENO} error installing dnf or locales : exiting"
- # exit ${ERR_1}
- #fi
- #if [ "${ARM_VERSION}" = "aarch64" ]; then
- #/usr/bin/dnf --installroot="${BUILD_PATH}" ${DNF_MIRROROPTS} --assumeyes install u-boot
- # installpkg "/u-boot*"
- # if [ ${?} -ne 0 ]; then
- # error "line ${LINENO} error installing u-boot : exiting"
- # exit ${ERR_1}
- # fi
- #fi
- #/usr/bin/dnf --installroot="${BUILD_PATH}" ${DNF_MIRROROPTS} --assumeyes install urpmi
- #installpkg "urpmi"
- #if [ ${?} -ne 0 ]; then
- # error "line ${LINENO} error installing urpmi : exiting"
- # exit ${ERR_1}
- #fi
- return 0
- }
- function installpkg()
- {
- if [ ${INSTALL_METHOD} = "urpmi" ]; then
- /usr/sbin/urpmi --urpmi-root "$BUILD_PATH" --ignorearch --no-verify-rpm --auto --split-level 200 --split-length 200 $3 $1;
- else
- /usr/bin/dnf $2 --installroot="${BUILD_PATH}" ${DNF_MIRROROPTS} --assumeyes install $1;
- fi
- }
- function preparechroot()
- {
- title "Preparing chrooting in ${BUILD_PATH}"
- info "making /etc/hostname"
- echo "${HOSTNAME}" > "${BUILD_PATH}/etc/hostname"
- info "copying second stage script in ${BUILD_PATH}"
- #echo "/usr/bin/cp ${CONFIG_PATH}/second_stage_install.sh ${BUILD_PATH}/"
- cp --preserve=mode "${CONFIG_PATH}/second_stage_install.sh" "${BUILD_PATH}/second_stage_install.sh"
- if [ ${?} -ne 0 ]; then
- error "line ${LINENO} error copying ${CONFIG_PATH}/second_stage_install.sh : exiting"
- exit ${ERR_1}
- fi
- info "configuring second_install_install.sh for ${INSTALL_METHOD}"
- if [ ${INSTALL_METHOD} = "urpmi" ]; then
- sed -i -e "s/<INSTALL_EXEC>/sbin\/urpmi --no-verify-rpm --auto/g" "${BUILD_PATH}/second_stage_install.sh"
- else
- sed -i -e "s/<INSTALL_EXEC>/bin\/dnf --nogpgcheck --assumeyes install/g" "${BUILD_PATH}/second_stage_install.sh"
- fi
- info "Preparation for setting root and user account"
- if [ -n "${ROOT_PWD}" ]; then #If root password defined, it will be setted at the end of the chroot of the second_stage_install.sh script
- echo -e "/usr/bin/passwd << EOF\n${ROOT_PWD}\n${ROOT_PWD}\nEOF\n" >>"${BUILD_PATH}/second_stage_install.sh"
- else
- info "No root password... One password will be set at the first login."
- echo -e "/usr/bin/passwd -d root\n/usr/bin/passwd -e root\n" >>"${BUILD_PATH}/second_stage_install.sh"
- fi
- if [ -n "${ID_USER}" ]; then #If user ID defined, it will be setted at the end of the chroot of the second_stage_install.sh script
- echo -e " /sbin/useradd ${ID_USER}\n" >>"${BUILD_PATH}/second_stage_install.sh"
- if [ -n "${PASSWORD}" ]; then
- echo -e "/usr/bin/passwd ${ID_USER} << EOF\n${PASSWORD}\n${PASSWORD}\nEOF" >>"${BUILD_PATH}/second_stage_install.sh"
- else
- echo -e "/usr/bin/passwd -d ${ID_USER}\n" >>"${BUILD_PATH}/second_stage_install.sh"
- fi
- else
- info "No user defined, no user account created."
- fi
- # BUG: This line break the default .bashrc file from the package "rootfiles" and the values as PATH (and /usr/sbin programs).
- # info "Copying skel in root directory"
- # /usr/bin/rsync -rlptH "${BUILD_PATH}/etc/skel/" "${BUILD_PATH}/root/"
- postPrepareChroot
- if [ ${?} -ne 0 ]; then
- error "line ${LINENO} error in postPrepareChroot function."
- exit ${ERR_1}
- fi
- return 0
- }
- function jumpchroot()
- {
- title "chrooting to ${BUILD_PATH}"
- info "mounting dev, sys, proc directories in chroot"
- /usr/bin/mount -B /dev "${BUILD_PATH}/dev"
- if [ ${?} -ne 0 ]; then
- error "line ${LINENO} error mounting ${BUILD_PATH}/dev : exiting"
- exit ${ERR_1}
- fi
- /usr/bin/mount -B /sys "${BUILD_PATH}/sys"
- if [ ${?} -ne 0 ]; then
- error "line ${LINENO} error mounting ${BUILD_PATH}/sys : exiting"
- exit ${ERR_1}
- fi
- /usr/bin/mount -B /proc "${BUILD_PATH}/proc"
- if [ ${?} -ne 0 ]; then
- error "line ${LINENO} error mounting ${BUILD_PATH}/proc : exiting"
- exit ${ERR_1}
- fi
- info "Copying resolv.conf"
- /usr/bin/cp -v --preserve=mode /etc/resolv.conf "${BUILD_PATH}/etc/"
- if [ ${?} -ne 0 ]; then
- error "line ${LINENO} error copying ${BUILD_PATH}/etc/resolv.conf : exiting"
- exit ${ERR_1}
- fi
- info "chrooting to ${BUILD_PATH}"
- if [ "$OPT" = "chroot" ]; then
- info "/sbin/chroot ${BUILD_PATH}"
- /sbin/chroot "${BUILD_PATH}"
- RET=${?}
- if [ ${RET} -ne 0 ]; then
- error "line ${LINENO} Warning : chrooting to ${BUILD_PATH} retrurn an error ${RET}"
- ERRORN=$((${ERRORN}+1))
- fi
- else
- info "/sbin/chroot --userspec root:root ${BUILD_PATH} /usr/bin/bash -v -c 'bash /second_stage_install.sh 2>&1'"
- /sbin/chroot --userspec root:root "${BUILD_PATH}" /usr/bin/bash -v -c 'bash /second_stage_install.sh 2>&1'
- RET=${?}
- if [ ${RET} -ne 0 ]; then
- error "line ${LINENO} Warning : chrooting to ${BUILD_PATH} retrurn an error ${RET}"
- ERRORN=$((${ERRORN}+1))
- fi
- fi
- info "unmounting dev, sys, proc"
- /usr/bin/umount -lf "${BUILD_PATH}/dev"
- if [ ${?} -ne 0 ]; then
- warning "line ${LINENO} Warning : error unmounting ${BUILD_PATH}/dev, continuing anyway"
- ERRORN=$((${ERRORN}+1))
- fi
- /usr/bin/umount -lf "${BUILD_PATH}/sys"
- if [ ${?} -ne 0 ]; then
- warning "line ${LINENO} Warning : error unmounting ${BUILD_PATH}/sys, continuing anyway"
- ERRORN=$((${ERRORN}+1))
- fi
- /usr/bin/umount -lf "${BUILD_PATH}/proc"
- if [ ${?} -ne 0 ]; then
- warning "line ${LINENO} Warning : error unmounting ${BUILD_PATH}/proc, continuing anyway"
- ERRORN=$((${ERRORN}+1))
- fi
- return 0
- }
- function createImageWrap()
- {
- title "Wrap image creation"
- preImgCreation
- if [ ${?} -ne 0 ]; then
- error "line ${LINENO} error in the process ${CONFIG_PATH}/specialFunctions.sh ."
- exit ${ERR_1}
- fi
- createimage
- if [ -z "${BOOTFS}" ]; then
- BOOTFS="ext4"
- fi
- formatpartitions ${BOOTFS} ext4
- return 0
- }
- function createimage()
- {
- title " in ${IMAGE}"
- if [ -f "${INSTALL_PATH}/${IMAGE}" ]; then
- warning "Deleting previous image"
- /usr/bin/rm -f "${INSTALL_PATH}/${IMAGE}"
- if [ ${?} -ne 0 ]; then
- error "line ${LINENO} error can't remove previous image at ${INSTALL_PATH}/${IMAGE} : exiting"
- exit ${ERR_1}
- fi
- fi
- warning "please wait until end of image creation"
- /usr/bin/dd if=/dev/zero of="${INSTALL_PATH}/${IMAGE}" bs=1MB count=$(( ${IMAGE_SIZE} * 1024 )) status=progress
- if [ ${?} -ne 0 ]; then
- error "line ${LINENO} can't make image at ${INSTALL_PATH}/${IMAGE} : exiting"
- exit ${ERR_1}
- fi
- loopingImage
- bunrningBootloader
- if [ ${?} -ne 0 ]; then
- error "line ${LINENO} error in the process ${CONFIG_PATH}/specialFunctions.sh ."
- exit ${ERR_1}
- fi
- info "making partitions"
- echo -e "${FDISK_SCRIPT}" | /sbin/fdisk ${DEVICE}
- #Activate 'pY' : /dev/loopXpY
- partx -vu "${DEVICE}"
- #Previous function give us a list of partition. It is easy to get it and define prior this list the partition.
- #But... How to distinguish between the boot p1 and the root p2 if both are empty and ext4 ? ...
- if [ 1 -eq ${SEPARATE_BOOT_PARTITION} ]; then
- BOOTP="${DEVICE}p1"
- ROOTP="${DEVICE}p2"
- else
- ROOTP="${DEVICE}p1"
- fi
- return 0
- }
- function loopingImage()
- {
- title "Looping image ..."
- # Mettre en place et contrôler des périphériques boucle.
- # -f, --find trouver le premier périphérique inutilisé
- # --show afficher le nom du périphérique après configuration (avec -f)
- DEVICE=$(/sbin/losetup -f --show "${INSTALL_PATH}/${IMAGE}")
- return 0
- }
- function formatpartitions()
- {
- info "Formatting partitions"
- if [ 1 -eq ${SEPARATE_BOOT_PARTITION} ]; then
- info "Boot : ${BOOTP} as ${1}"
- "/sbin/mkfs.${1}" "${BOOTP}"
- if [ ${?} -ne 0 ]; then
- error "line ${LINENO} error formating ${BOOTP} : exiting"
- /sbin/losetup -d "${DEVICE}"
- exit ${ERR_1}
- fi
- BOOT_UUID=$(blkid -s UUID -o value UUID "${BOOTP}")
- info "Boot UUID: ${BOOT_UUID}"
- fi
- info "Root : ${ROOTP} as ${2}"
- "/sbin/mkfs.${2}" "${ROOTP}"
- if [ ${?} -ne 0 ]; then
- error "line ${LINENO} error formating ${ROOTP} : exiting"
- /sbin/losetup -d "${DEVICE}"
- exit ${ERR_1}
- fi
- ROOT_UUID=$(blkid -s UUID -o value UUID "${ROOTP}")
- info "Root UUID: ${ROOT_UUID}"
- return 0
- }
- function generateExtlinux()
- {
- info "Generate extlinux if extlinux.conf exists."
- if [ -e "${CONFIG_PATH}/extlinux.conf" ]; then
- info "\tFound extlinux.conf"
- if [ ! -d "${BUILD_PATH}/boot/extlinux" ]; then
- info "making /boot/extlinux/extlinux.conf"
- /usr/bin/rm -rf "${BUILD_PATH}/boot/extlinux"
- mkdir -p "${BUILD_PATH}/boot/extlinux"
- fi
- if [ ! -f "${BUILD_PATH}/boot/extlinux/extlinux.conf" ]; then
- info "\tTuning extlinux.conf"
- cp "${CONFIG_PATH}/extlinux.conf" "${BUILD_PATH}/boot/extlinux/extlinux.conf"
- #In order to use the UUID, a initrd file is necessary.
- sed -i -e "s/<UUID>/${ROOT_UUID}/g" "${BUILD_PATH}/boot/extlinux/extlinux.conf"
- sed -i -e "s/<BOOT_ARGS>/${BOOT_ARGS}/g" "${BUILD_PATH}/boot/extlinux/extlinux.conf"
- KERNEL_ID=$(basename ${BUILD_PATH}/usr/lib/linux-*)
- sed -i -e "s/<FDTDIR>/${KERNEL_ID}/g" "${BUILD_PATH}/boot/extlinux/extlinux.conf"
- fi
- fi
- }
- function copyingsystem()
- {
- generateExtlinux
- info "Remove second_stage_install.sh"
- /usr/bin/rm -f "${BUILD_PATH}/second_stage_install.sh"
- if [ 1 -eq ${SEPARATE_BOOT_PARTITION} ]; then
- ARM_BOOT="${BUILD_PATH}/mnt/arm_boot"
- if ! [ -d "${ARM_BOOT}" ]; then
- rm -rf "${ARM_BOOT}"
- mkdir -p "${ARM_BOOT}"
- fi
- info "copying Mageia image to root partition"
- /usr/bin/rsync -rlptogDH --exclude "${ARM_BOOT}/" --exclude "qemu-arm-static*" "${BUILD_PATH}/" "${ROOT}/"
- /usr/bin/rsync -rlptogDH "${ARM_BOOT}/" "${BOOT}/"
- #/usr/bin/rsync -rlptogDH "${BUILD_PATH}/boot/" "${BOOT}/"
- else
- info "copying Mageia image to root partition"
- /usr/bin/rsync -rlptogDH --exclude "qemu-arm-static*" "${BUILD_PATH}/" "${ROOT}/"
- fi
- copyingCustomSystem
- if [ ${?} -ne 0 ]; then
- error "line ${LINENO} error in the process ${CONFIG_PATH}/specialFunctions.sh ."
- exit ${ERR_1}
- fi
- copyingcommon
- sync
- warning "You can now burn the image ( ${INSTALL_PATH}/${IMAGE} ) on SD card"
- return 0
- }
- function mountPartitions(){
- info "mounting partitions, making mountpoint if necessary"
- if [ 1 -eq ${SEPARATE_BOOT_PARTITION} ]; then
- if ! [ -d "${BOOT}" ]; then
- /usr/bin/mkdir "${BOOT}"
- if [ ${?} -ne 0 ]; then
- error "line ${LINENO} error making directory ${BOOT} : exiting"
- exit ${ERR_1}
- fi
- fi
- /usr/bin/mount "${BOOTP}" "${BOOT}"
- if [ ${?} -ne 0 ]; then
- error "line ${LINENO} error mounting ${BOOTP} : exiting"
- exit ${ERR_1}
- fi
- fi
- if ! [ -d "${ROOT}" ]; then
- /usr/bin/mkdir "${ROOT}"
- if [ ${?} -ne 0 ]; then
- error "line ${LINENO} error making directory ${ROOT} : exiting"
- exit ${ERR_1}
- fi
- fi
- /usr/bin/mount "${ROOTP}" "${ROOT}"
- if [ ${?} -ne 0 ]; then
- error "line ${LINENO} error mounting ${ROOTP} : exiting"
- exit ${ERR_1}
- fi
- }
- function unmountingPartitions()
- {
- title "Unmounting partitions..."
- # Syncing devices before unmounting
- /usr/bin/sync
- if [ -z "${BOOTP}" ] && [ -z "${ROOTP}" ] ; then
- warning "Root partition and Boot partition not defined !"
- for LOOP in $(losetup -l -O NAME,BACK-FILE -n | grep "${IMAGE}" | cut -d ' ' -f 1 ) ; do
- IFS=$'\n'
- for PARTITION in $(mount -l | grep ${LOOP}); do
- MOUNTPOINT=$(echo "$PARTITION" | cut -d ' ' -f 3)
- PARTITION=$(echo "$PARTITION" | cut -d ' ' -f 1)
- info "unmount ${PARTITION} and remove ${MOUNTPOINT}"
- /usr/bin/umount "${PARTITION}"
- if [ ${?} -eq 0 ]; then
- /usr/bin/rmdir "${MOUNTPOINT}"
- else
- error "line ${LINENO} error unmounting ${BOOT}..."
- fi
- done
- unset IFS
- done
- else
- if [ ! -z "${BOOTP}" ]; then
- /usr/bin/umount "${BOOTP}"
- if [ ${?} -eq 0 ]; then
- /usr/bin/rmdir "${BOOT}"
- else
- error "line ${LINENO} error unmounting ${BOOT}..."
- fi
- fi
- /usr/bin/umount "${ROOTP}"
- if [ ${?} -eq 0 ]; then
- /usr/bin/rmdir "${ROOT}"
- else
- error "line ${LINENO} error unmounting ${ROOT}..."
- fi
- fi
- return 0
- }
- function unloopingImage(){
- title "Unlooping image..."
- # Syncing devices before unmounting
- /usr/bin/sync
- # [root@jabztop mageia4arm (master)]# losetup -l -O NAME,BACK-FILE -n
- # /dev/loop0 /home/jibz/workspaces/mageia4arm/build/Mageia-7-bananaPro1.img (deleted)
- info "Looped devices to unmount : $(losetup -l -O NAME,BACK-FILE -n | grep "${IMAGE}" | cut -d ' ' -f 1 ) "
- for LOOP in $(losetup -l -O NAME,BACK-FILE -n | grep "${IMAGE}" | cut -d ' ' -f 1 ) ; do
- info "removing ${LOOP}"
- for PARTITION in ${LOOP}p* ; do
- partx -dv "${PARTITION}"
- done
- # losetup -d "${LOOP}"
- kpartx -d "${INSTALL_PATH}/${IMAGE}"
- done
- return 0
- }
- # Copying files common to all systems
- function copyingcommon()
- {
- title "Copying common files and configuration"
- rsync -rlptDH "${SOURCE_PATH}/common/" "${ROOT}/"
- rsync -rlptDH "${SOURCE_PATH}/tools/"*.sh "${ROOT}/usr/local/usr/bin/"
- chown root:root "${ROOT}/usr/local/usr/bin/"
- return 0
- }
- function mkfstab()
- {
- title "making /etc/fstab"
- ### BUG : /mnt/arm_boot is set to vfat for all plateforms, odroid configuration says ext4.
- #echo -e "proc /proc proc defaults 0 0\nUUID=${BOOT_UUID} /mnt/arm_boot vfat defaults 0 0\nUUID=${ROOT_UUID} / ext4 defaults 0 0" > "${BUILD_PATH}/etc/fstab"
- #echo -e "proc\t/proc\tproc\tdefaults\t0\t0\nUUID=${BOOT_UUID}\t/mnt/arm_boot\t${BOOTFS}\tdefaults\t0\t0\nUUID=${ROOT_UUID}\t/\text4\tdefaults\t0\t0" > "${BUILD_PATH}/etc/fstab"
- if [ 1 -eq ${SEPARATE_BOOT_PARTITION} ]; then
- echo -e "proc\t/proc\tproc\tdefaults\t0\t0\nUUID=${BOOT_UUID}\t/mnt/arm_boot\t${BOOTFS}\tdefaults\t0\t0\nUUID=${ROOT_UUID}\t/\text4\tdefaults\t0\t0" > "${BUILD_PATH}/etc/fstab"
- else
- echo -e "proc\t/proc\tproc\tdefaults\t0\t0\nUUID=${ROOT_UUID}\t/\text4\tdefaults\t0\t0" > "${BUILD_PATH}/etc/fstab"
- fi
- return 0
- }
- # cleaning build space
- function clean()
- {
- title "Cleaning"
- unmountingPartitions
- unloopingImage
- # Removing old Build directory
- if [ -d "${BUILD_PATH}" ]; then
- info "Removing ${BUILD_PATH}"
- /usr/bin/rm -Rf "${BUILD_PATH}"
- else
- warning "${BUILD_PATH} does not exists"
- fi
- # removing old image
- if [ -e "${INSTALL_PATH}/${IMAGE}" ]; then
- info "Removing ${IMAGE}"
- /usr/bin/rm -f "${INSTALL_PATH}/${IMAGE}"
- else
- warning "${IMAGE} does not exists"
- fi
- return 0
- }
- function preImgCreation()
- {
- error "This function is called if no sourced file about fdisk was lauched."
- return 1
- }
- function postPrepareChroot()
- {
- error "This function is called if no sourced file is containing postPrepareChroot function."
- return 1
- }
- function bunrningBootloader()
- {
- error "This function is called if no sourced file is containing bunrningBootloader function."
- return 1
- }
- function copyingCustomSystem()
- {
- error "This function is called if no sourced file is containing bunrningBootloader function."
- return 1
- }
- ERRORN=0
- ERR_1=1
- ERR_DEFAULT_CONFIG=2
- ERR_NO_CONFIG_FILE=3
- ERR_NOT_ROOT=4
- ERR_DEPENDENCY_MISSING=5
- ERR_NO_SPACE=6
- ERR_7=7
- ERR_8=8
- ERR_9=9
- SEPARATE_BOOT_PARTITION=1
- #######################
- # PROGRAMM START HERE #
- #######################
- info ${0}
- #Check dependencies :
- #if [ -e /usr/bin/qemu-arm-static ]; then
- # error "qemu-user-static package is needed."
- # exit ${ERR_DEPENDENCY_MISSING}
- #fi
- if [ 0 -ne $(id -u) ]; then
- error "Script need to be run as root."
- exit ${ERR_NOT_ROOT}
- fi
- SOURCE_PATH="$(/usr/bin/dirname "$(readlink -f "${0}")")"
- INSTALL_PATH="$(pwd)/build"
- # FILES_PATH="${SOURCE_PATH}/files"
- PLATFORMS_PATH="${SOURCE_PATH}/platforms"
- CMDNAME=$(/usr/bin/basename "${0}")
- if [ ${#} == 0 ]; then
- help
- exit
- fi
- # parsing commandline
- TEMP=$(getopt -o h,a --long all,help,clean,create-chroot,prepare-chroot,jump-chroot,add-urpmimedia,create-image,,config:,target:,target-version:,chroot,bootfs:,install-basesystem,update-mirror,build-path:,size:,nonfree,tainted -n ${CMDNAME} -- "${@}")
- if [ ${?} -ne 0 ] ; then error "line ${LINENO} Failed parsing options." >&2 ; exit ${ERR_1} ; fi
- eval set -- "${TEMP}"
- echo "${TEMP}"
- # Note the quotes around `$TEMP': they are essential! Or not, $( ) do the same as ` `. But there are the ` ' used ?
- while true; do
- case "${1}" in
- -h|--help)
- help
- exit 0
- ;;
- -a|--all)
- OPT="all"
- shift 1
- ;;
- --clean)
- OPT="clean"
- shift
- ;;
- --size)
- IMAGE_SIZE_P=${2}
- shift 2
- ;;
- --build-path)
- INSTALL_PATH_P=$(readlink -f "${2}")
- shift 2
- ;;
- --chroot)
- if [ -z ${OPT} ]; then
- OPT="chroot"
- fi
- shift
- ;;
- --create-chroot)
- if [ -z ${OPT} ]; then
- OPT="createchroot"
- fi
- shift
- ;;
- --prepare-chroot)
- if [ -z ${OPT} ]; then
- OPT="preparechroot"
- fi
- shift
- ;;
- --jump-chroot)
- if [ -z ${OPT} ]; then
- OPT="jumpchroot"
- fi
- shift
- ;;
- --add-urpmimedia)
- if [ -z ${OPT} ]; then
- OPT="addurpmimedia"
- fi
- shift
- ;;
- --update-mirror)
- if [ -z ${OPT} ]; then
- OPT="updatemirror"
- fi
- shift
- ;;
- --install-basesystem)
- if [ -z ${OPT} ]; then
- OPT="installbasesystem"
- fi
- shift
- ;;
- --create-image)
- if [ -z ${OPT} ]; then
- CREATEIMAGE=true
- OPT="createimage"
- fi
- shift
- ;;
- --target)
- TARGET_P=${2}
- shift 2
- ;;
- --target-version)
- TARGET_VERSION_P=${2}
- shift 2
- ;;
- --config)
- CONFIG_PATH="${PLATFORMS_PATH}/${2}"
- shift 2
- ;;
- --bootfs)
- BOOTFS_P=${2}
- shift 2
- ;;
- --nonfree)
- NONFREE_P=1
- shift
- ;;
- --tainted)
- TAINTED_P=1
- shift
- ;;
- --)
- shift
- break;;
- *)
- error "Parameter ${1} does not exists "
- exit ${ERR_1};;
- esac
- done
- # path of config file
- if ! [ -d "${CONFIG_PATH}" ]; then
- info " Config path does not exists, defaulting to ./platforms/${TARGET}"
- CONFIG_PATH="${PLATFORMS_PATH}/${TARGET}"
- warning "Do you want to create it and to copy the template file in? [Y|n] "
- read yn
- if [ -z ${yn} ] || [ ${yn} = "Y" ] || [ ${yn} = "y" ]; then
- /usr/bin/mkdir "${CONFIG_PATH}/"
- /usr/bin/cp --preserve=mode "${SOURCE_PATH}/mageia4arm.cfg.template" "${CONFIG_PATH}/mageia4arm.cfg"
- warning "You need now to modify the config file (${CONFIG_PATH}/mageia4arm.cfg) and relaunch the script"
- exit ${ERR_DEFAULT_CONFIG}
- else
- error "Error: Can't continue without config file, exiting"
- exit ${ERR_NO_CONFIG_FILE}
- fi
- fi
- if [ -e "${CONFIG_PATH}/mageia4arm.cfg" ]; then
- info "using ${CONFIG_PATH}/mageia4arm.cfg as config"
- source "${CONFIG_PATH}/mageia4arm.cfg" #NOTE1 : Here is sourced a file with variables.
- else
- warning "Config file does not exists, do you want to copy template ? [Y|n] "
- read yn
- if [ -z ${yn} ] || [ ${yn} = "Y" ] || [ ${yn} = "y" ]; then
- /usr/bin/cp --preserve=mode "${SOURCE_PATH}/mageia4arm.cfg.template" "${CONFIG_PATH}/mageia4arm.cfg"
- warning "You need now to modify the config file (${CONFIG_PATH}/mageia4arm.cfg) and relaunch the script"
- exit ${ERR_DEFAULT_CONFIG}
- fi
- fi
- if ! [ -z "${IMAGE_SIZE_P}" ]; then
- IMAGE_SIZE=${IMAGE_SIZE_P}
- fi
- if ! [ -z "${INSTALL_PATH_P}" ]; then
- INSTALL_PATH=${INSTALL_PATH_P}
- fi
- if ! [ -z "${TARGET_P}" ]; then
- TARGET=${TARGET_P}
- fi
- if ! [ -z "${TARGET_VERSION_P}" ]; then
- TARGET_VERSION=${TARGET_VERSION_P}
- fi
- if ! [ -z "${BOOTFS_P}" ]; then
- BOOTFS=${BOOTFS_P}
- fi
- if ! [ -z "${NONFREE_P}" ]; then
- NONFREE=${NONFREE_P}
- else
- NONFREE=0
- fi
- if ! [ -z "${TAINTED_P}" ]; then
- TAINTED=${TAINTED_P}
- else
- TAINTED=0
- fi
- info "Option: "${OPT}
- IMAGE="Mageia-${MAGEIA_VERSION}-${TARGET}${TARGET_VERSION}.img"
- BOOT="/mnt/boot"
- ROOT="/mnt/root"
- BUILD_PATH="${INSTALL_PATH}/build-${TARGET}${TARGET_VERSION}"
- # ARM_VERSION="armv7hl"
- DNF_CONFIGPATH="$(dirname $(realpath "$0"))/mageia4arm-dnf.conf"
- # Assigne a script for creating a particular target image (rpi, odroid, ...)
- if [ -e "${CONFIG_PATH}/specialFunctions.sh" ]; then
- source "${CONFIG_PATH}/specialFunctions.sh"
- fi
- info "target : ${TARGET}"
- info "target_version : ${TARGET_VERSION}"
- info "source path: ${SOURCE_PATH}"
- info "Install path: ${INSTALL_PATH}"
- info "Build path: ${BUILD_PATH}"
- info "Commande : ${CMDNAME}"
- info "Firmware path : ${FIRMWARE_PATH}"
- info "Firmware dir : ${FIRMWARE_DIR}"
- info "Installation method : ${INSTALL_METHOD}"
- # Defining default image size to 7 go
- if [[ "${IMAGE_SIZE}" -lt 2 ]] || [[ "${IMAGE_SIZE}" -gt 128 ]]; then
- IMAGE_SIZE=7
- fi
- # change dir to install path and create it if not existing
- if [ "${OPT}" != "clean" ]; then
- info "cd ${INSTALL_PATH}"
- if ! [ -e "${INSTALL_PATH}" ]; then
- /usr/bin/mkdir -p "${INSTALL_PATH}"
- if [ -z ${?} ]; then
- error "line ${LINENO} can't make directory ${INSTALL_PATH} , exiting"
- exit ${ERR_1}
- fi
- fi
- cd "${INSTALL_PATH}"
- if [ -z ${?} ]; then
- error "line ${LINENO} can't change to directory ${INSTALL_PATH} , exiting"
- exit ${ERR_1}
- fi
- fi
- #if no parameters then display help message
- if [ -z ${OPT} ]; then
- OPT="--help"
- fi
- info "Image size is: ${IMAGE_SIZE} GB"
- case ${OPT} in
- "clean")
- clean
- ;;
- "all")
- verify_disk_space
- if [ ${?} -ne 0 ]; then
- echo -e "Not enough space on disk\nDo you want to continue anyway ? [Y,n]"
- read yn
- if [ ${yn} = "n" ]; then
- exit ${ERR_NO_SPACE}
- fi
- fi
- createImageWrap #Create the empty .img
- createchroot #Check qemu and activate it
- if [ ${INSTALL_METHOD} = "urpmi" ]; then
- addurpmimedia
- else
- genusemirroroptions #Generate mirror options, if needed
- enableextrarepos
- fi
- installbasesystem #Generate the rootfs, rootfiles, ...
- mkfstab
- preparechroot
- jumpchroot
- mountPartitions
- copyingsystem
- unmountingPartitions
- unloopingImage
- ;;
- "createchroot")
- createchroot
- ;;
- "preparechroot")
- preparechroot
- ;;
- "jumpchroot")
- jumpchroot
- ;;
- "chroot")
- jumpchroot
- ;;
- "addurpmimedia")
- addurpmimedia
- ;;
- "createimage")
- verify_disk_space
- if [ ${?} -ne 0 ]; then
- warning "Not enough space on disk"
- exit ${ERR_NO_SPACE}
- fi
- createImageWrap
- ;;
- "installbasesystem")
- installbasesystem
- ;;
- esac
- if [ ${ERRORN} -ne 0 ]; then
- warning "Some errors occurs : ${ERRORN} errors"
- fi
- exit ${ERRORN}
|