A script to eject and spin down external drives | 15 comments | Create New Account
Click here to return to the 'A script to eject and spin down external drives' hint |
The following comments are owned by whoever posted them. This site is not responsible for what they say.
I tried to get this to work, but all it generates is a error that says it doesn't recognize the name of the drive ('LaCie HD').
Since you have a space in the name of your external volume, you'll either have to quote the name or escape the space character by preceding it with a backslash character.
---
-Bob
---------------------
I tend to think of [Mac] OS X as Linux with QA and Taste.
-James Gosling, Java Architect
---
-Bob
---------------------
I tend to think of [Mac] OS X as Linux with QA and Taste.
-James Gosling, Java Architect
:) this little script (re)mounts any unmounted volumes !
(auto detected - so you dont have to set any volume name)
1. copy/paste the script (starting with #!/usr/bin/perl) to bbedit
2. save it as '/bin/mountall' (may need admin password)
3. type 'sudo chmod +x /bin/mountall' in the terminal
4. type 'rehash' in the terminal
now you can use it with 'mountall' in the terminal
for us who like to type instead of clicking ;)
zeorge 040406
[code]
#!/usr/bin/perl
# mountall [zeorge 04-2004]
@line_in = `disktool -l | grep volName`;
foreach $line (@line_in) {
$dev = extract($line,'Appeared (','');
$mp = extract($line,'Mountpoint = ','');
$vn = extract($line,'volName = ','');
if($mp eq ' && $vn ne ') {
print 'mounting volume $vn ($dev) .. ';
print `diskutil mount $dev`;
}
}
sub extract {
my $string = shift;
my $start = shift;
my $end = shift;
my $pos = index($string,$start) + length($start);
$string = substr($string,$pos);
$pos = index($string,$end);
return substr($string,0,$pos);
}
[/code]
(auto detected - so you dont have to set any volume name)
1. copy/paste the script (starting with #!/usr/bin/perl) to bbedit
2. save it as '/bin/mountall' (may need admin password)
3. type 'sudo chmod +x /bin/mountall' in the terminal
4. type 'rehash' in the terminal
now you can use it with 'mountall' in the terminal
for us who like to type instead of clicking ;)
zeorge 040406
[code]
#!/usr/bin/perl
# mountall [zeorge 04-2004]
@line_in = `disktool -l | grep volName`;
foreach $line (@line_in) {
$dev = extract($line,'Appeared (','');
$mp = extract($line,'Mountpoint = ','');
$vn = extract($line,'volName = ','');
if($mp eq ' && $vn ne ') {
print 'mounting volume $vn ($dev) .. ';
print `diskutil mount $dev`;
}
}
sub extract {
my $string = shift;
my $start = shift;
my $end = shift;
my $pos = index($string,$start) + length($start);
$string = substr($string,$pos);
$pos = index($string,$end);
return substr($string,0,$pos);
}
[/code]
I type diskutil list in my terminal and this shows up /dev/disk0 (internal): #: TYPE NAME SIZE IDENTIFIER 0: GUIDpartitionscheme 121.3 GB disk0 1: EFI EFI 314.6 MB disk0s1 2: AppleCoreStorage Macintosh HD 91.0 GB disk0s2 3: AppleBoot Recovery HD 650.0 MB disk0s3 4: EFI 29.3 GB disk0s4 /dev/disk1 (internal, virtual): #: TYPE NAME SIZE. How to Eject a Drive Safely on Mac? To eject your external storage drive safely on your Mac, perform any one the following: Drag-and-drop the external storage drive icon from Desktop to Trash. Open Finder from Dock and click the eject icon beside the external storage drive. This method will simply eject the drive so you can remove it safely from the USB port. Wrap Up It's always a good idea to safely eject a USB drive or an SD card regardless you are using a Mac, PC.
My external FW drives always spin down when unmounted. I can quickly and easily mount them again using the freeware 'Mount Me!' script. (See: http://www.versiontracker.com/dyn/moreinfo/macosx/15077.---
simsamsep
simsamsep
This is a great little script! I had been using Disk Utility to mount / unmount my FW hard drive, but this method (like you said) does not keep the drive from spinning, sometimes for days between backups. I had resigned myself to the irritating plug/unplug method until now. Thanks a lot!
I use this command to remount my external FireWire hard drive:
diskutil mount /dev/disk1
I have it aliased to 'fwmount'. I suspect that it would not work if you have multiple external drives, though.
-Mark
Is there a way to make the remount script work with an iPod?
Here's a perl script to mount and unmount by volume name
This script combines a couple of the great ideas above into one perl script that will mount and unmount a drive based on the that name that it appears as on the desktop (i.e. no /dev stuff). It also uses touch to write a file before unmounting.
Like the others, I use it to mount my firewire drive before doing a backup, and then I unmount it so it'll spin down.
----------
Usage:
Mount a drive:
hd -m driveName
Unmount a drive:
hd -u driveName
-------------
To use this on your machine (just like the other perl example above)
1. Copy the text into a new text file called 'hd'
2. >chmod +x hd
3. Copy the file somewhere into your path
4. >rehash
--------------------------------- Perl script starts at next line:<code>
#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Std;
our $OPTIONS = {};
getopts('mu',$OPTIONS);
our $vol_name = $ARGV[0] || 'zbackup';
our $vol_regex = 'volNames*=s*'$vol_name';
our @entries = grep(/$vol_regex/o, `disktool -l`);
if (! @entries) {
die 'No such volume '$vol_name';
} elsif ($#entries > 0) {
warn('More than one mountable partition matching $vol_name, mounting the first one.');
}
if ($entries[0] =~ m|('([a-zA-Z0-9]+)'s*,s*Mountpoints*=s*'(.*?)'s*,s*fsTypes*=s*'(S*?)'|) {
my $disk = $1;
my $mount = $2;
my $fs = $3;
if ($OPTIONS->{u}) {
if ($mount) {
`touch $mount/.dummy`;
`disktool -u $disk`;
}
} elsif (! $mount) { #if you pass no flags it'll also mount
`disktool -m $disk`;
}
} else {
die('Entry did not match expectations: $entries[0]');
}
exit;
__END__
</code>
Like the others, I use it to mount my firewire drive before doing a backup, and then I unmount it so it'll spin down.
----------
Usage:
Mount a drive:
hd -m driveName
Unmount a drive:
hd -u driveName
-------------
To use this on your machine (just like the other perl example above)
1. Copy the text into a new text file called 'hd'
2. >chmod +x hd
3. Copy the file somewhere into your path
4. >rehash
--------------------------------- Perl script starts at next line:<code>
#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Std;
our $OPTIONS = {};
getopts('mu',$OPTIONS);
our $vol_name = $ARGV[0] || 'zbackup';
our $vol_regex = 'volNames*=s*'$vol_name';
our @entries = grep(/$vol_regex/o, `disktool -l`);
if (! @entries) {
die 'No such volume '$vol_name';
} elsif ($#entries > 0) {
warn('More than one mountable partition matching $vol_name, mounting the first one.');
}
if ($entries[0] =~ m|('([a-zA-Z0-9]+)'s*,s*Mountpoints*=s*'(.*?)'s*,s*fsTypes*=s*'(S*?)'|) {
my $disk = $1;
my $mount = $2;
my $fs = $3;
if ($OPTIONS->{u}) {
if ($mount) {
`touch $mount/.dummy`;
`disktool -u $disk`;
}
} elsif (! $mount) { #if you pass no flags it'll also mount
`disktool -m $disk`;
}
} else {
die('Entry did not match expectations: $entries[0]');
}
exit;
__END__
</code>
This script apparently does not work if the external drive is partitioned.
Need help to eject and mount partitioned firewire drives
I have an external firewire drive with two partitions. Normally I can eject or mount them from terminal like this:
-disktool -l ( this lists all their device numbers)
-disktool -m disk2s10 disk2s9 (mounts both partitions)
I want to use this shell script except for both devices but can't figure out the syntax to insert the space between the device numbers:
do shell script 'disktool -m `disktool -l | grep 'Partition 1' | awk '{ print substr($0,20,7) }'` '
I need to stick this part after a space for the second device:
`disktool -l | grep 'Partition 2' | awk '{ print substr($0,20,8) }'`
Notice that I change the number of characters in the substring to 8 because I know the device number is disk2s10. (8 characters) I know that's poor form and would like to look for the ' in the string to know when I'm at the end of the device name but I'm not that good a programmer. Can someone who is help me out, or at least point me to a good UNIX reference place.
Thanks
---
Istatistica advanced system monitor 4 6 1. When I die, I want to go peacefully in my sleep, like my Grandfather, not screaming in terror like his passengers.
-disktool -l ( this lists all their device numbers)
-disktool -m disk2s10 disk2s9 (mounts both partitions)
I want to use this shell script except for both devices but can't figure out the syntax to insert the space between the device numbers:
do shell script 'disktool -m `disktool -l | grep 'Partition 1' | awk '{ print substr($0,20,7) }'` '
I need to stick this part after a space for the second device:
`disktool -l | grep 'Partition 2' | awk '{ print substr($0,20,8) }'`
Notice that I change the number of characters in the substring to 8 because I know the device number is disk2s10. (8 characters) I know that's poor form and would like to look for the ' in the string to know when I'm at the end of the device name but I'm not that good a programmer. Can someone who is help me out, or at least point me to a good UNIX reference place.
Thanks
---
Istatistica advanced system monitor 4 6 1. When I die, I want to go peacefully in my sleep, like my Grandfather, not screaming in terror like his passengers.
A script to eject and spin down external drives: Modified
I modified the AppleScript to only grep for volName The result is that it now works for both volumes that have user defined mountpoints and Volumes that have spaces in there names.
Also there is only one thing you have to edit .
And there are now error messages.
########################################### ###########################################
Also there is only one thing you have to edit .
And there are now error messages.
########################################### ###########################################
A script to eject and spin down external drives: Modified
Is there any way to spin down external firewire drives that are partitioned? The scripts I see all require single volume names..
Any way to make this work with partitioned drives? I usually unplug the thing to make it shut-up, but it'd be nicer if it just spun-down.
Same problem here..
I just copy pasted the string and that way I can make it un and remount both volumes, but it won't actually spin down.
I just copy pasted the string and that way I can make it un and remount both volumes, but it won't actually spin down.
Mac Terminal Eject External Hard Drives
For anyone referring back to this Hint, disktool is deprecated in Tiger.
---
Mark Moorcroft
ELORET Corp. - NASA/Ames RC
Sys. Admin.
---
Mark Moorcroft
ELORET Corp. - NASA/Ames RC
Sys. Admin.
Sometimes OS X doesn’t want to let you eject a volume or an external drive. Sometimes this is because a program is using a file on the volume in question, but other times the volume just seems to be stuck! Fortunately, there’s an easy solution! We’ll show you two ways to force your Mac to let go!
Method 1
The easiest and safest way to force a volume to eject is to open a new Finder window, locate the drive or volume in the left-hand column, right-click or option-click the volume, and select Eject “[Volume Name]” in the drop down menu.
You may see a message like the one above, telling you that there may be a program using that volume. If you’re sure that’s not the case, then click “Force Eject…”, and Voila! That’s all there is to it!
Method 2
For those of you that prefer using Terminal, or would rather eject the drive with a single command, simply open Terminal, and type the following:
Unzip 7z file mac command line. diskutil unmountDisk force /Volumes/DISK_NAME
Just replace DISK_NAME with the name of the volume you want to eject, press enter, and you’re done!
Macbook External Hard Drive
I’ve found that the first method is much more successful – I’ve had the disk fail to unmount more than once using the Terminal command.
Seagate External Hard Drive Mac
Note: Make sure you have closed all applications that may be using the volume you want to eject, and that the volume is not in use. Otherwise, you risk facing data corruption or even data loss! And, as always, use Terminal commands at your own risk!