mklive: multiple improvements and fixes.

- Does not need bash anymore, a POSIX shell is enough.
- Avoid installing base-system twice to generate the initramfs.
- Reconfigure some pkgs in the rootfs... otherwise some system users won't be created.
- Added -K opt to not remove builddir.
This commit is contained in:
Juan RP 2014-12-17 18:26:28 +01:00
parent c204ab1f5f
commit 4ab914cdf7
2 changed files with 72 additions and 92 deletions

View File

@ -11,7 +11,6 @@ This repository contains utilities for Void Linux:
#### Dependencies #### Dependencies
* xbps>=0.35 * xbps>=0.35
* GNU bash
* parted (for mkimage) * parted (for mkimage)
* qemu-user-static binaries (for mkrootfs) * qemu-user-static binaries (for mkrootfs)

View File

@ -1,4 +1,4 @@
#!/bin/bash #!/bin/sh
# #
# vim: set ts=4 sw=4 et: # vim: set ts=4 sw=4 et:
# #
@ -26,27 +26,27 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#- #-
set -E trap "error_out $LINENO $?" INT TERM 0
trap "echo; error_out $LINENO $?" INT TERM HUP ERR
readonly REQUIRED_PKGS="base-files syslinux grub-x86_64-efi squashfs-tools xorriso" readonly REQUIRED_PKGS="base-files dash coreutils sed tar gawk syslinux grub-x86_64-efi squashfs-tools xorriso"
readonly INITRAMFS_PKGS="binutils xz device-mapper"
readonly PROGNAME=$(basename $0) readonly PROGNAME=$(basename $0)
info_msg() { info_msg() {
printf "\033[1m$@\n\033[m" printf "\033[1m$@\n\033[m"
} }
die() {
info_msg "ERROR: $@"
error_out 1
}
error_out() { error_out() {
info_msg "There was an error in line $1 ... cleaning up $BUILDDIR, exiting." [ -d "$BUILDDIR" -a -z "$KEEP_BUILDDIR" ] && rm -rf "$BUILDDIR"
exit ${1:=0}
[ -d "$BUILDDIR" ] && rm -rf "$BUILDDIR"
exit 1
} }
usage() { usage() {
cat <<_EOF cat <<_EOF
Usage: $(basename $0) [options] Usage: $PROGNAME [options]
Options: Options:
-a <xbps-arch> Set XBPS_ARCH (do not use it unless you know what it is) -a <xbps-arch> Set XBPS_ARCH (do not use it unless you know what it is)
@ -63,8 +63,9 @@ Options:
-C "cmdline args" Add additional kernel command line arguments. -C "cmdline args" Add additional kernel command line arguments.
-T "title" Modify the bootloader title. -T "title" Modify the bootloader title.
-K Do not remove builddir.
The $(basename $0) script generates a live image of the Void Linux distribution. The $PROGNAME script generates a live image of the Void Linux distribution.
This ISO image can be written to a CD/DVD-ROM or any USB stick. This ISO image can be written to a CD/DVD-ROM or any USB stick.
_EOF _EOF
exit 1 exit 1
@ -74,17 +75,20 @@ copy_void_keys() {
mkdir -p "$1"/var/db/xbps/keys mkdir -p "$1"/var/db/xbps/keys
cp keys/*.plist "$1"/var/db/xbps/keys cp keys/*.plist "$1"/var/db/xbps/keys
} }
copy_void_conf() { copy_void_conf() {
install -Dm644 data/void-vpkgs.conf "$1"/usr/share/xbps/virtualpkg.d/void.conf install -Dm644 data/void-vpkgs.conf "$1"/usr/share/xbps/virtualpkg.d/void.conf
} }
copy_dracut_files() {
mkdir -p $1/usr/lib/dracut/modules.d/01vmklive
cp dracut/*.sh $1/usr/lib/dracut/modules.d/01vmklive/
}
install_prereqs() { install_prereqs() {
copy_void_conf $VOIDHOSTDIR copy_void_conf $VOIDHOSTDIR
$XBPS_INSTALL_CMD -r $VOIDHOSTDIR $XBPS_REPOSITORY $XBPS_CACHEDIR -y ${REQUIRED_PKGS} >> $LOGFILE 2>&1 $XBPS_INSTALL_CMD -r $VOIDHOSTDIR $XBPS_REPOSITORY $XBPS_CACHEDIR -y ${REQUIRED_PKGS}
if [ $? -ne 0 ]; then [ $? -ne 0 ] && die "Failed to install required software, exiting..."
info_msg "Failed to install required software, exiting..."
error_out
fi
} }
install_packages() { install_packages() {
@ -93,23 +97,19 @@ install_packages() {
fi fi
copy_void_conf $ROOTFS copy_void_conf $ROOTFS
# Check that all pkgs are reachable. # Check that all pkgs are reachable.
${XBPS_INSTALL_CMD} -r $ROOTFS $XBPS_REPOSITORY $XBPS_CACHEDIR -yn ${PACKAGE_LIST} >>$LOGFILE 2>&1 ${XBPS_INSTALL_CMD} -r $ROOTFS $XBPS_REPOSITORY $XBPS_CACHEDIR -yn ${PACKAGE_LIST} ${INITRAMFS_PKGS}
if [ $? -ne 0 ]; then [ $? -ne 0 ] && die "Missing required binary packages, exiting..."
info_msg "Missing required binary packages, exiting..."
error_out
fi
${XBPS_INSTALL_CMD} -r $ROOTFS $XBPS_REPOSITORY $XBPS_CACHEDIR -y ${PACKAGE_LIST} >>$LOGFILE 2>&1 ${XBPS_INSTALL_CMD} -r $ROOTFS $XBPS_REPOSITORY $XBPS_CACHEDIR -y ${PACKAGE_LIST} ${INITRAMFS_PKGS}
${XBPS_INSTALL_CMD} -r $ROOTFS $XBPS_REPOSITORY $XBPS_CACHEDIR -yu >>$LOGFILE 2>&1 [ $? -ne 0 ] && die "Failed to install $PACKAGE_LIST"
${XBPS_REMOVE_CMD} -r $ROOTFS $XBPS_CACHEDIR -o >>$LOGFILE 2>&1
# Enable choosen UTF-8 locale and generate it into the target rootfs. # Enable choosen UTF-8 locale and generate it into the target rootfs.
if [ -f $ROOTFS/etc/default/libc-locales ]; then if [ -f $ROOTFS/etc/default/libc-locales ]; then
sed -e "s/\#\(${LOCALE}.*\)/\1/g" -i $ROOTFS/etc/default/libc-locales sed -e "s/\#\(${LOCALE}.*\)/\1/g" -i $ROOTFS/etc/default/libc-locales
xbps-uchroot $ROOTFS xbps-reconfigure -f glibc-locales >>$LOGFILE 2>&1 xbps-reconfigure -r $ROOTFS -f glibc-locales || die "Failed to reconfigure glibc-locales"
# reconfigure util-linux just to be safe
xbps-reconfigure -r $ROOTFS -f util-linux || die "Failed to reconfigure util-linux"
fi fi
# reconfigure util-linux just to be safe
xbps-uchroot $ROOTFS xbps-reconfigure -f util-linux >>$LOGFILE 2>&1
if [ -x installer.sh ]; then if [ -x installer.sh ]; then
install -Dm755 installer.sh $ROOTFS/usr/sbin/void-installer install -Dm755 installer.sh $ROOTFS/usr/sbin/void-installer
@ -122,28 +122,26 @@ install_packages() {
unset XBPS_ARCH unset XBPS_ARCH
} }
copy_dracut_files() {
mkdir -p $1/usr/lib/dracut/modules.d/01vmklive
cp dracut/*.sh $1/usr/lib/dracut/modules.d/01vmklive/
}
generate_initramfs() { generate_initramfs() {
# Install required pkgs in a temporary rootdir to create copy_dracut_files $ROOTFS
# the initramfs and to copy required files.
copy_dracut_files $VOIDHOSTDIR
copy_void_conf $VOIDHOSTDIR
$XBPS_INSTALL_CMD -r $VOIDHOSTDIR $XBPS_REPOSITORY $XBPS_CACHEDIR -y base-system xz lz4 >>$LOGFILE 2>&1
if [ "$BASE_SYSTEM_PKG" = "base-system-systemd" ]; then if [ "$BASE_SYSTEM_PKG" = "base-system-systemd" ]; then
_args="--add systemd" _args="--add systemd"
else else
_args="--omit systemd" _args="--omit systemd"
fi fi
xbps-uchroot $VOIDHOSTDIR /usr/bin/dracut --${INITRAMFS_COMPRESSION} \ xbps-uchroot $ROOTFS env -i /usr/bin/dracut --${INITRAMFS_COMPRESSION} \
--force-add "vmklive" ${_args} "/boot/initrd" $KERNELVERSION >>$LOGFILE 2>&1 --force-add "vmklive" ${_args} "/boot/initrd" $KERNELVERSION
[ $? -ne 0 ] && die "Failed to generate the initramfs"
mv $VOIDHOSTDIR/boot/initrd $BOOT_DIR mv $ROOTFS/boot/initrd $BOOT_DIR
cp $VOIDHOSTDIR/boot/vmlinuz-$KERNELVERSION $BOOT_DIR/vmlinuz cp $ROOTFS/boot/vmlinuz-$KERNELVERSION $BOOT_DIR/vmlinuz
}
cleanup_rootfs() {
for f in ${INITRAMFS_PKGS}; do
xbps-remove -r $ROOTFS -Ry ${f} || die "Failed to remove $f from rootfs"
done
rm -r $ROOTFS/usr/lib/dracut/modules.d/01vmklive
} }
generate_isolinux_boot() { generate_isolinux_boot() {
@ -176,22 +174,30 @@ generate_grub_efi_boot() {
-e "s|@@BOOT_TITLE@@|${BOOT_TITLE}|" \ -e "s|@@BOOT_TITLE@@|${BOOT_TITLE}|" \
-e "s|@@BOOT_CMDLINE@@|${BOOT_CMDLINE}|" \ -e "s|@@BOOT_CMDLINE@@|${BOOT_CMDLINE}|" \
-e "s|@@LOCALE@@|${LOCALE}|" $GRUB_DIR/grub_void.cfg -e "s|@@LOCALE@@|${LOCALE}|" $GRUB_DIR/grub_void.cfg
mkdir -p $GRUB_DIR/fonts
cp -f $GRUB_DATADIR/unicode.pf2 $GRUB_DIR/fonts
modprobe -q loop || : modprobe -q loop || :
# Create EFI vfat image. # Create EFI vfat image.
dd if=/dev/zero of=$GRUB_DIR/efiboot.img bs=1024 count=4096 >>$LOGFILE 2>&1 dd if=/dev/zero of=$GRUB_DIR/efiboot.img bs=1024 count=4096 >/dev/null 2>&1
mkfs.vfat -F12 -S 512 -n "grub_uefi" "$GRUB_DIR/efiboot.img" >>$LOGFILE 2>&1 mkfs.vfat -F12 -S 512 -n "grub_uefi" "$GRUB_DIR/efiboot.img" >/dev/null 2>&1
GRUB_EFI_TMPDIR="$(mktemp --tmpdir=$HOME -d)" GRUB_EFI_TMPDIR="$(mktemp --tmpdir=$HOME -d)"
LOOP_DEVICE="$(losetup --show --find ${GRUB_DIR}/efiboot.img)" LOOP_DEVICE="$(losetup --show --find ${GRUB_DIR}/efiboot.img)"
mount -o rw,flush -t vfat "${LOOP_DEVICE}" "${GRUB_EFI_TMPDIR}" >>$LOGFILE 2>&1 mount -o rw,flush -t vfat "${LOOP_DEVICE}" "${GRUB_EFI_TMPDIR}" >/dev/null 2>&1
cp -a $IMAGEDIR/boot $VOIDHOSTDIR cp -a $IMAGEDIR/boot $VOIDHOSTDIR
xbps-uchroot $VOIDHOSTDIR grub-mkstandalone --directory="/usr/lib/grub/x86_64-efi" \ xbps-uchroot $VOIDHOSTDIR grub-mkstandalone \
--directory="/usr/lib/grub/x86_64-efi" \
--format="x86_64-efi" \ --format="x86_64-efi" \
--compression="xz" --output="/tmp/bootx64.efi" \ --compression="xz" --output="/tmp/bootx64.efi" \
"boot/grub/grub.cfg" >>$LOGFILE 2>&1 "boot/grub/grub.cfg"
if [ $? -ne 0 ]; then
umount "$GRUB_EFI_TMPDIR"
losetup --detach "${LOOP_DEVICE}"
die "Failed to generate EFI loader"
fi
mkdir -p ${GRUB_EFI_TMPDIR}/EFI/boot mkdir -p ${GRUB_EFI_TMPDIR}/EFI/boot
cp -f $VOIDHOSTDIR/tmp/bootx64.efi ${GRUB_EFI_TMPDIR}/EFI/boot/ cp -f $VOIDHOSTDIR/tmp/bootx64.efi ${GRUB_EFI_TMPDIR}/EFI/boot/
umount "$GRUB_EFI_TMPDIR" umount "$GRUB_EFI_TMPDIR"
@ -207,16 +213,16 @@ generate_squashfs() {
fi fi
mkdir -p "$BUILDDIR/tmp/LiveOS" mkdir -p "$BUILDDIR/tmp/LiveOS"
dd if=/dev/zero of="$BUILDDIR/tmp/LiveOS/ext3fs.img" \ dd if=/dev/zero of="$BUILDDIR/tmp/LiveOS/ext3fs.img" \
bs="$((ROOTFS_SIZE+ROOTFS_FREESIZE))M" count=1 >>$LOGFILE 2>&1 bs="$((ROOTFS_SIZE+ROOTFS_FREESIZE))M" count=1 >/dev/null 2>&1
mkdir -p "$BUILDDIR/tmp-rootfs" mkdir -p "$BUILDDIR/tmp-rootfs"
mkfs.ext3 -F -m1 "$BUILDDIR/tmp/LiveOS/ext3fs.img" >>$LOGFILE 2>&1 mkfs.ext3 -F -m1 "$BUILDDIR/tmp/LiveOS/ext3fs.img" >/dev/null 2>&1
mount -o loop "$BUILDDIR/tmp/LiveOS/ext3fs.img" "$BUILDDIR/tmp-rootfs" mount -o loop "$BUILDDIR/tmp/LiveOS/ext3fs.img" "$BUILDDIR/tmp-rootfs"
cp -a $ROOTFS/* $BUILDDIR/tmp-rootfs/ cp -a $ROOTFS/* $BUILDDIR/tmp-rootfs/
umount -f "$BUILDDIR/tmp-rootfs" umount -f "$BUILDDIR/tmp-rootfs"
mkdir -p "$IMAGEDIR/LiveOS" mkdir -p "$IMAGEDIR/LiveOS"
mksquashfs "$BUILDDIR/tmp" "$IMAGEDIR/LiveOS/squashfs.img" \ mksquashfs "$BUILDDIR/tmp" "$IMAGEDIR/LiveOS/squashfs.img" \
-comp ${SQUASHFS_COMPRESSION} >>$LOGFILE 2>&1 -comp ${SQUASHFS_COMPRESSION} || die "Failed to generate squashfs image"
chmod 444 "$IMAGEDIR/LiveOS/squashfs.img" chmod 444 "$IMAGEDIR/LiveOS/squashfs.img"
# Remove rootfs and temporary dirs, we don't need them anymore. # Remove rootfs and temporary dirs, we don't need them anymore.
rm -rf "$ROOTFS" "$BUILDDIR/tmp-rootfs" "$BUILDDIR/tmp" rm -rf "$ROOTFS" "$BUILDDIR/tmp-rootfs" "$BUILDDIR/tmp"
@ -233,18 +239,19 @@ generate_iso_image() {
-no-emul-boot -boot-load-size 4 -boot-info-table \ -no-emul-boot -boot-load-size 4 -boot-info-table \
-eltorito-alt-boot -e boot/grub/efiboot.img -isohybrid-gpt-basdat -no-emul-boot \ -eltorito-alt-boot -e boot/grub/efiboot.img -isohybrid-gpt-basdat -no-emul-boot \
-isohybrid-mbr $SYSLINUX_DATADIR/isohdpfx.bin \ -isohybrid-mbr $SYSLINUX_DATADIR/isohdpfx.bin \
-output "$CURDIR/$OUTPUT_FILE" "$IMAGEDIR" >>$LOGFILE 2>&1 -output "$CURDIR/$OUTPUT_FILE" "$IMAGEDIR" || die "Failed to generate ISO image"
} }
# #
# main() # main()
# #
while getopts "a:b:r:c:C:T:k:l:i:s:S:o:p:h" opt; do while getopts "a:b:r:c:C:T:Kk:l:i:s:S:o:p:h" opt; do
case $opt in case $opt in
a) BASE_ARCH="$OPTARG";; a) BASE_ARCH="$OPTARG";;
b) BASE_SYSTEM_PKG="$OPTARG";; b) BASE_SYSTEM_PKG="$OPTARG";;
r) XBPS_REPOSITORY+="--repository=$OPTARG ";; r) XBPS_REPOSITORY+="--repository=$OPTARG ";;
c) XBPS_CACHEDIR="--cachedir=$OPTARG";; c) XBPS_CACHEDIR="--cachedir=$OPTARG";;
K) readonly KEEP_BUILDDIR=1;;
k) KEYMAP="$OPTARG";; k) KEYMAP="$OPTARG";;
l) LOCALE="$OPTARG";; l) LOCALE="$OPTARG";;
i) INITRAMFS_COMPRESSION="$OPTARG";; i) INITRAMFS_COMPRESSION="$OPTARG";;
@ -257,7 +264,7 @@ while getopts "a:b:r:c:C:T:k:l:i:s:S:o:p:h" opt; do
h) usage;; h) usage;;
esac esac
done done
shift $(($OPTIND - 1)) shift $((OPTIND - 1))
# Set defaults # Set defaults
: ${XBPS_CACHEDIR:=--cachedir=/var/cache/xbps} : ${XBPS_CACHEDIR:=--cachedir=/var/cache/xbps}
@ -271,12 +278,9 @@ shift $(($OPTIND - 1))
# Required packages in the image for a working system. # Required packages in the image for a working system.
PACKAGE_LIST="$BASE_SYSTEM_PKG $PACKAGE_LIST" PACKAGE_LIST="$BASE_SYSTEM_PKG $PACKAGE_LIST"
LOGFILE="$(mktemp -t vmklive-XXXXXXXXXX.log)"
# Check for root permissions. # Check for root permissions.
if [ "$(id -u)" -ne 0 ]; then if [ "$(id -u)" -ne 0 ]; then
echo "Must be run as root, exiting..." die "Must be run as root, exiting..."
exit 1
fi fi
readonly CURDIR="$PWD" readonly CURDIR="$PWD"
@ -298,6 +302,7 @@ ISOLINUX_CFG="$ISOLINUX_DIR/isolinux.cfg"
: ${XBPS_REPOSITORY:=--repository=http://repo.voidlinux.eu/current} : ${XBPS_REPOSITORY:=--repository=http://repo.voidlinux.eu/current}
: ${SYSLINUX_DATADIR:=$VOIDHOSTDIR/usr/share/syslinux} : ${SYSLINUX_DATADIR:=$VOIDHOSTDIR/usr/share/syslinux}
: ${GRUB_DATADIR:=$VOIDHOSTDIR/usr/share/grub}
: ${SPLASH_IMAGE:=data/splash.png} : ${SPLASH_IMAGE:=data/splash.png}
: ${XBPS_INSTALL_CMD:=xbps-install} : ${XBPS_INSTALL_CMD:=xbps-install}
: ${XBPS_REMOVE_CMD:=xbps-remove} : ${XBPS_REMOVE_CMD:=xbps-remove}
@ -308,9 +313,7 @@ ISOLINUX_CFG="$ISOLINUX_DIR/isolinux.cfg"
mkdir -p $ROOTFS $VOIDHOSTDIR $ISOLINUX_DIR $GRUB_DIR mkdir -p $ROOTFS $VOIDHOSTDIR $ISOLINUX_DIR $GRUB_DIR
info_msg "Redirecting stdout/stderr to $LOGFILE ..." info_msg "[1/8] Synchronizing XBPS repository data..."
info_msg "[1/9] Synchronizing XBPS repository data..."
# Sync index for remote repos first.
copy_void_keys $ROOTFS copy_void_keys $ROOTFS
$XBPS_INSTALL_CMD -r $ROOTFS ${XBPS_REPOSITORY} -S $XBPS_INSTALL_CMD -r $ROOTFS ${XBPS_REPOSITORY} -S
cp -a $ROOTFS/* $VOIDHOSTDIR cp -a $ROOTFS/* $VOIDHOSTDIR
@ -321,15 +324,9 @@ KERNELVERSION=$($XBPS_UHELPER_CMD getpkgversion $KERNELVERSION)
: ${OUTPUT_FILE="void-live-$(uname -m)-${KERNELVERSION}-$(date +%Y%m%d).iso"} : ${OUTPUT_FILE="void-live-$(uname -m)-${KERNELVERSION}-$(date +%Y%m%d).iso"}
#
# Install required packages to generate the image.
#
info_msg "[2/9] Installing software to generate the image: ${REQUIRED_PKGS} ..." info_msg "[2/9] Installing software to generate the image: ${REQUIRED_PKGS} ..."
install_prereqs install_prereqs
#
# Install live system and specified packages.
#
mkdir -p "$ROOTFS"/etc mkdir -p "$ROOTFS"/etc
[ -s data/motd ] && cp data/motd $ROOTFS/etc [ -s data/motd ] && cp data/motd $ROOTFS/etc
[ -s data/issue ] && cp data/issue $ROOTFS/etc [ -s data/issue ] && cp data/issue $ROOTFS/etc
@ -337,42 +334,26 @@ mkdir -p "$ROOTFS"/etc
info_msg "[3/9] Installing void pkgs into the rootfs: ${PACKAGE_LIST} ..." info_msg "[3/9] Installing void pkgs into the rootfs: ${PACKAGE_LIST} ..."
install_packages install_packages
export PATH=$VOIDHOSTDIR/usr/bin:$VOIDHOSTDIR/usr/sbin:$PATH export PATH=$VOIDHOSTDIR/usr/bin:$VOIDHOSTDIR/usr/sbin:$ROOTFS/usr/bin:$ROOTFS/usr/sbin:$PATH
export LD_LIBRARY_PATH=$VOIDHOSTDIR/usr/lib export LD_LIBRARY_PATH=$ROOTFS/usr/lib
#
# Generate the initramfs.
#
info_msg "[4/9] Generating initramfs image ($INITRAMFS_COMPRESSION)..." info_msg "[4/9] Generating initramfs image ($INITRAMFS_COMPRESSION)..."
generate_initramfs generate_initramfs
#
# Generate the isolinux boot.
#
info_msg "[5/9] Generating isolinux support for PC-BIOS systems..." info_msg "[5/9] Generating isolinux support for PC-BIOS systems..."
generate_isolinux_boot generate_isolinux_boot
#
# Generate the GRUB EFI boot.
#
info_msg "[6/9] Generating GRUB support for EFI systems..." info_msg "[6/9] Generating GRUB support for EFI systems..."
generate_grub_efi_boot generate_grub_efi_boot
# info_msg "[7/9] Cleaning up rootfs..."
# Generate the squashfs image from rootfs. cleanup_rootfs
#
info_msg "[7/9] Generating squashfs image ($SQUASHFS_COMPRESSION) from rootfs..." info_msg "[8/9] Generating squashfs image ($SQUASHFS_COMPRESSION) from rootfs..."
generate_squashfs generate_squashfs
# info_msg "[9/9] Generating ISO image..."
# Generate the ISO image.
#
info_msg "[8/9] Generating ISO image..."
generate_iso_image generate_iso_image
info_msg "[9/9] Removing build directory..."
rm -rf "$BUILDDIR"
hsize=$(du -sh "$CURDIR/$OUTPUT_FILE"|awk '{print $1}') hsize=$(du -sh "$CURDIR/$OUTPUT_FILE"|awk '{print $1}')
info_msg "Created $(readlink -f $CURDIR/$OUTPUT_FILE) ($hsize) successfully." info_msg "Created $(readlink -f $CURDIR/$OUTPUT_FILE) ($hsize) successfully."
exit 0