找到你要的答案

Q:virt-install script in crontab how to control tty

Q:虚拟安装脚本在crontab如何控制tty

I have a script that creates a virtual machine using virt-install. This script uses a kickstart file for unattended installation. It works perfectly fine when triggered through shell but its throws the following error when triggered through crontab:

error: Cannot run interactive console without a controlling TTY

The VM creation process continues at the backend but in my script it doesn't wait for the virt-install to complete and moves on to the next commands. I wanted my script to wait for the virt-install command to complete its job and then move to the next command. Is there any way i can either get a controll on TTY or make my script to wait for virt-install to complete?

Edit

Here is the virt-install command that my script executes (in case it helps you figuring out the issue):

virt-install --connect=qemu:///system \

--network=bridge:$BRIDGE \

$nic2 \

--initrd-inject=$tmp_ks_file \

--controller type=scsi,model=virtio-scsi \

--extra-args="ks=file:/$(basename $tmp_ks_file) console=tty0 console=ttyS0,115200" \

--name=$img_name \

--disk $libvirt_dir/$img_name.img,size=$disk \

--ram $mem \

--vcpus=2 \

--check-cpu \

--accelerate \

--hvm \

--location=$tree \

--nographics

Thanks in advance,

Kashif

我有一个脚本,创建一个虚拟机使用virt-install。这个脚本使用无人值守安装一个启动文件。这工作完全没有触发时通过壳但它抛出如下错误触发时通过crontab:

错误:无法运行交互式控制台没有控制终端

虚拟机的创建过程继续在后台但我的剧本不等待virt-install完成并移到下一个命令。我想我的剧本等待virt-install命令来完成其工作,然后移动到下一个命令。我有什么办法可以让tty控制或者让我的剧本等待virt-install完成?

编辑

这里是virt-install命令我的脚本执行(如果它可以帮助你找出问题):

virt-install --connect=qemu:///system \

--network=bridge:$BRIDGE \

$nic2 \

--initrd-inject=$tmp_ks_file \

--controller type=scsi,model=virtio-scsi \

--extra-args="ks=file:/$(basename $tmp_ks_file) console=tty0 console=ttyS0,115200" \

--name=$img_name \

--disk $libvirt_dir/$img_name.img,size=$disk \

--ram $mem \

--vcpus=2 \

--check-cpu \

--accelerate \

--hvm \

--location=$tree \

--nographics

先谢谢了。

卡席夫

answer1: 回答1:

I finally able to cater this issue through two steps:

  1. First of all remove the 'console' related configurations from the virt-install command. See extra-args in above command.

  2. Put some logic to wait for virt-install to complete. I did add shutdown in the post install section of kickstart file so that VM shutoff after it is done installing all the packages. Then in my script i actually 'waited' for VM to go to shutdown state before moving to the next command.

This way I am able to run my script in crontab. It also worked with jenkins too.

Hope this helps someone facing the same issue.

我终于能够通过两个步骤来应付这个问题:

  1. 首先删除“控制台”相关的配置从virt-install命令。在上面的命令中看到额外的参数。

  2. 把一些逻辑等虚拟安装完成。我加了关机后安装启动文件,虚拟机关闭完成后安装的所有软件包。然后在我的脚本中,我实际上'等待' VM关闭状态之前,移动到下一个命令。

这样,我跑我的剧本在crontab。它也与詹金斯。

希望这能帮助某人面对同样的问题。

crontab  tty  libvirt