jnjnjn
(usa Debian)
Enviado em 29/10/2018 - 11:03h
#!/bin/bash
PCM(){ PROMPT ;clear; PMENU;}
PVM(){ PROMPTVM ;clear; PVM;}
PROMPT()
{ printf "${CE}\e[18;$(( ($(tput cols)-46 )/2+1))f<< PRESSIONE QUALQUER TECLA PARA CONTINUAR >>${FC}";
read -e -n1 PRMPT ;
local PRMPT ;
}
PROMPTVM()
{ printf "${CE}\e[18;$(( ($(tput cols)-46 )/2+1))f<< PRESSIONE QUALQUER TECLA PARA CONTINUAR >>${FC}";
read -e -n1 PRMPTVM ;
local PRMPTVM ;
}
PMENU()
{
#GERACOR;
COLS;
}
bold() { echo -e "\e[1m$@\e[0m" ; }
red() { echo -e "\e[31m$@\e[0m" ; }
green() { echo -e "\e[32m$@\e[0m" ; }
yellow() { echo -e "\e[33m$@\e[0m" ; }
die() { red "ERR: $@" >&2 ; exit 2 ; }
silent() { "$@" > /dev/null 2>&1 ; }
output() { echo -e "- $@" ; }
outputn() { echo -en "- $@ ... " ; }
ok() { green "${@:-OK}" ; }
MENU() {
tput clear
printf "${CE}\e[2;${POS2}f----------------------------------------"
printf "\e[3;${POS1}f-------------- MENU MAQUINAS --------------"
printf "\e[4;${POS}f---\e[4;${POS3}f---"
printf "\e[5;${POS}f---${CSM} [1] Criar [2] Listar ${CE}---"
printf "\e[6;${POS}f---${CSM} [3] Iniciar [4] Deletar ${CE}---"
printf "\e[7;${POS}f---${CSM} [5] Pausar [6] Opção 6 ${CE}---"
printf "\e[8;${POS}f---${CSM} [7] Help [8] Exit ${CE}---"
printf "\e[9;${POS}f---\e[9;${POS3}f---"
printf "${CE}\e[10;${POS1}f------------------------------------------"
printf "\e[11;${POS2}f----------------------------------------\n\n"$FC""
read -p " OPÇãO: " -n1 OPC
case "$OPC" in
1) VM ;;
2) LISTAR ;;
3) INICIAR ;;
4) DELETAR ;;
5) echo -e "\n Criar FUNÇãO_05\n"; PCM ;;
6) echo -e "\n Criar FUNÇãO_06\n"; PCM ;;
7) echo -e "\n${CE} Centraliza e altera a cor do MENU${FC}"; sleep 1 ; PMENU ;;
8) echo -e "\n\n Saindo...\n";sleep 0.5; exit ;;
*) MSGM ; PMENU ;;
esac
}
COLS() {
COLS=$(tput cols)
POS=$(((COLS-44)/2+1))
POS1=$((POS+1))
POS2=$((POS+2))
POS3=$((POS2+39))
(($COLS>=44)) && MENU || { echo -e "$CVA AJUSTE A TELA!\n No MÍNIMO 44 Colunas $FC" ; PCM ; }
}
#COLSVM() {
# COLSVM=$(tput cols)
# POSVM=$(((COLSVM-44)/2+1))
# POSVM1=$((POSVM+1))
# POSVM2=$((POSVM+2))
# POSVM3=$((POSVM2+39))
# (($COLSVM>=44)) && MENU || { echo -e "$CVA AJUSTE A TELA!\n No MÍNIMO 44 Colunas $FC" ; PVM ; }
#}
GERACOR() {
local cnt=$((1+RANDOM%31));local atb=$((RANDOM%2));
CSM="\e["$atb";38;5;"$cnt"m"
}
MSGM() { echo -e "$CVA\n\n \" OPÇãO INVáLIDA! \" $FC" ; sleep 1 ; }
LISTAR() {
tput clear
virsh list --all
PCM
}
INICIAR() {
tput clear
virsh list --all
echo -e "Nome da vm que deseja iniciar: \n"
read in_name
if virsh list --all | grep '$in_name'
then
virsh start $in_name \
&& ok \
|| die "Não foi possivel startar a maquina virtual"
fi
PCM
}
PAUSAR() {
tput clear
virsh list --all
echo -e "Nome da vm que deseja pausar/desligar: \n"
read st_name
virsh shutdown $st_name
PCM
}
DELETAR() {
PCM
}
#PMENU
VM() {
tput clear
printf "${CE}\e[2;${POS2}f----------------------------------------"
printf "\e[3;${POS1}f-------------- MENU IMPORTAÇÃO --------------"
printf "\e[4;${POS}f---\e[4;${POS3}f---"
printf "\e[4;${POS}f---${CSM} [1] Debian 7 [2] Debian 9 ${CE}---"
printf "\e[5;${POS}f---${CSM} [3] Win.Server 2008 [4] Win 2007 ${CE}---"
printf "\e[6;${POS}f---${CSM} [5] Win.Server 2012 [6] Win 2010 ${CE}---"
printf "\e[7;${POS}f---${CSM} [7] Kali [8] Debian 8 ${CE}---"
printf "\e[8;${POS}f---${CSM} [9] Cancelar ${CE}---"
printf "\e[9;${POS}f---\e[10;${POS3}f---"
printf "${CE}\e[10;${POS1}f------------------------------------------"
printf "\e[11;${POS2}f----------------------------------------\n\n"$FC""
read -p " OPÇãO: " -n1 OPCVM
case "$OPCVM" in
1) Debian7 ;;
2) Debian9 ;;
3) WinServer08 ;;
4) Win07 ;;
5) WinServer12 ;;
6) Win10 ;;
7) Kali ;;
8) Debian8;;
9) echo -e "\n\n Cancelando...\n";sleep 0.5; exit ;;
*) MSGM ; PVM ;;
esac
}
MSGC() { echo -e "$CVA\n\n \" OPÇÃO INVÁLIDA! \" $FC" ; sleep 0.25 ; }
Debian7(){
tput clear
# echo -e "Nome da maquina virtual: \n"
# read name
read -p "Nome da maquina virtual:" name
# CI_ISO=$NAME-cidata.iso
read -p "Tamanho do HD (Máximo = 50 Gb):" tamanho
# Erro aqui :(
# test $tamanho > 0 && $tamanho =< 50
while [ $tamanho -ge 1 ] && [ $tamanho -le 50 ]# [ test $tamanho > 0 && $tamanho =< 50 ];
do
echo -e "O valor de memoria definida é de $tamanho \n"
while true
do
read -p "Confirmar? [S/n]" acao
# while true
# do
case "$acao" in
"s")
echo ""
break
;;
"n")
echo ""
break
;;
"S")
echo ""
break
;;
"N")
echo ""
break
;;
*)
MSGC ;break;;
esac
done
done
# echo -e "Tamanho da maquina virtual ( Máximo = 3Gb ou 3076B ): \n"
read -p "Tamanho da maquina virtual ( Máximo = 3Gb ou 3076B ):" memory
while [ $memory -ge 1 ] && [ $memory -le 3076 ]# test $memory > 0 && $memory =< 3076
do
echo -e "O valor de memoria definida é de $memory \n"
while true
do
read -p "Confirmar? [S/n]" acao
# while true
# do
case "$acao" in
"s")
echo ""
break
;;
"n")
echo ""
break
;;
"N")
echo ""
break
;;
"S")
echo ""
break
;;
*)
MSGC ;break;;
esac
done
done
read -p "Quantidade de Cpu: (1 ou 2)" vcpus
while [ $vcpus -ge 1 ] && [ $VAR -le 2 ]#[ test $vcpus != '' ]
do
echo -e "O valor quantide de Cpu definida é $vcpus \n"
while true
do
read -p "Confirmar? [S/n]" acao
# while true
# do
case "$acao" in
"s")
echo ""
break
;;
"S")
echo ""
break
;;
"n")
echo ""
break
;;
"N")
echo ""
break
;;
*)
MSGC ;break;;
esac
done
done
# ERROU FEIO SUHSHUHUS
MACADDRESS=openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'
NETWORK_PARAMS="bridge=${BRIDGE},model=virtio,mac=${MACADDRESS}"
virt-install --import --name $name --memory $memory --vcpus $vcpus --cpu host --disk $disk,format=qcow2,bus=virtio --disk ${CI_ISO},device=cdrom --network ${NETWORK_PARAMS} --os-type=$type --os-variant=$variant --graphics spice,port=$port,listen=$listen --noautoconsole
PMENU
}
# debian9base=
# EXT=qcow2
#
# cp $debian9base $IMGNAME.$EXT
#
# virt-sysprep --format=qcow2 --no-selinux-relabel -a $IMGNAME.$EXT
#
# chown qemu:qemu $IMGNAME.$EXT
#
# bold() { echo -e "\e[1m$@\e[0m" ; }
# red() { echo -e "\e[31m$@\e[0m" ; }
# green() { echo -e "\e[32m$@\e[0m" ; }
# yellow() { echo -e "\e[33m$@\e[0m" ; }
#
# die() { red "ERR: $@" >&2 ; exit 2 ; }
# silent() { "$@" > /dev/null 2>&1 ; }
# output() { echo -e "- $@" ; }
# outputn() { echo -en "- $@ ... " ; }
# ok() { green "${@:-OK}" ; }
#
# pushd() { command pushd "$@" >/dev/null ; }
# popd() { command popd "$@" >/dev/null ; }
#
# outputn "Generating ISO for cloud-init"
# if [ `which genisoimage &>/dev/null` ]
# then
# genisoimage -output $CI_ISO \
# -volid cidata \
# -joliet -r $USER_DATA $META_DATA &>> ${VMNAME}.log \
# && ok \
# || die "Could not generate ISO."
# else
# mkisofs -o $CI_ISO -V cidata -J -r $USER_DATA $META_DATA &>> ${VMNAME}.log \
# && ok \
# || die "Could not generate ISO."
# fi
#
# outputn "Cleaning up cloud-init files"
# rm $USER_DATA $META_DATA $CI_ISO && ok
#
# USER_DATA=user-data
# META_DATA=meta-data
# CI_ISO=${VMNAME}-cidata.iso
#
# { echo "instance-id: ${VMNAME}"; echo "local-hostname: ${VMNAME}"; } > $META_DATA
#
# outputn "Copying cloud image ($(basename ${IMAGE}))"
# DISK=${VMNAME}.qcow2
# cp $IMAGE $DISK && ok
# if $RESIZE_DISK
# then
# outputn "Resizing the disk to $DISK_SIZE"
# qemu-img create -f qcow2 \
# -o preallocation=metadata $DISK.new $DISK_SIZE &>> ${VMNAME}.log \
# && virt-resize --quiet --expand /dev/sda1 $DISK $DISK.new &>> ${VMNAME}.log \
# && (mv $DISK.new $DISK && ok) \
# || die "Could not resize disk."
# fi
# # Create CD-ROM ISO with cloud-init config
# outputn "Generating ISO for cloud-init"
# if [ `which genisoimage &>/dev/null` ]
# then
# genisoimage -output $CI_ISO \
# -volid cidata \
# -joliet -r $USER_DATA $META_DATA &>> ${VMNAME}.log \
# && ok \
# || die "Could not generate ISO."
# else
# mkisofs -o $CI_ISO -V cidata -J -r $USER_DATA $META_DATA &>> ${VMNAME}.log \
# && ok \
# || die "Could not generate ISO."
# fi
#
# # Remove the unnecessary cloud init files
# outputn "Cleaning up cloud-init files"
# rm $USER_DATA $META_DATA $CI_ISO && ok
#
# if [ ! -f "${DISKDIR}/${DISKNAME}" ]
# then
# outputn "Creating new '${TARGET}' disk image for domain ${VMNAME}"
# (qemu-img create -f ${FORMAT} -o size=$DISKSIZE,preallocation=metadata \
# ${DISKDIR}/${DISKNAME} &>> ${DISKDIR}/${VMNAME}.log && ok ) && \
#
# outputn "Attaching ${DISKNAME} to domain ${VMNAME}"
# (virsh attach-disk ${VMNAME} \
# --source $DISKDIR/${DISKNAME} \
# --target ${TARGET} \
# --subdriver ${FORMAT} \
# --cache none \
# --persistent &>> ${DISKDIR}/${VMNAME}.log && ok ) \
# || die "Could not attach disk."
# else
# die "Target ${TARGET} is already created or in use."
# fi
#
#
# IMAGE=${IMAGEDIR}/${QCOW}
#
# if [ ! -f ${IMAGEDIR}/${QCOW} ]
# then
# output "Cloud image not found. Downloading"
# set_wget
# ${WGET} --directory-prefix ${IMAGEDIR} ${IMAGE_URL}/${QCOW} || \
# die "Could not download image."
# fi
PMENU