#!/bin/bash # Copyright (C) 2017-2018 Daniel Tartavel-jeannot # # 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, 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 . 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" } SOURCE_PATH="`/bin/dirname \"$(readlink -f \"$0\")\"`" INSTALL_PATH="$SOURCE_PATH/raspi" FILES_PATH="$SOURCE_PATH/files" CMDNAME=`/bin/basename -z "$0"` #while read -r;do # for MAGEIA_VERSION in $REPLY # do # if [ $MAGEIA_VERSION <= 5 ];then # echo 'Mageia version >= 6 is needed' # exit # fi # break # done #done < /etc/version function help() { echo "$CMDNAME [option] [size of image in Go]" echo "Options:" echo "--all create ready to burn image of Mageia $MAGEIA_VERSION" echo "--size size of image default: 7Go" echo "--create-chroot Create the chroot directory" echo "--addmedia add mirrors" echo "--update-mirror update urpmi database" echo "--install-basesystem install base system" echo "--chroot chroot to arm directory and launch packages installation" echo "--create-image Create the image of Mageia $MAGEIA_VERSION" echo "--build-path Path to the build directory of the image of Mageia $MAGEIA_VERSION" echo "--target target system (for now rpi, xu3, xu4)" echo "--config Path to config files" echo "--bootfs filesystem of boot partition (ext4 or vfat) default: etx4" echo "\nFor image size, make sure it fit on physical support. (Default size is 7 Go)" } # cleaning function clean() { title "Cleaning" if ! [ -z `mountpoint -qd "$BUILD_PATH/dev"` ];then info "Unmounting $BUILD_PATH/dev" umount $BUILD_PATH/dev if ! [ $? -ne 0 ];then error "line $LINENO can't unmount $BUILD_PATH/dev : exiting" exit 1 fi fi if ! [ -z `mountpoint -qd "$BUILD_PATH/sys"` ];then info "Unmounting $BUILD_PATH/sys" umount $BUILD_PATH/sys if ! [ $? -ne 0 ];then error "line $LINENO can't unmount $BUILD_PATH/sys : exiting" exit 1 fi fi if ! [ -z `mountpoint -qd "$BUILD_PATH/proc"` ];then info "Unmounting $BUILD_PATH/proc" umount $BUILD_PATH/proc if ! [ $? -ne 0 ];then error "line $LINENO can't unmount $BUILD_PATH/proc : exiting" exit 1 fi fi if [ -d "$BUILD_PATH" ];then info "Removing $BUILD_PATH" /bin/rm -Rf $BUILD_PATH else warning "$BUILD_PATH does not exists" fi if [ -e "$IMAGE" ];then info "Removing "$IMAGE"" /bin/rm -f $INSTALL_PATH/$IMAGE else warning "$IMAGE does not exists" fi if [ -d "$BOOT" ];then info "Removing $BOOT" /bin/rmdir $BOOT else warning "$BOOT does not exists" fi if [ -d "$ROOT" ];then info "Removing $ROOT" /bin/rmdir $ROOT else warning "$ROOT does not exists" fi exit 0 } # creation of install path and copy of qemu ( installing it if not yet installed ) function createchroot() { if ! [ -f /bin/qemu-arm-static ]; then title "Qemu package not present : installing qemu packages" /sbin/urpmi --auto --no-recommends qemu-user-static if ! [ $? -eq 0 ];then error "line $LINENO can't install qemu-user-static : exiting" exit 1 fi fi /bin/systemctl is-active systemd-binfmt.service if [ $? -ne 0 ]; then title "Starting systemd-binfmt.service" /bin/systemctl start systemd-binfmt.service if ! [ $? -ne 0 ];then error "line $LINENO can't start qemu-user-static : exiting" exit 1 fi fi title "Creating $BUILD_PATH" /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 1 fi /bin/cp /bin/qemu-arm-static $BUILD_PATH/usr/bin/ if [ $? -ne 0 ];then error "line $LINENO can't copy /bin/qemu-user-static to $BUILD_PATH/usr/bin/ : exiting" exit 1 fi /bin/cp /usr/lib/binfmt.d/qemu-arm-static.conf $BUILD_PATH/usr/lib/binfmt.d if [ $? -ne 0 ];then error "line $LINENO can't copy /usr/lib/binfmt.d/qemu-arm-static.conf to $BUILD_PATH/usr/lib/binfmt.d : exiting" exit 1 fi } function addmedia() { title "Creating media $MIRROR" /sbin/urpmi.addmedia --urpmi-root $BUILD_PATH --distrib "$MIRROR" err=$? if [ $err -ne 0 ];then error "line $LINENO error $err - can't add medias from $MIRROR : exiting" exit 1 fi title "activating non-free and tainted" /sbin/urpmi.update --urpmi-root $BUILD_PATH --no-ignore Nonfree\ Release Nonfree\ Updates Tainted\ Release Tainted\ Updates err=$? if [ $err -ne 0 ];then error "line $LINENO error $err - can't activate medias : exiting" exit 1 fi } function updatemirror() { title "updating mirror" /sbin/urpmi.update -a -ff --urpmi-root "$BUILD_PATH" if [ $? -ne 0 ];then error "line $LINENO can't update mirrors : exiting" exit 1 fi } function installbasesystem() { title "installing basesystem" /sbin/urpmi --urpmi-root "$BUILD_PATH" --ignorearch --no-verify-rpm --auto --split-level 200 --split-length 200 shadow-utils basesystem-minimal if [ $? -ne 0 ];then error "line $LINENO error installing shadow-utils or basesystem-minimal : exiting" exit 1 fi #/sbin/urpmi --urpmi-root "$BUILD_PATH" --ignorearch --no-verify-rpm --auto --split-level 200 --split-length 200 basesystem-minimal #if [ $? -ne 0 ];then # error "line $LINENO error installing basesystem-minimal : exiting" # exit 1 #fi /sbin/urpmi --urpmi-root "$BUILD_PATH" --ignorearch --no-verify-rpm --auto --split-level 200 --split-length 200 urpmi locales-fr systemd if [ $? -ne 0 ];then error "line $LINENO error installing urpmi or locales.fr : exiting" exit 1 fi } function jumpchroot() { title "Preparing chrooting in $BUILD_PATH" info "mounting dev, sys, proc directories in chroot" /bin/mount -B /dev "$BUILD_PATH/dev" if [ $? -ne 0 ];then error "line $LINENO error mounting $BUILD_PATH/dev : exiting" exit 1 fi /bin/mount -B /sys "$BUILD_PATH/sys" if [ $? -ne 0 ];then error "line $LINENO error mounting $BUILD_PATH/sys : exiting" exit 1 fi /bin/mount -B /proc "$BUILD_PATH/proc" if [ $? -ne 0 ];then error "line $LINENO error mounting $BUILD_PATH/proc : exiting" exit 1 fi info "Copying resolv.conf" /bin/cp -a /etc/resolv.conf "$BUILD_PATH/etc/" if [ $? -ne 0 ];then error "line $LINENO error copying $BUILD_PATH/etc : exiting" exit 1 fi info "making /etc/hostname" echo "$HOSTNAME" > "$BUILD_PATH"/etc/hostname info "copying second stage script in $BUILD_PATH" echo "/bin/cp $SOURCE_PATH/second_stage_install.sh $BUILD_PATH/" cp "$SOURCE_PATH/second_stage_install.sh" "$BUILD_PATH/second_stage_install.sh" if [ $? -ne 0 ];then error "line $LINENO error copying $SOURCE_PATH/second_stage_install.sh : exiting" exit 1 fi info "copying kernel-odroid rpm in $BUILD_PATH" echo "/bin/cp $SOURCE_PATH/kernel-odroid-4.14.y-1.armv7hl.rpm $BUILD_PATH/" /usr/bin/cp "$SOURCE_PATH/kernel-odroid-4.14.y-1.armv7hl.rpm" "$BUILD_PATH/" if [ $? -ne 0 ];then error "line $LINENO error copying $SOURCE_PATH/kernel-odroid-4.14.y-1.armv7hl.rpm : exiting" exit 1 fi info "creation of user account" echo "/bin/passwd << EOF $ROOT_PWD $ROOT_PWD EOF /sbin/useradd "$ID_USER" /bin/passwd "$ID_USER" << EOF "$PASSWORD" "$PASSWORD" EOF" >>"$BUILD_PATH/second_stage_install.sh" info "Copying skel in root directory" /bin/rsync -a /etc/skel/ "$BUILD_PATH/root/" title "chrooting to $BUILD_PATH" /sbin/chroot --userspec root:root "$BUILD_PATH" /bin/bash -v -c '/second_stage_install.sh' if [ $? -ne 0 ];then warning "line $LINENO Warning : chrooting to $BUILD_PATH" fi info "unmounting dev, sys, proc" /bin/umount "$BUILD_PATH/dev" if [ $? -ne 0 ];then warning "line $LINENO Warning : error unmounting $BUILD_PATH/dev, continuing anyway" fi /bin/umount "$BUILD_PATH/sys" if [ $? -ne 0 ];then warning "line $LINENO Warning : error unmounting $BUILD_PATH/sys, continuing anyway" fi /bin/umount "$BUILD_PATH/proc" if [ $? -ne 0 ];then warning "line $LINENO Warning : error unmounting $BUILD_PATH/proc, continuing anyway" fi } function verify_disk_space() { info "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 } function createxu3image() { title "Creating Odroid XU3-4 image" createimage if [ -z $BOOTFS ];then BOOTFS="ext4" fi formatpartitions $BOOTFS ext4 copyingsystem } function createrpiimage() { title "Creating Pi image" if ! [ -d $FIRMWARE_PATH/$FIRMWARE_DIR ];then # the firmware directory does not exists echo `ls *.zip| wc -l` if [ `ls *.zip| wc -l` -eq 0 ];then # the firmware archive does not exists warning "The raspberry pi firmware need to be downloaded" info "Downloading FIRMWARE_FILENAME" wget $FIRMWARE_DOWNLOAD_URL if [ $? -ne 0 ];then error "line $LINENO error downloading raspberry firmware at $FIRMWARE_DOWNLOAD_URL :exiting" exit 1 fi fi info "extracting firmware" unzip -d "$FIRMWARE_PATH" *.zip if [ $? -ne 0 ];then error "line $LINENO error unzipping $FIRMWARE_PATH : exiting" exit 1 fi fi title "Making raspberry pi image" createimage if [ -z $BOOTFS ];then BOOTFS="vfat" fi formatpartitions $BOOTFS ext4 copyingsystem } createimage() { title " in $IMAGE" if [ -f "$INSTALL_PATH/$IMAGE" ];then warning "Deleting previous image" /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 1 fi fi warning "please wait until end of image creation" /bin/dd if=/dev/zero of="$INSTALL_PATH/$IMAGE" bs=1MB count=$(( $IMAGE_SIZE * 1024 )) if [ $? -ne 0 ];then error "line $LINENO can't make image at $INSTALL_PATH/$IMAGE : exiting" exit 1 fi info "making partitions" DEVICE=`/sbin/losetup -f --show "$INSTALL_PATH/$IMAGE"` /sbin/fdisk $DEVICE << EOF n p 1 +64M t c n p 2 w EOF /sbin/losetup -d $DEVICE DEVICE=`/sbin/partx -va "$INSTALL_PATH/$IMAGE" | /bin/grep -m 1 -E -o '/dev/loop.?'` info "device $DEVICE" info "partitions list:" info `/sbin/partx -v "$INSTALL_PATH/$IMAGE"` BOOTP=${DEVICE}p1 BOOT_UUID=`blkid -o value uuid $BOOTP | head -n 1` ROOTP=${DEVICE}p2 ROOT_UUID=`blkid -o value uuid $ROOTP | head -n 1` } formatpartitions() { info "Formatting partitions" info "boot as $1" /sbin/mkfs.$1 $BOOTP if [ $? -ne 0 ];then error "line $LINENO error formating $BOOTP : exiting" /sbin/losetup -d $DEVICE exit 1 fi info "root as $2" /sbin/mkfs.$2 $ROOTP if [ $? -ne 0 ];then error "line $LINENO error formating $ROOTP : exiting" /sbin/losetup -d $DEVICE exit 1 fi } copyingsystem() { # getting UUID of root partition #ROOT_ID=`/sbin/blkid -s UUID -o value "${DEVICE}p2"` #info "blkid -s UUID -o value ${DEVICE}p2" #info "root partition ID: $ROOT_ID" info "mounting partitions, creating mountpoint if necessary" if ! [ -d "$BOOT" ];then /bin/mkdir "$BOOT" if [ $? -ne 0 ];then error "line $LINENO error making directory $BOOT : exiting" exit 1 fi fi if ! [ -d "$ROOT" ];then /bin/mkdir "$ROOT" if [ $? -ne 0 ];then error "line $LINENO error making directory $ROOT : exiting" exit 1 fi fi /bin/mount "$BOOTP" "$BOOT" if [ $? -ne 0 ];then error "line $LINENO error mounting $BOOTP : exiting" exit 1 fi /bin/mount "$ROOTP" "$ROOT" if [ $? -ne 0 ];then error "line $LINENO error mounting $ROOTP : exiting" exit 1 fi info "copying Mageia image to root parition" #/bin/rsync -a --exclude boot/ --exclude "qemu-arm-static*" "$BUILD_PATH/" "$ROOT/" /bin/rsync -a "$BUILD_PATH/boot/" "$BOOT/" info "making /etc/fstab" echo "proc /proc proc defaults 0 0 $BOOT_UUID /boot vfat defaults 0 0 $ROOT_UUID / ext4 defaults 0 0" > "$BUILD_PATH"/etc/fstab case $TARGET in rpi) info "copying raspberry firmware in /boot" /bin/rsync -rlptDH "$FIRMWARE_PATH/firmware-stable/boot/" "$BOOT" cp -a $FIRMWARE_PATH/firmware-stable/extra/uname_string* $BOOT info "adding modules in /etc/modules" echo "vchiq snd_bcm2835 vc4 brcmfmac bcm2835-nrg" >> "$BUILD_PATH"/etc/modules info "copying modprobe.conf" /bin/cp -a $SOURCE_PATH/modprobe.conf $BUILD_PATH/etc/ chown root:root $BUILD_PATH/etc/modprobe.conf info "making /boot/cmdline.txt" echo "dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait" > "$BOOT/cmdline.txt" info "copying raspberry modules in /lib/modules" /bin/rsync -rlptDH "$FIRMWARE_PATH/firmware-stable/modules/" "$BUILD_PATH/lib/modules/" info "copying raspberry optionals files in opt" /bin/rsync -rlptDH "$FIRMWARE_PATH/firmware-stable/opt/" "$BUILD_PATH/opt/" info "copying wifi firmware file missing in Mageia" /bin/mkdir -p "$ROOT/usr/lib/firmware/brcm/" cp "$SOURCE_PATH/brcmfmac43430-sdio.txt" "$ROOT/usr/lib/firmware/brcm/" if [ $? -ne 0 ];then error "line $LINENO error copying wifi firmware in $ROOTP/usr/lib/firmware/brcm/ : exiting" exit 1 fi ;; xu3-4) ;; esac /bin/umount "$BOOT" "$ROOT" if [ $? -eq 0 ];then /bin/rmdir "$BOOT" "$ROOT" else error "line $LINENO error unmounting $ROOT or $BOOT : exiting" exit 1 fi /sbin/partx -d "$DEVICE" if [ $? -ne 0 ];then error "line $LINENO warning : error unmounting $DEVICE " exit 1 fi warning "You can now burn the image ( $IMAGE ) on SD card" } if [ $# == 0 ];then help exit fi # parsing commandline TEMP=`getopt -o h,a --long all,help,clean,create-chroot,addmedia,create-image,,config:,target:,chroot,bootfs:,install-basesystem,update-mirror,build-path:,size: -n $CMDNAME -- "$@"` if [ $? -ne 0 ] ; then error "line $LINENO Failed parsing options." >&2 ; exit 1 ; fi eval set -- "$TEMP" echo "$TEMP" # Note the quotes around `$TEMP': they are essential! while true; do case "$1" in -h|--help) help exit 0 ;; -a|--all) OPT="all" shift 1 ;; --clean) if [ -z $OPT ];then OPT="clean" fi shift ;; --size) IMAGE_SIZE=$2 shift 2 ;; --build-path) INSTALL_PATH=$2 shift 2 ;; --chroot) if [ -z $OPT ];then OPT="chroot" fi shift ;; --create-chroot) if [ -z $OPT ];then OPT="createchroot" fi shift ;; --addmedia) if [ -z $OPT ];then OPT="addmedia" 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 ;; --system) SYSTEM=$2 shift 2 ;; --config) CONFIG_PATH=$2 shift 2 ;; --bootfs) BOOTFS=$2 shift 2 ;; --) shift break;; esac done info "Option: "$OPT IMAGE="Mageia-${MAGEIA_VERSION}-$TARGET.img" BOOT="/mnt/boot" ROOT="/mnt/root" BUILD_PATH="$INSTALL_PATH/build" ARM_VERSION="armv7hl" if [ -z $CONFIG_PATH ];then CONFIG_PATH="./$TARGET" fi if [ -d "./$CONFIG_PATH" ];then if [-e "./$CONFIG_PATH/config.txt" ] info "using $CONFIG_PATH/config.txt as config" . "./$CONFIG_PATH/config.txt" else warning "Config file does not exists, do you want i copy template ? [Y|n] " read yn if [ -z $yn || $yn -eq "Y" || $yn -eq "y" ];then /usr/bin/cp ./config.template "./$CONFIG_PATH/config.txt" fi /usr/bin/cp ./config.template "./$CONFIG_PATH/config.txt" warning "You need now to modify the config file and relaunch the script" exit 1 fi else warning "Config directory does not exists, do you want i make it and copy the template file in? [Y|n] " read yn if [ -z $yn || $yn -eq "Y" || $yn -eq "y" ];then /usr/bin/mkdir ./$CONFIG_PATH/ /usr/bin/cp ./config.template "./$CONFIG_PATH/config.txt" warning "You need now to modify the config file and relaunch the script" fi fi # assign TARGET ( rpi or xu3-4 ) if [ $CREATEIMAGE ];then case $TARGET in xu3||xu4) CREATEIMG="createxu3-4image" break;; rpi) CREATEIMG="createrpiimage" FIRMWARE_PATH="$SOURCE_PATH/.." FIRMWARE_DOWNLOAD_URL="https://github.com/raspberrypi/firmware/archive/stable.zip" FIRMWARE_DIR="firmware-stable" break;; *) CREATEIMG="createxu3-4image" TARGET="xu4" break;; esac fi 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 "target : $TARGET" if [[ "$IMAGE_SIZE" -lt 7 ]] || [[ "$IMAGE_SIZE" -gt 128 ]];then IMAGE_SIZE=7 fi if [ "$OPT" != "clean" ];then info "cd $INSTALL_PATH" if ! [ -e "$INSTALL_PATH" ];then /bin/mkdir -p "$INSTALL_PATH" if [ -z $? ];then error "line $LINENO can't make directory $INSTALL_PATH , exiting" exit 1 fi fi cd "$INSTALL_PATH" if [ -z $? ];then error "line $LINENO can't change to directory $INSTALL_PATH , exiting" exit 1 fi fi if [ -z $OPT ];then OPT="--help" fi info "Image size is: $IMAGE_SIZE Go" case $OPT in clean) clean ;; all) verify_disk_space if [ $? -eq 1 ];then warning "Not enough space on disk" if ! [ -e /usr/bin/dialog ];then warning "installing cdialog" urpmi --auto --force cdialog fi dialog --yesno "Not enough space on disk\nDo you want to continue anyway ?" 7 40 if [ $? -eq 1 ];then exit 6 fi fi createchroot addmedia updatemirror installbasesystem jumpchroot createimage ;; createchroot) createchroot ;; chroot) jumpchroot ;; addmedia) addmedia ;; updatemirror) updatemirror ;; createimage) verify_disk_space if [ $? -eq 1 ];then warning "Not enough space on disk" exit 6 fi $CREATEIMG ;; installbasesystem) installbasesystem ;; esac