Using packer and type qemu in the json file to create a RHEL 8.4 guest kvm vm, but ssh timeout error coming

I have RHEL 8.5 as the KVM host. I want to create a guest vm of RHEL 8.4 through packer type qemu and have a json file where all the configurations are mentioned.

{

“builders”: [

{

“type”: “qemu”,

“iso_url”: “/var/lib/libvirt/images/test.iso”,

“iso_checksum”: “md5:3959597d89e8c20d58c4514a7cf3bc7f”,

“output_directory”: “/var/lib/libvirt/images/iso-dir/test”,

“disk_size”: “55G”,

“headless”: “true”,

“qemuargs”: [

        [

            "-m",

            "4096"

        ],

        [

            "-smp",

            "2"

        ]

],

“format”: “qcow2”,

“shutdown_command”: “echo ‘siedgerexuser’ | sudo -S shutdown -P now”,

“accelerator”: “kvm”,

“ssh_username”: “nonrootuser”,

“ssh_password”: “********”,

“ssh_timeout”: “20m”,

“vm_name”: “test”,

“net_device”: “virtio-net”,

“disk_interface”: “virtio”,

“http_directory”: “/home/azureuser/http”,

“boot_wait”: “10s”,

“boot_command”: [

“e inst.ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/anaconda-ks.cfg”

]

}

],

“provisioners”:

[

{

  "type": "file",

  "source": "/home/azureuser/service_status_check.sh",

  "destination": "/tmp/service_status_check.sh"

},

{

  "type": "file",

  "source": "/home/azureuser/service_check.sh",

  "destination": "/tmp/service_check.sh"

},  

{

  "type": "file",

  "source": "/home/azureuser/azure.sh",

  "destination": "/tmp/azure.sh"

},

{


  "type": "file",

  "source": "/home/azureuser/params.cfg",

  "destination": "/tmp/params.cfg"

},



{ 

  "type": "shell" ,



  "execute_command": "echo 'siedgerexuser' | {{.Vars}} sudo -E -S bash '{{.Path}}'",



  "inline": [
"echo copying" , "cp /tmp/params.cfg  /root/",
    "sudo ls -lrt /root/params.cfg",
    "sudo ls -lrt /opt/scripts/"
  ],


    "inline_shebang": "/bin/sh -x"

},

{

  "type": "shell",

  "pause_before": "5s",
  "expect_disconnect": true ,

  "inline": [
    "echo runningconfigurescript" , "sudo sh /opt/scripts/configure-env.sh"

  ]

},

{

  "type": "shell",

  "pause_before": "200s",

  "inline": [
    
    "sudo sh /tmp/service_check.sh",
    "sudo sh /tmp/azure.sh"

  ]

}

]

}

It is working fine in rhel 7.9, but the same thing giving ssh timeout error in RHEL 8.4.

But when i am creating guest vm with virt-install it is able to create a vm and i am able to see it in cockpit web ui, but when i initiate packer build then while giving ssh timeout error it is not visible in cockpit UI, so not able to see where the guest vm created get stuck.

Can anyone please help me to fix this issue

==> qemu: Waiting for SSH to become available…

2022/09/23 12:53:15 packer-builder-qemu plugin: [DEBUG] SSH handshake err: Timeout during SSH handshake

2022/09/23 12:53:22 packer-builder-qemu plugin: [INFO] Attempting SSH connection to 127.0.0.1:3565…

2022/09/23 12:53:22 packer-builder-qemu plugin: [DEBUG] reconnecting to TCP connection for SSH

2022/09/23 12:53:22 packer-builder-qemu plugin: [DEBUG] handshaking with SSH

2022/09/23 12:54:22 packer-builder-qemu plugin: [DEBUG] SSH handshake err: Timeout during SSH handshake

2022/09/23 12:54:29 packer-builder-qemu plugin: [INFO] Attempting SSH connection to 127.0.0.1:3565…

2022/09/23 12:54:29 packer-builder-qemu plugin: [DEBUG] reconnecting to TCP connection for SSH

2022/09/23 12:54:29 packer-builder-qemu plugin: [DEBUG] handshaking with SSH

2022/09/23 12:55:29 packer-builder-qemu plugin: [DEBUG] SSH handshake err: Timeout during SSH handshake

Resolved the issue, added inst.sshd in boot command

Hi Pravin,

Im trying to run packer for OEL 8.7 and Im getting the same error.

2023/06/14 03:42:34 packer-builder-qemu plugin: [INFO] Waiting for SSH, up to timeout: 20m0s
2023/06/14 03:42:34 packer-builder-qemu plugin: [INFO] Attempting SSH connection to 127.0.0.1:3571…
2023/06/14 03:42:34 packer-builder-qemu plugin: [DEBUG] reconnecting to TCP connection for SSH
2023/06/14 03:42:34 packer-builder-qemu plugin: [DEBUG] handshaking with SSH
2023/06/14 03:43:34 packer-builder-qemu plugin: [DEBUG] SSH handshake err: Timeout during SSH handshake

I tried using your resolution step but it is throwing same result.

Could you please let me know what additional steps you have followed to fix this issue.

Regards,
Prakash J

Hi Prakash,

Please share your json file details

Hi Pravin,

Please find the details:

``{
“variables”: {
“oracle_password”: “******”
},

“builders”: [
{
“accelerator”: “kvm”,
“vm_name”: “Oracle-packer.qcow2”,
“qemu_binary”: “/usr/libexec/qemu-kvm”,
“iso_urls”: [
“/iso/OracleLinux-R8-U7-x86_64-dvd.iso”
],
“iso_checksum”: “dd6ede6c0597189b7dffb800b32835002bd95f19c254734aeb58f58651aa03fb”,
“iso_target_path”: “iso”,
“format”: “qcow2”,
“output_directory”: “/test/output-oracle”,
“ssh_username”: “testuser1”,
“ssh_password”: “{{ user oracle_password }}”,
“ssh_wait_timeout”: “15m”,
“ssh_pty”: true,
“ssh_port”: 22,
“http_directory”: “http”,
“boot_command”: [
“ text ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/ks.cfg”
],
“boot_wait”: “40s”,
“shutdown_command”: “echo ‘{{ user oracle_password }}’ | sudo -S /sbin/halt -h -p”,
“type”: “qemu”,
“headless”: true,
“memory”: 4096,
“cpus”: 4
}
]
}``

Please change timeout from 15 min to 30min and also please change the boot command to the one that i have mentioned in this discussion and let me know if it is working or not?