我如何在开始之前检查特定服务器 运行 或不检查(使用 virsh 命令)|重启了吗?

How can i check specific server running or not(using virsh commands), before I start | restart it?

我正在尝试重新启动、启动、关闭特定虚拟机。 在这里,首先我想在我 运行 脚本之前检查虚拟机是否已经处于所需状态。

这些是虚拟机列表。

[root@demohost05 ~]# virsh list --all
Id    Name                           State
----------------------------------------------------
5     OwnCloud01                     running
6     OwnCloud02                     running
7     SiteMon                        running
-     vmtest                         shut off

我想在执行

之前检查 vmtest 是否 运行nnig
virsh start vmtest

如何在 shell 脚本中使用 if 条件检查状态?

当我必须使用sudo命令时,如何避免输入密码。

sudo virsh start vmtest

我还尝试使用

授予 root 权限
sudo -i
virsh start vmtest

但是脚本没有执行第二行就结束了。 如何在同一个脚本文件中使用这两个命令?

if [conditions]
then
{

}
fi

我不知道如何检查此类脚本的条件。

谢谢。

试试这个:


tmp=$(virsh list --all | grep " vmtest " | awk '{ print }')
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/

我喜欢 Sharad 的回答,但我在等待 VM 关闭时将其反转为一个繁忙的循环

    virsh shutdown $VM
    state=$(virsh list --all | grep " $VM " | awk '{ print }')
    while ([ "$state" != "" ] && [ "$state" == "running" ]); do
      sleep 10
      state=$(virsh list --all | grep " $VM " | awk '{ print }')
    done;
    # now do something else to the shutdown VM and finally restart it
    virsh start $VM

在我的例子中,我在 VM 关闭时创建快照,然后重新启动它。我 hard-coded 在循环中休眠了 10 秒,因为这似乎是一个合理的重试周期,因为如果 Windows VM 安装更新可能需要很长时间 - 可能是几分钟甚至更长时间。

我的场景有点不同,所以我得到了一个稍微不同的解决方案。

我想启动一个 VM (Windows) 并通过 xfreerdp 连接到它,全部在一个脚本中完成。为此,必须在 VM 内打开端口 3389,启动 VM 后并非如此,尽管 virsh list --all returns "running"。所以这是我使用 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...

如果 Windows 中未启用远程桌面功能,您可能可以检查其他一些端口。

如果来宾是 Linux-VM 端口 22 始终值得一试。

基于 Sharad 和 Gary 的回答,但仅查询相关 VM 的状态。

virsh shutdown $VM
state=$(virsh dominfo $VM | grep -w "State:" | awk '{ print }')
while ([ "$state" != "" ] && [ "$state" == "running" ]); do
  sleep 10
  state=$(virsh dominfo $VM | grep -w "State:" | awk '{ print }')
done;
# now do something else to the shutdown VM and finally restart it
virsh start $VM

如果您不需要使用virsh命令,在Ubuntu上,一个运行域名有一个PID文件和运行XML配置文件位于 "/var/run/libvirt/qemu/${DomainName}.{pid,xml}".

我写了一个脚本来在文件存在的情况下启动域。这样我就不必切换域自动启动选项。

================================================================================
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.

确保您的用户是 'libvirt' 组的成员。

sudo usermod -G libvirt <username>

您需要注销并重新登录才能生效。

请注意,在基于 RHEL 的系统上,您可能还需要确保设置了以下环境变量:

LIBVIRT_DEFAULT_URI=qemu:///system

然后尝试:

$ virsh domstate vmtest
running

不幸的是,上面没有给你一个很好的简单退出代码来测试你的 bash,所以你可以尝试以下(输出显示不是 运行 的机器):

$ virsh domstate vmtest | grep running
$ echo $? 
1

Bash 脚本看起来像:

#!/bin/bash
virsh domstate vmtest | grep running
if [ $? -ne 0 ] ; then
  echo Starting VM vmtest
  virsh start vmtest
fi