parabola-iso-remaster.sh
bill-auger 於 7 年之前 修改了此頁面
#!/bin/bash

# NOTE: ensure we are root and the current directory has gobs of free space

# ISO_IN=/mnt/usb-data/isos/parabola/parabola-mate-2016.11.03-dual.iso/parabola-mate-2016.11.03-dual.iso
ISO_IN=/mnt/data/vm/qemu/custom-parabola-mate-2016.11.03-dual.iso
ISO_VOLUME_LABEL="PARA_201611"
ISO_OUT_FILENAME="custom-`basename $ISO_IN`"
[ "`id -u`" == "0" -a -f $ISO_IN ] || exit

WORK_DIR=$PWD
ISO_MOUNTPOINT=/mnt/parabola-mate-iso
REMASTER_DIR=WORK_DIR/mnt/parabola-mate-iso-remaster
TARGET_SFS_MOUNTPOINT=WORK_DIR/mnt/parabola-mate-sfs-chroot
EFI_MOUNTPOINT=WORK_DIR/mnt/parabola-mate-efi
EFI_WORK_MOUNTPOINT=WORK_DIR/mnt/parabola-mate-efi-temp
ISO_SFS_DIR=$REMASTER_DIR/parabola


# clone live filesystem
mkdir -p $ISO_MOUNTPOINT
mount -o ro,loop -t iso9660 $ISO_IN $ISO_MOUNTPOINT/
cp -a $ISO_MOUNTPOINT/ $REMASTER_DIR/
umount $ISO_MOUNTPOINT/


# de-compress, modify, and re-compress target filesystems
declare -a TARGET_ARCHS=('i686' 'x86_64')
mkdir -p $TARGET_SFS_MOUNTPOINT
for arch in ${TARGET_ARCHS[@]}
do

[ "$arch" == "i686" ] && continue

  # unsquash and chroot into target filesystem
  unsquashfs $ISO_SFS_DIR/$arch/root-image.fs.sfs -d $WORK_DIR/squashfs-root
  mount           $WORK_DIR/squashfs-root/root-image.fs $TARGET_SFS_MOUNTPOINT
  mount -t proc   proc                                  $TARGET_SFS_MOUNTPOINT/proc/
  mount -t sysfs  sys                                   $TARGET_SFS_MOUNTPOINT/sys/
  mount -o bind   /dev                                  $TARGET_SFS_MOUNTPOINT/dev/
  mount -t devpts pts                                   $TARGET_SFS_MOUNTPOINT/dev/pts/
  cp -L /etc/resolv.conf                                $TARGET_SFS_MOUNTPOINT/etc/resolv.conf
  setarch $arch chroot $TARGET_SFS_MOUNTPOINT/ /bin/bash


    # initialize keyring
    pacman-key --init
    pacman-key --populate archlinux parabola
    pacman -Sy --noconfirm parabola-keyring
    pacman-key --populate parabola # (2016.11.03 ISO)
    pacman-key --refresh-keys      # (2016.11.03 ISO)

    # remove deprecated video driver packages (2016.11.03 ISO)
    pacman -R --noconfirm xf86-video-apm xf86-video-ark  xf86-video-chips xf86-video-glint xf86-video-i128 xf86-video-i740 xf86-video-mach64 xf86-video-neomagic xf86-video-nv xf86-video-r128 xf86-video-rendition  xf86-video-s3 xf86-video-s3virge xf86-video-savage xf86-video-siliconmotion xf86-video-sis xf86-video-tdfx xf86-video-trident xf86-video-tseng

    # upgrade target system
    pacman -Su --force --noconfirm parabolaiso linux-libre

    # recreate initcpio
    echo "HOOKS=\"base udev memdisk parabolaiso_shutdown parabolaiso parabolaiso_loop_mnt parabolaiso_pxe_common parabolaiso_pxe_nbd parabolaiso_pxe_http parabolaiso_pxe_nfs parabolaiso_kms block pcmcia filesystems keyboard\"" >> /etc/mkinitcpio.conf
    mkinitcpio -p linux-libre

    # compile new package list and exit chroot
    LANG=C pacman -Sl | awk '/\[installed\]$/ {print $1 "/" $2 "-" $3}' > /pkglist.txt
    exit


  # copy new kernel and ramdisk from target filesystem to live filesystem
  cp $TARGET_SFS_MOUNTPOINT/boot/vmlinuz-linux-libre       $REMASTER_DIR/parabola/boot/$arch/vmlinuz
  cp $TARGET_SFS_MOUNTPOINT/boot/initramfs-linux-libre.img $REMASTER_DIR/parabola/boot/$arch/parabolaiso.img
  mv $TARGET_SFS_MOUNTPOINT/pkglist.txt                    $REMASTER_DIR/parabola/pkglist.$arch.txt

  # unmount chroot filesystem
  umount $TARGET_SFS_MOUNTPOINT/dev/pts/
  umount $TARGET_SFS_MOUNTPOINT/dev/
  umount $TARGET_SFS_MOUNTPOINT/sys/
  umount $TARGET_SFS_MOUNTPOINT/proc/
  umount $TARGET_SFS_MOUNTPOINT/

  # recreate compressed filesystem image
  rm $ISO_SFS_DIR/$arch/root-image.fs.sfs
  mksquashfs $WORK_DIR/squashfs-root $ISO_SFS_DIR/$arch/root-image.fs.sfs
  rm -rf $WORK_DIR/squashfs-root/

  # compute new checksum of compressed filesystem image
  pushd $ISO_SFS_DIR/
  md5sum $arch/root-image.fs.sfs > checksum.$arch.md5
  popd
done


# recreate EFI boot image
mkdir -p $EFI_MOUNTPOINT
mkdir -p $EFI_WORK_MOUNTPOINT
efi_kernel_size=` stat -c '%s' "$REMASTER_DIR/parabola/boot/$arch/vmlinuz"`
efi_ramdisk_size=`stat -c '%s' "$REMASTER_DIR/parabola/boot/$arch/parabolaiso.img"`
efi_image_size=$(($(($(($efi_kernel_size+$efi_ramdisk_size))/1000000))+1))
dd if=/dev/zero bs=1M count=$efi_image_size of=$WORK_DIR/efiboot.img
mkfs.fat -n "PARABOLAISO_EFI" $WORK_DIR/efiboot.img
mount -t fat -o loop $REMASTER_DIR/EFI/parabolaiso/efiboot.img         $EFI_MOUNTPOINT/
mount -t fat -o loop $WORK_DIR/efiboot.img                             $EFI_WORK_MOUNTPOINT/
rm                   $EFI_MOUNTPOINT/EFI/parabolaiso/vmlinuz.efi
rm                   $EFI_MOUNTPOINT/EFI/parabolaiso/parabolaiso.img
cp -r                $EFI_MOUNTPOINT/*                                 $EFI_WORK_MOUNTPOINT/
cp                   $REMASTER_DIR/parabola/boot/$arch/vmlinuz         $EFI_WORK_MOUNTPOINT/EFI/parabolaiso/vmlinuz.efi
cp                   $REMASTER_DIR/parabola/boot/$arch/parabolaiso.img $EFI_WORK_MOUNTPOINT/EFI/parabolaiso/parabolaiso.img
umount                                                                 $EFI_WORK_MOUNTPOINT/
umount                                                                 $EFI_MOUNTPOINT/
rm                   $REMASTER_DIR/EFI/parabolaiso/efiboot.img
mv $WORK_DIR/efiboot.img $REMASTER_DIR/EFI/parabolaiso/efiboot.img


# recreate ISO image
genisoimage -l -r -J -V $ISO_VOLUME_LABEL -b isolinux/isolinux.bin -no-emul-boot -boot-load-size 4 -boot-info-table -c isolinux/boot.cat -o $WORK_DIR/$ISO_OUT_FILENAME $REMASTER_DIR/


# NOTE: The ISO label must remain the same as the original label (in this case PARA_201611) for the image to boot successfully.
# NOTE: The resulting ISO image will boot only from optical disc. See https://wiki.parabola.nu/Remastering_the_Install_ISO