162 lines
4.3 KiB
Bash
162 lines
4.3 KiB
Bash
#!/usr/bin/bash
|
|
|
|
set -x
|
|
set -e
|
|
PASSWD_FILE=/home/narenas/passwd
|
|
DIR=$(pwd)
|
|
CHROOT_DIR=$DIR/chroot
|
|
DIST=noble
|
|
VARIANT=minbase
|
|
CLEAN=${CLEAN:-1}
|
|
CLEAN_CHROOT=${CLEAN_CHROOT:-1}
|
|
OUTPUT_DIR=/media/sf_Downloads/
|
|
UBUNTU_CHROOT_DIR=$DIR/ubuntu_chroot
|
|
|
|
|
|
## FUNCIONES
|
|
function exec_as_sudo () {
|
|
cat $PASSWD_FILE | sudo -S $@
|
|
}
|
|
|
|
function exec_as_sudo_in_chroot () {
|
|
exec_as_sudo chroot $CHROOT_DIR $@
|
|
}
|
|
|
|
function umount_in_chroot() {
|
|
if grep $CHROOT_DIR$1 /etc/mtab;
|
|
then
|
|
echo "Unmounting $1"
|
|
exec_as_sudo_in_chroot umount $1
|
|
return $?
|
|
fi
|
|
echo "Mount point $1 not found"
|
|
return 0
|
|
}
|
|
|
|
function umount_in_host() {
|
|
if findmnt -M $1
|
|
then
|
|
echo "Unmounting $1"
|
|
exec_as_sudo umount $1
|
|
return $?
|
|
fi
|
|
echo "Mount point $1 not found"
|
|
return 0
|
|
}
|
|
|
|
|
|
if [ -d $CHROOT_DIR ] && [ $CLEAN -eq 1 ];
|
|
then
|
|
SYS_MOUNT_POINT_CHROOT="/proc /sys /dev/pts /dev"
|
|
SYS_MOUNT_POINT="$CHROOT_DIR/dev $CHROOT_DIR/run"
|
|
for i in $SYS_MOUNT_POINT_CHROOT;
|
|
do
|
|
umount_in_chroot $i
|
|
done
|
|
|
|
for i in $SYS_MOUNT_POINT;
|
|
do
|
|
umount_in_host $i
|
|
done
|
|
# exec_as_sudo_in_chroot umount /proc
|
|
# exec_as_sudo_in_chroot umount /sys
|
|
# exec_as_sudo_in_chroot umount /dev/pts
|
|
# exec_as_sudo umount $CHROOT_DIR/dev
|
|
# exec_as_sudo umount $CHROOT_DIR/run
|
|
if [ $CLEAN_CHROOT -eq 1 ];
|
|
then
|
|
exec_as_sudo rm -rf $CHROOT_DIR
|
|
fi
|
|
exec_as_sudo rm -rf image
|
|
exec_as_sudo rm -f $OUTPUT_DIR/ubuntu-from-scratch.iso
|
|
fi
|
|
|
|
mkdir -p $CHROOT_DIR
|
|
# # Copio el script chroot_setup.sh al directorio chroot
|
|
cp chroot_setup.sh $CHROOT_DIR
|
|
chmod +x $CHROOT_DIR/chroot_setup.sh
|
|
|
|
# # Deploy boot strap
|
|
if [ $CLEAN_CHROOT -eq 1 ];
|
|
then
|
|
exec_as_sudo debootstrap --arch=amd64 --variant=$VARIANT $DIST $CHROOT_DIR http://us.archive.ubuntu.com/ubuntu/
|
|
fi
|
|
exec_as_sudo mkdir -p $CHROOT_DIR/etc/calamares/
|
|
exec_as_sudo cp -r calamares/* $CHROOT_DIR/etc/calamares/
|
|
|
|
# # Create mount points
|
|
exec_as_sudo mount --bind /dev/ $CHROOT_DIR/dev
|
|
exec_as_sudo mount --bind /run/ $CHROOT_DIR/run
|
|
|
|
|
|
# Execute chroot_setup.sh
|
|
exec_as_sudo_in_chroot /chroot_setup.sh
|
|
|
|
|
|
# Clean up the environment
|
|
umount_in_host $CHROOT_DIR/dev
|
|
umount_in_host $CHROOT_DIR/run
|
|
|
|
# Compress the chroot
|
|
exec_as_sudo mv chroot/image .
|
|
|
|
|
|
# Create squashfs imagesudo
|
|
exec_as_sudo mksquashfs $CHROOT_DIR image/casper/filesystem.squashfs \
|
|
-noappend -no-duplicates -no-recovery \
|
|
-wildcards \
|
|
-comp xz -b 1M -Xdict-size 100% \
|
|
-e "var/cache/apt/archives/*" -e "root/*" -e "root/.*" -e "tmp/*" -e "tmp/.*" -e "swapfile"
|
|
|
|
printf $(exec_as_sudo du -sx --block-size=1 chroot | cut -f1) | sudo tee image/casper/filesystem.size
|
|
|
|
# Deboot strap real Ubuntu image
|
|
exec_as_sudo debootstrap --arch=amd64 --variant=minbase noble $UBUNTU_CHROOT_DIR http://us.archive.ubuntu.com/ubuntu/
|
|
# Compress the ububtu chroot
|
|
exec_as_sudo mksquashfs $UBUNTU_CHROOT_DIR image/casper/filesystem.ubuntu.squashfs \
|
|
-noappend -no-duplicates -no-recovery \
|
|
-wildcards \
|
|
-comp xz -b 1M -Xdict-size 100% \
|
|
-e "var/cache/apt/archives/*" -e "root/*" -e "root/.*" -e "tmp/*" -e "tmp/.*" -e "swapfile"
|
|
|
|
|
|
|
|
# Create ISO image
|
|
|
|
cd image && \
|
|
exec_as_sudo xorriso \
|
|
-as mkisofs \
|
|
-iso-level 3 \
|
|
-full-iso9660-filenames \
|
|
-J -joliet-long \
|
|
-volid "Ubuntu_scratch" \
|
|
-output $OUTPUT_DIR/ubuntu-from-scratch.iso \
|
|
-eltorito-boot isolinux/bios.img \
|
|
-no-emul-boot \
|
|
-boot-load-size 4 \
|
|
-boot-info-table \
|
|
--eltorito-catalog boot.catalog \
|
|
--grub2-boot-info \
|
|
--grub2-mbr ../chroot/usr/lib/grub/i386-pc/boot_hybrid.img \
|
|
-partition_offset 16 \
|
|
--mbr-force-bootable \
|
|
-eltorito-alt-boot \
|
|
-no-emul-boot \
|
|
-e isolinux/efiboot.img \
|
|
-append_partition 2 28732ac11ff8d211ba4b00a0c93ec93b isolinux/efiboot.img \
|
|
-appended_part_as_gpt \
|
|
-iso_mbr_part_type a2a0d0ebe5b9334487c068b6b72699c7 \
|
|
-m "isolinux/efiboot.img" \
|
|
-m "isolinux/bios.img" \
|
|
-e '--interval:appended_partition_2:::' \
|
|
-exclude isolinux \
|
|
-graft-points \
|
|
"/EFI/boot/bootx64.efi=isolinux/bootx64.efi" \
|
|
"/EFI/boot/mmx64.efi=isolinux/mmx64.efi" \
|
|
"/EFI/boot/grubx64.efi=isolinux/grubx64.efi" \
|
|
"/EFI/ubuntu/grub.cfg=isolinux/grub.cfg" \
|
|
"/isolinux/bios.img=isolinux/bios.img" \
|
|
"/isolinux/efiboot.img=isolinux/efiboot.img" \
|
|
"."
|
|
cd ..
|