3

I'm creating an application running on a debian OS using C# and Mono. My aim is to unpack an imagefile and transfer it to a target folder on a different harddrive. I'm trying to pass the command

"gunzip -c imagefile.img.gz | dd of=/dev/sda" 

to the OS in order to restore an imagefile to the local hard drive. I did some research and found here that it probably have to redirect the standardinput/output. The problem in my case is that the ouput is somewhere between 1-60 gigabyte and the answer suggests to read it into memory.

Up to now I ceated this method:

public bool RecoverImageDD(string imageFile, string targetDiskNr)
{
    string commandToExecute="";
    string arguments="";
    string imgExtension = Path.GetExtension(imageFile).ToLower();
    if (imgExtension == ".gz")
    {
        commandToExecute = $"gunzip";
        string par1 = "-c";
        string par3 = "| dd";
        string par4 = $"if={imageFile}";
        string par5 = $"of={targetDiskNr}";
        arguments = string.Join(" ", par1, imageFile, par3, par4, par5);
        // gunzip -c image.img.gz | dd of=/dev/sda
    }

    Process proc = new Process
    {
        StartInfo = new ProcessStartInfo(commandToExecute)
        {
            Arguments = arguments,
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        }
    };
    return true;
}

I expected this code to work as if I would enter the above command into the shell. Which would pipe the output of gunzip into dd and dd would write it into the specified target file.

What I get here as result is that everything simply ends up as output in the shell.

How can I pipe the output into 'dd'? preferably without redirecting the standard output and flood my memory.

Any help would be appreciated. This is my first question so please have patience with me. Please ask if I left something unclear

Salex
  • 31
  • 2
  • change these two `RedirectStandardOutput = true, UseShellExecute = false,` to be `RedirectStandardOutput = false, UseShellExecute = true,` – Ben Dec 09 '20 at 10:53
  • @Ben: Thanks a lot for the immediate answer. I'll give this a try. Unfortunately I need my linux hardware to do this but this hardware will be not available until tomorrow. Thanks, Alex – Salex Dec 09 '20 at 11:02
  • The commands you wanted to run are connected via pipe, which cannot easily be wrapped in C# with a single ˋProcess` object. You should use two objects and redirect output of the first to the input of the second. – Lex Li Dec 12 '20 at 16:24

0 Answers0