[英]How can i check specific server running or not(using virsh commands), before I start | restart it?
I'm trying to restart, start, shutdown a specific virtual machine.我正在尝试重新启动、启动、关闭特定的虚拟机。 Here, at first I want to check if the virtual machine is already in required state or not before I run the script.在这里,首先我想在运行脚本之前检查虚拟机是否已经处于所需状态。
These are the list of VMs.这些是虚拟机列表。
[root@demohost05 ~]# virsh list --all
Id Name State
----------------------------------------------------
5 OwnCloud01 running
6 OwnCloud02 running
7 SiteMon running
- vmtest shut off
I want to check if vmtest is runnnig or not before I implement我想在实施之前检查 vmtest 是否正在运行
virsh start vmtest
How can check the status using if condition in shell script ?如何在 shell 脚本中使用 if 条件检查状态?
How can I avoid to enter password when I've to use sudo command.当我必须使用 sudo 命令时,如何避免输入密码。
sudo virsh start vmtest
I also tried to give root permission using我还尝试使用授予root权限
sudo -i
virsh start vmtest
But script ends without implementing 2nd line.但是脚本没有实现第二行就结束了。 How can I use both commands in same script file?如何在同一个脚本文件中使用这两个命令?
if [conditions]
then
{
}
fi
I couldnt figure out how to check conditions for such scripts.我不知道如何检查此类脚本的条件。
Thank you.谢谢你。
Try this:尝试这个:
tmp=$(virsh list --all | grep " vmtest " | awk '{ print $3}')
if ([ "x$tmp" == "x" ] || [ "x$tmp" != "xrunning" ])
then
echo "VM does not exist or is shut down!"
# Try additional commands here...
else
echo "VM is running!"
fi
# For passwordless sudo:
sudo cat /etc/sudoers
# You'll see this:
# User privilege specification
root ALL=(ALL:ALL) ALL
# To add user sharad as a sudo user:
# User privilege specification
root ALL=(ALL:ALL) ALL
sharad ALL=(ALL:ALL) ALL
# To add user sharad as a sudo user such that it doesn't ask for password (note the NOPASSWD):
# User privilege specification
root ALL=(ALL:ALL) ALL
sharad ALL=(ALL:ALL) NOPASSWD: ALL
# Read this for reference: http://www.ducea.com/2006/06/18/linux-tips-password-usage-in-sudo-passwd-nopasswd/
I like Sharad's answer but I reversed it into a busy loop while waiting for a VM to shutdown我喜欢沙拉德的回答,但我在等待虚拟机关闭时将其反转为一个繁忙的循环
virsh shutdown $VM
state=$(virsh list --all | grep " $VM " | awk '{ print $3}')
while ([ "$state" != "" ] && [ "$state" == "running" ]); do
sleep 10
state=$(virsh list --all | grep " $VM " | awk '{ print $3}')
done;
# now do something else to the shutdown VM and finally restart it
virsh start $VM
In my case I create a snapshot when the VM is shut down, then I restart it.就我而言,我在关闭 VM 时创建一个快照,然后重新启动它。 I hard-coded a 10 second sleep into the loop because it seems like a reasonable retry period, given that a Windows VM could take a long time if it is installing updates - could be several minutes or even longer.我将 10 秒的睡眠时间硬编码到循环中,因为这似乎是一个合理的重试周期,因为 Windows 虚拟机在安装更新时可能需要很长时间——可能是几分钟甚至更长。
My scenario is a bit different, so I got a slightly different solution.我的情况有点不同,所以我得到了一个稍微不同的解决方案。
I want to start a VM (Windows) and connect to it via xfreerdp
all in one script.我想在一个脚本中启动一个虚拟机(Windows)并通过xfreerdp
连接到它。 For this to work port 3389 has to be open inside the VM, which is not the case directly after starting the VM, although virsh list --all
returns "running".为此,必须在 VM 内打开端口 3389,但在启动 VM 后并非直接打开这种情况,尽管virsh list --all
返回“正在运行”。 So here is my little script using nc
(aka netcat
) to check the port:所以这是我使用nc
(又名netcat
)检查端口的小脚本:
# "windows_vm" is the VM-name, while "windows_guest" is the VM's hostname
virsh dominfo windows_vm | grep 'State: *shut off'
if [ $? -eq 0 ]; then
echo "Start windows_vm..."
virsh start windows_vm
while true; do
echo "Waiting for start..."
# nc-parameters:
# -w1: wait 1s
# -z: ony scan the port without sending data
nc -w1 -z windows_guest 3389
if [ $? -eq 0 ]; then
break
fi
sleep 1
done
fi
xfreerdp /v:windows_guest ...some-more-parameters...
If Remote Desktop capability is not enabled in Windows, there's maybe some other port you can check.如果 Windows 中未启用远程桌面功能,则可能还有其他一些可以检查的端口。
If the guest is a Linux-VM port 22 is always worth a try.如果来宾是 Linux-VM 端口 22 总是值得一试的。
Building on Sharad's and Gary's answers, but only querying the status of VM in question.基于 Sharad 和 Gary 的答案,但仅查询有问题的 VM 的状态。
virsh shutdown $VM
state=$(virsh dominfo $VM | grep -w "State:" | awk '{ print $2}')
while ([ "$state" != "" ] && [ "$state" == "running" ]); do
sleep 10
state=$(virsh dominfo $VM | grep -w "State:" | awk '{ print $2}')
done;
# now do something else to the shutdown VM and finally restart it
virsh start $VM
If you don't need to use virsh
commands, on Ubuntu, a running domain name has a PID file and running XML config file at "/var/run/libvirt/qemu/${DomainName}.{pid,xml}"
.如果您不需要使用virsh
命令,在 Ubuntu 上,正在运行的域名有一个 PID 文件和运行 XML 配置文件,位于"/var/run/libvirt/qemu/${DomainName}.{pid,xml}"
。
I wrote a script to start domains on the condition a file exists.我编写了一个脚本来在文件存在的情况下启动域。 This way I don't have to toggle domain autostart options.这样我就不必切换域自动启动选项。
================================================================================
2022/01/15 16:18:42: Okay to start.
2022/01/15 16:18:42: 'ParrotKde' appears to be running already. PID = 24470.
2022/01/15 16:18:45: 'UbuntuServer20' has been started. PID = 25209.
================================================================================
2022/01/15 16:20:45: '/scripts/startvms' doesn't exist. Aborting.
How can I avoid to enter password when I've to use sudo command.当我必须使用 sudo 命令时,如何避免输入密码。
Ensure your user is a member of the 'libvirt' group.确保您的用户是“libvirt”组的成员。
sudo usermod -G libvirt <username>
You will need to log out and in again for this to take effect.您需要注销并重新登录才能生效。
Note that on RHEL based systems, you might also need to ensure you have the following environment variable set:请注意,在基于 RHEL 的系统上,您可能还需要确保设置了以下环境变量:
LIBVIRT_DEFAULT_URI=qemu:///system
Then try:然后尝试:
$ virsh domstate vmtest
running
Unfortunately, the above doesn't give you a nice easy exit code to test in your bash, so you can try the following (output shown for a machine that isn't running):不幸的是,上面没有给你一个很好的简单退出代码来在你的 bash 中测试,所以你可以尝试下面的(输出显示为一台没有运行的机器):
$ virsh domstate vmtest | grep running
$ echo $?
1
Bash script would look like: Bash 脚本如下所示:
#!/bin/bash
virsh domstate vmtest | grep running
if [ $? -ne 0 ] ; then
echo Starting VM vmtest
virsh start vmtest
fi
I was just looking at this.我只是在看这个。 Instead of piping to grep
and awk
, there is a built in command.代替管道到grep
和awk
,有一个内置命令。 I don't know if that was the case when this question was first asked but for anyone else that may come across this:我不知道第一次提出这个问题时是否是这种情况,但对于可能遇到此问题的其他任何人:
virsh domstate <vm name>
Make sure to use quotes of the name has a space in it.确保使用的名称的引号中有一个空格。 Also, the accepted answer only works if the name does not have a space in it.此外,仅当名称中没有空格时,接受的答案才有效。 If it does have a space, you need to adjust the awk
command.如果确实有空格,则需要调整awk
命令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.