USB pendrive : the copy takes about 3 minutes but the unmount takes a very long time : 10 to 12 minutes

7,158

Solution 1

You are probably suffering from buffering caching. To speed up writing to USB sticks (and hard disks in general), Linux uses a filesystem cache:

When you (think you) write something to the stick then it is first written to the cache (in RAM) and the cp command (for instance) returns immediately pretending a really fast write operation. While you do other things, the contents of the cache is then written to the stick in background. You may notice that an LED on the stick still flashes showing write operations (depends on your stick) although nothing apparent happens.

When you issue umount soon after a write operation, then umount waits until all the filesystem's cache content is written to the stick in order to make sure no data gets lost.

With sync you can manually force emptying the cache and writing the data to the stick. However, this won't speed up the total elapsed time because then you will have to wait for the sync to complete (instead of waiting for umount). But the umount will then return instantaneously because the cache is already flushed.

In summary you have three choices after copying large or many files to the stick:

  • umount and wait 10 minutes for it to complete
  • sync, wait 10 minutes to complete, followed by umount (will return almost immediately)
  • simply wait for 10 minutes (perhaps a bit more) and do nothing (or something unrelated to the stick) and then issue umount. Because the cache gets written in background automatically, umount will then return almost immediately as well.

Solution 2

When you copy files to your pendrive, they are not written on it directly. Filesystem synchronization is happening on unmount command, the actual data is written while you wait your unmount. If you execute sync before umount, the umount is instant.

Share:
7,158

Related videos on Youtube

SebMa
Author by

SebMa

Updated on September 18, 2022

Comments

  • SebMa
    SebMa almost 2 years

    I'm using Ubuntu 17.10.

    I formatted an USB pen-drive to NTFS to prepare a Windows7 USB Installer.

    I set the bootable flag on this pen-drive and copied the files into it.

    EDIT 1: The USB pen-drive is automatically mounted by udev.

    umount /dev/sdb1 takes from 10 upto 12 minutes to complete.

    Here are the mount options :

    $ mount | grep sdb
    /dev/sdb1 on /media/mansfeld/Win7_USB_Installer type fuseblk (rw,nosuid,nodev,relatime,user_id=0,group_id=0,default_permissions,allow_other,blksize=4096,uhelper=udisks2)
    

    EDIT 2: The cp operation is not INSTANTANEOUS at all, it took 3 minutes to copy the files to the USB pen-drive.

    EDIT 3: The sync operation (done right after the cp) took 12 minutes to complete ! But then the umount will be instantaneous.

    For FAT32, (with sync also disabled during mount), I notice the same behaviour.

    Any ideas why it takes so long to unmount NTFS USB pendrive ?

    • sudodus
      sudodus over 6 years
      How do you unmount it (which command or GUI action)? Did you try with the following command line? sudo umount /dev/sdb1; Maybe there are some buffered data, that must be written to the drive before it can be unmounted. You can flush the buffers with sync ; After sync has returned to prompt, unmounting should be fast.
    • SebMa
      SebMa over 6 years
      @sudodus The computer on which I ran this is at the university so I'll have to wait 'till monday to try and time the sync command.
    • sudodus
      sudodus over 6 years
      I noticed your edit. This is according to my experience. Copying takes some time (at first it is fast (writing to RAM), but after a while, it must flush the buffer (when it is full or almost full according to some rule), and the copying will be slower). Yet, when the copy command finished (cpreturns to prompt), there are still lots of data in the buffer. If you run sync right after the cp command, and wait until sync returns to prompt, the unmount should be fast. Please try that :-)
    • sudodus
      sudodus over 6 years
      I think you know now :-)
  • SebMa
    SebMa over 6 years
    The computer on which I ran this is at the university so I'll have to wait 'till monday to try and time the sync command.
  • Pasi Suominen
    Pasi Suominen over 6 years
    Or mount the filesystem with -o sync flag enabled, that disables write caching.
  • PerlDuck
    PerlDuck over 6 years
    Yes, @Pasi, that's another reasonable approach. Thank you for pointing that out. But then the "10 minutes" will kick in when you actually cp the files. It just depends on what behaviour you prefer. Waiting for cp or waiting for umount.
  • SebMa
    SebMa over 6 years
    @PasiSuominen How can I have udev mount my USB pen-drive automatically with the sync option enabled ?
  • Pasi Suominen
    Pasi Suominen over 6 years
    /etc/udev/rules.d/ is your answer. In udev rule you can specify mount parameters.
  • SebMa
    SebMa over 6 years
    @PasiSuominen On ubuntu server, this dir is empty, what else can I do ?
  • Pasi Suominen
    Pasi Suominen over 6 years
    You can specify your device on /etc/fstab
  • Owl
    Owl almost 5 years
    I'll have to wait till monday for the sync to finish!