#!/bin/sh
# Sisyphus-install-virt
# v0.10
# 2025-08-25

. /etc/ntools/sisyphus-install-virt.conf

function usage() {
	echo "${0##*/} [Keys] <config-file-name>"
	echo
	echo "Use <config-file-name> from '$cfgpath' folder"
	echo "Keys are:"
	echo "	-v | --verbose ) Verbose"
	echo
	echo "Available config files are:"
	ls "$cfgpath"
	exit 1
}

function get_type {
	local RES=$(cat "$cfgfile" | yq -r ".parts[] | select(.name == \"${PART}\").type")
	if [[ -z "$RES" ]] || [[ "$RES" == null ]]; then
		RES="$DEFTYPE"
	fi
	echo "$RES"
	return 0
}

function get_partfs {
	local RES=$(cat "$cfgfile" | yq -r ".parts[] | select(.name == \"${PART}\").fs")
	if [[ -z "$RES" ]] || [[ "$RES" == null ]]; then
		[[ "$PART" == 'swap' ]]	&& RES='swap' || RES="$DEFPARTFS"
	fi
	echo "$RES"
	return 0
}

function get_partsize() {
	local RES=$(cat "$cfgfile" | yq -r ".parts[] | select(.name == \"${PART}\").size")
	if [[ -z "$RES" ]] || [[ "$RES" == null ]]; then
		RES="$DEFPARTSIZE"
	fi
	echo "${RES^^}" | numfmt --from=iec
	return 0
}

function get_partmp() {
	local RES=$(cat "$cfgfile" | yq -r ".parts[] | select(.name == \"${PART}\").mp")
	if [[ -z "$RES" ]] || [[ "$RES" == null ]]; then
		[[ "$PART" == 'swap' ]] && RES='swap' || return 1
	fi
	echo "$RES"
	return 0
}

function get_pv() {
	local RES=$(cat "$cfgfile" | yq -r ".parts[] | select(.name == \"${PART}\").pv")
	if [[ "$RES" == null ]]; then
		RES=''
	fi
	echo "$RES"
	return 0
}

function get_pool() {
	local RES=$(cat "$cfgfile" | yq -r ".parts[] | select(.name == \"${PART}\").pool")
	if [[ "$RES" == null ]]; then
		RES="$DEFPOOL"
	fi
	echo "$RES"
	return 0
}

function get_mac() {
	local RES=$(cat "$cfgfile" | yq -r ".nics[] | select(.name == \"${NIC}\").mac")
	if [[ "$RES" == null ]]; then
		RES=''
	fi
	echo "$RES"
	return 0
}

function get_ip() {
	local RES=$(cat "$cfgfile" | yq -r ".nics[] | select(.name == \"${NIC}\").ip")
	if [[ "$RES" == null ]]; then
		RES=''
	fi
	echo "$RES"
	return 0
}

function get_gw() {
	local RES=$(cat "$cfgfile" | yq -r ".nics[] | select(.name == \"${NIC}\").gw")
	if [[ "$RES" == null ]]; then
		RES=''
	fi
	echo "$RES"
	return 0
}

function create_part() {
	case "$TYPE" in
		lvm ) 
			PV=$(get_pv)
			VGNAME=$(cat "$cfgfile" | yq -r ".parts[] | select(.name == \"${PART}\").vg")
			if [[ -z "$VGNAME" ]] || [[ "$VGNAME" == null ]]; then
				VGNAME="$DEFVGNAME"
			fi
			vgdisplay "${VGNAME}" &> /dev/null
			if [ "$?" -ne "0" ]; then
				echo "Error! VG ${VGNAME} not exists."
				EXIT_WITH_ERRORS=1
				return
			fi
			local LESIZE=$(vgdisplay ${VGNAME} --units B | awk '/PE Size/ {print $3}')

			DEV="/dev/${VGNAME}/${VMLNAME}-${PART}"
			local RES
			RES=$(lvdisplay "${DEV}" 2>/dev/null)
			if [ "$?" -eq "0" ]; then
				if ! [ "$ALL" ] ; then
					echo "Warning! LV ${VGNAME}/${VMLNAME}-${PART} already exists."
					echo
					read -p "Do you wish to continue? ('Y', 'n' or 'a' for all) " -r
					echo
					if [[ $REPLY =~ ^[Nn]$ ]]; then
						EXIT_WITH_ERRORS=1
						return
					fi
					if [[ $REPLY =~ ^[Aa]$ ]]; then
						ALL=true
					fi
				fi
				CLE=$(echo "$RES" | awk '/Current LE/ {print $3}')
				if [[ $(( $CLE*$LESIZE )) -ne "$PARTSIZE" ]]; then
					if ! [ "$ALL" ] ; then
						echo "Partition $PART size not equal ${PARTSIZE}. Resize needed."
						echo
						read -p "Do you wish to continue? ('y', 'n' or 'a' for all) " -r
						echo
						if [[ $REPLY =~ ^[Nn]$ ]]; then
							EXIT_WITH_ERRORS=1
							return
						fi
						if [[ $REPLY =~ ^[Aa]$ ]]; then
							ALL=true
						fi
					fi
				fi
				lvresize "${DEV}" -L "${PARTSIZE}B" -y &> /dev/null
			else
				lvcreate -n "${VMLNAME}-${PART}" -L "${PARTSIZE}B" "${VGNAME}" ${PV} &> /dev/null
				ERR=$?
				if [ "$ERR" -ne "0" ]; then
				if ! [ "$ALL" ] ; then
						echo "Error to create lv $VMLNAME-${PART}:";
						echo
						read -p "Do you wish to continue? ('y', 'n' or 'a' for all) " -r
						echo
						if [[ $REPLY =~ ^[Nn]$ ]]; then
							EXIT_WITH_ERRORS=1
							return
						fi
						if [[ $REPLY =~ ^[Aa]$ ]]; then
							ALL=true
						fi
					fi
				fi
			fi
			if [ -n "$TPART" ]; then
				LETTER=$(echo "$LETTER" | tr "a-z" "b-z_")
				TPART="${TPART}
	 '/dev/$VGNAME/$VMLNAME-${PART},raw,xvd$LETTER,rw',"
			else
				LETTER='a'
				TPART="disk = [ '/dev/$VGNAME/$VMLNAME-${PART},raw,xvd$LETTER,rw',"
			fi
		;;
		rbd ) 
			POOL=$(get_pool)
			local RBD="${POOL}/$VMLNAME-${PART}"
			RES=$(rbd --format json info "${RBD}" 2>/dev/null)
			if [ "$?" -eq "0" ]; then
				if ! [ "$ALL" ] ; then
					echo "Warning! RBD ${RBD} already exists."
					echo
					read -p "Do you wish to continue? ('Y', 'n' or 'a' for all) " -r
					echo
					if [[ $REPLY =~ ^[Nn]$ ]]; then
						EXIT_WITH_ERRORS=1
						return
					fi
					if [[ $REPLY =~ ^[Aa]$ ]]; then
						ALL=true
					fi
				fi
				SIZE=$(echo "$RES" | jq -r ".size")
				if [[ "$SIZE" -ne "$PARTSIZE" ]]; then
					if ! [ "$ALL" ] ; then
						echo "Partition $PART size not equal ${PARTSIZE}. Resize needed."
						echo
						read -p "Do you wish to continue? ('y', 'n' or 'a' for all) " -r
						echo
						if [[ $REPLY =~ ^[Nn]$ ]]; then
							EXIT_WITH_ERRORS=1
							return
						fi
						if [[ $REPLY =~ ^[Aa]$ ]]; then
							ALL=true
						fi
					fi
				fi
				rbd resize "${RBD}" --size "${PARTSIZE}B" &> /dev/null
			else
				rbd create "${RBD}" --size "${PARTSIZE}B" &> /dev/null
			fi
			DEV=$(rbd device map "${RBD}" 2> /dev/null)
			if [ -n "$TPART" ]; then
				LETTER=$(echo "$LETTER" | tr "a-z" "b-z_")
				TPART="${TPART}
	 'script=block-rbd,vdev=xvd${LETTER},target=${POOL}:${VMLNAME}-${PART}',"
			else
				LETTER='a'
				TPART="disk = [ 'script=block-rbd,vdev=xvd${LETTER},target=${POOL}:${VMLNAME}-${PART}',"
			fi
		;;
	esac
}

function free_part() {
	sync
	case "${PARTFS}" in
		'xfs'|'ext4' )
			umount "$BASEDIR${PARTMP}"
		;;
	esac
	case "$TYPE" in
		lvm ) 
		;;
		rbd ) 
			POOL=$(get_pool)
			local RBD="${POOL}/$VMLNAME-${PART}"
			rbd device unmap "${RBD}" 2> /dev/null
		;;
	esac
}

OSHORT='hv'
OLONG='verbose,help'
OPTS=`getopt -o $OSHORT: --long $OLONG: -q -- "$@"`
eval set -- "$OPTS"

HELP=false

while true; do
	case "$1" in
		-v | --verbose ) VERBOSE=true; shift ;;
    -h | --help )    HELP=true; shift ;;
    -- ) shift; break ;;
    * ) break ;;
  esac
done

if $HELP; then
	usage
fi

cfg=$1

if [[ -z "$cfg" ]]; then
	usage
fi

valid='0-9a-zA-Z_-'
if [[ "$cfg" =~ [^$valid] ]]; then
	echo "Invalid config name."
	exit 1
fi
cfgfile="$cfgpath/$cfg"
if [[ ! -f "$cfgfile" ]]; then
	echo "Config file not found."
	exit 1
fi

if [[ -z "$VMNAME" ]]; then
	VMNAME=$(cat "$cfgfile" | yq -r '.vmname | values')
	if [[ -z "$VMNAME" ]]; then
		VMNAME="$cfg"
	fi
fi

if [[ -z "$VERBOSE" ]]; then
	VERBOSE=$(cat "$cfgfile" | yq '.verbose | values')
	if [ "${VERBOSE}" == true ]; then
		set -x
	fi
else
	set -x
fi

if [[ -z "$MAINPACKAGES" ]]; then
	MAINPACKAGES=$(cat "$cfgfile" | yq -r '.mainpackages')
	if [[ -z "$MAINPACKAGES" ]] || [[ "$MAINPACKAGES" == null ]]; then
		MAINPACKAGES="$DEFMAINPACKAGES"
	fi
fi

if [[ -z "$PACKAGES" ]]; then
	PACKAGES=$(cat "$cfgfile" | yq -r '.packages')
	if [[ -z "$PACKAGES" ]] || [[ "$PACKAGES" == null ]]; then
		if [[ -z "$EXTRAPACKAGES" ]]; then
			EXTRAPACKAGES=$(cat "$cfgfile" | yq -r '.extrapackages')
		fi
		if [[ -z "$EXTRAPACKAGES" ]] || [[ "$EXTRAPACKAGES" == null ]]; then
			PACKAGES="$MAINPACKAGES"
		else
			PACKAGES="$MAINPACKAGES $EXTRAPACKAGES"
		fi
	fi
fi


VMLNAME=`echo "$VMNAME" | tr '[:upper:]' '[:lower:]'`

if [[ -z "$XENROOT" ]]; then
	XENROOT=$(cat "$cfgfile" | yq -r '.xenroot')
	if [[ -z "$XENROOT" ]] || [[ "$XENROOT" == null ]]; then
		XENROOT="$DEFXENROOT"
	fi
fi

if [[ -z "$XENBOOT" ]]; then
	XENBOOT=$(cat "$cfgfile" | yq -r '.xenboot')
	if [[ -z "$XENBOOT" ]] || [[ "$XENBOOT" == null ]]; then
		XENBOOT="$DEFXENBOOT"
	fi
fi

if [[ -z "$CFGPREFIX" ]]; then
	CFGPREFIX=$(cat "$cfgfile" | yq -r '.cfgprefix')
	if [[ -z "$CFGPREFIX" ]] || [[ "$CFGPREFIX" == null ]]; then
		CFGPREFIX="$DEFCFGPREFIX"
	fi
fi

XENCFG="${XENROOT}/${CFGPREFIX}-${VMLNAME}"

if [[ -z "$BASEDIR" ]]; then
	BASEDIR=$(cat "$cfgfile" | yq -r '.basedir')
	if [[ -z "$BASEDIR" ]] || [[ "$BASEDIR" == null ]]; then
		BASEDIR="$DEFBASEDIR"
	fi
fi

if [[ -z "$HOST" ]]; then
	HOST=$(cat "$cfgfile" | yq -r '.hostname')
	if [[ -z "$HOST" ]] || [[ "$HOST" == null ]]; then
		HOST="$VMLNAME"
	fi
fi

if [[ -z "$DOMAINNAME" ]]; then
	DOMAINNAME=$(cat "$cfgfile" | yq -r '.domain')
	if [[ -z "$DOMAINNAME" ]] || [[ "$DOMAINNAME" == null ]]; then
		DOMAINNAME=$(hostname -d)
	fi
fi

if [ "$(xl uptime $VMNAME 2>/dev/null | grep $VMNAME)" != '' ]; then
	echo "Error: Virtual machine '$VMNAME' have already started!!!"
	exit 1
fi

if [ "$(mount| grep $BASEDIR)" != '' ]; then
	echo "Warning: Folder '$BASEDIR' have already mounted!!!"
	exit 1
fi

PARTS=$(cat "$cfgfile" | yq -r '.parts[].name')
RPARTS=$(echo "$PARTS" | tac )

for PART in $PARTS; do
	TYPE=$(get_type) 
	PARTSIZE=$(get_partsize) 
	DEV=''
	create_part
	[ -z $EXIT_WITH_ERRORS ] || break
	PARTFS=$(get_partfs) 
	PARTMP=$(get_partmp) 
	if [ "$?" -ne "0" ]; then
		echo "Error! Mount point not set!"
		EXIT_WITH_ERRORS=1
		break
	fi
	RES=$(wipefs "$DEV")
	if [ -n "$RES" ]; then
		if ! [ "$ALL" ] ; then
			echo "$DEV has a filesystem. FS will be wiped!";
			echo
			read -p "Do you wish to continue? ('y', 'n' or 'a' for all) " -r
			echo
			if [[ $REPLY =~ ^[Nn]$ ]]; then
				EXIT_WITH_ERRORS=1
				break
			fi
			if [[ $REPLY =~ ^[Aa]$ ]]; then
				ALL=true
			fi
		fi
		wipefs -a "$DEV" &> /dev/null
	fi
	case "${PARTFS}" in
		'xfs'|'ext4' )
			mkfs.${PARTFS} $DEV &> /dev/null || { EXIT_WITH_ERRORS=1; break; }
			mkdir -p $BASEDIR${PARTMP} || { EXIT_WITH_ERRORS=1; break; }
			mount $DEV $BASEDIR${PARTMP} || { EXIT_WITH_ERRORS=1; break; }
			if [ ${PARTFS} == 'ext4' ]; then
				FSOPT="1"
			else
				FSOPT="0"
			fi
			if [ ${PARTMP} == '/' ]; then
				FSOPT="$FSOPT 1"
			else
				FSOPT="$FSOPT 2"
			fi
		;;
		'swap' )
			mkswap "$DEV" &> /dev/null || exit 1
			FSOPT="0 0"
			PARTMP='swap'
		;;
		* )
			echo "Unknown FSTYPE ${PARTFS[$i]}."
			EXIT_WITH_ERRORS=1
			break
		;;
	esac

	UUID=$(blkid -o value "${DEV}" | head -1)
	FSLIST="${FSLIST}UUID=\"$UUID\"\t${PARTMP}\t${PARTFS}\tdefaults\t$FSOPT\n"
done

if [ -z $EXIT_WITH_ERRORS ]; then
	cd "$BASEDIR" || { EXIT_WITH_ERRORS=1; echo "Can't change directory to ${BASEDIR}."; } > /dev/null
fi
if [ -z $EXIT_WITH_ERRORS ]; then
	mkdir -p "$BASEDIR/dev"
	mkdir -p "$BASEDIR/var/lib/rpm"
	mkdir -p "$BASEDIR/var/cache/apt/archives/partial"
	mkdir -p "$BASEDIR/var/lib/apt/lists/partial"

	mknod "$BASEDIR/dev/null" c 1 3
	mknod "$BASEDIR/dev/urandom" c 1 9

	rpm --root "$BASEDIR" --initdb
	apt-get \
		-o APT::Architecture="x86_64" \
		-o RPM::RootDir="$BASEDIR" \
		-o Dir::State="$BASEDIR/var/lib/apt/" \
		-o Dir::Cache="$BASEDIR/var/cache/apt/" \
			update
	apt-get \
		-o APT::Architecture="x86_64" \
		-o RPM::RootDir="$BASEDIR" \
		-o Dir::State="$BASEDIR/var/lib/apt/" \
		-o Dir::Cache="$BASEDIR/var/cache/apt/" \
		-y install $PACKAGES || EXIT_WITH_ERRORS=1;
fi

if [ -z $EXIT_WITH_ERRORS ]; then
	cp "/etc/resolv.conf" "$BASEDIR/etc/"
	cp "/root/.vimrc" "$BASEDIR/root/"
	cp -r "/root/.vim" "$BASEDIR/root/"
	cp "/root/.ssh/authorized_keys" "$BASEDIR/root/.ssh/"
#	/bin/cp -r "/root/bin" "$BASEDIR/root/bin"

	echo "ALT Linux Sisyphus" > "$BASEDIR/etc/altlinux-release"

	cat > "$BASEDIR/etc/issue" <<EOL
Welcome to \\R
Kernel \\r on \\m / \\l
EOL

	echo "net.ipv6.conf.all.disable_ipv6=1" >> "$BASEDIR/etc/net/sysctl.conf"

	sed -i'' 's/^#\(rpm \[alt\] http:.*Sisyphus\/x86_64\s\)/\1/g' "$BASEDIR/etc/apt/sources.list.d/alt.list"
	sed -i'' 's/^#\(rpm \[alt\] http:.*Sisyphus\/noarch\s\)/\1/g' "$BASEDIR/etc/apt/sources.list.d/alt.list"

	sed -i'' "s/alias l=.*/alias l='ls -lp --group-directories-first'/" "$BASEDIR/etc/bashrc.d/alias.sh"
	sed -i'' "s/alias ll=.*/alias ll='ls -lap --group-directories-first'/" "$BASEDIR/etc/bashrc.d/alias.sh"

	sed -i'' "s/^HOSTNAME=\(.*\)/HOSTNAME=$HOST.$DOMAINNAME/" "$BASEDIR/etc/sysconfig/network"
	sed -i'' "s/^DOMAINNAME=\(.*\)/DOMAINNAME=$DOMAINNAME/" "$BASEDIR/etc/sysconfig/network"

	sed -i'' "1i $FSLIST" "$BASEDIR/etc/fstab"

	mount --bind /dev "$BASEDIR/dev"
	mount --bind /dev/pts "$BASEDIR/dev/pts"
	mount --bind /dev/shm "$BASEDIR/dev/shm"
	mount --bind /proc "$BASEDIR/proc"
	mount --bind /sys "$BASEDIR/sys"
	mount --bind /tmp "$BASEDIR/tmp"

	if [[ -z "$KERNELFROM" ]]; then
		KERNELFROM=$(cat "$cfgfile" | yq -r '.kernel | values')
	fi
	if [[ -n "$KERNELFROM" ]]; then
		chroot "$BASEDIR" /bin/bash -c "wget -nv -O /tmp/newkernel.rpm \"$KERNELFROM\""
		chroot "$BASEDIR" /bin/bash -c "rpm -i --force --noscripts /tmp/newkernel.rpm"
		KERNELVERSION=$(chroot "$BASEDIR" /bin/bash -c "rpm -qlp /tmp/newkernel.rpm | grep /boot/vmlinuz | sed \"s/^\/boot\/vmlinuz-\(.*\)/\1/\"")
		chroot "$BASEDIR" /bin/bash -c "rm -f /tmp/newkernel.rpm"
	else
		KERNELVERSION=$(chroot "$BASEDIR" /bin/bash -c "ls -1 /boot/ | grep vmlinuz | sed \"s/^vmlinuz-\(.*\)/\1/\"")
	fi
	chroot "$BASEDIR" alternatives-update
	chroot "$BASEDIR" usermod -p '' root
	chroot "$BASEDIR" systemctl enable network sshd
	chroot "$BASEDIR" /etc/rc.d/init.d/clock tzset

	chroot "$BASEDIR" /bin/bash -c "/sbin/depmod \"$KERNELVERSION\"; /usr/sbin/make-initrd -k \"$KERNELVERSION\""

	if [ -e "$BASEDIR/etc/sysconfig/grub2" ]; then
		sed -i "s/^GRUB_AUTOUPDATE_CFG=.*/GRUB_AUTOUPDATE_CFG=false/" "$BASEDIR/etc/sysconfig/grub2"
		sed -i "s/^GRUB_VMLINUZ_SYMLINKS=.*/GRUB_VMLINUZ_SYMLINKS=false/" "$BASEDIR/etc/sysconfig/grub2"
		sed -i "s/^GRUB_VMLINUZ_FAILSAFE=.*/GRUB_VMLINUZ_FAILSAFE=false/" "$BASEDIR/etc/sysconfig/grub2"
		sed -i "s/^GRUB_TERMINAL_OUTPUT=.*/GRUB_TERMINAL_OUTPUT='console'/" "$BASEDIR/etc/sysconfig/grub2"
		sed -i "s/^GRUB_SAVEDEFAULT=.*/GRUB_SAVEDEFAULT=false/" "$BASEDIR/etc/sysconfig/grub2"

		chroot "$BASEDIR" /usr/sbin/update-grub
	fi

	chroot "$BASEDIR" /usr/bin/etckeeper init

	killall minilogd
	sync
	umount "$BASEDIR/tmp"
	umount "$BASEDIR/sys"
	umount "$BASEDIR/proc"
	umount "$BASEDIR/dev/shm"
	umount "$BASEDIR/dev/pts"
	umount "$BASEDIR/dev"

	NICS=$(cat "$cfgfile" | yq -r '.nics[].name')
	for NIC in $NICS; do
		NETMAC=$(get_mac) 
		if [ -z "${NETMAC}" ]; then
			NETMAC=$(cat /dev/urandom | tr -dc 'a-f0-9' | fold -w 6 | head -n 1|sed 's/^\(..\)\(..\)\(..\).*$/00:16:3e:\1:\2:\3/')
		fi
	if [ -n "$TNET" ]; then
		TNET="${TNET}
	 'mac=${NETMAC}, bridge=xbr-${NIC}',"
	else
		TNET="vif =  [ 'mac=${NETMAC}, bridge=xbr-${NIC}',"
	fi
	echo "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"${NETMAC}\", KERNEL==\"eth*\", NAME=\"eth-${NIC}\"" >> "$BASEDIR/etc/udev/rules.d/79-nic-name.rules"

	mkdir "$BASEDIR/etc/net/ifaces/eth-${NIC}"
	cat > "$BASEDIR/etc/net/ifaces/eth-${NIC}/options" <<- EOF
# This file doesn't contain comments any more, refer to etcnet-options(5)
# manpage for detailed options description.

ONBOOT=yes
DISABLED=no
TYPE=eth
BOOTPROTO=static
MODULE=xen-netfront
USE_IFPLUGD=no
EOF
	NETIP=$(get_ip) 
	if [ -n "${NETIP}" ]; then
		echo "${NETIP}" > "$BASEDIR/etc/net/ifaces/eth-${NIC}/ipv4address"
	fi
	NETROUTE=$(get_gw) 
	if [ -n "${NETROUTE}" ]; then
		echo "${NETROUTE}" > "$BASEDIR/etc/net/ifaces/eth-${NIC}/ipv4route"
	fi
done
	if [[ -z "$VCPUS" ]]; then
		VCPUS=$(cat "$cfgfile" | yq -r '.vcpus')
		if [[ -z "$VCPUS" ]] || [[ "$VCPUS" == null ]]; then
			VCPUS="$DEFVCPUS"
		fi
	fi

	if [[ -z "$MEMORY" ]]; then
		MEMORY=$(cat "$cfgfile" | yq -r '.memory')
		if [[ -z "$MEMORY" ]] || [[ "$MEMORY" == null ]]; then
			MEMORY="$DEFMEMORY"
		fi
	fi

	NOW=$(date +"%F %T")
	cat > "$XENCFG" <<- EOF
# $VMNAME configuration file"
# $NOW

# Guest name
name = "$VMNAME"

# Kernel image to boot
kernel = "$XENROOT/boot/$VMLNAME/vmlinuz-$KERNELVERSION"

# Ramdisk (optional)
ramdisk = "$XENROOT/boot/$VMLNAME/initrd-$KERNELVERSION.img"

# Kernel command line options
extra = "root=/dev/xvda audit=0"

# Initial memory allocation (MB)
memory = $MEMORY

# Number of VCPUS
vcpus = $VCPUS

# Network devices
${TNET::-1} ]

# Disk Devices
${TPART::-1} ]

# Guest VGA console configuration, either SDL or VNC
sdl = 0

#----------------------------------------------------------------------------

vnc=0
EOF

	mkdir -p "$XENBOOT/$VMLNAME"
	cp "$BASEDIR/boot/vmlinuz-$KERNELVERSION" "$XENBOOT/$VMLNAME/"
	cp "$BASEDIR/boot/initrd-$KERNELVERSION.img" "$XENBOOT/$VMLNAME/"
fi

cd /

for PART in $RPARTS; do
	TYPE=$(get_type) 
	PARTFS=$(get_partfs) 
	PARTMP=$(get_partmp) 
	free_part
done

sync
