123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 |
- #!/bin/bash
- #
- # vim: set ts=4 sw=4 et:
- #
- #-
- # Copyright (c) 2009-2013 Juan Romero Pardines.
- # All rights reserved.
- #
- # Redistribution and use in source and binary forms, with or without
- # modification, are permitted provided that the following conditions
- # are met:
- # 1. Redistributions of source code must retain the above copyright
- # notice, this list of conditions and the following disclaimer.
- # 2. Redistributions in binary form must reproduce the above copyright
- # notice, this list of conditions and the following disclaimer in the
- # documentation and/or other materials provided with the distribution.
- #
- # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- #-
- set -E
- trap "echo; error_out $LINENO $?" INT TERM HUP ERR
- readonly PROGNAME=$(basename $0)
- info_msg() {
- printf "\033[1m$@\n\033[m"
- }
- error_out() {
- info_msg "There was an error in line $1 ... cleaning up $BUILDDIR, exiting."
- [ -d "$BUILDDIR" ] && rm -rf "$BUILDDIR"
- exit 1
- }
- write_etc_motd() {
- cat >> "$ROOTFS/etc/motd" <<_EOF
- ###############################################################################
- Autogenerated by void-mklive "@@MKLIVE_VERSION@@"
- ###############################################################################
- Welcome to the Void Linux Live system, you have been autologged in.
- This user has full sudo(8) permissions without any password, be careful
- executing commands through sudo(8).
- To start the installation please type:
- $ sudo void-installer
- and follow the on-screen instructions. Thanks for using Void Linux.
- http://www.voidlinux.eu
- ###############################################################################
- _EOF
- }
- write_conf_file() {
- cat > "$1" <<_EOF
- # *-*- sh -*-*
- # Default configuration file for vmklive-@VERSION@.
- #
- # List of packages to be installed into the live image.
- # NOTE: base-system and dialog packages are always added by default.
- #
- #PACKAGE_LIST=""
- # Default Void Linux splash image for grub/syslinux.
- SPLASH_IMAGE=/usr/share/void-artwork/splash.png
- # Default keymap to use.
- KEYMAP=us
- # Default locale to use.
- LOCALE=en_US.UTF-8
- # Path to XBPS utilities.
- #XBPS_INSTALL_CMD=xbps-install
- #XBPS_QUERY_CMD=xbps-query
- #XBPS_RECONFIGURE_CMD=xbps-reconfigure
- #XBPS_REMOVE_CMD=xbps-remove
- #XBPS_RINDEX_CMD=xbps-rindex
- #XBPS_UHELPER_CMD=xbps-uhelper
- # XBPS cache directory to install packages from.
- #REPOSITORY_CACHE=/blah/foo
- _EOF
- chmod 644 "$1"
- }
- usage() {
- cat <<_EOF
- Usage: $(basename $0) [options]
- Options:
- -C file Path to configuration file (defaults to ~/.mklive.conf)
- -c (gzip|bzip2|xz) Compression type for the initramfs/squashfs image.
- -r rootdir Use this directory to generate the image (if unset, defaults to cwd).
- -o outfile Output file name for the ISO image.
- -s splash Splash image file for isolinux.
- -V Show version.
- _EOF
- exit 1
- }
- copy_void_keys() {
- if [ ! -d "$1"/var/db/xbps/keys ]; then
- mkdir -p "$1"/var/db/xbps/keys
- cp /var/db/xbps/keys/*.plist "$1"/var/db/xbps/keys
- fi
- }
- install_packages() {
- for f in ${PACKAGE_LIST}; do
- info_msg " $f"
- done
- # Check that all pkgs are reachable.
- ${XBPS_INSTALL_CMD} ${XBPS_ARGS} -n ${PACKAGE_LIST} >>$LOGFILE 2>&1
- if [ $? -ne 0 ]; then
- info_msg "Missing required binary packages, exiting..."
- error_out
- fi
- ${XBPS_INSTALL_CMD} ${XBPS_ARGS} ${PACKAGE_LIST} >>$LOGFILE 2>&1
- ${XBPS_INSTALL_CMD} ${XBPS_ARGS} -u >>$LOGFILE 2>&1
- ${XBPS_REMOVE_CMD} ${XBPS_ARGS} -o >>$LOGFILE 2>&1
- ${XBPS_QUERY_CMD} -r "$ROOTFS" -l | sort > "${OUTPUT_FILE%.iso}"-package-list.txt
- # Enable choosen UTF-8 locale and generate it into the target rootfs.
- sed -e "s/\#\(${LOCALE}.*\)/\1/g" -i $ROOTFS/etc/default/libc-locales
- systemd-nspawn -D $ROOTFS xbps-reconfigure -fa >>$LOGFILE 2>&1
- # Enable some services if found.
- if [ -f $ROOTFS/usr/lib/systemd/system/NetworkManager.service ]; then
- systemd-nspawn -D $ROOTFS systemctl enable NetworkManager.service >>$LOGFILE 2>&1
- fi
- if [ -x installer.sh ]; then
- install -Dm755 installer.sh $ROOTFS/usr/sbin/void-installer
- else
- install -Dm755 /usr/sbin/void-installer $ROOTFS/usr/sbin/void-installer
- fi
- }
- generate_initramfs() {
- copy_void_keys $ROOTFS/kernel_temp
- # Install required pkgs in a temporary rootdir to create
- # the initramfs and to copy required files.
- $XBPS_INSTALL_CMD -r $ROOTFS/kernel_temp -Sy \
- base-system void-mklive -c $REPOSITORY_CACHE >>$LOGFILE 2>&1
- systemd-nspawn -D $ROOTFS/kernel_temp /usr/bin/dracut --${COMPRESSTYPE} \
- --force-add "vmklive" "/boot/initrd.lz" $KERNELVERSION >>$LOGFILE 2>&1
- mv $ROOTFS/kernel_temp/boot/initrd.lz $BOOT_DIR
- cp $ROOTFS/kernel_temp/boot/vmlinuz-$KERNELVERSION $BOOT_DIR/vmlinuz
- rm -rf $ROOTFS/kernel_temp
- }
- generate_isolinux_boot() {
- cp -f $SYSLINUX_DATADIR/isolinux.bin "$ISOLINUX_DIR"
- cp -f $SYSLINUX_DATADIR/ldlinux.c32 "$ISOLINUX_DIR"
- cp -f $SYSLINUX_DATADIR/libcom32.c32 "$ISOLINUX_DIR"
- cp -f $SYSLINUX_DATADIR/vesamenu.c32 "$ISOLINUX_DIR"
- cp -f $SYSLINUX_DATADIR/libutil.c32 "$ISOLINUX_DIR"
- cp -f $SYSLINUX_DATADIR/chain.c32 "$ISOLINUX_DIR"
- cp -f $MKLIVE_DATADIR/isolinux.cfg.in \
- "$ISOLINUX_DIR"/isolinux.cfg
- if [ -f "$SPLASH_IMAGE" ]; then
- cp -f $SPLASH_IMAGE "$ISOLINUX_DIR"
- fi
- sed -i -e "s|@@SPLASHIMAGE@@|$(basename $SPLASH_IMAGE)|" \
- -e "s|@@KERNVER@@|${KERNELVERSION}|" \
- -e "s|@@KEYMAP@@|${KEYMAP}|" \
- -e "s|@@ARCH@@|$(uname -m)|" \
- -e "s|@@LOCALE@@|${LOCALE}|" $ISOLINUX_DIR/isolinux.cfg
- }
- generate_grub_efi_boot() {
- cp -f $MKLIVE_DATADIR/grub.cfg $GRUB_DIR
- cp -f $MKLIVE_DATADIR/grub_void.cfg.in $GRUB_DIR/grub_void.cfg
- sed -i -e "s|@@SPLASHIMAGE@@|$(basename $SPLASH_IMAGE)|" \
- -e "s|@@KERNVER@@|${KERNELVERSION}|" \
- -e "s|@@KEYMAP@@|${KEYMAP}|" \
- -e "s|@@ARCH@@|$(uname -m)|" \
- -e "s|@@LOCALE@@|${LOCALE}|" $GRUB_DIR/grub_void.cfg
- mkdir -p $GRUB_DIR/fonts
- cp -f $GRUB_DATADIR/unicode.pf2 $GRUB_DIR/fonts
- # Create EFI vfat image.
- dd if=/dev/zero of=$GRUB_DIR/efiboot.img bs=1024 count=4096 >>$LOGFILE 2>&1
- mkfs.vfat -F12 -S 512 -n "grub_uefi" "$GRUB_DIR/efiboot.img" >>$LOGFILE 2>&1
- GRUB_EFI_TMPDIR="$(mktemp --tmpdir=$HOME -d)"
- LOOP_DEVICE="$(losetup --show --find ${GRUB_DIR}/efiboot.img)"
- mount -o rw,flush -t vfat "${LOOP_DEVICE}" "${GRUB_EFI_TMPDIR}" >>$LOGFILE 2>&1
- mkdir -p "${GRUB_EFI_TMPDIR}/EFI/boot/"
- cd "$BUILDDIR"
- grub-mkstandalone --directory="/usr/lib/grub/x86_64-efi" \
- --format="x86_64-efi" \
- --compression="xz" --output="${GRUB_EFI_TMPDIR}/EFI/boot/bootx64.efi" \
- "boot/grub/grub.cfg" >>$LOGFILE 2>&1
- umount "$GRUB_EFI_TMPDIR"
- losetup --detach "${LOOP_DEVICE}"
- rm -rf $GRUB_EFI_TMPDIR
- }
- generate_squashfs() {
- # Find out required size for the rootfs and create an ext3fs image off it.
- ROOTFS_SIZE=$(du -sk "$ROOTFS"|awk '{print $1}')
- mkdir -p "$BUILDDIR/tmp/LiveOS"
- dd if=/dev/zero of="$BUILDDIR/tmp/LiveOS/ext3fs.img" \
- bs="$((${ROOTFS_SIZE}+($ROOTFS_SIZE/6)))K" count=1 >>$LOGFILE 2>&1
- mkdir -p "$BUILDDIR/tmp-rootfs"
- mkfs.ext3 -F -m1 "$BUILDDIR/tmp/LiveOS/ext3fs.img" >>$LOGFILE 2>&1
- mount -o loop "$BUILDDIR/tmp/LiveOS/ext3fs.img" "$BUILDDIR/tmp-rootfs"
- cd $BUILDDIR
- cp -a rootfs/* tmp-rootfs/
- umount -f "$BUILDDIR/tmp-rootfs"
- mkdir -p "$BUILDDIR/LiveOS"
- mksquashfs "$BUILDDIR/tmp" "$BUILDDIR/LiveOS/squashfs.img" \
- -comp ${COMPRESSTYPE} >>$LOGFILE 2>&1
- chmod 444 "$BUILDDIR/LiveOS/squashfs.img"
- # Remove rootfs and temporary dirs, we don't need them anymore.
- rm -rf "$ROOTFS" "$BUILDDIR/tmp-rootfs" "$BUILDDIR/tmp"
- }
- generate_iso_image() {
- xorriso -as mkisofs \
- -iso-level 3 -rock -joliet \
- -max-iso9660-filenames -omit-period \
- -omit-version-number -relaxed-filenames -allow-lowercase \
- -volid "VOID_LIVE" \
- -eltorito-boot boot/isolinux/isolinux.bin \
- -eltorito-catalog boot/isolinux/boot.cat \
- -no-emul-boot -boot-load-size 4 -boot-info-table \
- -eltorito-alt-boot -e boot/grub/efiboot.img -isohybrid-gpt-basdat -no-emul-boot \
- -isohybrid-mbr $SYSLINUX_DATADIR/isohdpfx.bin \
- -output "$OUTPUT_FILE" "$BUILDDIR" >>$LOGFILE 2>&1
- }
- #
- # main()
- #
- while getopts "C:c:o:r:s:hV" opt; do
- case $opt in
- C) CONFIG_FILE="$OPTARG";;
- c) COMPRESSTYPE="$OPTARG";;
- o) OUTPUT_FILE="$OPTARG";;
- r) ROOTDIR="$OPTARG";;
- s) SPLASH_IMAGE="$OPTARG";;
- h) usage;;
- V) echo "$PROGNAME @@MKLIVE_VERSION@@"; exit 0;;
- esac
- done
- shift $(($OPTIND - 1))
- # Set defaults
- if [ -z "$CONFIG_FILE" ]; then
- CONFIG_FILE="$HOME/.mklive.conf"
- fi
- LOGFILE="$(mktemp -t vmklive-XXXXXXXXXX.log)"
- if [ -z "$SYSLINUX_DATADIR" ]; then
- SYSLINUX_DATADIR=/usr/share/syslinux
- fi
- if [ -z "$GRUB_DATADIR" ]; then
- GRUB_DATADIR=/usr/share/grub
- fi
- if [ -z "$MKLIVE_DATADIR" ]; then
- MKLIVE_DATADIR=/usr/share/void-mklive
- fi
- if [ -z "$SPLASH_IMAGE" ]; then
- SPLASH_IMAGE=/usr/share/void-artwork/splash.png
- fi
- if [ -z "$XBPS_INSTALL_CMD" ]; then
- XBPS_INSTALL_CMD=xbps-install
- fi
- if [ -z "$XBPS_REMOVE_CMD" ]; then
- XBPS_REMOVE_CMD=xbps-remove
- fi
- if [ -z "$XBPS_QUERY_CMD" ]; then
- XBPS_QUERY_CMD=xbps-query
- fi
- if [ -z "$XBPS_RINDEX_CMD" ]; then
- XBPS_RINDEX_CMD=xbps-rindex
- fi
- if [ -z "$XBPS_UHELPER_CMD" ]; then
- XBPS_UHELPER_CMD=xbps-uhelper
- fi
- if [ -z "$XBPS_RECONFIGURE_CMD" ]; then
- XBPS_RECONFIGURE_CMD=xbps-reconfigure
- fi
- if [ -z "$COMPRESSTYPE" ]; then
- COMPRESSTYPE=xz
- fi
- if [ -z "$REPOSITORYCACHE" ]; then
- REPOSITORY_CACHE="/var/cache/xbps"
- fi
- # Create or read configuration file.
- if [ ! -r $CONFIG_FILE ]; then
- info_msg "Creating config file at $CONFIG_FILE."
- write_conf_file $CONFIG_FILE
- fi
- . $CONFIG_FILE
- if [ -z "$PACKAGE_LIST" ]; then
- PACKAGE_LIST="base-system dialog"
- else
- PACKAGE_LIST="base-system dialog $PACKAGE_LIST"
- fi
- if [ ! -f $SYSLINUX_DATADIR/isolinux.bin ]; then
- echo "Missing required isolinux files in $SYSLINUX_DATADIR, exiting..."
- exit 1
- fi
- # Check for root permissions.
- if [ "$(id -u)" -ne 0 ]; then
- echo "Must be run as root, exiting..."
- exit 1
- fi
- ISO_VOLUME="VOID_LIVE"
- if [ -n "$ROOTDIR" ]; then
- BUILDDIR=$(mktemp --tmpdir="$ROOTDIR" -d)
- else
- BUILDDIR=$(mktemp --tmpdir="$(pwd -P)" -d)
- fi
- BUILDDIR=$(readlink -f $BUILDDIR)
- ROOTFS="$BUILDDIR/rootfs"
- BOOT_DIR="$BUILDDIR/boot"
- ISOLINUX_DIR="$BOOT_DIR/isolinux"
- GRUB_DIR="$BOOT_DIR/grub"
- ISOLINUX_CFG="$ISOLINUX_DIR/isolinux.cfg"
- mkdir -p $ISOLINUX_DIR $GRUB_DIR
- #
- # Check there are repos registered before anything.
- #
- ${XBPS_QUERY_CMD} -L >/dev/null 2>&1
- if [ $? -ne 0 ]; then
- echo "No repositories available, exiting..."
- error_out
- fi
- XBPS_ARGS="-r $ROOTFS -y"
- XBPS_ARGS="$XBPS_ARGS -c $REPOSITORY_CACHE"
- XBPS_VERSION=$($XBPS_QUERY_CMD -V|awk '{print $2}')
- case $XBPS_VERSION in
- *) ;;
- esac
- # Sync index for remote repos first.
- copy_void_keys $ROOTFS
- $XBPS_INSTALL_CMD -r $ROOTFS -S >/dev/null 2>&1
- _linux_series=$($XBPS_QUERY_CMD -r $ROOTFS -Rx linux)
- KERNELVERSION=$($XBPS_QUERY_CMD -r $ROOTFS -R --property pkgver ${_linux_series})
- KERNELVERSION=$($XBPS_UHELPER_CMD getpkgversion $KERNELVERSION)
- if [ -z "$OUTPUT_FILE" ]; then
- OUTPUT_FILE="$HOME/void-live-$(uname -m)-${KERNELVERSION}-$(date +%Y%m%d).iso"
- fi
- info_msg "Redirecting stdout/stderr to $LOGFILE ..."
- #
- # Install live system and specified packages.
- #
- info_msg "[1/7] Installing packages into the rootfs..."
- install_packages
- #
- # Prepare /etc/motd.
- #
- mkdir -p "$ROOTFS"/etc
- write_etc_motd
- #
- # Generate the initramfs.
- #
- info_msg "[2/7] Generating initramfs image ($COMPRESSTYPE)..."
- generate_initramfs
- #
- # Generate the isolinux boot.
- #
- info_msg "[3/7] Generating isolinux support for PC-BIOS systems..."
- generate_isolinux_boot
- #
- # Generate the GRUB EFI boot.
- #
- info_msg "[4/7] Generating GRUB support for EFI systems..."
- generate_grub_efi_boot
- #
- # Generate the squashfs image from rootfs.
- #
- info_msg "[5/7] Generating squashfs image ($COMPRESSTYPE) from rootfs..."
- generate_squashfs
- #
- # Generate the ISO image.
- #
- info_msg "[6/7] Generating ISO image..."
- generate_iso_image
- info_msg "[7/7] Removing build directory..."
- rm -rf "$BUILDDIR"
- hsize=$(du -sh "$OUTPUT_FILE"|awk '{print $1}')
- info_msg "Created $(readlink -f $OUTPUT_FILE) ($hsize) successfully."
- exit 0
|