From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 57155 invoked by alias); 23 Jan 2018 21:13:29 -0000 Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner@cygwin.com Mail-Followup-To: cygwin@cygwin.com Received: (qmail 57147 invoked by uid 89); 23 Jan 2018 21:13:28 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL,BAYES_00,KAM_LAZY_DOMAIN_SECURITY,RCVD_IN_DNSWL_LOW autolearn=no version=3.3.2 spammy=clicking, vintage, DOS, fischer X-HELO: smtp-out-no.shaw.ca Received: from smtp-out-no.shaw.ca (HELO smtp-out-no.shaw.ca) (64.59.134.12) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 23 Jan 2018 21:13:27 +0000 Received: from [192.168.1.100] ([24.64.240.204]) by shaw.ca with ESMTP id e5sieI74f7Gjae5sje8t0S; Tue, 23 Jan 2018 14:13:25 -0700 X-Authority-Analysis: v=2.3 cv=I59Luuog c=1 sm=1 tr=0 a=MVEHjbUiAHxQW0jfcDq5EA==:117 a=MVEHjbUiAHxQW0jfcDq5EA==:17 a=N659UExz7-8A:10 a=cYCMvJSPAAAA:8 a=T1n7gGMsGpWxAFVXtnMA:9 a=pILNOxqGKmIA:10 a=0cYAWHTMCJUZ_s7sn94O:22 Reply-To: Brian.Inglis@SystematicSw.ab.ca Subject: Re: Ejecting a USB drive using Cygwin (sync)? To: cygwin@cygwin.com References: <1516706057.3270332.1245015392.5FE803A3@webmail.messagingengine.com> From: Brian Inglis Message-ID: Date: Tue, 23 Jan 2018 21:13:00 -0000 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 MIME-Version: 1.0 In-Reply-To: <1516706057.3270332.1245015392.5FE803A3@webmail.messagingengine.com> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-CMAE-Envelope: MS4wfObFaZGwX1P7W3HvUIJIKwXCgpttkjm/KAYTi4Tq8/Cs7ifNj4HSN/Q0DG4BqxRhDEQL4InPbV05YxgsyfRxpDqMfeWSJdRmp+Lrlh5BO9WYuf469Ydr BTE8KFECih1P5CfKHKoX/1ggQQNPraekb2S928egJnRoUO4b60W7LenqsNa26ariw0dctTBdP9WeyA== X-IsSubscribed: yes X-SW-Source: 2018-01/txt/msg00236.txt.bz2 On 2018-01-23 04:14, Ronald Fischer wrote: > 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