#!/bin/sh ## live-build(7) - System Build Scripts ## Copyright (C) 2016-2020 The Debian Live team ## 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="Build binary image" USAGE="${PROGRAM} [--force]" # Processing arguments and configuration files Init_config_data "${@}" if [ "${LB_IMAGE_TYPE}" != "hdd" ]; then exit 0 fi Echo_message "Begin building binary hdd image..." # Requiring stage file Require_stagefiles config bootstrap chroot_proc # Checking stage file Check_stagefile # Acquire lock file Acquire_lockfile # 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 Check_package host /sbin/fdisk fdisk Check_package host /sbin/losetup mount # Restoring cache Restore_package_cache binary # Installing depends Install_packages # Remove old binary if [ -f ${LB_IMAGE_NAME}.img ] then rm -f ${LB_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="$(losetup -f)" MAKEDEV=false 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 [ "${LB_BOOTLOADER_EFI}" = "grub-efi" ]; 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 [ "${LB_BOOTLOADER_EFI}" = "grub-efi" ]; 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="$(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 [ "${LB_BOOTLOADER_EFI}" = "grub-efi" ]; then Chroot chroot "mkfs.${MKFS} ${MKFS_OPTIONS} ${FREELO}p3" else Chroot chroot "mkfs.${MKFS} ${MKFS_OPTIONS} ${FREELO}p2" fi ;; false) if [ "${LB_BOOTLOADER_EFI}" = "grub-efi" ]; 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 [ "${LB_BOOTLOADER_EFI}" = "grub-efi" ]; 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 [ "${LB_VYOS_PERSISTENCE}" = "true" ] then mkdir -p chroot/binary.tmp/${LB_VYOS_VERSION}/rw mkdir -p chroot/binary.tmp/${LB_VYOS_VERSION}/work mv chroot/binary.tmp/${LB_VYOS_VERSION}/filesystem.squashfs \ chroot/binary.tmp/${LB_VYOS_VERSION}/$(basename ${LB_VYOS_VERSION}).squashfs cat > chroot/binary.tmp/persistence.conf << EOF / union EOF fi if [ "${LB_BOOTLOADER_BIOS}" = "grub-pc" ]; 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 [ "${LB_BOOTLOADER_EFI}" = "grub-efi" ]; 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 $MAKEDEV; then rm -rf chroot/dev mv chroot/dev.tmp chroot/dev fi mv chroot/binary.img ${LB_IMAGE_NAME}-${LB_ARCHITECTURE}.img # Saving cache Save_package_cache binary # Removing depends Remove_packages # Creating stage file Create_stagefile