#!/bin/sh

# dm-mod.ko and dmsetup must be contained in the initramfs
mount_by_dmsetup() {
    vtoydump -L > /tmp/iso.table

    # use dmsetup to create /dev/dm-X
    dmsetup create ventoy /tmp/iso.table --readonly

    # mount the iso
    mount /dev/mapper/ventoy    /mnt
}


# Exfat fs is natively supported from linux kernel 5.4
mount_directly() {
    DISK=$(vtoydump | grep name | awk -F': ' '{print $2}')
    PART=$(vtoydump | grep part | awk -F': ' '{print $2}')
    PATH=$(vtoydump | grep path | awk -F': ' '{print $2}')
    
    mount ${DISK}${PART}  /mntdisk    
    mount /mntdisk${PATH} /mnt
}



############################
# Step 1: check
############################

#If vtoydump return non-zero, it means current system is not booted by Ventoy, just exit.
if ! vtoydump > /dev/null; then
    exit 0
fi

#######################################
# Step 2: mount the iso file
#######################################

# If current system support to mount exfat fs directly, you can call mount_directly
# or you can call mount_by_dmsetup to mount the iso file.


#
# ... Now you can use /mnt as the install medium ....
#
