#!/bin/sh

# Copyright (c) 2010, The ULBSD Project
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * 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 COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR CONTRIBUTORS 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.
#
# $Id: ulbsd-clone-ufs 59 2021-03-10 09:27:55Z sv $

if [ $# -ne 4 ]
then
    echo "Wrong number of parameters!" && exit
fi

work_disk=${1}
swap_size=${2}
mnt_dir=${3}
label=${4}

zpool list -H -o name | while read pool; do
    zpool export -f $pool
done
cd /tmp && rm -Rf *

gpart destroy -F ${work_disk}

diskinfo ${work_disk} | while read disk sectorsize size sectors other
do
# Delete MBR and partition table
    dd if=/dev/zero of=/dev/${disk} bs=${sectorsize} count=65536
# Delete GEOM metadata
    dd if=/dev/zero of=/dev/${disk} bs=${sectorsize} oseek=`expr $sectors - 65536`
done

gpart create -s GPT ${work_disk}
gpart add -b 34 -t freebsd-boot -a 4k -i 1 -l boot -s 512k ${work_disk}
gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 ${work_disk}
gpart add -t efi -a 4k -i 2 -l efi -s 1M ${work_disk}

if [ "${swap_size}" -gt "0" ]
then
    gpart add -t freebsd-swap -a 4k -i 4 -l swap -s ${swap_size}M ${work_disk}
fi

gpart add -t freebsd-ufs -a 4k -i 3 -l ${label} ${work_disk}
gpart show ${work_disk}

newfs_msdos /dev/${work_disk}p2
mount -t msdosfs /dev/${work_disk}p2 ${mnt_dir}
mkdir -p ${mnt_dir}/EFI/BOOT
cp /boot/loader.efi ${mnt_dir}/EFI/BOOT/BOOTX64.efi
echo 'BOOTX64.efi' >> ${mnt_dir}/EFI/BOOT/startup.nsh
sync
umount -f ${mnt_dir}

newfs -U /dev/${work_disk}p3
tunefs -j disable /dev/${work_disk}p3
mkdir -p ${mnt_dir}
mount /dev/${work_disk}p3 ${mnt_dir}

cd ${mnt_dir}
dump -0Lf - / | restore -rf -
cd /

if [ -f ${mnt_dir}/.sujournal ]
then
    chflags noschg,nosunlnk ${mnt_dir}/.sujournal
    rm -f ${mnt_dir}/.sujournal
fi

if [ -f ${mnt_dir}/restoresymtable ]
then
    rm -f ${mnt_dir}/restoresymtable
fi

if [ -d ${mnt_dir}/.snap ]
then
    rm -Rf ${mnt_dir}/.snap
fi

if [ -f ${mnt_dir}/etc/fstab ]
then
    cp ${mnt_dir}/etc/fstab ${mnt_dir}/etc/fstab.bak
    sed -i '' -e '/\/dev\/gpt\//Id' ${mnt_dir}/etc/fstab
    echo "/dev/gpt/${label}	/	ufs	rw	1	1" >> ${mnt_dir}/etc/fstab
    if [ "${swap_size}" -gt "0" ]
    then
        echo '/dev/gpt/swap	none	swap	sw	0	0' >> ${mnt_dir}/etc/fstab
    fi
fi

sync
umount -f ${mnt_dir}

echo 'All done!'
