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