<p>I'm writing an application that on some stage performs low-level disk operations in Linux environment. The app actually consists of 2 parts, one runs on Windows and interacts with a user and another is a linux part that runs from a LiveCD. User makes a choice of Windows drive letters and then a linux part performs actions with corresponding partitions. The problem is finding a match between a Windows drive letter (like C:) and a linux device name (like /dev/sda1). This is my current solution that I rate as ugly:</p>

<ul>
<li><p>store partitions information (i.e. drive letter, number of blocks, drive serial number etc.) in Windows in some pre-defined place (i.e. the root of the system partition).</p></li>
<li><p>read a list of partitions from /proc/partitions. Get only those partitions that has major number for SCSI or IDE hard drives and minor number that identifies them as real partitions and not the whole disks.</p></li>
<li><p>Try to mount each of them with either ntfs or vfat file systems. Check whether the mounted partition contains the information stored by Windows app.</p></li>
<li><p>Upon finding the required information written by the Windows app make the actual match. For each partition found in /proc/partitions acquire drive serial number (via HDIO_GET_IDENTITY syscall), number of blocks (from /proc/partitions) and drive offset (/sys/blocks/drive_path/partition_name/start), compare this to the Windows information and if this matches - store a Windows drive letter along with a linux device name. </p></li>
</ul>

<p>There are a couple of problems in this scheme:</p>

<ul>
<li><p>This is ugly. Writing data in Windows and then reading it in Linux makes testing a nightmare.</p></li>
<li><p>linux device major number is compared only with IDE or SCSI devices. This would probably fail, i.e. on USB or FireWire disks. It's possible to add these types of disks, but limiting the app to only known subset of possible devices seems to be rather bad idea.</p></li>
<li><p>looks like HDIO_GET_IDENTITY works only on IDE and SATA drives.</p></li>
<li><p>/sys/block hack may not work on other than IDE or SATA drives.</p></li>
</ul>

<p>Any ideas on how to improve this schema? Perhaps there is another way to determine windows names without writing all the data in windows app?</p>

<p>P.S. The language of the app is C++. I can't change this.</p>

[{'answer_id': 18975, 'author': 'SCdF', 'author_id': 1666, 'author_profile': 'https://Stackoverflow.com/users/1666', 'pm_score': 2, 'selected': True, 'text': "<p>Partitions have UUIDs associated with them. I don't know how to find these in Windows but in linux you can find the UUID for each partition with:</p>\n\n<blockquote>\n  <p>sudo vol_id -u device (e.g. /dev/sda1)</p>\n</blockquote>\n\n<p>If there is an equivilent function in Windows you could simply store the UUIDs for whatever partition they pick then iterate through all known partitions in linux and match the UUIDs.</p>\n\n<p><strong>Edit:</strong> This may be a linux-only thing, and it may speficially be the volid util that generates these from something (instead of reading off meta-data for the drive). Having said that, there is nothing stopping you getting the source for volid and checking out what it does.</p>\n"}, {'answer_id': 19033, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 2, 'selected': False, 'text': '<blockquote>\n  <p>Partitions have UUIDs associated with them</p>\n</blockquote>\n\n<p>My knowledge of this is very shallow, but I thought that was only true for disks formatted with GPT (Guid Partition Table) partitions, rather than the old-style MBR format which 99% of the world is still stuck with?</p>\n'}, {'answer_id': 19156, 'author': 'SCdF', 'author_id': 1666, 'author_profile': 'https://Stackoverflow.com/users/1666', 'pm_score': 1, 'selected': False, 'text': '<blockquote>\n  <p>My knowledge of this is very shallow,\n  but I thought that was only true for\n  disks formatted with GPT (Guid\n  Partition Table) partitions, rather\n  than the old-style MBR format which\n  99% of the world is still stuck with?</p>\n</blockquote>\n\n<p>Not to sounds like a linux user cliche but it Works For Me.. I use it with NTFS partitions and have had no problems. As I said in my edit, vol_id may be generating them itself. If that were the case there would be no reliance on any particular partition format, which would be swell.</p>\n'}, {'answer_id': 19169, 'author': 'Brad Gilbert', 'author_id': 1337, 'author_profile': 'https://Stackoverflow.com/users/1337', 'pm_score': 0, 'selected': False, 'text': "<p>You need to either mark the drive in some way (e.g. write a file etc.), or find some identifier that is only associated with that particular drive.</p>\n\n<p>It is very hard, almost impossible to figure out what letter Windows would assign to a particular drive partition, without actually running Windows. This is because Windows always associates the drive that it is run from with C:. Which could be any drive, if you have more than one operating system installed. Windows also allows you to choose what drive letter it will try first, for a specific partition, causing further problems.</p>\n\n<p>It would be a whole lot easier to do the GUI stuff inside Linux, than to try this mixed Window/Linux solution. I'm not say don't try it this way, what I am saying is there are very many possible pitfalls with this approach. I'm sure I don't even know about all of them.</p>\n\n<p>Another option would be to see if you could actually do the Linux part, inside of Windows. If you are a very good Windows programmer, you can actually get access to the raw file-system. There are probably just as many pitfalls with this approach, because Windows will be running while all of this is in operation.</p>\n\n<p>So to re-iterate I would see if you could do everything from within Linux, if you can. It's just a whole lot simpler in the long run.</p>\n"}, {'answer_id': 19450, 'author': 'alex', 'author_id': 2206, 'author_profile': 'https://Stackoverflow.com/users/2206', 'pm_score': 1, 'selected': False, 'text': "<blockquote>\n  <p>Partitions have UUIDs associated with them. I don't know how to find these in Windows but in linux you can find the UUID for each partition with:</p>\n  \n  <blockquote>\n    <p>sudo vol_id -u device (e.g. /dev/sda1)</p>\n  </blockquote>\n  \n  <p>If there is an equivilent function in Windows you could simply store the UUIDs for whatever partition they pick then iterate through all known partitions in linux and match the UUIDs.</p>\n</blockquote>\n\n<p>That's a good point, thank you! I've looked to the sources of vol_id (a part of the udev tarball) and it seems that for FAT(32) and NTFS it generates UUUD using the volume serial number that is read from the predefined location on the partition. Since I don't expect anything other then fat32 and ntfs I consider to use this information as a partition identifier.</p>\n"}, {'answer_id': 2194258, 'author': 'Bernhard', 'author_id': 265528, 'author_profile': 'https://Stackoverflow.com/users/265528', 'pm_score': 0, 'selected': False, 'text': '<p>In Windows you can read the "NTFS Volume Serial Number" which seams to match the UUID under Linux.</p>\n\n<p>Possibilities to get the "NTFS Volume Serial" from <strong>Windows</strong>:</p>\n\n<ul>\n<li><p>commandline since XP: <em>fsutil.exe fsinfo ntfsinfo C:</em></p></li>\n<li><p>under c++</p>\n\n<pre><code>HANDLE fileHandle = CreateFile(L"\\\\\\\\.\\\\C:", // or use syntax "\\\\?\\Volume{GUID}" \n                               GENERIC_READ,\n                               FILE_SHARE_READ|FILE_SHARE_WRITE,\n                               NULL,\n                               OPEN_EXISTING,\n                               NULL,\n                               NULL);\nDWORD i;\nNTFS_VOLUME_DATA_BUFFER ntfsInfo;\nDeviceIoControl(fileHandle, \n                FSCTL_GET_NTFS_VOLUME_DATA, \n                NULL, \n                0, \n                &amp;ntfsInfo,\n                sizeof(ntfsInfo), \n                &amp;i, \n                NULL));\ncout &lt;&lt; "UUID is " &lt;&lt; std::hex &lt;&lt; ntfsInfo.VolumeSerialNumber.HighPart &lt;&lt; std::hex &lt;&lt; ntfsInfo.VolumeSerialNumber.LowPart &lt;&lt; endl;\n</code></pre></li>\n</ul>\n\n<hr>\n\n<p>Possibilities to get the UUID under <strong>Linux</strong>:</p>\n\n<ul>\n<li>ls -l /dev/disk/by-uuid</li>\n<li>ls -l /dev/disk/by-label</li>\n<li>blkid /dev/sda1</li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/18959', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2206/']