Gentoo Archives: gentoo-commits

From: "Doug Goldstein (cardoe)" <cardoe@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] gentoo-x86 commit in app-emulation/libvirt/files: libvirtd.confd-r2 libvirtd.init-r2
Date: Thu, 27 Jan 2011 21:03:07
Message-Id: 20110127210256.76D3520057@flycatcher.gentoo.org
1 cardoe 11/01/27 21:02:56
2
3 Added: libvirtd.confd-r2 libvirtd.init-r2
4 Log:
5 Add to the init script the shutting down of NATed networks created by libvirt itself. This functionality was written by Jan Vansteenkiste <jan@××××××.eu>. It complements the 'virt-network' USE flag.
6
7 (Portage version: 2.1.9.35/cvs/Linux x86_64)
8
9 Revision Changes Path
10 1.1 app-emulation/libvirt/files/libvirtd.confd-r2
11
12 file : http://sources.gentoo.org/viewvc.cgi/gentoo-x86/app-emulation/libvirt/files/libvirtd.confd-r2?rev=1.1&view=markup
13 plain: http://sources.gentoo.org/viewvc.cgi/gentoo-x86/app-emulation/libvirt/files/libvirtd.confd-r2?rev=1.1&content-type=text/plain
14
15 Index: libvirtd.confd-r2
16 ===================================================================
17 # /etc/conf.d/libvirtd
18
19 # LIBVIRTD_OPTS
20 # You may want to add '--listen' to have libvirtd listen for tcp/ip connections
21 # if you want to use libvirt for remote control
22 # Please consult 'libvirtd --help' for more options
23 #LIBVIRTD_OPTS="--listen"
24
25 # LIBVIRTD_KVM_SHUTDOWN
26 # Valid options:
27 # * shutdown - Sends an ACPI shutdown (think when you tap the power button
28 # on your machine and it begins a graceful shutdown). If your
29 # VM ignores this, it will have the power yanked out from under
30 # it in LIBVIRTD_KVM_SHUTDOWN_MAXWAIT seconds.
31 # * managedsave - Performs a state save external to the VM. qemu-kvm will stop
32 # stop the CPU and save off all state to a separate file. When
33 # the machine is started again, it will resume like nothing ever
34 # happened. This is guarenteed to always successfully stop your
35 # machine and restart it. However it may take some time to finish.
36 # * none - No attempts will be made to stop any VMs. If you are restarting your
37 # machine the qemu-kvm process will be simply killed, which may result
38 # in your VMs having disk corruption.
39 LIBVIRTD_KVM_SHUTDOWN="managedsave"
40
41 # LIBVIRTD_KVM_SHUTDOWN_MAXWAIT
42 # Timeout in seconds until stopping libvirtd and "pulling the plug" on the
43 # remaining VM's still in a running state
44 #LIBVIRTD_KVM_SHUTDOWN_MAXWAIT="500"
45
46 # LIBVIRTD_NET_SHUTDOWN
47 # If libvirtd created networks for you (e.g. NATed networks) then this init
48 # script will shut them down for you if this is set to 'yes'. Otherwise,
49 # the networks will be left running once libvirt is shutdown. For this
50 # option to be useful you must have enabled the 'virt-network' USE flag and
51 # have had libvirt create a NATed network for you.
52 # Valid values: 'yes' or 'no'
53 #LIBVIRTD_NET_SHUTDOWN="yes"
54
55
56
57 1.1 app-emulation/libvirt/files/libvirtd.init-r2
58
59 file : http://sources.gentoo.org/viewvc.cgi/gentoo-x86/app-emulation/libvirt/files/libvirtd.init-r2?rev=1.1&view=markup
60 plain: http://sources.gentoo.org/viewvc.cgi/gentoo-x86/app-emulation/libvirt/files/libvirtd.init-r2?rev=1.1&content-type=text/plain
61
62 Index: libvirtd.init-r2
63 ===================================================================
64 #!/sbin/runscript
65
66 opts="start stop status reload restart"
67
68 depend() {
69 need net
70 before sshd ntp-client ntpd nfs nfsmount rsyncd portmap dhcp
71 }
72
73 libvirtd_virsh() {
74 # Silence errors because virsh always throws an error about
75 # not finding the hypervisor version when connecting to libvirtd
76 LC_ALL=C virsh -c qemu:///system "$@" 2>/dev/null
77 }
78
79 libvirtd_dom_list() {
80 # Make sure that it wouldn't be confused if the domain name
81 # contains the word running.
82 libvirtd_virsh list | awk '$3 == "running" { print $1 }'
83 }
84
85 libvirtd_dom_count() {
86 # Make sure that it wouldn't be confused if the domain name
87 # contains the word running.
88 libvirtd_virsh list | awk 'BEGIN { count = 0 } \
89 $3 == "running" { count++ } \
90 END { print count }'
91 }
92
93 libvirtd_net_list() {
94 # The purpose of the awk is to avoid networks with 'active' in the name
95 libvirtd_virsh net-list | awk '$2 == "active" { print $1 }'
96 }
97
98 libvirtd_net_count() {
99 # The purpose of the awk is to avoid networks with 'active' in the name
100 libvirtd_virsh net-list | awk 'BEGIN { count = 0 } \
101 $2 == "active" { count++ } \
102 END { print count }'
103 }
104
105
106 start() {
107 ebegin "Starting libvirtd"
108 start-stop-daemon --start \
109 --env KRB5_KTNAME=/etc/libvirt/krb5.tab \
110 --exec /usr/sbin/libvirtd -- -d ${LIBVIRTD_OPTS}
111 eend $?
112 }
113
114 stop() {
115 ebegin "Stopping libvirtd"
116 # try to shutdown all (KVM/Qemu) domains
117 DOM_COUNT="$(libvirtd_dom_count)"
118 if [ "${LIBVIRTD_KVM_SHUTDOWN}" != "none" ] \
119 && [ "${DOM_COUNT}" != "0" ] ; then
120
121 einfo " Shutting down domain(s):"
122 for DOM_ID in $(libvirtd_dom_list) ; do
123 NAME="$(libvirtd_virsh domname ${DOM_ID} | head -n 1)"
124 einfo " ${NAME}"
125 libvirtd_virsh ${LIBVIRTD_KVM_SHUTDOWN} ${DOM_ID} > /dev/null
126 done
127
128 if [ -n "${LIBVIRTD_KVM_SHUTDOWN_MAXWAIT}" ] ; then
129 COUNTER="${LIBVIRTD_KVM_SHUTDOWN_MAXWAIT}"
130 else
131 COUNTER=500
132 fi
133
134 if [ "${LIBVIRTD_KVM_SHUTDOWN}" = "shutdown" ]; then
135 einfo " Waiting ${COUNTER} seconds while domains shutdown ..."
136 DOM_COUNT="$(libvirtd_dom_count)"
137 while [ ${DOM_COUNT} -gt 0 ] && [ ${COUNTER} -gt 0 ] ; do
138 DOM_COUNT="$(libvirtd_dom_count)"
139 sleep 1
140 COUNTER=$((${COUNTER} - 1))
141 echo -n "."
142 done
143 fi
144
145 DOM_COUNT="$(libvirtd_dom_count)"
146 if [ "${DOM_COUNT}" != "0" ] ; then
147 eerror " !!! Some guests are still running, stopping anyway"
148 fi
149
150 fi
151
152 NET_COUNT="$(libvirtd_net_count)"
153 if [ "${LIBVIRTD_NET_SHUTDOWN}" != "no" \
154 && "${NET_COUNT}" != "0" ]; then
155
156 einfo " Shutting down network(s):"
157 for NET_NAME in $(libvirtd_net_list); do
158 einfo " ${NET_NAME}"
159 libvirtd_virsh net-destroy ${NET_NAME} > /dev/null
160 done
161
162 NET_COUNT="$(libvirtd_net_count)"
163 if [ "${NET_COUNT}" != "0" ]; then
164 eerror " !!! Some networks are still active, stopping anyway"
165 fi
166 fi
167
168 # Now actually stop the daemon
169 start-stop-daemon --stop --quiet --exec \
170 /usr/sbin/libvirtd --pidfile=/var/run/libvirtd.pid
171 eend $?
172 }
173
174 reload() {
175 ebegin "Reloading libvirtd without shutting down your VMs"
176 start-stop-daemon --stop --quiet --exec \
177 /usr/sbin/libvirtd --pidfile=/var/run/libvirtd.pid
178 if [ $? -ne 0 ]; then
179 eend $?
180 fi
181 start-stop-daemon --start --quiet --exec \
182 /usr/sbin/libvirtd -- -d ${LIBVIRTD_OPTS}
183 eend $?
184 }