commit b5c7f1fdc4d023ea60773cb11c07df1dec0f09d9 Author: Eliezer Croitoru Date: Sat Dec 23 18:01:43 2023 +0000 1 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cd9c53c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +images/*.qcow2 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c6b242b --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +all: install-deps choose-image + +install-deps: + apt update + apt install qemu-utils -y + +choose-image: + bash gen-write-image-command.sh + + diff --git a/gen-write-image-command.sh b/gen-write-image-command.sh new file mode 100644 index 0000000..e423170 --- /dev/null +++ b/gen-write-image-command.sh @@ -0,0 +1,93 @@ +#!/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 diff --git a/images/.placeholder b/images/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/init.sh b/init.sh new file mode 100644 index 0000000..b6c4119 --- /dev/null +++ b/init.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env sh + +apt update +apt install make -y diff --git a/scripts/are-you-sure.sh b/scripts/are-you-sure.sh new file mode 100644 index 0000000..0f9ec91 --- /dev/null +++ b/scripts/are-you-sure.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +read -r -p "Are you sure? [y/N] " response +case "$response" in + [yY][eE][sS]|[yY]) + # Write to disk + ;; + *) + # Exit politly + ;; +esac diff --git a/scripts/gen-write-image-command.sh b/scripts/gen-write-image-command.sh new file mode 100644 index 0000000..e423170 --- /dev/null +++ b/scripts/gen-write-image-command.sh @@ -0,0 +1,93 @@ +#!/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 diff --git a/scripts/list-disks.sh b/scripts/list-disks.sh new file mode 100644 index 0000000..61dda46 --- /dev/null +++ b/scripts/list-disks.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +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