#!/bin/sh ## live-build(7) - System Build Scripts ## Copyright (C) 2006-2015 Daniel Baumann ## ## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING. ## This is free software, and you are welcome to redistribute it ## under certain conditions; see COPYING for details. set -e # Including common functions [ -e "${LIVE_BUILD}/scripts/build.sh" ] && . "${LIVE_BUILD}/scripts/build.sh" || . /usr/lib/live/build.sh # Setting static variables DESCRIPTION="$(Echo 'build binary image')" HELP="" USAGE="${PROGRAM} [--force]" Arguments "${@}" # Reading configuration files Read_conffiles config/all config/common config/bootstrap config/chroot config/binary config/source Set_defaults if ! In_list hdd "${LIVE_IMAGE_TYPE}" then exit 0 fi Echo_message "Begin building binary hdd image..." # Requiring stage file Require_stagefile .build/config .build/bootstrap .build/chroot_proc # Checking stage file Check_stagefile .build/binary_hdd # Checking lock file Check_lockfile .lock # Creating lock file Create_lockfile .lock # Checking depends case "${LB_BINARY_FILESYSTEM}" in fat*) Check_package chroot /sbin/mkdosfs dosfstools ;; ntfs) Check_package chroot /sbin/mkfs.ntfs ntfs-3g ;; esac Check_package chroot /usr/share/doc/mtools mtools Check_package chroot /usr/sbin/sgdisk gdisk LB_BOOTLOADERS="$(echo "${LB_BOOTLOADERS}" | tr "," " ")" for BOOTLOADER in $LB_BOOTLOADERS; do case "${BOOTLOADER}" in grub-pc) GRUB_PC_ENABLED="true" ;; grub-efi) EFI_ENABLED="true" ;; esac done # Restoring cache Restore_cache cache/packages.binary # Installing depends Install_package # Remove old binary if [ -f ${LIVE_iMAGE_NAME}.img ] then rm -f ${LIVE_iMAGE_NAME}.img fi case "${LB_BINARY_FILESYSTEM}" in fat*) # If the target does not support hardlinks, tell du to # count them double DU_OPTIONS="--count-links" ;; *) DU_OPTIONS="" ;; esac # Enforce fat32 if we find individual files bigger than 2GB if [ "${LB_BINARY_FILESYSTEM}" = "fat16" ] && [ -n "$(find binary -size +1999M)" ] then Echo_warning "FAT16 doesn't support files larger than 2GB, automatically enforcing FAT32." LB_BINARY_FILESYSTEM="fat32" export LB_BINARY_FILESYSTEM fi # Enforce fat32 if we have images in total bigger than 2GB if [ "${LB_BINARY_FILESYSTEM}" = "fat16" ] && [ "$(du ${DU_OPTIONS} -s binary | awk '{ print $1 }')" -gt "1900000" ] then Echo_warning "FAT16 doesn't support partitions larger than 2GB, automatically enforcing FAT32" LB_BINARY_FILESYSTEM="fat32" export LB_BINARY_FILESYSTEM fi # Enforce ntfs if we find individual files bigger than 4GB if [ "${LB_BINARY_FILESYSTEM}" = "fat32" ] && [ -n "$(find binary -size +3999M)" ] then Echo_warning "FAT32 doesn't support files larger than 4GB, automatically enforcing NTFS." LB_BINARY_FILESYSTEM="ntfs" export LB_BINARY_FILESYSTEM fi # Everything which comes here needs to be cleaned up, if [ "$LB_HDD_SIZE" = "auto" ]; then DU_DIM="$(du ${DU_OPTIONS} -ms binary | cut -f1)" REAL_DIM="$(Calculate_partition_size ${DU_DIM} ${LB_BINARY_FILESYSTEM})" else REAL_DIM=$LB_HDD_SIZE fi dd if=/dev/zero of=chroot/binary.img bs=1024k count=0 seek=${REAL_DIM} FREELO="$(${LB_LOSETUP} -f)" if [ ! -b chroot/${FREELO} ] then MAKEDEV="true" mv chroot/dev chroot/dev.tmp find /dev | cpio -dmpu chroot fi echo "!!! The following error/warning messages can be ignored !!!" Losetup $FREELO chroot/binary.img 0 case "${LB_BINARY_FILESYSTEM}" in ext2|ext3|ext4) PARTITION_TYPE="ext2" ;; fat16|fat32) PARTITION_TYPE="${LB_BINARY_FILESYSTEM}" ;; ntfs) PARTITION_TYPE="NTFS" ;; *) Echo_error "Unsupported binary filesystem %s" "${LB_BINARY_FILESYSTEM}" exit 1 ;; esac case "${LB_BUILD_WITH_CHROOT}" in true) Chroot chroot "sgdisk --zap-all ${FREELO}" || true if [ -n "${EFI_ENABLED}" ] then Chroot chroot "sgdisk -a1 -n1:34:2047 -t1:EF02 \ -n2:2048:+256M -t2:EF00 \ -n3:0:0:+100% -t3:8300 ${FREELO}" || true else if [ "x${LB_HDD_PARTITION_START}" = "x" ]; then Chroot chroot "sgdisk -a1 -n1:34:2047 -t1:EF02 \ -n2:0:0:+100% -t2:8300 ${FREELO}" || true else Echo_message "using partition start at ${LB_HDD_PARTITION_START}" Chroot chroot "sgdisk -a1 -n1:34:2047 -t1:EF02 \ -n2:${LB_HDD_PARTITION_START}:+100% -t2:8300 ${FREELO}" || true fi fi ;; false) sgdisk --zap-all ${FREELO} || true if [ -n "${EFI_ENABLED}" ] then sgdisk -a1 -n1:34:2047 -t1:EF02 \ -n2:2048:+256M -t2:EF00 \ -n3:0:0:+100% -t3:8300 ${FREELO} || true else if [ "x${LB_HDD_PARTITION_START}" = "x" ]; then sgdisk -a1 -n1:34:2047 -t1:EF02 \ -n2:2048:+100% -t2:8300 ${FREELO} || true else Echo_message "using partition start at ${LB_HDD_PARTITION_START}" sgdisk -a1 -n1:34:2047 -t1:EF02 \ -n2:${LB_HDD_PARTITION_START}:+100% -t2:8300 ${FREELO} || true fi fi ;; esac Lodetach ${FREELO} FREELO="$(${LB_LOSETUP} -f)" Losetup $FREELO chroot/binary.img 0 case "${LB_BINARY_FILESYSTEM}" in ext2|ext3|ext4) MKFS="${LB_BINARY_FILESYSTEM}" MKFS_OPTIONS="-L ${LB_HDD_LABEL} -m 0 -O ^64bit" MOUNT_OPTIONS="" ;; fat16) MKFS="vfat" MKFS_OPTIONS="-F 16 -n ${LB_HDD_LABEL}" MOUNT_OPTIONS="" ;; fat32) MKFS="vfat" MKFS_OPTIONS="-F 32 -n ${LB_HDD_LABEL}" MOUNT_OPTIONS="" ;; ntfs) MKFS="ntfs" MKFS_OPTIONS="-L ${LB_HDD_LABEL}" MOUNT_OPTIONS="-t ntfs-3g" ;; esac case "${LB_BUILD_WITH_CHROOT}" in true) if [ -n "${EFI_ENABLED}" ] then Chroot chroot "mkfs.${MKFS} ${MKFS_OPTIONS} ${FREELO}p3" else Chroot chroot "mkfs.${MKFS} ${MKFS_OPTIONS} ${FREELO}p2" fi ;; false) if [ -n "${EFI_ENABLED}" ] then mkfs.${MKFS} ${MKFS_OPTIONS} ${FREELO}p3 else mkfs.${MKFS} ${MKFS_OPTIONS} ${FREELO}p2 fi ;; esac case "${LB_BINARY_FILESYSTEM}" in fat*) CP_OPTIONS="-r -L" ;; *) CP_OPTIONS="-a" ;; esac Echo_message "Copying binary contents into image..." mkdir -p chroot/binary.tmp if [ -n "${EFI_ENABLED}" ] then mount ${MOUNT_OPTIONS} ${FREELO}p3 chroot/binary.tmp else mount ${MOUNT_OPTIONS} ${FREELO}p2 chroot/binary.tmp fi cp -T ${CP_OPTIONS} binary/ chroot/binary.tmp if [ -n "${GRUB_PC_ENABLED}" ] then case "${LB_BUILD_WITH_CHROOT}" in true) Chroot chroot "grub-install --no-floppy --target=i386-pc --root-directory=/binary.tmp ${FREELO} --force" ;; false) grub-install --no-floppy --target=i386-pc --root-directory=/binary.tmp ${FREELO} --force ;; esac fi if [ -n "${EFI_ENABLED}" ] then MKFSEFI="vfat" MKFSEFI_OPTIONS="-n EFI -F 32 -s 1" MOUNTEFI_OPTIONS="" case "${LB_BUILD_WITH_CHROOT}" in true) Chroot chroot "mkfs.${MKFSEFI} ${MKFSEFI_OPTIONS} ${FREELO}p2" ;; false) mkfs.${MKFSEFI} ${MKFSEFI_OPTIONS} ${FREELO}p2 ;; esac mkdir -p chroot/efi.tmp mount ${MOUNTEFI_OPTIONS} ${FREELO}p2 chroot/efi.tmp case "${LB_BUILD_WITH_CHROOT}" in true) Chroot chroot "grub-install --no-floppy --recheck --target=x86_64-efi --force-extra-removable --root-directory=/binary.tmp --efi-directory=/efi.tmp --bootloader-id='VyOS' --no-uefi-secure-boot" ;; false) grub-install --no-floppy --recheck --target=x86_64-efi --force-extra-removable --root-directory=/binary.tmp --efi-directory=/efi.tmp --bootloader-id='VyOS' --no-uefi-secure-boot ;; esac umount chroot/efi.tmp rmdir chroot/efi.tmp fi umount chroot/binary.tmp rmdir chroot/binary.tmp Lodetach ${FREELO} echo "!!! The above error/warning messages can be ignored !!!" if [ -n "${MAKEDEV}" ] then rm -rf chroot/dev mv chroot/dev.tmp chroot/dev fi mv chroot/binary.img ${LIVE_IMAGE_NAME}-${LB_ARCHITECTURES}.img # Saving cache Save_cache cache/packages.binary # Removing depends Remove_package # Creating stage file Create_stagefile .build/binary_hdd