Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the '10.5: Mount a partition or volume on demand' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.5: Mount a partition or volume on demand
Authored by: Ram-Z on Aug 12, '09 11:30:45AM

Very nice hint thx.

but it didn't work for me at first, trying to disbable mouting of my bootcamp partition. the comand:

dev_id=`diskutil list | awk '/Windows/{ print $6; }'`

didn't return the device id but the size of the partition...

I had to change it to:

dev_id=`diskutil list | awk '/Windows/{ print $8; }'`

This was because

diskutil list

returned

#: TYPE NAME SIZE IDENTIFIER
...
3: Microsoft Basic Data Windows 48.9 Gi disk0s3
...

So a little tinkering might be necessary depending on filesystem type.



[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: tedw on Aug 12, '09 12:53:48PM
ah, I see. I'd assumed it was computer-readable output when in fact it's only human-readable. since it seems to be a fixed width layout, however, you can try this:
dev_id=`diskutil list | awk '/Windows/{ print substr($0,69); }'`
or if you want to be tricky, this:
dev_id=`diskutil list | awk '/Windows/{ match( $0, /disk[0-9]+(s[0-9]+)?/ ); print substr( $0, RSTART, RLENGTH); }'`
which ought to extract the correct identifier without relying on a (non-existent) field structure. sorry about that.

[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: Ram-Z on Aug 12, '09 01:27:20PM

This matches the correct column but also matches another partition. But this is because of the LABEL of my partition:

3: Microsoft Basic Data Windows 48.9 Gi disk0s3
...
1: Windows_NTFS Evolution 3.8 Gi disk2s1

It maches the first line correctly and the second because of the TYPE.
I'm not that familiar with awk regex but it should be able to match on word boundary like this in perl:

/bWindowsb/

But my workaround by selecting the 8th column works fine, so i'll be using this for now...

Thanks

[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: tedw on Aug 12, '09 01:35:37PM

yeah, sjk caught this kind of problem in the post below yours. I posted a workaround there, and either he or I (or someone else) will think of something better in a bit.



[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: sjk on Aug 12, '09 02:15:01PM
sjk caught this kind of problem in the post below yours
Thanks for noticing; I can't the last time someone acknowledged one of my comments on this site. Usually I arrive too late to the party, after most everyone else has moved on. :)

I didn't see your reply to Ram-Z with the non-existent field structure examples 'til after I'd posted. Glad we're all understanding the issue. I'm busy with other things for awhile so it's doubtful I'll be the one to think of something better in a bit. :)

I'll check back here later. Hmm, just realized subscribing to comment RSS feeds for specific articles in Apple Mail is a reasonable workaround for lack of e-mail notifications and could likely be my preferred method to monitor for article/comment followups on this site.

[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: sjk on Aug 12, '09 01:17:26PM
diskutil list
returned

#: TYPE NAME SIZE IDENTIFIER 
... 
3: Microsoft Basic Data Windows 48.9 Gi disk0s3
...
So a little tinkering might be necessary depending on filesystem type.
It can also be necessary to consider whitespace in partition/volume names under the NAME field, e.g. Macintosh HD for this line:
   2:                  Apple_HFS Macintosh HD            54.9 Gi    disk0s3
And you have to be careful matching names that have the same pre-whitespace prefix. For example, with Boot, Boot i386, Boot ppc volume names the awk '/Boot/…' command will match all three when the intention may be to match Boot.

I consider my workarounds for whitespace issues too icky for posting here. Eventually I'll write (or find) a nicer diskutil list output parser/matcher to handle them.

[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: tedw on Aug 12, '09 01:31:23PM
And you have to be careful matching names that have the same pre-whitespace prefix. For example, with Boot, Boot i386, Boot ppc volume names the awk '/Boot/…' command will match all three when the intention may be to match Boot.
I hadn't considered this, either, but since it's regular expressions it's not too difficult to resolve. if you have multiple volumes with similar names (like your boot, boot i3686, boot ppc) all you need to do is space-pad the regexp; i.e. use /boot / or /boot {3,}/ with a large enough number of spaces to exclude other names. diskutil will always output with space padding (so you don't need to worry about other whitespace), and the regexp won't match shorter names, so this should do the trick.

good point, thank you.

[ Reply to This | # ]

10.5: Mount a partition or volume on demand
Authored by: sjk on Aug 12, '09 02:18:30PM

That's my thinking on it, too. I'm just not code-savvy enough to quickly bang out a clean script. :)



[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: Ram-Z on Aug 12, '09 02:09:59PM
So this is what i got:

dev_id=`diskutil list | awk '/\\yPARTION_NAME\\y/{ match( $0, /disk[0-9]+(s[0-9]+)?/ ); print substr( $0, RSTART, RLENGTH); }'` && echo $dev_id
It does the trick nicely for me. I just added a word boundary around the partition name e.g. \y...\y

I don't have any partition with spaces but it might work as well.

[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: Ram-Z on Aug 12, '09 02:14:23PM
The && echo... is of course not necessary... I was just using for tests purposes.
The bash script would look like this:

#! /bin/bash
dev_id=`diskutil list | awk '/\\yPARTION_NAME\\y/{ match( $0, /disk[0-9]+(s[0-9]+)?/ ); print substr( $0, RSTART, RLENGTH); }'`
[[ $dev_id ]] && diskutil mount $dev_id


[ Reply to This | # ]
10.5: Mount a partition or volume on demand
Authored by: tedw on Aug 12, '09 02:47:54PM
So this is what i got: dev_id=`diskutil list | awk '/\yPARTION_NAME\y/{ match( $0, /disk[0-9]+(s[0-9]+)?/ ); print substr( $0, RSTART, RLENGTH); }'` && echo $dev_id It does the trick nicely for me. I just added a word boundary around the partition name e.g. y...y I don't have any partition with spaces but it might work as well.
I puddled with it a bit and decided to drop awk altogether - ended up with this:
dev_id=`diskutil list | egrep '.{33}PARTION_NAME {2,}' | egrep -o 'disk[0-9]+(s[0-9]+)?'`
the first grep returns a line where the partition name is in the name column and has at least two spaces following; the second grep digs out the actual device name from that line. not very elegant, though...

is there a way to tell awk to use fixed width fields rather than delimiters? apparently there is supposed to be a FIELDWIDTHS variable that can be set, but I can't seem to get it to work in Darwin.

[ Reply to This | # ]

10.5: Mount a partition or volume on demand
Authored by: rduke15 on Nov 08, '10 11:13:31PM
I had to change it to: dev_id=`diskutil list | awk '/Windows/{ print $8; }'`
This seems very fragile. If the disk is always the last word on the line, use $NF (Number of Fields) instead of a fixed position:
dev_id=`diskutil list | awk '/Windows/{ print $NF; }'` 


[ Reply to This | # ]