#!/bin/bash
#================
# FILE          : linuxrc
#----------------
# PROJECT       : OpenSuSE KIWI Image System
# COPYRIGHT     : (c) 2006 SUSE LINUX Products GmbH. All rights reserved
#               :
# AUTHOR        : Marcus Schaefer <ms@suse.de>
#               :
# BELONGS TO    : Operating System images
#               :
# DESCRIPTION   : OEM repartition code functions. This file is used
#               : to setup the partition table according to the OEM
#               : specifications
#               :
# STATUS        : BETA
#----------------
#======================================
# OEMRepartInit 
#--------------------------------------
function OEMRepartInit {
	# /.../
	# calculate memory based swapsize amount and initialize
	# size of recovery archive
	# ----
	mem_size=`grep MemTotal: /proc/meminfo | tr -dc '[0-9]'`
	swapsize=$(( $mem_size *2 / 1024 ))
	recoMByte=0
	if [ ! -z "$OEM_SWAPSIZE" ];then
		swapsize=$OEM_SWAPSIZE
	fi
	if [ ! -z "$OEM_WITHOUTSWAP" ];then
		swapsize=0
	fi
	if [ ! -z "$OEM_RECOVERY" ];then
		mkdir -p /reco-root
		if ! mount $imageRootDevice /reco-root >/dev/null;then
			systemException "Failed to mount root device" "reboot"
		fi
		if [ ! -f /reco-root/recovery.partition.size ];then
			systemException "Can't find recovery part size info" "reboot"
		fi
		recoMByte=$(cat /reco-root/recovery.partition.size)
		recoID=83
		if [ ! -z "$OEM_RECOVERY_ID" ];then
			recoID=$OEM_RECOVERY_ID
		fi
		umount /reco-root && rmdir /reco-root
	fi
	export input=/part.input
	export bootid=1
	rm -f $input
}

#======================================
# OEMRepartStandard
#--------------------------------------
function OEMRepartStandard {
	# /.../
	# repartition disk with read/write root filesystem
	# Initial partition table layout is:
	# =====================================
	# pX:   [ boot ]
	# pX+1: ( root )  +luks
	# -------------------------------------
	local newparts=0
	local partsize=0
	#======================================
	# check for boot partition
	#--------------------------------------
	createBootDeviceData
	#======================================
	# calculate partition sizes
	#--------------------------------------
	local diskFreeMBytes=$(($(getFreeDiskBytes $imageDiskDevice) / 1048576))
	local diskRootMBytes=$(($(partitionSize $imageRootDevice) / 1024))
	local diskRSysMBytes=$((
		diskRootMBytes + diskFreeMBytes - swapsize - recoMByte
	))
	if [ ! -z "$OEM_SYSTEMSIZE" ];then
		if [ $OEM_SYSTEMSIZE -le $diskRSysMBytes ];then
			diskRSysMBytes=$OEM_SYSTEMSIZE
		fi
	fi
	#======================================
	# check repart operation
	#--------------------------------------
	if [ $diskRSysMBytes -lt $diskRootMBytes ];then
		# /.../
		# Requested system partition size is smaller than
		# existing partition, will not re-partition
		# ----
		Echo "Current system partition is bigger than requested size"
		Echo "Disk won't be re-partitioned"
		disableOEMParameters
		disableRepart
	fi
	#======================================
	# write new partition table
	#--------------------------------------
	if [ -z "$DONT_PARTITION" ];then
		pID=1
		bootParam=1
		rootParam=1
		recoParam=no
		swapParam=no
		cowpParam=no
		#======================================
		# close open device mappings
		#--------------------------------------
		if [ "$haveLuks" = "yes" ];then
			luksClose
		fi
		#======================================
		# clean/backup boot partition
		#--------------------------------------
		if [ "$haveBootPartition" = "yes" ];then
			pID=$((kiwi_BootPart + 1))
			rootParam=$pID
		fi
		#======================================
		# count partitions to be created
		#--------------------------------------
		if [ -z "$OEM_WITHOUTSWAP" ];then
			newparts=$((newparts + 1))
		fi
		if [ ! -z "$OEM_RECOVERY" ];then
			newparts=$((newparts + 1))
		fi
		#======================================
		# repart root partition
		#--------------------------------------
		deactivateMDRaid
		partsize=+"$diskRSysMBytes"M
		if [ -z "$OEM_SYSTEMSIZE" ] && [ $newparts -eq 0 ];then
			partsize=.
		fi
		createPartitionerInput \
			d $pID n p $pID . $partsize
		#======================================
		# add swap partition
		#--------------------------------------
		if [ -z "$OEM_WITHOUTSWAP" ] && [ $pID -lt 4 ];then
			pID=$((pID + 1))
			partsize=+"$swapsize"M
			if [ -z "$OEM_SYSTEMSIZE" ] && [ $newparts -eq 1 ];then
				partsize=.
			fi
			createPartitionerInput \
				n p $pID . $partsize t $pID 82
			newparts=$((newparts - 1))
			swapParam=$pID
		else
			OEM_WITHOUTSWAP=1
		fi
		#======================================
		# add recovery partition
		#--------------------------------------
		if [ ! -z "$OEM_RECOVERY" ] && [ $pID -lt 4 ];then
			pID=$((pID + 1))
			partsize=+"$recoMByte"M
			createPartitionerInput \
				n p $pID . $partsize t $pID $recoID
			newparts=$((newparts - 1))
			recoParam=$pID
		else
			unset OEM_RECOVERY
		fi
		#======================================
		# setup new device names
		#--------------------------------------
		setupDeviceNames \
			$rootParam $swapParam $recoParam $bootParam $cowpParam
		callPartitioner $input
		activateMDRaid
		#======================================
		# extend mdraid array if required
		#--------------------------------------
		resizeMDRaid
	fi
}

#======================================
# OEMRepartSplit
#--------------------------------------
function OEMRepartSplit {
	# /.../
	# repartition disk for overlay systems with aufs, clicfs
	# and friends and also systems of image type split.
	# Initial partition table layout is:
	# =====================================
	# pX:   ( boot )
	# pX+1: ( RO compressed )
	# pX+2: ( RW write area ) +luks
	# -------------------------------------
	local newparts=0
	local partsize=0
	#====================================== 
	# no recovery support in union mode
	#--------------------------------------
	unset OEM_RECOVERY
	#======================================
	# check for boot partition
	#--------------------------------------
	createBootDeviceData
	#====================================== 
	# check for read-write partition
	#--------------------------------------
	if ! partitionSize $(ddn $imageDiskDevice 3) &>/dev/null;then
		Echo "No read-write partition in this split/overlay image"
		disableOEMParameters
		disableRepart
	fi
	if [ -z "$DONT_PARTITION" ];then
		#======================================
		# calculate partition sizes
		#--------------------------------------
		local diskFreeMBytes=$(($(getFreeDiskBytes $imageDiskDevice) / 1048576))
		local diskRootMBytes=$(($(partitionSize $imageRWDevice) / 1024))
		local diskRSysMBytes=$((
			diskRootMBytes + diskFreeMBytes - swapsize
		))
		if [ ! -z "$OEM_SYSTEMSIZE" ];then
			if [ $OEM_SYSTEMSIZE -le $diskRSysMBytes ];then
				diskRSysMBytes=$OEM_SYSTEMSIZE
			fi
		fi
		#======================================
		# check repart operation
		#--------------------------------------
		if [ $diskRSysMBytes -lt $diskRootMBytes ];then
			# /.../
			# Requested read/write partition size is smaller than
			# existing partition, will not re-partition
			# ----
			Echo "Current read/write partition is bigger than requested size"
			Echo "Disk won't be re-partitioned"
			disableOEMParameters
			disableRepart
		fi
	fi
	#======================================
	# write new partition table
	#--------------------------------------
	if [ -z "$DONT_PARTITION" ];then
		pID=$((kiwi_BootPart + 2))
		bootParam=$kiwi_BootPart
		rootParam=$((kiwi_BootPart + 1))
		cowpParam=$pID
		swapParam=no
		recoParam=no
		#======================================
		# close open device mappings
		#--------------------------------------
		if [ "$haveLuks" = "yes" ];then
			luksClose
		fi
		#======================================
		# count partitions to be created
		#--------------------------------------
		if [ -z "$OEM_WITHOUTSWAP" ];then
			newparts=$((newparts + 1))
		fi
		#======================================
		# repart cow partition
		#--------------------------------------
		partsize=+"$diskRSysMBytes"M
		if [ -z "$OEM_SYSTEMSIZE" ] && [ $newparts -eq 0 ];then
			partsize=.
		fi
		createPartitionerInput \
			d $pID n p $pID . $partsize
		#======================================
		# add swap partition
		#--------------------------------------
		if [ ! $swapsize = 0 ] && [ $pID -lt 4 ];then
			pID=$((pID + 1))
			createPartitionerInput \
				n p $pID . +"$swapsize"M t $pID 82
			swapParam=$pID
		else
			OEM_WITHOUTSWAP=1
		fi
		#======================================
		# setup new device names
		#--------------------------------------
		setupDeviceNames \
			$rootParam $swapParam $recoParam $bootParam $cowpParam
		callPartitioner $input
	fi
}

#======================================
# OEMRepartLVM
#--------------------------------------
function OEMRepartLVM {
	# /.../
	# repartition disk if LVM partition plus boot partition
	# is used. Initial partition table layout is:
	# =====================================
	# pX:   ( boot )
	# pX+1: ( LVM  )  +luks
	# -------------------------------------
	#======================================
	# check for boot partition
	#--------------------------------------
	createBootDeviceData
	#======================================
	# calculate partition sizes
	#--------------------------------------
	local extendID
	local VOverHeadMB=300
	local LVMdevice=$((kiwi_BootPart + 1))
	local LVMdevice=$(ddn $imageDiskDevice $LVMdevice)
	local diskFreeMBytes=$(($(getFreeDiskBytes $imageDiskDevice) / 1048576))
	local diskRootMBytes=$(($(partitionSize $LVMdevice) / 1024))
	local diskRSysMBytes=$((
		diskRootMBytes + diskFreeMBytes - swapsize - recoMByte
	))
	if [ ! -z "$OEM_SYSTEMSIZE" ];then
		if [ $OEM_SYSTEMSIZE -le $diskRSysMBytes ];then
			diskRSysMBytes=$OEM_SYSTEMSIZE
		fi
	fi
	#======================================
	# check repart operation
	#--------------------------------------
	if [ $diskRSysMBytes -lt $diskRootMBytes ];then
		# /.../
		# Requested LVM partition size is smaller than
		# existing partition, will not re-partition
		# ----
		Echo "Current LVM partition is bigger than requested size"
		Echo "Disk won't be re-partitioned"
		disableOEMParameters
		disableRepart
	fi
	#======================================
	# write new partition table
	#--------------------------------------
	if [ -z "$DONT_PARTITION" ];then
		pID=$((kiwi_BootPart + 1))
		bootParam=$kiwi_BootPart
		rootParam=$pID
		recoParam=no
		swapParam=no
		#======================================
		# setup all free volume name
		#--------------------------------------
		export allFreeVolume=LVRoot
		if [ ! -z "$kiwi_allFreeVolume" ];then
			allFreeVolume=$kiwi_allFreeVolume
		fi
		#======================================
		# close open device mappings
		#--------------------------------------
		if [ "$haveLuks" = "yes" ];then
			luksClose
		fi
		deactivateVolumeGroup
		deactivateMDRaid
		#======================================
		# resize LVM partition
		#--------------------------------------
		if [ $recoMByte -eq 0 ];then
			diskRSysMBytes=.
		fi
		createPartitionerInput \
			d $pID n p $pID . $diskRSysMBytes t $pID 8e
		extendID=$pID
		#======================================
		# add recovery partition
		#--------------------------------------
		if [ ! -z "$OEM_RECOVERY" ] && [ $pID -lt 4 ];then
			pID=$((pID + 1))
			createPartitionerInput \
				n p $pID . . t $pID $recoID
			recoParam=$pID
		else
			unset OEM_RECOVERY
		fi
		#======================================
		# setup new device names
		#--------------------------------------
		setupDeviceNames \
			1 $swapParam $recoParam $bootParam yes $VGROUP
		callPartitioner $input
		waitForStorageDevice $(ddn $imageDiskDevice $extendID)
		activateMDRaid
		deactivateVolumeGroup
		#======================================
		# extend mdraid array if required
		#--------------------------------------
		resizeMDRaid
		#======================================
		# extend volume group with rest space
		#--------------------------------------
		resizeLVMPVs $extendID
		#======================================
		# activate volume group $VGROUP
		#--------------------------------------
		activateVolumeGroup
		#======================================
		# Add LVSwap if requested.
		#--------------------------------------
		if [ -z "$OEM_WITHOUTSWAP" ];then
			lvcreate --size $swapsize"M" -n LVSwap $VGROUP
			swapParam=yes
		fi
		#======================================
		# Resize volumes to requested size
		#--------------------------------------
		resizeLVMVolumes
		#======================================
		# Extend root + optional allFreeVolume
		#--------------------------------------
		if [ -z "$OEM_SYSTEMSIZE" ];then
			lvextend -l +100%FREE /dev/$VGROUP/$allFreeVolume
		else
			lvextend --size $OEM_SYSTEMSIZE"M" /dev/$VGROUP/LVRoot
			if [ "$allFreeVolume" != "LVRoot" ];then
				lvextend -l +100%FREE /dev/$VGROUP/$allFreeVolume
			fi
		fi
		#======================================
		# setup new device names
		#--------------------------------------
		setupDeviceNames \
			$rootParam $swapParam $recoParam $bootParam yes $VGROUP
	fi
}

#======================================
# OEMPartitionInstall
#--------------------------------------
function OEMPartitionInstall {
	# /.../
	# don't repart the disk but prepare it for use with
	# the existing partition table
	# ----
	#======================================
	# no recovery support for part install
	#--------------------------------------
	unset OEM_RECOVERY
	#======================================
	# check for LVM, resize to full space
	#--------------------------------------
	if searchVolumeGroup;then
		#======================================
		# setup all free volume name
		#--------------------------------------
		export allFreeVolume=LVRoot
		if [ ! -z "$kiwi_allFreeVolume" ];then
			allFreeVolume=$kiwi_allFreeVolume
		fi
		#======================================
		# resize PV's
		#--------------------------------------
		pvresize $(ddn $imageDiskDevice $rID)
		#======================================
		# Resize volumes to requested size
		#--------------------------------------
		resizeLVMVolumes
		#======================================
		# Extend root + optional allFreeVolume
		#--------------------------------------
		if [ -z "$OEM_SYSTEMSIZE" ];then
			lvextend -l +100%FREE /dev/$VGROUP/$allFreeVolume
		else
			lvextend --size $OEM_SYSTEMSIZE"M" /dev/$VGROUP/LVRoot
			if [ "$allFreeVolume" != "LVRoot" ];then
				lvextend -l +100%FREE /dev/$VGROUP/$allFreeVolume
			fi
		fi
	fi
	#======================================
	# resize boot filesystem if needed
	#--------------------------------------
	if [ ! $imageBootDevice = $imageRootDevice ];then
		resizeFilesystem $imageBootDevice
	fi
	#======================================
	# cleanup temporary flags
	#--------------------------------------
	unset DONT_PARTITION
}

#======================================
# OEMRepart
#--------------------------------------
function OEMRepart {
	# /.../
	# call the appropriate repartition functions
	# ----
	if [ ! $LOCAL_BOOT = "no" ];then
		return
	fi
	#======================================
	# check for luks extension
	#--------------------------------------
	if [ "$haveLuks" = "yes" ];then
		unset  OEM_RECOVERY
	fi
	#======================================
	# Initialize
	#--------------------------------------
	OEMRepartInit
	#======================================
	# Do the repartitioning
	#--------------------------------------
	if [ ! -z "$OEM_PARTITION_INSTALL" ];then
		OEMPartitionInstall
	elif [ ! -z "$haveLVM" ];then
		OEMRepartLVM
	elif isFSTypeReadOnly;then
		OEMRepartSplit
	else
		OEMRepartStandard
	fi
	sleep 1
	#======================================
	# setup luks maps
	#--------------------------------------
	if [ "$haveLuks" = "yes" ];then
		createLuksMaps
	fi
	#======================================
	# find fstype for root partition
	#--------------------------------------
	probeFileSystem $imageRootDevice
	#======================================
	# Activate swap space
	#--------------------------------------
	if [ -z "$DONT_PARTITION" ] && [ $swapsize -gt 0 ]; then
		if partitionSize $imageSwapDevice &>/dev/null;then
			Echo "Activating swap space on $imageSwapDevice"
			if ! mkswap $imageSwapDevice 1>&2;then
				systemException "Failed to create swap signature" "reboot"
			fi
		fi
	fi
	#======================================
	# Create recovery file system
	#--------------------------------------
	if [ -z "$DONT_PARTITION" ] && [ ! -z "$OEM_RECOVERY" ];then
		Echo "Creating Recovery filesystem on $imageRecoveryDevice"
		if ! mke2fs -T ext2 -q -L recovery $imageRecoveryDevice 1>&2;then
			systemException "Failed to create Recovery fs" "reboot"
		fi
	fi
	#======================================
	# Setup recovery contents
	#--------------------------------------
	if [ -z "$DONT_PARTITION" ] && [ ! -z "$OEM_RECOVERY" ];then
		Echo "Setting up recovery archive..."
		mkdir -p /reco-root
		if ! mount $imageRootDevice /reco-root >/dev/null;then
			systemException "Failed to mount root device" "reboot"
		fi
		if [ ! -z "$haveLVM" ];then
			mkdir -p /recoboot && mount $imageBootDevice /recoboot
			mount --bind /recoboot/boot /reco-root/boot
		fi
		mkdir -p /reco-save
		if ! mount $imageRecoveryDevice /reco-save >/dev/null;then
			systemException "Failed to mount recovery device" "reboot"
		fi
		if ! mv /reco-root/recovery.tar.files /reco-save >/dev/null;then
			systemException "Failed to move recovery file count" "reboot"
		fi
		if ! mv /reco-root/recovery.tar.filesystem /reco-save >/dev/null;then
			systemException "Failed to move recovery filesystem info" "reboot"
		fi
		if ! mv /reco-root/recovery.tar.size /reco-save >/dev/null;then
			systemException "Failed to move recovery size info" "reboot"
		fi
		if ! mv /reco-root/recovery.partition.size /reco-save >/dev/null;then
			systemException "Failed to move recovery part size info" "reboot"
		fi
		if [ -f /reco-root/recovery.tar.gz ];then
			if ! mv /reco-root/recovery.tar.gz /reco-save >/dev/null;then
				systemException "Failed to move recovery archive" "reboot"
			fi
		else
			pushd /reco-root
			Echo "Creating recovery root tarball..."
			tar --numeric-owner -czpf /reco-save/recovery.tar.gz . \
				--exclude "./dev" \
				--exclude "./proc" \
				--exclude "./sys" \
				--exclude "./recovery.*"
			popd
		fi
		mkdir /reco-save/boot
		if ! cp /reco-root/boot/initrd.vmx /reco-save/boot/initrd;then
			systemException "Failed to copy recovery initrd" "reboot"
		fi
		if ! cp /reco-root/boot/linux.vmx /reco-save/boot/vmlinuz;then
			systemException "Failed to copy recovery kernel" "reboot"
		fi
		if [ "$loader" = "grub" ];then
			if ! cp /reco-root/boot/message /reco-save/boot/message;then
				systemException "Failed to copy recovery gfx message" "reboot"
			fi
		fi
		if [ "$loader" = "grub2" ];then
			if ! cp /reco-root/boot/unicode.pf2 /reco-save/boot/;then
				systemException "Failed to copy recovery unicode font" "reboot"
			fi
		fi
		if ! dd if=$imageDiskDevice of=/reco-save/mbr bs=1 count=512;then
			systemException "Failed to store MBR" "reboot"
		fi
		if [ ! -z "$haveLVM" ];then
			if ! vgcfgbackup -f /reco-save/lvm $VGROUP;then
				systemException "Failed to store LVM metadata" "reboot"
			fi
		fi
		if [ ! -z "$haveLVM" ];then
			umount /reco-root/boot
			umount /recoboot && rmdir /recoboot
		fi
		umount /reco-save && rmdir /reco-save
		umount /reco-root && rmdir /reco-root
	fi
}

#======================================
# setupDeviceNames
#--------------------------------------
function setupDeviceNames {
	local rootID=$1
	local swapID=$2
	local recoID=$3
	local bootID=$4
	local iorwID=$5
	local vgroup=$6
	if [ -z "$vgroup" ];then
		#======================================
		# set root device name
		#--------------------------------------
		if [ ! -z "$kiwi_RaidDev" ];then
			export imageRootDevice=$kiwi_RaidDev
		else
			export imageRootDevice=$(ddn $imageDiskDevice $rootID)
		fi
		#======================================
		# set swap device name
		#--------------------------------------
		if [ ! $swapID = "no" ];then
			export imageSwapDevice=$(ddn $imageDiskDevice $swapID)
		fi
		#======================================
		# set recovery device name
		#--------------------------------------
		if [ ! $recoID = "no" ];then
			export imageRecoveryDevice=$(ddn $imageDiskDevice $recoID)
			export recoid=$recoID
		fi
		#======================================
		# set read-write device name
		#--------------------------------------
		if [ ! $iorwID = "no" ];then
			export imageIOWRDevice=$(ddn $imageDiskDevice $iorwID)
			export imageRWDevice=$imageIOWRDevice
			export imageRODevice=$imageRootDevice
		fi
		#======================================
		# set boot device name
		#--------------------------------------
		export imageBootDevice=$(ddn $imageDiskDevice $bootID)
		#======================================
		# set boot partition id
		#--------------------------------------
		export bootid=$bootID
	else
		#======================================
		# set LVM root device name
		#--------------------------------------
		export imageRootDevice=/dev/$vgroup/LVRoot
		#======================================
		# set LVM swap device name
		#--------------------------------------
		if [ ! $swapID = "no" ];then
			export imageSwapDevice=/dev/$vgroup/LVSwap
		fi
		#======================================
		# set LVM recovery device name
		#--------------------------------------
		if [ ! $recoID = "no" ];then
			export imageRecoveryDevice=$(ddn $imageDiskDevice $recoID)
			export recoid=$recoID
		fi
		#======================================
		# set LVM read-write device name
		#--------------------------------------
		if [ ! $iorwID = "no" ];then
			if [ -e /dev/$vgroup/LVComp ];then
				export imageRootDevice=/dev/$vgroup/LVComp
			fi
			export imageIOWRDevice=/dev/$vgroup/LVRoot
			export imageRWDevice=$imageIOWRDevice
			export imageRODevice=/dev/$vgroup/LVComp
		fi
		#======================================
		# set LVM boot device name
		#--------------------------------------
		export imageBootDevice=$(ddn $imageDiskDevice $bootID)
		#======================================
		# set LVM boot partition id
		#--------------------------------------
		export bootid=$bootID
	fi
}

#======================================
# createBootDeviceData
#--------------------------------------
function createBootDeviceData {
	#====================================== 
	# check for boot partition
	#--------------------------------------
	if [ "$kiwi_BootPart" != "$kiwi_RootPart" ];then
		export haveBootPartition=yes
	fi
	#====================================== 
	# check for overlay type
	#--------------------------------------
	local unionFST=`echo $UNIONFS_CONFIG | cut -d , -f 3`
	if [ "$unionFST" = "clicfs" ];then
		export haveClicFS=yes
	fi
	sleep 1
}

#======================================
# createLuksMaps
#--------------------------------------
function createLuksMaps {
	luksOpen $imageRootDevice
	export imageRootDevice=$luksDeviceOpened
	if isFSTypeReadOnly;then
		luksOpen $imageIOWRDevice luksReadWrite
		export imageIOWRDevice=$luksDeviceOpened
		export imageRWDevice=$imageIOWRDevice
		export imageRODevice=$imageRootDevice
	fi
}

#======================================
# disableOEMParameters
#--------------------------------------
function disableOEMParameters {
	export OEM_WITHOUTSWAP=1
	unset OEM_RECOVERY
	unset OEM_SYSTEMSIZE
}

#======================================
# disableRepart
#--------------------------------------
function disableRepart {
	DONT_PARTITION=1
}

#======================================
# resizeLVMVolumes
#--------------------------------------
function resizeLVMVolumes {
	local variable
	local volume
	local content
	local mode
	local size
	for i in $(cat /.profile | grep kiwi_LVM_);do
		variable=$(echo $i|cut -f1 -d=)
		volume=$(echo $i| cut -f3- -d_ | cut -f1 -d=)
		content=$(eval echo \$$variable)
		mode=$(echo $content | cut -f1 -d:)
		size=$(echo $content | cut -f2 -d:)
		Echo "Resizing volume $volume..."
		if [ $mode = "size" ];then
			lvextend -L ${size}M /dev/$VGROUP/$volume
		else
			lvextend -L +${size}M /dev/$VGROUP/$volume
		fi
		if [ $? = 0 ];then
			unset FSTYPE
			resizeFilesystem /dev/$VGROUP/$volume
		else
			Echo "Warning: requested size cannot be reached !"
		fi
	done
}

#======================================
# getFreeDiskBytes
#--------------------------------------
function getFreeDiskBytes {
	local disk=$1
	if [ ! -b "$disk" ];then
		echo 0 ; return
	fi
	local disk_Bytes=$(blockdev --getsize64 $disk)
	local rest_Bytes=$disk_Bytes
	local part_Bytes=0
	for i in 1 2 3 4;do
		local part=$(ddn $disk $i)
		if [ ! -b "$part" ];then
			continue
		fi
		local size=$(blockdev --getsize64 $part)
		part_Bytes=$((part_Bytes + size))
	done
	rest_Bytes=$((rest_Bytes - part_Bytes))
	echo $rest_Bytes
}
