# -*-Shell-script-*- # # Common functions for build scripts used by various packages # incorporated (e.g., build, bootcd, nodeimage, sliceimage) # # Marc E. Fiuczynski # Copyright (C) 2007 The Trustees of Princeton University # Thierry Parmentelat INRIA # # supported distros fedoras, and debians/ubuntus to a lesser extent # centos's and scientific linux's have been used too quite a while ago # # for locating pkgs.py export PATH=.:$PATH # returns 'Fedora' or 'CentOS' for now function pl_getDistro() { if [ -f "/etc/redhat-release" ] ; then distro=$(awk ' { print $1 } ' /etc/redhat-release) case $distro in Scientific*) distro="SL" ; esac elif [ -f /etc/lsb-release ] ; then . /etc/lsb-release distro=$DISTRIB_CODENAME elif [ -f /etc/debian_version ] ; then case $(cat /etc/debian_version) in 7.*) distro=wheezy ;; # might be that I'm getting 'jessie'sid' just because it's still testing.. 8.*|jessie*) distro=jessie ;; *) distro=unknown.debian.in.build.common ;; esac fi [ -z "$distro" ] && { echo "build.common.pl_getDistro-unknown"; exit 1; } echo "$distro" return 0 } # returns something like 8, 10, or 5.3 function pl_getRelease() { if [ -f "/etc/redhat-release" ] ; then release=$(awk ' { if ($1=="Fedora" && $2=="Core") print $4 ; if (($1=="Fedora" && $2!="Core")||$1=="CentOS") print $3 ; if ($1=="Scientific") print $4 } ' /etc/redhat-release) else echo "build.common.pl_getRelease-unknown" exit 1 fi # keep only the major number echo "$release" | cut -d. -f1 return 0 } # returns distro shortname, something like 'f8' or 'centos5' function pl_getReleaseName () { distro=$1; shift release=$1; shift case $distro in [Ff]edora*) releasename=f$release ;; [Cc]entOS*) old_IFS="$IFS" IFS="." set -- $release IFS="$old_IFS" releasename=centos$1 ;; [Ss]L*) releasename=sl$release ;; wheezy|jessie|trusty|xenial|bionic|focal|jammy) releasename=$distro ;; *) releasename="unknown-name-for-${pl_DISTRO}-please-edit-build.common" echo 1>&2 "build.common: WARNING - releasename not set for distro=$distro" return 1 ;; esac echo "$releasename" return 0 } # yum exclusions are now defined in yumexclude.pkgs # so they can now depend both on the linux distro and the pl distro function pl_yumexclude () { keyword=$1; shift fcdistro=$1; shift pldistro=$1; shift builddir=$1; shift # search for file "yumexclude.pkgs" yumexclude_file=$(pl_locateDistroFile $builddir $pldistro "yumexclude.pkgs") # # check if pkgs.py is in PATH type -p pkgs.py >& /dev/null || export PATH=$builddir:$PATH # parse it pkgs.py -a $pl_DISTRO_ARCH -f $fcdistro -d $pldistro $keyword $yumexclude_file || \ { echo pl_yumexclude failed with fcdistro=$fcdistro and pldistro=$pldistro; return 1 ; } } # <> fcdistro pldistro builddir # node side : use the 'nodeyumexclude' keywork in yumexclude.pkgs function pl_nodeyumexclude () { pl_yumexclude 'nodeyumexclude' "$@" ; } # server side : use the 'plcyumexclude' keywork in yumexclude.pkgs function pl_plcyumexclude () { pl_yumexclude 'plcyumexclude' "$@" ; } # figure out which redhat distro we are using (fedora, centos, redhat) pl_DISTRO=$(pl_getDistro) # select basearch of the host devel environment - protected for macos for local tests # try arch for testing stuff on a mac pl_DISTRO_ARCH=$(arch 2> /dev/null || echo unknownarch) # the release number (plain number) pl_DISTRO_RELEASE=$(pl_getRelease) # the release name - something like 'f8' or 'sl6' pl_DISTRO_NAME=$(pl_getReleaseName $pl_DISTRO $pl_DISTRO_RELEASE) # get path to appropriate yumgroups.xml file # Thierry: quick & dirty improvement # this file is updated by the toplevel build, from the .pkgs files pl_DISTRO_YUMGROUPS="../../../RPMS/yumgroups.xml" function pl_process_fedora_options () { # Get options shiftcount=0 while getopts "l:r:a:h" opt ; do case $opt in l) pl_DISTRO_URL=$OPTARG let shiftcount=$shiftcount+2 ;; r) pl_DISTRO_RELEASE=$OPTARG let shiftcount=$shiftcount+2 ;; a) pl_DISTRO_ARCH=$OPTARG let shiftcount=$shiftcount+2 ;; h|*) echo "Usage: $0 [OPTION]..." echo " -l url distro mirror location (default: $pl_DISTRO_URL)" echo " -r release distro release number (default: $pl_DISTRO_RELEASE)" echo " -a arch distro architecture (default: $pl_DISTRO_ARCH)" echo "where distro can be either fedora, centos, or redhat" echo " -h This message" exit 1 ;; esac done return $shiftcount } ######################################## handling a root image function pl_root_rpm_macros () { cat <.mirrors" echo " -v Be verbose" echo " -h This message" # echo " target selection (defaults based on current build VM context)" # echo " -r release Fedora release number (default: $releasever)" # echo " -a arch Fedora architecture (default: $basearch)" exit 1 } function pl_root_mkfedora () { echo "* Entering pl_root_mkfedora" "$@" if [ $UID -ne 0 ] ; then echo "Error: You must run this script as root." exit 1 fi # Verbosity verbose=0 # Release and architecture to install : defaults to current vm settings or previously parsed fedora_options releasever=$pl_DISTRO_RELEASE basearch=$pl_DISTRO_ARCH # Get options while getopts "vh" opt ; do case $opt in v) verbose=1; set -x ;; h|*) pl_root_mkfedora_usage ;; esac done shift $(($OPTIND - 1)) [[ "$#" -lt 3 ]] && pl_root_mkfedora_usage vroot=$1 ; shift pldistro=$1 ; shift pkgsfile="$@" vroot=$(cd $vroot && pwd -P) [ -d $vroot ] || pl_root_mkfedora_usage # parse pkgsfile and add to local vars fcdistro=${pl_DISTRO_NAME} pkgs_packages=$(pkgs.py -a $pl_DISTRO_ARCH -f $fcdistro -d $pldistro package $pkgsfile) pkgs_groups=$(pkgs.py -a $pl_DISTRO_ARCH -f $fcdistro -d $pldistro group $pkgsfile) # what can get trashed to save space pkgs_junk=$(pkgs.py -a $pl_DISTRO_ARCH -f $fcdistro -d $pldistro junk $pkgsfile) # but not this pkgs_precious=$(pkgs.py -a $pl_DISTRO_ARCH -f $fcdistro -d $pldistro precious $pkgsfile) # formerly related to mkfedora -k : packages to take from our own build # and thus need be excluded frem the stock repos # locate builddir by looking for pkgs.py builddir=$(dirname $(type -p pkgs.py)) SUBST_NODEYUMEXCLUDE=$(pl_nodeyumexclude $fcdistro $pldistro $builddir) pkgs_yumexclude=$(pkgs.py -a $pl_DISTRO_ARCH -f $fcdistro -d $pldistro yumexclude $pkgsfile | sed -e s,@NODEYUMEXCLUDE@,"$SUBST_NODEYUMEXCLUDE",) # get mirrors if not specified with -l if [ -z "$mirrors" ] ; then mirrorsfile=$(pl_locateDistroFile ../build/ $pldistro "$pldistro.mirrors") # do not sort mirrors, keep order from input mirrors=$(pkgs.py -u -a $pl_DISTRO_ARCH -f $fcdistro -d $pldistro mirror $mirrorsfile) fi yumexclude_line="" [ -n "$pkgs_yumexclude" ] && yumexclude_line="exclude=$pkgs_yumexclude" echo "$0: candidate mirrors" for mirror in $mirrors ; do echo "* candidate mirror $mirror" done # the repo part of the final yum.conf yum_conf_repos=$vroot/xxxmkfedora-repos.confxxx if ! yumconf_mirrors $yum_conf_repos ../build/ $fcdistro "$yumexclude_line" $mirrors ; then echo xxx -- error ; return 1 fi # Do not tolerate errors set -e public_gpg_key=$(yumconf_gpgkey $yum_conf_repos) ## make rpms ignore installing stuff to special fs entries like /proc # Because of https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=52725 # you have to use at least one language beside 'C' # Prevent all locales from being installed in reference image mkdir -p $vroot/etc/rpm pl_root_rpm_macros > $vroot/etc/rpm/macros # Trick rpm and yum, who read the real root /etc/rpm/macros file # rather than the one installed in the reference image, despite what # you might expect the --root and --installroot options to mean. Both # programs always read $HOME/.rpmmacros. export HOME=$vroot/tmp mkdir -p $vroot/tmp pl_root_rpm_macros > $vroot/tmp/.rpmmacros function mkfedora_cleanup () { umount -l $vroot/proc umount -l $vroot/dev/shm umount -l $vroot/dev/pts } # Clean up before exiting if anything goes wrong trap "mkfedora_cleanup" ERR INT # Mount in reference image mount -t devpts none $vroot/dev/pts mount -t tmpfs none $vroot/dev/shm mkdir -p $vroot/proc mount -t proc none $vroot/proc # Create a /var/lib dirs for yum & rpm mkdir -p $vroot/var/lib/yum mkdir -p $vroot/var/lib/rpm mkdir -p $vroot/usr/share/info # Create a dummy /etc/fstab in reference image mkdir -p $vroot/etc cat >$vroot/etc/fstab < $vroot/etc/sysconfig/network # Initialize RPM database in reference image mkdir -p $vroot/var/lib/rpm rpm --root $vroot --initdb rpm --root $vroot --import $public_gpg_key # Initialize yum in reference image mkdir -p $vroot/var/cache/yum $vroot/var/log # yum.conf is for building only - store in different location than /etc/yum.conf yum_conf=$vroot/etc/mkfedora-yum.conf cat > $yum_conf <> $yum_conf # If we are being built as part of an automated RPM build, solve the # bootstrap problem by including any just built packages in the yum # configuration. This cooperates with the PlanetLab build system. if [ -n "$RPM_BUILD_DIR" ] ; then RPM_RPMS_DIR=$(cd $(dirname $RPM_BUILD_DIR)/RPMS && pwd -P) # If run under sudo, allow user to delete the headers/ and # repodata/ directories. if [ -n "$SUDO_USER" ] ; then chown -R $SUDO_USER $RPM_RPMS_DIR fi cat >> $yum_conf < /dev/null && yum_options="$yum_options --verbose" yum_options="$yum_options -y" yum_options="$yum_options -c $yum_conf" yum_options="$yum_options --installroot=$vroot" yum_options="$yum_options --releasever=$releasever" # glibc must be specified explicitly for the correct arch to be # chosen. echo "* Installing glibc" # ignore yum's return code that is basically undefined yum $yum_options install glibc || : # Go, baby, go if [ -n "$pkgs_packages" ] ; then echo "* Installing optional packages" $pkgs_packages # ignore yum's return code that is basically undefined echo "* Install options" $vroot $yum_options yum $yum_options install $pkgs_packages || : if ! rpm --root $vroot -q $pkgs_packages >/dev/null ; then echo "* Warning: Missing packages" rpm --root $vroot -q $pkgs_packages | grep "not installed" fi fi if [ -n "$pkgs_groups" ] ; then ## call yum sequentially to get finer-grained info on dependencies for group_plus in $pkgs_groups ; do group=$(echo $group_plus | sed -e "s,+++, ,g") echo "* Installing optional group $group" # ignore yum's return code that is basically undefined yum $yum_options groupinstall "$group" || : done fi # formerly in bootcd/prep.sh : to optimize footprint if [ -n "$pkgs_junk" ] ; then echo "* Removing unnecessary junk" pushd $vroot # Save precious files [ -n "$pkgs_precious" ] && tar --ignore-failed-read -cpf precious.tar $pkgs_precious # Remove unnecessary junk [ -n "$pkgs_junk" ] && rm -rf $pkgs_junk # Restore precious files [ -n "$pkgs_precious" ] && tar -xpf precious.tar && rm -f precious.tar popd fi # Clean yum cache echo "* Cleaning up" # # NOTE: this hack is for Fedora >= 12. # # if kernel-debug is installed, clean it up # # kernel-debug manually # # we link to our version of kernel/initrd and clean up # if rpm --root $vroot --quiet -q kernel-debug ; then # echo "* Cleaning up kernel-debug - (workaround for f12)" # pushd $vroot/boot/ # rm -rf kernel-boot kernel-bootsmp initrd-boot initrd-bootsmp # ln -s vmlinuz-*${pldistro}* kernel-boot # ln -s vmlinuz-*${pldistro}* kernel-bootsmp # ln -s initrd-*${pldistro}* initrd-boot # ln -s initrd-*${pldistro}* initrd-bootsmp # rpm --root $vroot --nodeps -e kernel-debug || : # popd # fi # ignore yum's return code that is basically undefined yum $yum_options clean all || : # Clean RPM state rm -f $vroot/var/lib/rpm/__db* # Set time zone to UTC if [ -f $vroot/usr/share/zoneinfo/UTC -a -f $vroot/etc/localtime ] ; then rm -f $vroot/etc/localtime ln -s /usr/share/zoneinfo/UTC $vroot/etc/localtime fi echo "Dumping current list of rpms in /etc/mkfedora-rpms.txt" chroot $vroot rpm -aq | sort > $vroot/etc/mkfedora-rpms.txt # remove trap handler, as we are about to call it directly. trap - ERR INT # Clean up mkfedora_cleanup return 0 } function pl_root_tune_image () { root=$1; shift # This tells the Boot Manager that it is okay to update # /etc/resolv.conf and /etc/hosts whenever the network configuration # changes. Users are free to delete this file. touch $root/etc/AUTO_UPDATE_NET_FILES # all this sounds terribly old and out of scope # turning off for fedora31 where it just fails return 0 # Disable all services in reference image chroot $root sh -c "/sbin/chkconfig --list | awk '{ print \$1 }' | xargs -i /sbin/chkconfig {} off" # FC2 minilogd starts up during shutdown and makes unmounting # impossible. Just get rid of it. rm -f $root/sbin/minilogd ln -nsf /bin/true $root/sbin/minilogd } # Move specified directories out of a src tree into a dst tree, and # then for each moved directory create a symlink in src to dst. function pl_move_dirs() { root=$1 data=$2 store=$3 shift 3 mkdir -p $root/data for datadir in "$@" ; do mkdir -p ${data}${datadir} if [ -d ${root}/${datadir} -a ! -h ${root}/${datadir} ] ; then (cd ${root} && find ./${datadir} | cpio -p -d -u ../${data}/) fi rm -rf ${root}/${datadir} mkdir -p $(dirname ${root}/${datadir}) ln -nsf ${store}/${datadir} ${root}/${datadir} done } # Construct an image file from given some directory # XXX in the future maybe use livecdtools? function pl_make_image() { root=$1 image=$2 extraspace=$3 # Leave about 100 MB free space and allow for about 20% inode overhead bytes=$((($(du -sb $root | cut -f1) + $extraspace) * 120 / 100)) bs=4096 blocks=$(($bytes / $bs)) dd bs=$bs count=$blocks if=/dev/zero of=$image mkfs.ext3 -b $bs -j -F $image # Temporarily mount it tmp=$(mktemp -d tmp.XXXXXX) mount -o loop $image $tmp trap "umount $tmp; rmdir $tmp" ERR INT # Move files to it (cd $root && tar cpf - .) | (cd $tmp && tar xpf -) # Unmount it umount $tmp rmdir $tmp trap - ERR INT } # Fix permissions on tmp directories function pl_fixtmp_permissions() { root=$1 chmod 1777 $root/tmp $root/usr/tmp $root/var/tmp } function pl_fixdirs() { root=$1 datadirs=$2 for datadir in datadirs ; do if [ -h ${root}/${datadir} ] ; then rm -f ${root}/${datadir} mkdir -p ${root}/${datadir} fi done } ########## .pkgs format # Usage: pl_parsePkgs keyword [-a arch] fcdistro pldistro pkgs-file[..s] # pkgs.py should be found in PATH, like this file build.common # only usage should be for pl_getPackages and pl_getGroups, # which in turn are usednow be in {node,slice}image/build.sh function pl_parsePkgs () { target_arch=$pl_DISTRO_ARCH keyword=$1;shift [ "$1" == "-a" ] && { shift; target_arch="$1"; shift; } fcdistro=$1; shift pldistro=$1; shift echo 1>&2 "pl_parsePkgs: using -a $target_arch -f $fcdistro -d $pldistro $keyword $@" pkgs.py -a $target_arch -f $fcdistro -d $pldistro $keyword "$@" } # usage: pl_getPackages [-a arch] fcdistro pldistro pkg-file[..s] function pl_getPackages() { pl_parsePkgs package "$@" ; } function pl_getGroups() { pl_parsePkgs group "$@" ; } function pl_getPips() { pl_parsePkgs pip "$@" ; } function pl_getGems() { pl_parsePkgs gem "$@" ; } ############################## # locates a pldistro-dependant file # tries first in build//, then in build/planetlab/ function pl_locateDistroFile () { builddir=$1; shift pldistro=$1; shift pkgsfile=$1; shift pkgspath="" # if config dir is missing but a .svnpath or a .gitpath exists, use it to extract the config dir configdir="$builddir/config.${pldistro}" if [ ! -d $configdir ] ; then if [ -f "${configdir}.svnpath" -o -f "${configdir}.gitpath" ] ; then echo 1>&2 "Invoking make to extract remote config.${pldistro}" # we set PLDISTROTAGS here to /dev/null because when dealing with remote distros # at a very early stage (like searching for devel.pkgs even before the build VM is created) # then make screams because it cannot find a mandatory include file # OTOH this mechanism here is not intended to depend on tags specifically make 1>&2 --no-print-directory -C $builddir stage1=true config.${pldistro} PLDISTROTAGS=/dev/null fi fi # locate it paths="$builddir/config.$pldistro/$pkgsfile $builddir/config.planetlab/$pkgsfile" for path in $paths; do if [ -f $path ] ; then pkgspath=$path break fi done if [ -z "$pkgspath" ] ; then echo 1>&2 "pl_locateDistroFile - in $(pwd) : cannot locate $pkgsfile in $builddir" echo 1>&2 "candidates were $paths" echo "not-found-by-pl_locateDistroFile" return 1 else echo 1>&2 "pl_locateDistroFile: using $pkgspath" echo $pkgspath return 0 fi } function yumgroups_from_pkgs () { builddir=$1; shift pldistro=$1; shift fcdistro=$1; shift pkgsnames=$@ sedargs="-e s,@FCDISTRO@,$fcdistro,g" cat <<__header __header for pkgsname in $pkgsnames; do pkgsfile=$(pl_locateDistroFile $builddir $pldistro $pkgsname) packages=$(pl_getPackages $fcdistro $pldistro $pkgsfile) groupname=$(pkgs.py groupname $pkgsfile | sed $sedargs) groupdesc=$(pkgs.py groupdesc $pkgsfile | sed $sedargs) if [ -z "$groupname" -o -z "$groupdesc" ] ; then echo "Cannot find groupname: and groupdesc: in $pkgsfile -- skipped" 1>&2 continue fi cat << __group_header $(echo $groupname|tr A-Z a-z) $groupname $groupdesc true __group_header for package in $packages; do echo "$package" done cat << __group_footer __group_footer done cat <<__footer __footer } function build_fetch () { curl --fail --silent --max-time 60 --output /dev/null "$1" } # tries to compute a valid yum.conf for that pldistro from the template in mirroring/ # returns 0 and writes on success # returns 1 on failure, in which case is deleted function yumconf_mirrors () { dest_yumconf=$1; shift builddir=$1; shift fcdistro=$1; shift yumexclude_line="$1" ; shift mirrors="$@" template=$builddir/mirroring/$fcdistro/yum.repos.d/building.repo.in if [ ! -f $template ] ; then echo "yumconf_mirrors: cannot locate template $template" rm -f $dest_yumconf return 1 fi for mirror in $mirrors; do if yumconf_mirror $dest_yumconf $template "$yumexclude_line" $mirror; then return 0 fi done echo 'yumconf_mirrors in build.common : ran out of mirrors -- BAILING OUT' rm -f $dest_yumconf return 1 } # computes a yum.conf from the template, and checks that all baseurl defined in there are valid repos # returns 0 on success and 1 on failure function yumconf_mirror () { dest_yumconf=$1; shift template=$1; shift yumexclude_line="$1" ; shift mirror=$1; shift sed -e "s,@MIRRORURL@,$mirror,g" \ -e "/baseurl=/i\\ $yumexclude_line" $template > $dest_yumconf # capture all lines defining baseurl baseurl_defs=$(grep '^baseurl=' $dest_yumconf) if [ -z "$baseurl_defs" ] ; then return 1 fi for baseurl_def in $baseurl_defs; do baseurl=$(echo $baseurl_def | sed \ -e s,baseurl=,, \ -e 's,$basearch,'"$pl_DISTRO_ARCH",g) repomd=$baseurl/repodata/repomd.xml echo "* Trying to fetch $repomd" if ! build_fetch $repomd ; then echo "* Failed to fetch $repomd" return 1 fi done echo "* Selected mirror $mirror" return 0 } # from a yum.conf as generated above, computes the gpgkey urls function yumconf_gpgkey () { dest_yumconf=$1; shift values=$(grep -h '^gpgkey=' $dest_yumconf | sed -e s,gpgkey=,, | sed -e 's,$basearch,'"$pl_DISTRO_ARCH",g | sed -e 's, ,\n,g' | sort | uniq | xargs) [ -n "$values" ] || return 1 echo $values return 0 } # patches a yum conf to insert an exclude line in each declared repo function yumconf_exclude () { repo=$1; shift yumexclude_line="$1" ; shift sed -i -e "/#baseurl=.*$/i\\ $yumexclude_line" $repo }