#!/usr/bin/env bash function showDisksInfo() { echo "blkid output:" echo "###" blkid echo "###" echo echo "lsblk outpu:" echo "####" lsblk echo "####" } function listImages() { find images/ -name *.qcow2 -type f } function listDisks() { disk=() size=() name=() while IFS= read -r -d $'\0' device; do device=${device/\/dev\//} disk+=($device) name+=("`cat "/sys/class/block/$device/device/model"`") size+=("`cat "/sys/class/block/$device/size"`") done < <(find "/dev/" -regex '/dev/sd[a-z]\|/dev/vd[a-z]\|/dev/hd[a-z]' -print0) for i in `seq 0 $((${#disk[@]}-1))`; do echo -e "${disk[$i]}\t${name[$i]}\t${size[$i]}" done } showDisksInfo echo 'Please select from the DISKS list:' disks=$(listDisks) echo -e "$disks" | nl; count="$(echo -e "$disks" | wc -l)" read -p 'Select option: ' n for i in `echo -e "$disks" | nl`; do if [ "$n" == "$i" ]; then value=$(echo -e "$disks" | sed -n ${i}p) 2>&1 break fi done; echo "The user selected option number $n: '$value'" DISK="${value}" DISK_FD="$(echo "/dev/$(echo "${value}" |awk '{ print $1}')")" echo "You choose the next disk:" echo "${DISK_FD}" read -r -p "Are you sure? [y/N] " response case "$response" in [yY][eE][sS]|[yY]) echo 'Please select image from the list:' images=$(listImages) echo -e "$images" | nl; count="$(echo -e "$images" | wc -l)" read -p 'Select option: ' n for i in `echo -e "$images" | nl`; do if [ "$n" == "$i" ]; then value=$(echo -e "$images" | sed -n ${i}p) 2>&1 break fi done; echo "The user selected option number $n: '$value'" IMAGE="${value}" read -r -p "Are you sure? [y/N] " response case "$response" in [yY][eE][sS]|[yY]) echo "Command is:" echo "qemu-img convert -p -f qcow2 -O raw ${IMAGE} ${DISK_FD}" echo "Good luck" ;; *) echo "You choose to exit" # Exit politly ;; esac ;; *) echo "You choose to exit" # Exit politly ;; esac