This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: associating volume labels with drive letters


On 14/09/2012, at 12:35 AM, Nellis, Kenneth wrote:

> From: Corinna Vinschen
> On Jul 29 15:34, Schwarz, Konrad wrote:
>>>>> Can you answer the following question:
>>>>> 
>>>>> Given a volume label, how does one figure out where the 
>>>> corresponding 
>>>>> volume has been mounted into the Cygwin namespace?
>>>> 
> <snip>
>>> In Linux, you can look up the mount point for device /dev/sdXY
>>> in /proc/mounts or in the output of mount(8).  Thus, given
>>> a volume label, you can figure out where to access the files
>>> on the volume.
>>> 
>>> How do you do that in Cygwin?
>> 
>> ls /cygdrive.  Insert the disk.  ls /cygdrive again.  There's a new
>> directory entry now.
>> 
>> Or, open the "Computer" Window on your desktop.
> 
> Dredging up an old thread
> (http://sourceware.org/ml/cygwin/2011-07/msg00390.html)...
> 
> Regarding Corinna's last suggestion, it seems to me that if 
> Windows can associate a volume label with a drive letter, 
> there must be a way to script this association, whether 
> using Cygwin shell commands directly or indirectly through 
> DOS commands.
> 
> Using blkid(8), from the util-linux package, I can get a device 
> name from a label:
> 
> $ blkid -L CRUZER
> /dev/sdc1
> 
> Now, if I'm on the right path, how can I associate that 
> block-special file with a drive letter or a /cygdrive/x 
> path reference?
> 
> --Ken Nellis

Hi,

Here is a perl script which I believe should do the trick.

It adds:  (for example)
  DRIVE="C:"
to the end of the blkid output where the drive letter matches the correct drive location.

Along the same lines I've also played around with a customised version of cygwin that allows the mount
command to use UUID's to identify the drive letter instead of hard coding into fstab.  This would be useful
if you wanted specific drives to be mounted to specific locations without dependence on the drive letter.

I haven't extensively tested this, just quickly put it together to give you the idea.

------------ cut -------------
#!/usr/bin/perl
 
use strict;
use Cwd 'abs_path';
 
# no buffer
$| = 1;
 
my %drives;
for (my $i = 0; $i < 26; $i++)
{
                my $chr = chr(ord('A') + $i);
                my $str = sprintf ("/proc/sys/GLOBAL\?\?/$chr:");
                if (-l $str)
                {
                                my $path = abs_path ($str);
                                $drives{"$path"} = "$chr";
                }
}
 
open (BLKID, "blkid |") or die "Failed: $!";
while (<BLKID>)
{
                chomp;
                my $line = $_;
                my $drive;
 
                # Print the blkid line.
                printf ("%s", $line);
 
                # Deal with harddisk case.
                if (s|(^/dev/sd)([a-z]*)([0-9]*):.*$|$1$2$3|)
                {
                                # Convert to cygwin device.
                                my $disk = ord(substr($2, 0, 1)) - ord('a');
                                if (length($2) == 2)
                                {
                                                $disk = ($disk + 1) * 26 + (ord(substr($2, 1, 1)) - ord('a'));
                                }
 
                                my $partition = ord(substr($3, 0, 1)) - ord('0');
                                if (length($3) == 2)
                                {
                                                $partition = ($partition * 10) + (ord(substr($3, 1, 1)) - ord('0'));
                                }
 
                                my $path = abs_path("/proc/sys/Device/harddisk${disk}/partition${partition}");
                                $drive = $drives{$path};
                                printf (" DRIVE=\"%s:\"\n", $drive);
                }
}
------------ cut -------------

Cheers,
Mark.

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]