Difference between revisions of "USB storage devices"

From ArchWiki
Jump to: navigation, search
m (Maximizing performance: request merging with Maximizing Performance)
(47 intermediate revisions by 20 users not shown)
Line 1: Line 1:
 
[[Category:Storage]]
 
[[Category:Storage]]
{{i18n|USB Storage Devices}}
+
[[es:USB Storage Devices]]
 +
[[it:USB Storage Devices]]
 +
[[ru:USB Storage Devices]]
 +
This document describes how to use the popular USB memory sticks with Linux. However, it is also valid for other devices such as digital cameras that act as if they were just a USB storage device.
  
This document describes how to use the popular USB memory sticks with Linux. However, it is also valid for other devices such as digital cameras that act as if they were just a USB storage.
+
If you have an up-to-date system with the standard Arch kernel and a modern [[Desktop environment]] your device should just show up on your desktop, with no need to open a console.
  
== Mounting USB devices ==
+
== Auto-mounting with udisks ==
If you have an up-to-date system with the standard Arch kernel and a modern [[Desktop Environment]] your device should just show up on your desktop, no need to open a console.
+
  
Otherwise see sections below.
+
This is the easiest and most frequently used method. It is used by many [[Desktop environment|desktop environments]], but can be used separately too. See [[Udisks]] for details.
  
=== Auto-mounting with udev ===
+
== Manual mounting ==
See [[Udev#Auto mounting USB devices|Udev:Auto mounting USB devices]]
+
  
=== Manual mounting ===
+
{{Note|Before you decide that Arch Linux does not mount your USB device, be sure to check all available ports. Some ports might not share the same controller, preventing you from mounting the device.}}
{{Note|Before you decide that your Arch Linux does not mount your USB device, be sure to check all available ports. Since some might not share the same controller, and will then not be able to mount the device.}}
+
 
 +
=== Getting a kernel that supports usb_storage ===
  
==== Getting a kernel that supports usb_storage ====
 
 
If you do not use a custom-made kernel, you are ready to go, for all Arch Linux stock kernels are properly configured. If you do use a custom-made kernel, ensure it is compiled with SCSI-Support, SCSI-Disk-Support and usb_storage. If you use the latest [[udev]], you may just plug your device in and the system will automatically load all necessary kernel modules. Older releases of udev would need hotplug installed too. Otherwise, you can do the same thing manually:
 
If you do not use a custom-made kernel, you are ready to go, for all Arch Linux stock kernels are properly configured. If you do use a custom-made kernel, ensure it is compiled with SCSI-Support, SCSI-Disk-Support and usb_storage. If you use the latest [[udev]], you may just plug your device in and the system will automatically load all necessary kernel modules. Older releases of udev would need hotplug installed too. Otherwise, you can do the same thing manually:
  
Line 21: Line 22:
 
  # modprobe sd_mod      (only for non SCSI kernels)
 
  # modprobe sd_mod      (only for non SCSI kernels)
  
==== Identifying device ====
+
{{Tip|In case of manually loading modules, you may also need to load the {{ic|sg}} module (SCSI generic driver).}}
First thing one need to access storage device is it's identifier assigned by kernel.
+
  
===== Using device node names ( /sd* ) =====
+
=== Identifying device ===
This is the simplest way, but assigned name depends on order of insertion. Ways to get node name:
+
  
* search in the output of {{Ic|dmesg}} for the kernel device name, you can use {{Ic|grep}} to help you find what you are looking for:
+
First thing one need to access storage device is it's identifier assigned by kernel. See [[fstab#Identifying filesystems]] for details.
$ dmesg | egrep "sd[a-z]"
+
* running
+
# fdisk -l
+
lists all available partition tables.
+
  
{{Note|If you cannot find your device you can use lsusb to verify that it is indeed recognized by the system.}}
+
{{Tip|To see which device is your USB device, you can compare the output of {{ic|lsblk -f}} (explained in the linked article) when the USB device is connected and when it is unconnected.}}
  
===== Using UUID =====
+
=== Mounting USB memory ===
Every drive creates a [[Wikipedia:UUID|UUID]] (Universally Unique Identifier), these identifiers can be used to track individual drive no matter their device node (ie {{Ic|/dev/sda}}).
+
  
To find the current UUIDs execute:
+
{{Merge|fstab#Writing to FAT32 as Normal User|This section assumes that the partition on USB storage uses FAT32 or NTFS filesystem, so we have two sections covering the same topic. Either merge everything here or in the linked section.}}
{{hc|# blkid -o list -c /dev/null|<nowiki>
+
device        fs_type  label    mount point        UUID
+
------------------------------------------------------------------------------------------
+
/dev/sda1      ext2              /boot              7f4cef7e-7ee2-489a-b759-d52ba23b692c
+
/dev/sda2      swap              (not mounted)      a807fff3-e89f-46d0-ab17-9b7ad3efa7b5
+
/dev/sda3      ext4              /                  81917291-fd1a-4ffe-b95f-61c05cfba76f
+
/dev/sda4      ext4              /home              c4c23598-19fb-4562-892b-6fb18a09c7d3
+
/dev/sdb1      ext4    X2        /mnt/X1            4bf265f7-da17-4575-8758-acd40885617b
+
/dev/sdc1      ext4    X1        /mnt/X2            4bf265f7-da17-4575-8758-acd40885617b
+
/dev/sdd1      ext4    Y2        /mnt/Y2            8a976a06-3e56-476f-b73a-ea3cad41d915
+
/dev/sde1      ext4    Z2        /mnt/Z2            9d35eaae-983f-4eba-abc9-434ecd4da09c
+
/dev/sdf1      ext4    Y1        /mnt/Y1            e2ec37a9-0689-46a8-a07b-0609ce2b7ea2
+
/dev/sdg1      ext4    Z1        /mnt/Z1            9fa239c1-720f-42e0-8aed-39cf53a743ed
+
/dev/sdj1      ext4    RAPT      (not mounted)      a9ed7ecb-96ce-40fe-92fa-e07a532ed157
+
/dev/sdj2      swap              <swap>            20826c74-eb6d-46f8-84d8-69b933a4bf3f
+
</nowiki>}}
+
  
'' At this point you should see a list of your system drives and a long strings of characters. These long strings are the uuids.''
 
 
* Now connect your USB device and wait for a few seconds . . .
 
 
* Reexecute {{Ic|blkid -o list -c /dev/null}}
 
'' Notice a new device and UUID? That is your USB storage''
 
 
{{Tip| If {{Ic|blkid}} does not work as expected, You can look for the UUIDs in {{Ic|/dev/disk/by-uuid/}}:
 
 
$ ls -lF /dev/disk/by-uuid/
 
 
}}
 
 
==== Mounting USB memory ====
 
 
You need to create the directory in which you are going to mount the device:
 
You need to create the directory in which you are going to mount the device:
 
  # mkdir /mnt/usbstick
 
  # mkdir /mnt/usbstick
  
===== As root =====
+
==== As root ====
 +
 
 
Mount the device as root with this command (do not forget to replace '''device_node''' by the path you found):
 
Mount the device as root with this command (do not forget to replace '''device_node''' by the path you found):
 
  # mount '''device_node''' /mnt/usbstick
 
  # mount '''device_node''' /mnt/usbstick
Line 79: Line 44:
 
  # mount -U '''UUID''' /mnt/usbstick
 
  # mount -U '''UUID''' /mnt/usbstick
  
If {{Ic|mount}} does not recognized the format of the device you can try to use the {{Ic|-t}} argument, see {{Ic|man mount}} for details.
+
If {{ic|mount}} does not recognize the format of the device you can try to use the {{ic|-t}} argument, see {{ic|man mount}} for details.
  
 
{{Note|If mounting your stick does not work you can try to repartition it, see [[Format a device]].}}
 
{{Note|If mounting your stick does not work you can try to repartition it, see [[Format a device]].}}
  
===== As normal user with mount =====
+
==== As normal user with mount ====
 +
 
 
If you want non-root users to be able to write to the USB stick, you can issue the following command:
 
If you want non-root users to be able to write to the USB stick, you can issue the following command:
  
  $ sudo mount -o gid=users,fmask=113,dmask=002 /dev/sda1 /mnt/usbstick
+
  # mount -o gid=users,fmask=113,dmask=002 /dev/sda1 /mnt/usbstick
 +
 
 +
==== As normal user with fstab ====
  
===== As normal user with fstab =====
 
 
If you want non-root users to be able to mount a USB memory stick via [[fstab]], add the following line to your {{ic|/etc/fstab}} file:
 
If you want non-root users to be able to mount a USB memory stick via [[fstab]], add the following line to your {{ic|/etc/fstab}} file:
 
  /dev/sda1 /mnt/usbstick vfat '''user''',noauto,noatime,flush 0 0
 
  /dev/sda1 /mnt/usbstick vfat '''user''',noauto,noatime,flush 0 0
Line 95: Line 62:
 
(see description of '''user''' and other options in the [[Fstab|main article]])
 
(see description of '''user''' and other options in the [[Fstab|main article]])
  
{{Note|Where {{Ic|/dev/sda1}} is replaced with the path to your own usbstick, see [[USB_Storage_Devices#Mounting_USB_memory|Mounting USB memory]].}}
+
{{Note|Where {{ic|/dev/sda1}} is replaced with the path to your own usbstick, see [[USB_Storage_Devices#Mounting_USB_memory|Mounting USB memory]].}}
  
 
Now, any user can mount it with:
 
Now, any user can mount it with:
Line 103: Line 70:
 
  $ umount /mnt/usbstick
 
  $ umount /mnt/usbstick
  
===== Poor copy performance to USB pendrive  =====
+
=== Maximizing performance ===
If you experienced slow copy speed to pendrive (mainly in KDE), then merge this three line to the end of your /etc/rc.local:
+
 
echo madvise > /sys/kernel/mm/transparent_hugepage/enabled
+
{{Merge|Maximizing Performance#Storage devices}}
echo madvise > /sys/kernel/mm/transparent_hugepage/defrag
+
 
echo 0 > /sys/kernel/mm/transparent_hugepage/khugepaged/defrag
+
You can try with other scheduler than CFQ like in [[SSD#Using_udev_for_one_device_or_HDD.2FSSD_mixed_environment|SSD#I/O Scheduler]] for a flash drive but its posible than your USB Storage Device where detected as rotational. Another posibility is the use of removable attribute.
And paste these at the end of your /etc/sysctl.conf
+
 
kernel.shmmax=134217728
+
== Unmounting devices mounted with udev or systemd/udev ==
vm.dirty_background_bytes = 4194304
+
 
vm.dirty_bytes = 4194304
+
Create an executable file (e.g. {{ic|/usr/local/bin/unmount.sh}}):
And reboot. This also reduces the freezes of the KDE's panel.
+
{{bc|<nowiki>
 +
#!/bin/sh
 +
 
 +
# Global variables
 +
TITLE="Unmount Utility"
 +
COLUMNS=3 # TARGET,SOURCE,FSTYPE
 +
#IFS=$'\n'
 +
 
 +
# Populate list of unmountable devices
 +
deviceList=($(findmnt -Do TARGET,SOURCE,FSTYPE | grep -e "sd[b-z]"))
 +
deviceCount=$((${#deviceList[@]} / $COLUMNS))
 +
 
 +
# Start of program output
 +
echo $TITLE
 +
 
 +
# Display list of devices that can be unmounted
 +
for ((device=0; device<${#deviceList[@]}; device+=COLUMNS))
 +
do
 +
  printf "%4s)  %-25s%-13s%-10s\n"\
 +
    "$(($device / $COLUMNS))"\
 +
    "${deviceList[$device]}"\
 +
    "${deviceList[$(($device + 1))]}"\
 +
    "${deviceList[$(($device + 2))]}"
 +
done
 +
 
 +
printf "%4s)  Exit\n" "x"
 +
 
 +
# Get input from user
 +
read -p "Choose a menu option: " input
 +
 
 +
# Input validation
 +
if [ "$input" = "X" ] || [ "$input" = "x" ]
 +
then
 +
  echo "Exiting"
 +
  exit 0
 +
fi
 +
 
 +
if (( $input>=0 )) && (( $input<$deviceCount ))
 +
then
 +
  echo "Unmounting: ${deviceList[$(($input * $deviceCount))]}"
 +
  sudo umount "${deviceList[$(($input * $deviceCount))]}"
 +
  exit 0
 +
else
 +
  echo "Invalid menu choice"
 +
  exit 1
 +
fi
 +
</nowiki>}}
 +
 
 +
{{Note|This script requires that you have sudo installed and that you have sudo rights.}}
 +
 
 +
== Troubleshooting ==
 +
 
 +
=== Poor copy performance to USB pendrive  ===
 +
 
 +
{{Merge|Maximizing Performance}}
 +
 
 +
{{Expansion|provide better description, or at least some reference}}
 +
 
 +
If you experienced slow copy speed to pendrive (mainly in KDE), then append these three lines in a [[systemd]] tmpfile:
 +
 
 +
{{hc|/etc/tmpfiles.d/local.conf|
 +
w /sys/kernel/mm/transparent_hugepage/enabled - - - - madvise
 +
w /sys/kernel/mm/transparent_hugepage/defrag - - - - madvise
 +
w /sys/kernel/mm/transparent_hugepage/khugepaged/defrag - - - - 0
 +
}}
 +
 
 +
See also [[Maximizing Performance#Tuning kernel parameters]].

Revision as of 06:24, 1 October 2013

This document describes how to use the popular USB memory sticks with Linux. However, it is also valid for other devices such as digital cameras that act as if they were just a USB storage device.

If you have an up-to-date system with the standard Arch kernel and a modern Desktop environment your device should just show up on your desktop, with no need to open a console.

Auto-mounting with udisks

This is the easiest and most frequently used method. It is used by many desktop environments, but can be used separately too. See Udisks for details.

Manual mounting

Note: Before you decide that Arch Linux does not mount your USB device, be sure to check all available ports. Some ports might not share the same controller, preventing you from mounting the device.

Getting a kernel that supports usb_storage

If you do not use a custom-made kernel, you are ready to go, for all Arch Linux stock kernels are properly configured. If you do use a custom-made kernel, ensure it is compiled with SCSI-Support, SCSI-Disk-Support and usb_storage. If you use the latest udev, you may just plug your device in and the system will automatically load all necessary kernel modules. Older releases of udev would need hotplug installed too. Otherwise, you can do the same thing manually:

# modprobe usb-storage
# modprobe sd_mod      (only for non SCSI kernels)
Tip: In case of manually loading modules, you may also need to load the sg module (SCSI generic driver).

Identifying device

First thing one need to access storage device is it's identifier assigned by kernel. See fstab#Identifying filesystems for details.

Tip: To see which device is your USB device, you can compare the output of lsblk -f (explained in the linked article) when the USB device is connected and when it is unconnected.

Mounting USB memory

Merge-arrows-2.pngThis article or section is a candidate for merging with fstab#Writing to FAT32 as Normal User.Merge-arrows-2.png

Notes: This section assumes that the partition on USB storage uses FAT32 or NTFS filesystem, so we have two sections covering the same topic. Either merge everything here or in the linked section. (Discuss in Talk:USB storage devices#)

You need to create the directory in which you are going to mount the device:

# mkdir /mnt/usbstick

As root

Mount the device as root with this command (do not forget to replace device_node by the path you found):

# mount device_node /mnt/usbstick

or

# mount -U UUID /mnt/usbstick

If mount does not recognize the format of the device you can try to use the -t argument, see man mount for details.

Note: If mounting your stick does not work you can try to repartition it, see Format a device.

As normal user with mount

If you want non-root users to be able to write to the USB stick, you can issue the following command:

# mount -o gid=users,fmask=113,dmask=002 /dev/sda1 /mnt/usbstick

As normal user with fstab

If you want non-root users to be able to mount a USB memory stick via fstab, add the following line to your /etc/fstab file:

/dev/sda1 /mnt/usbstick vfat user,noauto,noatime,flush 0 0

or better:

UUID=E8F1-5438 /mnt/usbstick vfat user,noauto,noatime,flush 0 0

(see description of user and other options in the main article)

Note: Where /dev/sda1 is replaced with the path to your own usbstick, see Mounting USB memory.

Now, any user can mount it with:

$ mount /mnt/usbstick

And unmount it with:

$ umount /mnt/usbstick

Maximizing performance

Merge-arrows-2.pngThis article or section is a candidate for merging with Maximizing Performance#Storage devices.Merge-arrows-2.png

Notes: please use the second argument of the template to provide more detailed indications. (Discuss in Talk:USB storage devices#)

You can try with other scheduler than CFQ like in SSD#I/O Scheduler for a flash drive but its posible than your USB Storage Device where detected as rotational. Another posibility is the use of removable attribute.

Unmounting devices mounted with udev or systemd/udev

Create an executable file (e.g. /usr/local/bin/unmount.sh):

#!/bin/sh

# Global variables
TITLE="Unmount Utility"
COLUMNS=3 # TARGET,SOURCE,FSTYPE
#IFS=$'\n'

# Populate list of unmountable devices
deviceList=($(findmnt -Do TARGET,SOURCE,FSTYPE | grep -e "sd[b-z]"))
deviceCount=$((${#deviceList[@]} / $COLUMNS))

# Start of program output
echo $TITLE

# Display list of devices that can be unmounted
for ((device=0; device<${#deviceList[@]}; device+=COLUMNS))
do
  printf "%4s)   %-25s%-13s%-10s\n"\
    "$(($device / $COLUMNS))"\
    "${deviceList[$device]}"\
    "${deviceList[$(($device + 1))]}"\
    "${deviceList[$(($device + 2))]}"
done

printf "%4s)   Exit\n" "x"

# Get input from user
read -p "Choose a menu option: " input

# Input validation
if [ "$input" = "X" ] || [ "$input" = "x" ]
then
  echo "Exiting"
  exit 0
fi

if (( $input>=0 )) && (( $input<$deviceCount ))
then
  echo "Unmounting: ${deviceList[$(($input * $deviceCount))]}"
  sudo umount "${deviceList[$(($input * $deviceCount))]}"
  exit 0
else
  echo "Invalid menu choice"
  exit 1
fi
Note: This script requires that you have sudo installed and that you have sudo rights.

Troubleshooting

Poor copy performance to USB pendrive

Merge-arrows-2.pngThis article or section is a candidate for merging with Maximizing Performance.Merge-arrows-2.png

Notes: please use the second argument of the template to provide more detailed indications. (Discuss in Talk:USB storage devices#)

Tango-view-fullscreen.pngThis article or section needs expansion.Tango-view-fullscreen.png

Reason: provide better description, or at least some reference (Discuss in Talk:USB storage devices#)

If you experienced slow copy speed to pendrive (mainly in KDE), then append these three lines in a systemd tmpfile:

/etc/tmpfiles.d/local.conf
w /sys/kernel/mm/transparent_hugepage/enabled - - - - madvise
w /sys/kernel/mm/transparent_hugepage/defrag - - - - madvise
w /sys/kernel/mm/transparent_hugepage/khugepaged/defrag - - - - 0

See also Maximizing Performance#Tuning kernel parameters.