#!/bin/sh #----------------------------------------------------------------------------- # /var/install/bin/xen-import-domain - add a disk-file to eisxen-config # # Creation: 2007-12-24 rg # Last Update: $Id$ # # Copyright (c) 2007-2012 the eisfair team, team(at)eisfair(dot)org # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #----------------------------------------------------------------------------- . /var/install/include/xenlib #exec 2> /tmp/xen-import-domain-trace$$.log #set -x cleanUpOnError() { cd # important for umount unMount "$mountpoint" unMount "$mountpointImage" remove "$mountpoint" remove "$mountpointLvm" if ${swapImageCreated:-false} then echo "Deleting swap image..." echo 'y' | lvremove -f /dev/$volumeGroup/$domainName-swap fi if ${diskImageCreated:-false} then echo "Deleting disk image..." echo 'y' | lvremove -f $importPath/$domainName fi exit 1 } #----------------------------------------------------------------------------- # collect settings #----------------------------------------------------------------------------- collect_settings () { defaultName=`findDefaultname $importDisk` proposedName=`customizeHostname $defaultName` # user dialog domainName="`determineName $proposedName index | awk 'BEGIN { RS=""; FS="\n" } { print $NF }'`" mem=`/var/install/bin/ask --tty --info "Please enter memory size in MB:" "64" "+"` while :; do case "$mem" in "") mem=64 break ;; *) mem=`expr $mem + 0` 2>/dev/null if [ "$?" != "0" ] then mecho --error "Only numeric input is allowed" mem=`/var/install/bin/ask --tty --info "Please enter memory size in MB:" "64" "+"` elif [ ${mem} -lt 64 ] then mecho --error "Memory size must be at least 64 MB" mem=`/var/install/bin/ask --tty --info "Please enter memory size in MB:" "64" "+"` else break fi ;; esac done if ! "$importphySwap" then swapimage=$domainName-swap if /var/install/bin/ask --tty --info "Is this a HVM domain" no then hvm=true swapsize=0 else hvm=false if [ -f $swapimage ] then filesize=`ls -l $swapimage | awk '{ print $5 }'` # Bytes swapsize=`expr $filesize/1048576` # MBytes else swapsize=`/var/install/bin/ask --tty --info "Please enter swap space in MB (0 = no swap)" "0" "*"` while :; do case "$swapsize" in [0]) break ;; *) if [ "$?" != "0" ] then mecho --error "only numeric input is allowed" swapsize=`/var/install/bin/ask --tty --info "Please enter swap space in MB (0 = no swap)" "0" "*"` elif [ $swapsize -lt 32 ] then mecho --error "Swap size must be at least 32 MB or 0 for no swap" swapsize=`/var/install/bin/ask --tty --info "Please enter swap space in MB (0 = no swap)" "0" "*"` else break fi ;; esac done if [ $swapsize -gt 31 ] then vgsizefree=`vgs -o vg_free --noheadings --nosuffix --units b $volumeGroup` vgsizefreeK=`expr $vgsizefree / 1024` # KBytes swapSizeK=`expr $swapsize \* 1024` spaceleft=`expr $vgsizefreeK - $swapSizeK` if [ $spaceleft -lt 0 ] then mecho --error "There is not enough space left in your volume group 'vg'" cleanUpOnError fi ${hvm:-false} || { if vgs | grep -qs $volumeGroup then swapImageCreated=: createLvmImage swap $domainName $volumeGroup $swapSizeK diskN=2 else swapImageCreated=: createDiskImage swap $domainName $swapSizeK diskN=2 fi } fi fi fi fi bootOrder=`/var/install/bin/ask --tty --info "Please enter boot order (min 01 max 99):" "50" "+"` while :; do case "$bootOrder" in "") bootOrder=50 break ;; [1-9]) bootOrder=0$bootOrder break ;; [1-9][0-9]) break ;; *) mecho --error "only numbers between 1 an 99" bootOrder=`/var/install/bin/ask --tty --info "Please enter boot order (min 01 max 99):" "50" "+"` ;; esac done /var/install/bin/ask --tty --info "Automatic start at boot" no && autostart=yes [ -f "$swapimage" -o "$importphySwap" ] && diskN=2 } #----------------------------------------------------------------------------- # write_settings #----------------------------------------------------------------------------- write_settings () { ${hvm:-false} && rootDev="hda" cat<<-EOF >$domUConfigfile$delimiter$domainName AUTO_START="${autostart:-no}" IS_FLI4L='no' HVM="`${hvm:-false} && echo yes || echo no`" USE_CUSTOM_KERNEL='no' KERNEL="`${hvm:-false} && echo $defaultDomUkernel || echo /usr/lib/xen/boot/hvmloader`" MEMORY="$mem" ROOT_DEVICE="/dev/${rootDev:=hda1}" KERNEL_PARAMS='ro quiet xencons=tty1' VIF_N='1' VIF_1_BRIDGE='1' VIF_1_MAC="$mac" PCI_N='0' LEGACY_N='0' DISK_N="${diskN:-1}" DISK_1_TYPE='`if [ "$importPath" = "/dev" -o "$importPath" = "/dev/$volumeGroup" ]; then echo phy; else echo file; fi`' DISK_1_UNAME="$importPath/$importDisk" DISK_1_MAP_TO="$rootDev" DISK_1_MODE='w' EOF [ ${diskN:-1} -eq 2 ] && cat<<-EOF >> $domUConfigfile$delimiter$domainName DISK_2_TYPE='`if [ "$importphySwap" -o "$importPath" = "/dev/$volumeGroup" ]; then echo phy; else echo file; fi`' DISK_2_UNAME="$importPath/$swapimage" DISK_2_MAP_TO="${swapDev:-hda2}" DISK_2_MODE='w' EOF ${hvm:-false} && cat <<-EOF >>$domUConfigfile$delimiter$domainName builder = 'hvm' device_model = '/usr/lib/xen/bin/qemu-dm' EOF ln_relative -s $domUConfigfile$delimiter$domainName ${domUConfigfile}_${bootOrder}_$domainName preSetup # only check for kernel modules if no hvm domU ${hvm:-false} || check_modules $domainName sh /var/install/config.d/$domUConfigPrefix$delimiter$domainName.sh registrate_domain $domainName mecho --info "\n\n Added domain $domainName to configuration." mecho --info "\n You may verify the item Domain $domainName in" mecho --info " the domU Administration-menu before the first boot.\n\n" } #----------------------------------------------------------------------------- # Main #----------------------------------------------------------------------------- # clrhome clrhome echo if [ $# -eq 0 ] then mecho --info "Scanning for yet unimported disk images and devices ..." cleanvmConfigPath # this is important to free unused devices and images for reimport searchUnusedImages searchUnusedPartitions searchUnusedSwapPartitions if [ -f $unusedImages -a -f $unusedPartitions -a -f $unusedLVMPartitions ] then importTarget=`/var/install/bin/ask --tty --info \ "What kind of domU you want to import? please make your choice\n" \ "i" \ "disk [i]mage|i|diskImage" \ "phisical [d]evice|d|phisicalDevice" \ "[lvm] device|lvm|lvmDevice"` $0 $importTarget elif [ -f $unusedImages -a -f $unusedLVMPartitions ] then importTarget=`/var/install/bin/ask --tty --info \ "What kind of domU you want to import? please make your choice\n" \ "i" \ "disk [i]mage|i|diskImage" \ "[lvm] device|lvm|lvmDevice"` $0 $importTarget elif [ -f $unusedPartitions -a -f $unusedLVMPartitions ] then importTarget=`/var/install/bin/ask --tty --info \ "What kind of domU you want to import? please make your choice\n" \ "lvm" \ "phisical [d]evice|d|phisicalDevice" \ "[lvm] device|lvm|lvmDevice"` $0 $importTarget elif [ -f $unusedImages -a -f $unusedPartitions ] then importTarget=`/var/install/bin/ask --tty --info \ "What kind of domU you want to import? please make your choice\n" \ "i" \ "disk [i]mage|i|diskImage" \ "phisical [d]evice|d|phisicalDevice"` $0 $importTarget elif [ -f $unusedImages ] then $0 diskImage elif [ -f $unusedPartitions ] then $0 phisicalDevice elif [ -f $unusedLVMPartitions ] then $0 lvmDevice else mecho --warn "Nothing to import!" anykey fi exit 0 elif [ $# -eq 1 ] then importTarget="${*}" if [ "${importTarget}" = "diskImage" ] then /var/install/bin/list-files.cui -t "Select a diskfile" \ -p "$xenImagePath" \ -f `awk 'BEGIN { RS="\n"; FS="\n"; ORS=","; OFS="," } { print }' $unusedImages | sed -e 's#,$##'` \ -o 1 \ -q 'Use diskfile >>%ls<>%ls<>%ls<< as swap partition?' \ --helpfile=/var/install/help/xen \ --helpname=XEN_IMPORT_DOMU \ --helpview \ -s "$0 $importPath $importDisk" exit 0 fi fi fi if [ $# -eq 3 ] then importPath="$1" importDisk="$2" swapimage="$1/$3" importphySwap=true else importphySwap=false fi echo mecho --info "Selected: $importDisk" if echo $importDisk | grep -Eqs "*\.disk$" then mecho --info "Renaming image file..." diskBasename=`basename $importDisk .disk` mv $importPath/$importDisk $importPath/${diskBasename}.img importDisk=${diskBasename}.img fi collect_settings if echo $importDisk | grep -Eqs "*\.img$" then vgsizefree=`vgs -o vg_free --noheadings --nosuffix --units b $volumeGroup` vgsizefreeK=`expr $vgsizefree / 1024` # KBytes filesize=`ls -l $importPath/$importDisk | awk '{ print $5 }'` # Bytes diskSizeK=`expr $filesize / 1024` # KBytes spaceleft=`expr $vgsizefreeK - $diskSizeK` if [ $spaceleft -lt 0 ] then mecho --error "There is not enough space left in your volume group 'vg'" cleanUpOnError fi mecho --info "Create logical volume..." diskImageCreated=true if vgs | grep -qs $volumeGroup then createLvmImage noformat $domainName $volumeGroup $diskSizeK # parameter noformat is a fake. else createDiskImage noformat $domainName $diskSizeK fi # copy via dd /usr/bin/bar -o /dev/$volumeGroup/$domainName $importPath/$importDisk importPath="/dev/$volumeGroup" importDisk=$domainName fi write_settings mecho --info "\nThe domU was imported into the logical volume '/dev/$volumeGroup/$domainName'" mecho --info "The source image ist still available under \"$xenImagePath/\". Please delete it manually, if necessary!\n" anykey exit 0 # Regular end of script. #set +x