Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
qemu: Herunterfahren per Script/telnet
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

 
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum
View previous topic :: View next topic  
Author Message
l3u
Advocate
Advocate


Joined: 26 Jan 2005
Posts: 2537
Location: Konradsreuth (Germany)

PostPosted: Tue Dec 24, 2013 1:50 pm    Post subject: qemu: Herunterfahren per Script/telnet Reply with quote

Hallo :-)

Wenn ich eine virtuelle qemu-Maschine mit
Code:
-qmp tcp:127.0.0.1:4444,server,nowait

starte, dann kann ich sie mit
Code:
telnet 127.0.0.1 4444 <<JSON
{ "execute": "qmp_capabilities" }
{ "execute": "system_powerdown" }

JSON

(also per Script) herunterfahren. Allerdings funktioniert die Kommunikation asynchron, also wird der Befehl sofort beendet, und nicht erst, wenn die Maschine heruntergefahren ist. Abgesehen davon sagt telnet auch „Connection closed by foreign host.“ und $? ist nicht 0.

Jetzt hierzu zwei Fragen:

1. Kann ich den telnet-Aufruf so gestalten, dass das Script „normal“ beendet wird, also $? hinterher 0 ist?

2. Wie finde ich heraus, wann die virtuelle Maschine fertig heruntergefahren ist? Bzw. kann ich das evtl. in den telnet-Aufruf packen, dass der Befehl eben nicht asynchron ausgeführt werden soll, so dass das Script erst dann zurückkommt, wenn die Maschine auch heruntergefahren ist?

Der Hintergrund ist, dass ein Server mit einer unterbrechungsfreien Stromversorgung im Ausfall-Fall erstmal die virtuelle Maschine herunterfahren soll, die er hostet und dann sich selbst – was natürlich sinnvollerweise erst dann angestoßen werden sollte, wenn die VM vollständig heruntergefahren ist.

Vielen Dank für die Antworten schonmal :-)
Back to top
View user's profile Send private message
l3u
Advocate
Advocate


Joined: 26 Jan 2005
Posts: 2537
Location: Konradsreuth (Germany)

PostPosted: Fri Dec 27, 2013 1:05 pm    Post subject: Reply with quote

Es geht mit expect:
Code:
#!/usr/bin/expect

set timeout -1

spawn telnet 127.0.0.1 4444

expect "QMP"
send "{ 'execute': 'qmp_capabilities' }\n"

expect "return"
send "{ 'execute': 'system_powerdown' }\n"

expect "SHUTDOWN"
Back to top
View user's profile Send private message
m_neutron
n00b
n00b


Joined: 26 Feb 2007
Posts: 29
Location: south brandenburg /germany

PostPosted: Tue Mar 07, 2017 2:13 pm    Post subject: Reply with quote

danke, der Thread hat mir sehr geholfen mit dem QMP-Interface
warm zu werden ;)

benutze jetzt dieses stop script mit netcat und fuser [timeout]:

Code:

#!/bin/sh
#do a local telnet job to stop some vm
GUEST=vm.qcow2
IMAGE=/home/vms/win10ding
LOGGFILE=${IMAGE}/${GUEST}_kvm_start.log
CTRPORT=4444
ENCE="nc -i 1 -q 1 -t"

echo "sending powerdown signal to $GUEST..."
echo "`date`: sending powerdown signal to GUEST..." >> $LOGGFILE

#echo -e "\nsystem_powerdown\n" | $ENCE 127.0.0.1 $CTRPORT
echo -e "{ \"execute\": \"qmp_capabilities\" } \n { \"execute\": \"system_powerdown\" }" | $ENCE 127.0.0.1 $CTRPORT

echo "waiting for $GUEST at $IMAGE to shutdown..."
echo "`date`: waiting for $GUEST to shutdown..." >> $LOGGFILE
for (( c=1; c<=300; c++ ))
do
   if  timeout 2 fuser -s $IMAGE/$GUEST
   then
   sleep 2
   echo -n "."
   echo -n ".">> $LOGGFILE
   else
   echo done
   echo -e "`date`:shutdown comleted\n\n" >> $LOGGFILE
   exit 0
   fi
done
echo "timeout: check $GUEST VM" >> $LOGGFILE
exit 1
Back to top
View user's profile Send private message
l3u
Advocate
Advocate


Joined: 26 Jan 2005
Posts: 2537
Location: Konradsreuth (Germany)

PostPosted: Wed Mar 08, 2017 1:08 pm    Post subject: Reply with quote

Danke für die Blumen ;-) Mit expect spart man sich die Warteschleife … und ich denke mal, dass das auch genau für so einen Fall gedacht ist. Aber geht natürlich auch so.
Back to top
View user's profile Send private message
toralf
Developer
Developer


Joined: 01 Feb 2004
Posts: 3919
Location: Hamburg

PostPosted: Sun Mar 12, 2017 9:34 am    Post subject: Reply with quote

/me hätte schwören können, daß es für Telnet eigentlich keine Anwendungsfälle mehr gibt - außer mal schnell 'nen lokalen Port testen - aber man lernt nie aus.
Back to top
View user's profile Send private message
l3u
Advocate
Advocate


Joined: 26 Jan 2005
Posts: 2537
Location: Konradsreuth (Germany)

PostPosted: Mon Mar 13, 2017 7:09 pm    Post subject: Reply with quote

Braucht man schon immer mal wieder ;-)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum