Create usb flash drive from binary image in Qt4
-
Hi!
I need to create a kind of usb flash drive creator. The problem is that it must be multiplatform and have GUI. GUI is not the problem because I'm quite familiar with Qt4. Hovewer, I don't know how to create flash drives from image file.
On linux I could use dd for that (just invoke it with QProcess but still it's not the ideal solution) because it could make exact copy of binary file (image) to usb drive (e.g. /dev/sdc). How can I achieve that on my own using Qt4? I can't simple copy file by file because I want to create whole disk structure (partition, bootloader etc.). I hope I've explained well what I need.
The solution must work on Windows and MacOS. Linux users can use simple bash script that uses dd to achieve the same thing.
Thanks in advance for any help or tips.
-
Windows counterparts of whole-disks /dev/sda /dev/sdb (and so on) are "\.\PhysicalDrive0" "\.\PhysicalDrive1" and so on (mind the backslashes, you need to escape them gaining ridiculous \\.\Physical...).
Windows counterparts of partitions /dev/sda1 /dev/sda2 (and so on) are \.\C: \.\D: and so on (as long as partition is recognized by Windows).
Note, that Windows (AFAIK) allows accesses only by multiples of sector size in length and only from sector boundary (which is 512 bytes for most HDDs - however for same new multi-terabyte disks it might be 4kbytes). For whole-device access starting from 0 you get this for free :)
On Linux side you may access /dev/sd* just like plain files via QFile, so there's no need to use QProcess and indirectly dd - of course you need respective R / W rights to the device (just checked - works, Sample code follows). Probably the same applies to MacOS, but I don't know the respective device names. Probably on Windows you may access \.-ies the same way.
-- Sample code --
@#include <QtCore>
#include <QFile>int main(int argc, char *argv[]) {
#ifdef Q_WS_X11
QFile hdd("/dev/sda");
#endif#ifdef Q_WS_WIN
QFile hdd("\\.\PhysicalDrive0");
#endif#ifdef Q_WS_MACX
QFile hdd("/dev/disk1"); // I'm not sure
#endifif (!hdd.open(QIODevice::ReadOnly)) return 255; QByteArray d = hdd.read(512); // Linux allows non-512 mutiplies hdd.close(); QFile file("out.bin"); if (!file.open(QIODevice::WriteOnly)) return 255; file.write(d); file.close(); return 0;
}@
-- /Sample code --
HTH.
-
Thank you very much for the answer (or should I say 'dziękuję'?) :)
Your sample code shows how to write an image from flash drive. I want to achieve something opposite - write file image to flash drive. Hovewer, I think it's gonna look the same or it's gonna be very similar.
I suppose it's a lot easier with unix systems which recognise disks as normal files (to be more accurate, special type of files but still). I have no idea if it's possible to do that on Windows. I will try to use that solution and post the answer if it works or not!
-
Yay! Nie ma sprawy! ("No problem")
I just didn't want to trash my workstation's MBR and had no free USB drive laying around, so example is drive->file :)
Well, just swap the paths around. Read file, write device. For Linux it's OK as long as you have permission to write to the device. This should be OK for the other two systems, too.
-
This solution works perfect on Linux. Hovewer, there is a problem on Windows. I've successfully made image of pendrive and save it to file but I couldn't write anything to pendrive. It seems that the problem is with opening file with WriteOnly or ReadWrite flag:
[code]
QFile hdd("\\.\PhysicalDrive1");
hdd.open(QIODevice::WriteOnly); // this line returns false
[/code]I was using Windows Vista. Any ideas? Do I have to use WinAPI functions to get write access?
-
Try running the program with administrative credentials ("Run as administrator" or sth). As reading works, this may be simple ACL problem.
-
This post is deleted!