Discussion:
Ejecting a USB drive using Cygwin (sync)?
Ronald Fischer
2018-01-23 11:14:17 UTC
Permalink
I'm looking for a command, which would allow me from a shell script to prepare the removal of a USB device (stick, external hard drive etc.). With other words, after issuing the command, I should be able to physically remove the USB device.

Can the `sync` command be used, for instance

sync -f /cygdrive/e

assuming that the USB device is on drive E:? The man page of *sync* is a bit vague in this respect. Or is there another Cygwin command which can be used for this purpose?

Ronald

--
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
Nem W Schlecht
2018-01-23 15:41:23 UTC
Permalink
Post by Ronald Fischer
I'm looking for a command, which would allow me from a shell script to prepare the removal of a USB device (stick, external hard drive etc.). With other words, after issuing the command, I should be able to physically remove the USB device.
Can the `sync` command be used, for instance
sync -f /cygdrive/e
sync is not quite what you want to do the eject (wouldn't hurt to do
so beforehand, but any good 'eject' program will do a sync for you).


There are several solutions listed here:
https://superuser.com/questions/443162/remove-usb-device-from-command-line

RemoveDrive and devcon look like the best bets for what you're looking for.
--
Nem W Schlecht
"Perl did the magic. I just waved the wand."

--
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
Andrey Repin
2018-01-23 18:41:56 UTC
Permalink
Greetings, Ronald Fischer!
Post by Ronald Fischer
I'm looking for a command, which would allow me from a shell script to
prepare the removal of a USB device (stick, external hard drive etc.).
Windows device disconnect option syncs the device.
Just find a tool that plays right.
Post by Ronald Fischer
With other words, after issuing the command, I should be able to physically
remove the USB device.
Can the `sync` command be used, for instance
sync -f /cygdrive/e
assuming that the USB device is on drive E:? The man page of *sync* is a
bit vague in this respect. Or is there another Cygwin command which can be
used for this purpose?
There's no "eject" implementation, to my knowledge.
Also, please keep in mind that ejecting drive vs. detaching a device is a
distinctive operation.
I.e. detaching a multi-mode card reader may leave system unable to access
newly inserted cards.
--
With best regards,
Andrey Repin
Tuesday, January 23, 2018 21:33:21

Sorry for my terrible english...


--
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
Brian Inglis
2018-01-23 21:13:24 UTC
Permalink
Post by Ronald Fischer
I'm looking for a command, which would allow me from a shell script to
prepare the removal of a USB device (stick, external hard drive etc.). With
other words, after issuing the command, I should be able to physically remove
the USB device.
Can the `sync` command be used, for instance
sync -f /cygdrive/e
assuming that the USB device is on drive E:? The man page of *sync* is a bit
vague in this respect. Or is there another Cygwin command which can be used
for this purpose?
Running sync as above, and umount are always advisable.
Some ejection utilities detach the device, leaving it unusable until the device
is attached by forcing enumeration, or the system restarted.
I found the following utility works well without elevation - Windows code from
http://www.leapsecond.com/tools/eject.{c,exe}:

// eject -- Allow safe removal of USB thumb drive.
// - Command line tool to flush/dismount/eject USB drive.
// - Simpler than mouse clicking through taskbar "Safely Remove Hardware" icon.
// - Usable in batch file scripts.
// 02-Nov-2012 Tom Van Baak (tvb) www.LeapSecond.com/tools
...
// Per MSDN, follow procedure for safe removal of USB drive.
int drive_eject (char *drive)
{
HANDLE hDev;
// Convert vintage DOS drive letter to weird Windows object pathname.
char path[10];
sprintf(path, "\\\\.\\%s", drive);
// Open (with write, but no lock) to flush pending writes.
DEV_OPEN(path, GENERIC_READ | GENERIC_WRITE);
FlushFileBuffers(hDev);
CloseHandle(hDev);
// Open (with read, and lock) to dismount and eject.
DEV_OPEN(path, GENERIC_READ);
DEV_IOCTL(FSCTL_LOCK_VOLUME);
DEV_IOCTL(FSCTL_DISMOUNT_VOLUME);
DEV_IOCTL(IOCTL_STORAGE_EJECT_MEDIA);
DEV_IOCTL(FSCTL_UNLOCK_VOLUME);
CloseHandle(hDev);
return 0;
}

Cygwin does not appear to use ioctls that perform the same Windows functions
except for floppy lock volume.
--
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

--
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
Steven Penny
2018-01-23 23:36:45 UTC
Permalink
Post by Brian Inglis
I found the following utility works well without elevation - Windows code from
// eject -- Allow safe removal of USB thumb drive.
// - Command line tool to flush/dismount/eject USB drive.
// - Simpler than mouse clicking through taskbar "Safely Remove Hardware" icon.
// - Usable in batch file scripts.
// 02-Nov-2012 Tom Van Baak (tvb) www.LeapSecond.com/tools
...
// Per MSDN, follow procedure for safe removal of USB drive.
int drive_eject (char *drive)
{
HANDLE hDev;
// Convert vintage DOS drive letter to weird Windows object pathname.
char path[10];
sprintf(path, "\\\\.\\%s", drive);
// Open (with write, but no lock) to flush pending writes.
DEV_OPEN(path, GENERIC_READ | GENERIC_WRITE);
FlushFileBuffers(hDev);
CloseHandle(hDev);
// Open (with read, and lock) to dismount and eject.
DEV_OPEN(path, GENERIC_READ);
DEV_IOCTL(FSCTL_LOCK_VOLUME);
DEV_IOCTL(FSCTL_DISMOUNT_VOLUME);
DEV_IOCTL(IOCTL_STORAGE_EJECT_MEDIA);
DEV_IOCTL(FSCTL_UNLOCK_VOLUME);
CloseHandle(hDev);
return 0;
}
No, this is not the code from that site. The code is similar, but I see you have
at least removed some empty lines. If you are going to do so, you need to
clearly present said changes to the community - something like "adapted from
http://..." or "modified from http://...", etc.

Software copyright - even open source license is serious, and you should give it
due respect.


--
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
Brian Inglis
2018-01-25 03:02:36 UTC
Permalink
Post by Steven Penny
Post by Brian Inglis
I found the following utility works well without elevation - Windows code from
// eject -- Allow safe removal of USB thumb drive.
// - Command line tool to flush/dismount/eject USB drive.
// - Simpler than mouse clicking through taskbar "Safely Remove Hardware" icon.
// - Usable in batch file scripts.
// 02-Nov-2012 Tom Van Baak (tvb) www.LeapSecond.com/tools
...
// Per MSDN, follow procedure for safe removal of USB drive.
int drive_eject (char *drive)
{
    HANDLE hDev;
    // Convert vintage DOS drive letter to weird Windows object pathname.
    char path[10];
    sprintf(path, "\\\\.\\%s", drive);
    // Open (with write, but no lock) to flush pending writes.
    DEV_OPEN(path, GENERIC_READ | GENERIC_WRITE);
    FlushFileBuffers(hDev);
    CloseHandle(hDev);
    // Open (with read, and lock) to dismount and eject.
    DEV_OPEN(path, GENERIC_READ);
    DEV_IOCTL(FSCTL_LOCK_VOLUME);
    DEV_IOCTL(FSCTL_DISMOUNT_VOLUME);
    DEV_IOCTL(IOCTL_STORAGE_EJECT_MEDIA);
    DEV_IOCTL(FSCTL_UNLOCK_VOLUME);
    CloseHandle(hDev);
    return 0;
}
No, this is not the code from that site. The code is similar, but I see you have
at least removed some empty lines. If you are going to do so, you need to
clearly present said changes to the community - something like "adapted from
http://..." or "modified from http://...", etc.
Software copyright - even open source license is serious, and you should give it
due respect.
I was providing information, that's why I said /from/, gave the link to the
original, and included the copyright, which does not include a link to the MSDN
article it mentions: checking any licences are left as an exercise for any
reader who wants to make use of the program or code other than for their own
private use.

What do you mean by "/even/ open source licence"?!
You sound like the early Bill Gates ;^>
--
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

--
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
David Allsopp
2018-01-25 09:58:39 UTC
Permalink
Post by Brian Inglis
Post by Steven Penny
Post by Brian Inglis
I found the following utility works well without elevation - Windows code from
// eject -- Allow safe removal of USB thumb drive.
// - Command line tool to flush/dismount/eject USB drive.
// - Simpler than mouse clicking through taskbar "Safely Remove Hardware" icon.
// - Usable in batch file scripts.
// 02-Nov-2012 Tom Van Baak (tvb) www.LeapSecond.com/tools ...
// Per MSDN, follow procedure for safe removal of USB drive.
int drive_eject (char *drive)
{
    HANDLE hDev;
    // Convert vintage DOS drive letter to weird Windows object pathname.
    char path[10];
    sprintf(path, "\\\\.\\%s", drive);
    // Open (with write, but no lock) to flush pending writes.
    DEV_OPEN(path, GENERIC_READ | GENERIC_WRITE);
    FlushFileBuffers(hDev);
    CloseHandle(hDev);
    // Open (with read, and lock) to dismount and eject.
    DEV_OPEN(path, GENERIC_READ);
    DEV_IOCTL(FSCTL_LOCK_VOLUME);
    DEV_IOCTL(FSCTL_DISMOUNT_VOLUME);
    DEV_IOCTL(IOCTL_STORAGE_EJECT_MEDIA);
    DEV_IOCTL(FSCTL_UNLOCK_VOLUME);
    CloseHandle(hDev);
    return 0;
}
No, this is not the code from that site. The code is similar, but I
see you have at least removed some empty lines. If you are going to do
so, you need to clearly present said changes to the community -
something like "adapted from http://..." or "modified from http://...",
etc.
Software copyright - even open source license is serious, and you
should give it due respect.
I was providing information, that's why I said /from/, gave the link to
the original, and included the copyright, which does not include a link
I expect it's referring to this article:
https://support.microsoft.com/en-us/help/165721.
There's another quite good article on it at
https://www.codeproject.com/Articles/259577/How-to-flush-a-storage-volumes-t
he-file-cache-lock
Post by Brian Inglis
checking any licences are left as an
exercise for any reader who wants to make use of the program or code
other than for their own private use.
What licence is the ellipsis you added to the copyright line released under?
;o)


--dra


--
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
Brian Inglis
2018-01-25 20:37:36 UTC
Permalink
Post by David Allsopp
What licence is the ellipsis you added to the copyright line released under?
;o)
Probably sourceware.org DWTFYW from link obfuscation ;^>
--
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

--
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
Doug Henderson
2018-01-25 21:42:52 UTC
Permalink
On 25 January 2018 at 13:37, Brian Inglis
Post by Brian Inglis
Post by David Allsopp
What licence is the ellipsis you added to the copyright line released under?
;o)
Probably sourceware.org DWTFYW from link obfuscation ;^>
If you compare the content of the referenced source file
[ http://www.leapsecond.com/tools/eject.c ]
and the snippet from the OP, it is clear that the ellipsis indicates
that several lines from the original source were removed. It would
have been clearer to have the ellipsis on a new line. There was no
copyright claim in the original source.

HTH
Doug
--
Doug Henderson, Calgary, Alberta, Canada - from gmail.com

--
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
Brian Inglis
2018-01-26 01:31:24 UTC
Permalink
Post by Doug Henderson
On 25 January 2018 at 13:37, Brian Inglis
Post by Brian Inglis
Post by David Allsopp
What licence is the ellipsis you added to the copyright line released
under? ;o)
Probably sourceware.org DWTFYW from link obfuscation ;^>
If you compare the content of the referenced source file
[ http://www.leapsecond.com/tools/eject.c ]
and the snippet from the OP, it is clear that the ellipsis indicates that
several lines from the original source were removed. It would have been
clearer to have the ellipsis on a new line. There was no copyright claim in
the original source.
In my OP on Tue and most subsequent responses on Wed, the ellipsis is clearly on
a separate line. Some later replies seem to have warped^Wwrapped it up on to the
previous line.

There is a date, name, and link, so when I make fair use of a quote I include
available credit, as publication establishes a copyright, regardless of any
claim. IANAL but neither has anyone posting about this topic claimed to be.
When that author TVB refers to his tools code, he sometimes uses the term free,
and I would consider that as a scientist, he expects his content would be
treated as public domain research tools and results, and credit be given.

As for removing blank lines, I mostly trim (and often rewrap) content, as some
lists require that.
--
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

--
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
Marco Atzeri
2018-01-25 12:45:42 UTC
Permalink
Post by Steven Penny
Post by Brian Inglis
I found the following utility works well without elevation - Windows code from
the original code location is reported
Post by Steven Penny
Post by Brian Inglis
// eject -- Allow safe removal of USB thumb drive.
// - Command line tool to flush/dismount/eject USB drive.
// - Simpler than mouse clicking through taskbar "Safely Remove Hardware" icon.
// - Usable in batch file scripts.
// 02-Nov-2012 Tom Van Baak (tvb) www.LeapSecond.com/tools
...
The proper copyright is reported, the "..."
are clearly showing an extract posted in line.
Post by Steven Penny
Post by Brian Inglis
// Per MSDN, follow procedure for safe removal of USB drive.
No, this is not the code from that site. The code is similar, but I see you have
at least removed some empty lines. If you are going to do so, you need to
clearly present said changes to the community - something like "adapted from
http://..." or "modified from http://...", etc.
Software copyright - even open source license is serious, and you should give it
due respect.
he was not providing a full functional code, not claiming ownership,
just highlighting the key calls.

It seems to me that you are barking to the wrong tree.

Marco

--
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
Loading...