简体   繁体   中英

Command not fully executing under ssh.net in c#

I'm taking the string generated by constructVirtBuilderCommand and using it as a command needed for my server to generate an image in xen. However, the command returns multiple things as it informs you of the multiple steps executed within libvirt in the process of downloading the image, the first (and only) thing returned is, "[ 2.5] Downloading: http://libguestfs.org/download/builder/ubuntu-18.04.xz " but that is just one line out of the entire output of:

[3.2] Downloading: http://libguestfs.org/download/builder/ubuntu-18.04.xz

[4.7] Planning how to build this image

[4.7] Uncompressing

[14.7] Resizing (using virt-resize) to expand the disk to 15.0G

[497.3] Installing firstboot command: reboot

[497.7] Setting passwords

[510.8] Finishing off

         Output file: test.img
         Output size: 15.0G
         Output format: raw
         Total usable space: 14.7G
         Free space: 13.2G (99%)

This seems to be causing an issue for me. In that the command isn't fully executed before returning. Has anyone had experience with this issue? No image is downloaded for me - and if it is it only exists for a moment and the file suddenly disappears as soon as the download of another image starts. I have tried a few things to force my client to wait for it finish downloading the image. The first being appending " | grep Finishing" to the end of the command. The second thing I tried was " > file.txt" to generate this file AFTER the command has fully executed, this didn't work as well.

Can anyone help me figure out how to get libvirt to fully execute this command? It works just fine if I manually enter it in on my server and via ssh. Thank you!

    private String constructVirtBuilderCommand()
    {
        String virt_builder_command = "sudo virt-builder ";
        virt_builder_command += operating_system_comboBox.SelectedItem.ToString();
        virt_builder_command += "-";
        virt_builder_command += operating_system_version_comboBox.SelectedItem.ToString();
        virt_builder_command += " --arch ";
        virt_builder_command += operating_system_architecture_comboBox.SelectedItem.ToString();
        virt_builder_command += " -o nodeand#.img --size ";
        virt_builder_command += storage_textBox.Text;
        virt_builder_command += "G --root-password password:toor --firstboot-command 'reboot'";
        return virt_builder_command;
    }



    private void add_new_cluster_button_Click(object sender, EventArgs e)
    {

        if (new_cluster_name_textBox.Text != "")
        {

            String virtbuilder = constructVirtBuilderCommand();
            String virtinstaller = constructVirtInstallCommand();
            for (int i=0;i<Int32.Parse(number_of_vms_textBox.Text);i++)
            {
                String tmp_virtbuilder_command = virtbuilder.Replace("nodeand#", new_cluster_name_textBox.Text + i.ToString());
                String tmp_virtbuilder_result = sshclient.CreateCommand(tmp_virtbuilder_command).Execute();
                Console.WriteLine(tmp_virtbuilder_result);      
            }
            label19.Text = "completed";
        }
        else
        {
            // some error message popup
        }
    }

EDIT:

1) Extending the timeout time seems to help make it further into the command. But the file downloaded is deleted at the next iteration of image download for some reason. If timeout is extended via: sshclient.ConnectionInfo.Timeout = TimeSpan.FromSeconds(1000); Then I am able to get to this point in the output:

[3.2] Downloading: http://libguestfs.org/download/builder/ubuntu-18.04.xz

[4.7] Planning how to build this image

[4.7] Uncompressing

[14.7] Resizing (using virt-resize) to expand the disk to 15.0G

Extending the timeout to a larger length of time is not needed as this all should execute pretty quickly. I just set it to 1000 seconds to be sure that this was not the main issue.

2) If you want to recreate the expected output enter: sudo virt-builder centos-7.1 --arch x86_64 -o testc0.img --size 15G --root-password password:toor --firstboot-command 'reboot'

into terminal

解决方案是使用sshclient.CreateShellStream(),以使ssh api不会仅仅因为在shell中执行未返回任何内容而返回。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM