diff --git a/Makefile b/Makefile index 8f9df66..27d5256 100644 --- a/Makefile +++ b/Makefile @@ -61,12 +61,14 @@ LIBS = \ CPIOHOOKS = \ initcpio/hooks/miso \ + initcpio/hooks/miso_overlayfs \ initcpio/hooks/miso_loop_mnt \ initcpio/hooks/miso_pxe_common \ initcpio/hooks/miso_pxe_http CPIOINST = \ initcpio/inst/miso \ + initcpio/inst/miso_overlayfs \ initcpio/inst/miso_loop_mnt \ initcpio/inst/miso_pxe_common \ initcpio/inst/miso_pxe_http \ diff --git a/initcpio/hooks/miso_overlayfs b/initcpio/hooks/miso_overlayfs new file mode 100644 index 0000000..249a669 --- /dev/null +++ b/initcpio/hooks/miso_overlayfs @@ -0,0 +1,257 @@ +# kernel_cmdline +# Looks for a parameter on the kernel's boot-time command line. +# +# returns: 0 if param was found. Also prints its value if it was a K=V param. +# 1 if it was not. Also prints value passed as +# + +kernel_cmdline () +{ + for param in $(/bin/cat /proc/cmdline); do + case "${param}" in + $1=*) echo "${param##*=}"; return 0 ;; + $1) return 0 ;; + *) continue ;; + esac + done + [ -n "${2}" ] && echo "${2}" + return 1 +} + +# 1$ : kernel argument to check +overlay_arg_available() { + for arg in $(echo "$(kernel_cmdline overlay)" | /bin/sed 's/,/ /g') + do + [ "${arg}" == "$1" ] && return 0 + done + + return 1 +} + +# args: source, mountpoint +_mnt_bind() { + local src="${1}" + local mnt="${2}" + msg "::: Binding ${src} to ${mnt}" + mkdir -p "${mnt}" + /bin/mount -o bind "${src}" "${mnt}" +} + +# args: /path/to/image_file, mountpoint +_mnt_squashfs() { + local img="${1}" + local mnt="${2}" + local img_fullname="${img##*/}"; + local img_name="${img_fullname%.*}" + local tmp_mnt="/ro_branch/${img_name}" + + if [ "${copytoram}" = "y" ]; then + msg -n ":: Copying squashfs image to RAM..." + /bin/cp "${img}" "/copytoram/${img_fullname}" + if [ $? -ne 0 ]; then + echo ">> ERROR: while copy ${img} to /copytoram/${img_fullname}" + launch_interactive_shell + fi + img="/copytoram/${img_fullname}" + msg "done." + fi + + mkdir -p "${tmp_mnt}" + /bin/mount -r -t squashfs "${img}" "${tmp_mnt}" + if [ $? -ne 0 ]; then + echo ">> ERROR: while mounting ${img} to ${tmp_mnt}" + launch_interactive_shell + fi + + if [ "/${mnt#/*/}" = "/" ]; then + overlayfs="${overlayfs}:${tmp_mnt}" + else + _mnt_bind "${tmp_mnt}" "${mnt}" + fi +} + +run_hook() { + modprobe loop + if [ "x${arch}" = "x" ]; then + arch="$(uname -m)" + fi + + if [ "x${rw_branch_size}" = "x" ]; then + rw_branch_size="75%" + fi + + if [ "x${copytoram_size}" = "x" ]; then + copytoram_size="75%" + fi + + if [ "x${misobasedir}" = "x" ]; then + misobasedir="manjaro" + fi + + if [ "x${isomounts}" != "x" ]; then + isomounts="/bootmnt/${isomounts}" + else + isomounts="/bootmnt/${misobasedir}/isomounts" + fi + + # set mount handler for miso + mount_handler="miso_mount_handler" +} + +testdevice() { + fstype=$( blkid -s TYPE -o value ${MDEV} ) + mount -r -t ${fstype} -o noatime "${MDEV}" "/bootmnt" >/dev/null 2>/dev/null + if [ $? -eq 0 ]; then + if [ -f "/bootmnt/.miso" ]; then + msg "::: INFO: miso system at ${MDEV}" + found="yes" + else + /bin/umount "/bootmnt/" 2>/dev/null + msg "::: INFO: No miso system at ${MDEV}" + MDEV="" + fi + else + msg ">> ERROR: Couldn't mount ${MDEV}" + MDEV="" + fi +} + +probedevice() { + + if [[ -n "${ip}" && -n "${miso_http_srv}" && -f "${isomounts}" ]]; then + found="yes" + return + fi + +# Loop device test every second up to some limit + if [ "x${usbdelay}" != "x" ]; then + waittime=${usbdelay} + else + waittime=35 + fi + waitcount=0 + while [ ${waittime} -ne ${waitcount} ]; do + # first we look for root, e.g. /dev/sda1 to see if .miso will be found + if [ "x${root}" != "x" ]; then + [ ${waitcount} -eq 0 ] && msg ":: Looking for ${root}" + if [ -e ${root} ]; then + MDEV=${root} + testdevice + fi + fi + + # if still not found, we will look for misolabel + if [ "x${misolabel}" != "x" ]; then + [ ${waitcount} -eq 0 ] && msg ":: Looking for LABEL ${misolabel}" + MDEV=$( blkid -L ${misolabel} ) + if [ "x${MDEV}" != "x" ]; then + testdevice + else + msg "::: INFO: LABEL ${misolabel} is not valid!" + misolabel="" + fi + fi + + # still nothing found, we will look for boot device + if [ -z "${found}" ]; then + [ ${waitcount} -eq 0 ] && msg ":: Looking for boot device" + if [ "x${nocd}" = "x" ]; then + # Look for CD + cdroms=$( /bin/cat /proc/sys/dev/cdrom/info | { while read a b c; do + if [ "${a}" = "drive" -a "${b}" = "name:" ]; then + echo "${c}" + break + fi + done + } ) + for i in ${cdroms}; do + MDEV=/dev/${i} + testdevice + if [ "x${MDEV}" != "x" ]; then + break + fi + done + fi + + # Test partitions + if [ "x${MDEV}" = "x" ]; then + for d in /dev/sd[a-z][0-9]*; do + MDEV=${d} + testdevice + if [ "x${MDEV}" != "x" ]; then + break + fi + done + fi + + fi + + [ "x${MDEV}" != "x" ] && break + + /bin/sleep 1 + waitcount=$(( ${waitcount} + 1 )) + done +} + +miso_mount_handler() { + local newroot="${1}" + local found + local overlayfs + + # Probe all devices + probedevice + + if [ -z "${found}" ]; then + echo ">> ERROR: unable to find boot device" + echo " Falling back to interactive prompt" + echo " You can try to fix the problem manually, log out when you are finished" + launch_interactive_shell + fi + + if [ "${copytoram}" = "y" ]; then + msg -n ":: Mounting /copytoram (tmpfs) filesystem, size=${copytoram_size}..." + mount -t tmpfs -o "size=${copytoram_size}",mode=0755 copytoram /copytoram + msg "done." + fi + + msg -n ":: Mounting rw_branch (tmpfs) filesystem, size=${rw_branch_size}..." + mount -t tmpfs -o "size=${rw_branch_size}",mode=0755 rw_branch /rw_branch + msg "done." + + msg ":: Mounting images" + while read img imgarch mountpoint type kernelarg; do + # check if this line is a comment (starts with #) + [ "${img#"#"}" != "${img}" ] && continue + + [ "$imgarch" != "$arch" ] && continue + + [ ! -r "/bootmnt/${misobasedir}/${img}" ] && continue + + # check if the overlay should be loaded + overlay_arg_available "$kernelarg" + + + if [ $? == 1 ] && [ "$kernelarg" != "" ]; then + continue + fi + + if [ "${type}" = "bind" ]; then + _mnt_bind "/bootmnt/${misobasedir}/${img}" "${newroot}${mountpoint}" + elif [ "${type}" = "squashfs" ]; then + _mnt_squashfs "/bootmnt/${misobasedir}/${img}" "${newroot}${mountpoint}" + fi + done < "${isomounts}" + + msg "Mounting ${overlayfs:1}" + mkdir -p "/rw_branch/upper" + mkdir -p "/rw_branch/work" + mount -t overlay overlay -olowerdir="${overlayfs:1}",upperdir="/rw_branch/upper",workdir="/rw_branch/work" "${newroot}/" + + if [ "${copytoram}" = "y" ]; then + /bin/umount /bootmnt + else + _mnt_bind /bootmnt "${newroot}/bootmnt" + fi +} + +# vim:ft=sh:ts=4:sw=4:et: diff --git a/initcpio/inst/miso_overlayfs b/initcpio/inst/miso_overlayfs new file mode 100644 index 0000000..d88ba38 --- /dev/null +++ b/initcpio/inst/miso_overlayfs @@ -0,0 +1,31 @@ +#!/bin/bash + +build() { + add_module "cdrom" + add_module "overlay" + add_module "squashfs" + add_module "isofs" + add_module "loop" + add_module "usb_storage" + add_module "sd_mod" + add_module "sr_mod" + add_module "virtio_pci" + add_module "virtio_blk" + + add_dir /rw_branch + add_dir /ro_branch + add_dir /copytoram + add_dir /bootmnt + + add_runscript + + add_binary /usr/lib/udev/cdrom_id + add_binary blockdev + add_binary losetup + add_binary mountpoint + + add_file /usr/lib/udev/rules.d/60-cdrom_id.rules +} + +# vim: set ft=sh ts=4 sw=4 et: + diff --git a/lib/util-iso.sh b/lib/util-iso.sh index e9b48c2..e2c061e 100644 --- a/lib/util-iso.sh +++ b/lib/util-iso.sh @@ -204,8 +204,13 @@ make_image_custom() { msg "Prepare [${custom} installation] (${custom}-image)" local path="${work_dir}/${custom}-image" mkdir -p ${path} - umount_image_handler - aufs_mount_root_image "${path}" + if [[ ${use_overlayfs} == "true" ]];then + mkdir -p "${work_dir}/work" + mount -t overlay overlay -olowerdir="${work_dir}/root-image",upperdir="${path}",workdir="${work_dir}/work" "${path}" + else + umount_image_handler + aufs_mount_root_image "${path}" + fi chroot_create "${path}" "${packages}" clean_up_image "${path}" pacman -Qr "${path}" > "${path}/${custom}-image-pkgs.txt" @@ -213,8 +218,13 @@ make_image_custom() { [[ -d ${custom}-overlay ]] && copy_overlay_custom configure_custom_image "${path}" ${is_custom_pac_conf} && clean_pacman_conf "${path}" - umount_image_handler - aufs_clean "${path}" + if [[ ${use_overlayfs} == "true" ]];then + umount "${path}" + rm -rf "${work_dir}/work" + else + umount_image_handler + aufs_clean "${path}" + fi : > ${work_dir}/build.${FUNCNAME} msg "Done [${custom} installation] (${custom}-image)" fi @@ -225,12 +235,21 @@ make_image_livecd() { msg "Prepare [livecd installation] (livecd-image)" local path="${work_dir}/livecd-image" mkdir -p ${path} - umount_image_handler - if [[ -n "${custom}" ]] ; then - aufs_mount_custom_image "${path}" - aufs_append_root_image "${path}" + if [[ ${use_overlayfs} == "true" ]];then + mkdir -p "${work_dir}/work" + if [[ -n "${custom}" ]] ; then + mount -t overlay overlay -olowerdir="${work_dir}/${custom}-image":"${work_dir}/root-image",upperdir="${path}",workdir="${work_dir}/work" "${path}" + else + mount -t overlay overlay -olowerdir="${work_dir}/root-image",upperdir="${path}",workdir="${work_dir}/work" "${path}" + fi else - aufs_mount_root_image "${path}" + umount_image_handler + if [[ -n "${custom}" ]] ; then + aufs_mount_custom_image "${path}" + aufs_append_root_image "${path}" + else + aufs_mount_root_image "${path}" + fi fi chroot_create "${path}" "${packages}" clean_up_image "${path}" @@ -243,8 +262,13 @@ make_image_livecd() { ${is_custom_pac_conf} && clean_pacman_conf "${path}" # Clean up GnuPG keys? rm -rf "${path}/etc/pacman.d/gnupg" - umount_image_handler - aufs_clean "${path}" + if [[ ${use_overlayfs} == "true" ]];then + umount "${path}" + rm -rf "${work_dir}/work" + else + umount_image_handler + aufs_clean "${path}" + fi : > ${work_dir}/build.${FUNCNAME} msg "Done [livecd-image]" fi @@ -255,12 +279,21 @@ make_image_xorg() { msg "Prepare [pkgs-image]" local path="${work_dir}/pkgs-image" mkdir -p ${path}/opt/livecd/pkgs - umount_image_handler - if [[ -n "${custom}" ]] ; then - aufs_mount_custom_image "${path}" - aufs_append_root_image "${path}" + if [[ ${use_overlayfs} == "true" ]];then + mkdir -p "${work_dir}/work" + if [[ -n "${custom}" ]] ; then + mount -t overlay overlay -olowerdir="${work_dir}/${custom}-image":"${work_dir}/root-image",upperdir="${path}",workdir="${work_dir}/work" "${path}" + else + mount -t overlay overlay -olowerdir="${work_dir}/root-image",upperdir="${path}",workdir="${work_dir}/work" "${path}" + fi else - aufs_mount_root_image "${path}" + umount_image_handler + if [[ -n "${custom}" ]] ; then + aufs_mount_custom_image "${path}" + aufs_append_root_image "${path}" + else + aufs_mount_root_image "${path}" + fi fi download_to_cache "${path}" "${packages_xorg}" copy_cache_xorg @@ -273,8 +306,13 @@ make_image_xorg() { rm -r ${path}/var make_repo "${path}/opt/livecd/pkgs/gfx-pkgs" "${path}/opt/livecd/pkgs" configure_xorg_drivers "${path}" - umount_image_handler - aufs_clean "${path}" + if [[ ${use_overlayfs} == "true" ]];then + umount "${path}" + rm -rf "${work_dir}/work" + else + umount_image_handler + aufs_clean "${path}" + fi : > ${work_dir}/build.${FUNCNAME} msg "Done [pkgs-image]" fi @@ -285,12 +323,21 @@ make_image_lng() { msg "Prepare [lng-image]" local path="${work_dir}/lng-image" mkdir -p ${path}/opt/livecd/lng - umount_image_handler - if [[ -n "${custom}" ]] ; then - aufs_mount_custom_image "${path}" - aufs_append_root_image "${path}" + if [[ ${use_overlayfs} == "true" ]];then + mkdir -p "${work_dir}/work" + if [[ -n "${custom}" ]] ; then + mount -t overlay overlay -olowerdir="${work_dir}/${custom}-image":"${work_dir}/root-image",upperdir="${path}",workdir="${work_dir}/work" "${path}" + else + mount -t overlay overlay -olowerdir="${work_dir}/root-image",upperdir="${path}",workdir="${work_dir}/work" "${path}" + fi else - aufs_mount_root_image "${path}" + umount_image_handler + if [[ -n "${custom}" ]] ; then + aufs_mount_custom_image "${path}" + aufs_append_root_image "${path}" + else + aufs_mount_root_image "${path}" + fi fi if [[ -n ${packages_lng_kde} ]]; then download_to_cache "${path}" "${packages_lng} ${packages_lng_kde}" @@ -307,8 +354,13 @@ make_image_lng() { cp ${PKGDATADIR}/pacman-lng.conf ${path}/opt/livecd rm -r ${path}/var make_repo ${path}/opt/livecd/lng/lng-pkgs ${path}/opt/livecd/lng - umount_image_handler - aufs_clean "${path}" + if [[ ${use_overlayfs} == "true" ]];then + umount "${path}" + rm -rf "${work_dir}/work" + else + umount_image_handler + aufs_clean "${path}" + fi : > ${work_dir}/build.${FUNCNAME} msg "Done [lng-image]" fi @@ -323,18 +375,32 @@ make_image_boot() { cp ${work_dir}/root-image/boot/vmlinuz* ${path_iso}/${arch}/${iso_name} local path="${work_dir}/boot-image" mkdir -p ${path} - umount_image_handler - if [[ -n "${custom}" ]] ; then - aufs_mount_custom_image "${path}" - aufs_append_root_image "${path}" + if [[ ${use_overlayfs} == "true" ]];then + mkdir -p "${work_dir}/work" + if [[ -n "${custom}" ]] ; then + mount -t overlay overlay -olowerdir="${work_dir}/${custom}-image":"${work_dir}/root-image",upperdir="${path}",workdir="${work_dir}/work" "${path}" + else + mount -t overlay overlay -olowerdir="${work_dir}/root-image",upperdir="${path}",workdir="${work_dir}/work" "${path}" + fi else - aufs_mount_root_image "${path}" + umount_image_handler + if [[ -n "${custom}" ]] ; then + aufs_mount_custom_image "${path}" + aufs_append_root_image "${path}" + else + aufs_mount_root_image "${path}" + fi fi copy_initcpio "${path}" || die "Failed to copy initcpio." gen_boot_image "${path}" mv ${path}/boot/${iso_name}.img ${path_iso}/${arch}/${iso_name}.img [[ -f ${path}/boot/intel-ucode.img ]] && copy_ucode "${path}" "${path_iso}" - umount_image_handler + if [[ ${use_overlayfs} == "true" ]];then + umount "${path}" + rm -rf "${work_dir}/work" + else + umount_image_handler + fi rm -R ${path} : > ${work_dir}/build.${FUNCNAME} msg "Done [${iso_name}/boot]" diff --git a/lib/util.sh b/lib/util.sh index 003d9ff..f10d6c7 100644 --- a/lib/util.sh +++ b/lib/util.sh @@ -404,6 +404,8 @@ init_buildiso(){ [[ -z ${iso_compression} ]] && iso_compression='xz' [[ -z ${iso_checksum} ]] && iso_checksum='md5' + + [[ -z ${use_overlayfs} ]] && use_overlayfs='false' } load_config(){