USB Flash Memory HOWTO

Niko Sauer <nikos AT friedrichs DOT up DOT ac DOT za>

Revision History:

2008-08-31 21:58:05

0.06

2004-03-10

ns

Added section on Linux-2.6.

0.06

2004-03-10

ns

Added section on Linux-2.6.

0.05

2003-12-26

ejh

Technical review.

0.05

2003-12-26

ejh

Technical review.

0.04

2003-12-11

ns

0.04

2003-12-11

ns

0.03

2003-12-02

ejh

Technical review.

0.03

2003-12-02

ejh

Technical review.

0.02

2003-10-30

ns

Changed

0.02

2003-10-30

ns

Changed

This document describes procedures for installing USB Flash Memory devices and the way of formatting them for various file systems such as vfat (the way they usually come) and ext2 (the way they fit in better with Linux). It is also described how to partition the device in two sections with different file types.

Purpose

The purpose of this document is to describe procedures for implementing USB flash memory devices (memory sticks) onLinux. This entails the following: The purpose of this document is to describe procedures for implementing USB flash memory devices (memory sticks) on Linux. This entails the following: making over-the-counter devices, pre-formatted for the MS Windows system, work on Linux; making over-the-counter devices, pre-formatted for the MS Windows system, work on Linux; formatting the device in the ext2 file system on a single partition and making it work. This is intended for situations where the device will be used exclusively on Linux machines; formatting the device in the ext2 file system on a single partition and making it work. This is intended for situations where the device will be used exclusively on Linux machines; formatting the device on a dual partition in the ext2 and vfat file systems and making it work. The purpose of this is for situations where Linux + Linux and Linux + Windows are possible; formatting the device on a dual partition in the ext2 and vfat file systems and making it work. The purpose of this is for situations where Linux + Linux and Linux + Windows are possible; making the device more user friendly; making the device more user friendly; maintaining the file system of the device; maintaining the file system of the device; using additional features of Linux-2.6.x. using additional features of Linux-2.6.x.

The procedures described in this document should work on any USB memory stick device which can be operated as a read-write device. Read-only devices do not fall in this category. The procedures will not work if the write facility of the device is disabled (i.e. if the write protect switch is on). The procedures described in this document should work on any USB memory stick device which can be operated as a read-write device. Read-only devices do not fall in this category. The procedures will not work if the write facility of the device is disabled (i.e. if the write protect switch is on).

The procedures described for formatting and maintenance in particular file systems may (in principle) be adapted for other file systems such as ext3 and reiserfs. These have not been tried and tested. Neither has any attention been paid to adaptation of the procedures to other related operating systems. The procedures described for formatting and maintenance in particular file systems may (in principle) be adapted for other file systems such as ext3 and reiserfs. These have not been tried and tested. Neither has any attention been paid to adaptation of the procedures to other related operating systems.

Physical devices

Flash memory devices come mainly in the form of small, portable devices often referred to as memory sticks or keychains, and as part of digital cameras. They are non-volatile devices which operate on the principle that electric charges are used to represent data in binary format. These charges can remain unchanged almost indefinitely, but changes (such as writing to the device) limit the life span of the device (100000 writes of 8MBeach). Flash memory devices come mainly in the form of small, portable devices often referred to as or, and as part of digital cameras. They are which operate on the principle that electric charges are used to represent data in binary format. These charges can remain unchanged almost indefinitely, but changes (such as writing to the device) limit the life span of the device (100000 writes of 8MB each). Memory sticks plug directly into a USB-port at the back of your computer. The power it needs is supplied by the USB-port. It is sometimes convenient to use a USB-extension cable to bring the device within easy reach. Memory sticks plug directly into a USB-port at the back of your computer. The power it needs is supplied by the USB-port. It is sometimes convenient to use a USB-extension cable to bring the device within easy reach. Memory sticks usually have write protect switches which should be turned off if you want to use it as read-write device. If the switch is in the on position,the device is read-only. The devices are usually equipped with a led (light emitting diode), which indicates that the device is operational. The led also flickers while data is being transferred to or from the device.

Memory sticks usually have which should be turned off if you want to use it as read-write device. If the switch is in the position, the device is . The devices are usually equipped with a led (light emitting diode), which indicates that the device is operational. The led also flickers while data is being transferred to or from the device.

Digital cameras use flash memory for image storage. These are powered by the camera and connected to the computer's USB-port via an electronic interface. To be able to connect to the computer,the camera's power must be turned on for the duration of the interfacing, and should only be turned off after the device had been unmounted from the computer. It is important to remember that the device takes power from the camera's battery. The operation should therefore be terminated as soon as possible. Digital cameras use flash memory for image storage. These are powered by the camera and connected to the computer's USB-port via an electronic interface. To be able to connect to the computer, the camera's power must be turned on for the duration of the interfacing, and should only be turned off after the device had been unmounted from the computer. It is important to remember that the device takes power from the camera's battery. The operation should therefore be terminated as soon as possible.

Buying a memory stick

Important considerations when buying a memory stick are the following: Important considerations when buying a memory stick are the following:

Reading

Users of this document might want to acquaint themselves with the following: This document leans heavily on the texts marked with an asterisk Users of this document might want to acquaint themselves with the following:

Hardware and kernel

Setup

This is a description of the salient features of the setup used to develop the procedures described below. All the procedures havebeen tried and tested, also for Linux-2.6. The screen-like displays are precise copies of what appeared on my screen. This is a description of the salient features of the setup used to develop the procedures described below. All the procedures have been tried and tested, also for Linux-2.6. The screen-like displays are precise copies of what appeared on my screen.

Kernel options

It is uncertain if USB-support is sufficient in kernels earlier than 2.4.xx. The following support, relevant to this document, was compiled into the kernel. A modular approach may also be followed. It is uncertain if USB-support is sufficient in kernels earlier than 2.4.xx. The following support, relevant to this document, was compiled into the kernel. A modular approach may also be followed.

SCSI support

File systems

USB support

Notes

In the lists above the entities in uppercase refer to the variable names to be found in the .configfile in the upper level directory of the kernel source (/usr/src/linux/). The entitiesxxx.o refer to the modules created when a modular approach is followed. When there is no reference to a module, the option can only be hard-compiled into the kernel. In the lists above the entities in uppercase refer to the variable names to be found in the .config

file in the upper level directory of the kernel source ( /usr/src/linux/ ). The entities

xxx.o

modular approach is followed. When there is no reference to a module, the option can only be hard-compiled into the kernel.

Different kernel versions may have different indications of options when, for example, or are run. Variables such as CONFIG_USB, which can be gleaned from the various options, may be a more reliable indication. Very recent Linux distributions such as RedHat and SuSE probably have the appropriate kernel options compiled in. Under USB-support, options for a number of digital cameras are available. Please consult the relevant texts as set out in if you consider (re)compiling your kernel.

Some basic concepts

In this section we discuss in a very brief manner, some basic Linux concepts which should contribute to an understanding of the procedures described below. In this section we discuss in a very brief manner, some basic Linux concepts which should contribute to an understanding of the procedures described below.

The /proc file system

/proc/bus/usb/

/proc/scsi/ . These will be used to

SCSI devices

Your memory stick will be considered as a USB mass storage device posing as a removable SCSI disk (sd).SCSI disk devices are mapped to the/dev(devices) directory under/dev/sda ,/dev/sdb , ... When different disk devices are present,they will be mapped to /dev/sda,/dev/sdb, etc.If, for example, a memory stick and a digital camera are plugged in, the one would be mapped to /dev/sda and the other to/dev/sdb. Tests have indicated that the first device to be detected is mapped to sda and represented in the directory /proc/scsi/usb-storage-0. The second will be mapped to sdb and represented in/proc/scsi/usb-storage-0.What will happen when both devices are present at boot-up, is notknown. An overview of the partitions relevant to this document,the file /proc/partitions may be consulted.Look for entries like sda, sda1, sdb. Under the Linux-2.6 series of kernels this problem may be very neatly resolved (). Your memory stick will be considered as a USB mass storage device posing as a removable SCSI disk (sd).SCSI disk devices are mapped to the /dev

(devices) directory under /dev/sda

/dev/sdb

they will be mapped to /dev/sda ,

/dev/sdb , etc.If, for example, a memory stick and a digital camera are plugged in, the one would be mapped to /dev/sda

/dev/sdb . Tests have indicated that the first device to be detected is mapped to sda

represented in the directory /proc/scsi/usb-storage-0 . The second will be mapped to sdb

/proc/scsi/usb-storage-0 . What will happen when both devices are present at boot-up, is not known. An overview of the partitions relevant to this document, the file /proc/partitions

Look for entries like sda, sda1, sdb . Under the Linux-2.6 series of kernels this problem may be very neatly resolved ().

In the descriptions further on, it will be assumed that only one memory stick is present, and therefore mapped to/dev/sda. In the descriptions further on, it will be assumed that only one memory stick is present, and therefore mapped to

/dev/sda . In most distributions these points exist in abundance. You should check that this is the case in your setup (ls /dev/sda*. If there are none you can create them by doing (as root, indicated by the hash). In most distributions these points exist in abundance. You should check that this is the case in your setup

doing (as root, indicated by the hash).

#mknod /dev/sda b 8 0#mknod /dev/sda1 b 8 1#mknod /dev/sda2 b 8 2

etc, up to /dev/sda15 if necessary. The device as a whole is mapped to /dev/sda,while /dev/sdax (x = 1 ... 15) represent the various partitions of the device. We shall describe, later on,how to create different partitions for different file systems (see and). If you intend to use your memory stick with a single partition(file system), /dev/sda1 is sufficient. etc, up to /dev/sda15

device as a whole is mapped to /dev/sda , while /dev/sdax (x = 1 ... 15)

the various partitions of the device. We shall describe, later on, how to create different partitions for different file systems (see and). If you intend to use your memory stick with a single partition (file system), /dev/sda1

The USB device file system

This dynamically generated filesystem should be mounted at/proc/bus/usb/. It is therefore essential that this mount point exists. Once it is mounted, there should be more to be seen in /proc/bus/usb/ and in /proc/scsi/ (). To find out which USB devices are present, read the file /proc/bus/usb/devices( less /proc/bus/usb/devices). It takes a small effort to interpret the writing on the screen, but it is not difficult. A memory stick is indicated by Mass Storage Device. This dynamically generated file system should be mounted at

/proc/bus/usb/ . It is therefore essential that this mount point exists. Once it is mounted, there should be more to be seen in /proc/bus/usb/

/proc/scsi/

present, read the file /proc/bus/usb/devices

(). It takes a small effort to interpret the writing on the screen, but it is not difficult. A memory stick is indicated by .

The ext2 file system

The ext2 (second extended) file system still is the most prevalent in Linux setups. It is most versatile and sophisticated, carrying with it permissions (read-write-execute, who is permitted to do what), ownership (user, group, others), a time stamp (when last modified), etc. Moreover, tools for the proper maintenance of a device endowed with ext2, exist ().If a flash memory device is to be used exclusively within Linux setups, it is therefore desirable to format it in ext2 (see ). The ext2 (second extended) file system still is the most prevalent in Linux setups. It is most versatile and sophisticated, carrying with it permissions (read-write-execute, who is permitted to do what), ownership (user, group, others), a time stamp (when last modified), etc. Moreover, tools for the proper maintenance of a device endowed with ext2, exist ().If a flash memory device is to be used exclusively within Linux setups, it is therefore desirable to format it in ext2 (see ).

The vfat file system

Enabling vfat in the kernel makes it possible to mount file systems created under Dos/Windows to be mounted in a Linux system. Most memory sticks are formatted for direct use on Windows and should therefore be considered as vfat-formatted. The vfat file system is less sophisticated than ext2 and will therefore be more economic as far as memory space is concerned. It does not carry permissions in the same way as ext2with the result that an ext2-file saved in vfat will re-appear with its permissions changed. Nonetheless, if a memory stick isto be used for the purpose of carrying data between Linux and Windows machines, it is best to keep it in the vfat format. A compromise is to partition the memory stick in two parts: one invfat and the other in ext2. At least the Linux machine should be able to handle both. This is described in Enabling vfat in the kernel makes it possible to mount file systems created under Dos/Windows to be mounted in a Linux system. Most memory sticks are formatted for direct use on Windows and should therefore be considered as vfat-formatted. The vfat file system is less sophisticated than ext2 and will therefore be more economic as far as memory space is concerned. It does not carry permissions in the same way as ext2 with the result that an ext2-file saved in vfat will re-appear with its permissions changed. Nonetheless, if a memory stick is to be used for the purpose of carrying data between Linux and Windows machines, it is best to keep it in the vfat format. A compromise is to partition the memory stick in two parts: one in vfat and the other in ext2. At least the Linux machine should be able to handle both. This is described in

Explanation of command options

Here is a short list of command options that occur in this document:

Basic verifications

A quick check

It is possible that your system is already set up for handling aUSB-flash memory device. To check this, simply issue the command$ mount from an X-terminal. If something like It is possible that your system is already set up for handling a USB-flash memory device. To check this, simply issue the command from an X-terminal. If something like

none on /proc/bus/usb type usbfs (rw)

appears among the output, you are ready to go to and proceed from there. It may bevirtuous, however, to glance through the intervening sections.If the test is unsuccessful, all is not lost. Please read on. appears among the output, you are ready to go to and proceed from there. It may be virtuous, however, to glance through the intervening sections. If the test is unsuccessful, all is not lost. Please read on.

Probing the /proc filesystem

A few things in the/procdirectory can be checked so see if the kernel options have beenproperly included or the appropriate modules properly inserted. Asa first check, see if the directory /proc/bus/usb exists. If it does,your kernel supports the USB-subsystem correctly. If this is notthe case the kernel has to be re-compiled with appropriate USBsupport (see ) or the kernel has to beupdated. Next, check if the directory /proc/scsi exists. If it does, you'rewell-away. If it doesn't, SCSI support has not been compiled intoyour kernel (see ). A few things in the /proc

directory can be checked so see if the kernel options have been properly included or the appropriate modules properly inserted. As a first check, see if the directory /proc/bus/usb

your kernel supports the USB-subsystem correctly. If this is not the case the kernel has to be re-compiled with appropriate USB support (see ) or the kernel has to be updated. Next, check if the directory /proc/scsi

well-away. If it doesn't, SCSI support has not been compiled into your kernel (see ). In/proc there should beprovision for a mount point for the USB filesystem. That point is/proc/bus/usb. If itexists, the kernel is correctly set up. In /proc

provision for a mount point for the USB filesystem. That point is

/proc/bus/usb . If it exists, the kernel is correctly set up.

Mounting the USB filesystem

If the verifications of the previous sections were positive, thenext step is to mount the USB filesystem. This is issued from rootas follows: If the verifications of the previous sections were positive, the next step is to mount the USB filesystem. This is issued from root as follows:

# mount -t usbfs none /proc/bus/usb

If there are no complaints, do some more tests. The first is thequick test ().A deeper probe would be toissue the command # ls -l /proc/bus/usb . Thisshould give something similar to If there are no complaints, do some more tests. The first is the quick test ().A deeper probe would be to issue the command. This should give something similar to

dr-xr-xr-x 1 root root 0 Sep 19 14:21 001 dr-xr-xr-x 1 root root 0 Sep 19 14:21 002 -r--r--r-- 1 root root 0 Sep 19 22:30 devices -r--r--r-- 1 root root 0 Sep 19 22:30 drivers

The path/proc/scsi/usb-storage-0/ should now exist and one ofthe files at the end will show this. In my setup the command$ less /proc/scsi/usb-storage-0/1 gives The path /proc/scsi/usb-storage-0/

the files at the end will show this. In my setup the command gives

Host scsi1: usb-storage Vendor: Generic Product: Mass Storage Device Serial Number: None Protocol: Transparent SCSI Transport: Bulk GUID: 0ed166800000000000000000 Attached: Yes/No

If the flashdrive is present, the last line will have the entry"Yes". If it is not plugged in, the entry will be "No". If the flashdrive is present, the last line will have the entry "Yes". If it is not plugged in, the entry will be "No".

Tests - vfat

You are now ready to find out if the memory stick is working. Let us suppose that you bought it over the counter. Its instruction leaflet will most likely tell you how to set it up for Windows. That is an indication that it is formatted in vfat. Before you try to mount it, create a mount point for it. Do something like # mkdir -m 777 /mnt/memstick. The mounting command would most likely beYou are now ready to find out if the memory stick is working. Let us suppose that you bought it over the counter. Its instruction leaflet will most likely tell you how to set it up for Windows. That is an indication that it is formatted in vfat. Before you try to mount it, create a mount point for it. Do something like . The mounting command would most likely be

# mount -t vfat /dev/sda1 /mnt/memstick

If everything went smoothly, you should be able to see the device:# ls/mnt/memstick. If everything went smoothly, you should be able to see the device: . Now try some standard things like making a directory on the deviceand copying a favourite text file to it: Now try some standard things like making a directory on the device and copying a favourite text file to it:

# mkdir /mnt/memstick/apollo # cp /home/myname/myfavourite_file /mnt/memstick/apollo/.

List again (# ls -l /mnt/memstick ) andpay attention to the permissions. List again ( ) and pay attention to the permissions. Unmount the device (#umount/dev/sda1 ) and mount it again as above. List againand check the permissions. Most likely your favourite text filewill now have an x-permission. It became executable. That isnormal in the vfat filesystem. If you are happy with that,unmount the device and skip to. Unmount the device ( ) and mount it again as above. List again and check the permissions. Most likely your favourite text file will now have an x-permission. It became executable. That is normal in the vfat filesystem. If you are happy with that, unmount the device and skip to.

Setting up an Ext2 filesystem

The procedures described here will give the entire flash memorydevice a single partition endowed with the ext2 filesystem. Thiswill make the device more suitable for use between Linux machines.Don't do this if the device is supposed to operate betweenLinux and Windows machines. The procedures described here will give the entire flash memory device a single partition endowed with the ext2 filesystem. This will make the device more suitable for use between Linux machines. Don't do this if the device is supposed to operate between Linux and Windows machines.

Partitioning

It is assumed that the flash memory is mapped to/dev/sda. In this section we treat the situation wherethe whole flash memory device will become devoted to a single ext2partition. In the example procedure shown below, a 128MB flashmemory was formatted for ext2. That is why figures like 131MB and888 cylinders appear. The fdisk tool, which is used, simply readsthat from the device. It is assumed that the flash memory is mapped to /dev/sda . In this section we treat the situation where the whole flash memory device will become devoted to a single ext2 partition. In the example procedure shown below, a 128MB flash memory was formatted for ext2. That is why figures like 131MB and 888 cylinders appear. The fdisk tool, which is used, simply reads that from the device. All operations are done by root. The single partition will becreated on/dev/sda(please note: not/dev/sda1 ) The procedure is described in a series ofsteps with comments. The standard prompt offdisk isCommand (m forhelp): and you can, at any stage enterm to see the available commands. If you dothat the result would be All operations are done by root. The single partition will be created on /dev/sda

(please note: not /dev/sda1

steps with comments. The standard prompt of fdisk isand you can, at any stage enter to see the available commands. If you do that the result would be

Command action a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition l list known partition types m print this menu n add a new partition o create a new empty DOS partition table p print the partition table q quit without saving changes s create a new empty Sun disklabel t change a partition's system id u change display/entry units v verify the partition table w write table to disk and exit x extra functionality (experts only)

Your memory stick must be plugged in, but not mounted. Take carethat write protect is off. Your memory stick must be plugged in, but not mounted. Take care that write protect is off. Here goes: Here goes:

# fdisk /dev/sda Command (m for help):d {enter } Selected partition 1 Command (m for help):n {enter} Command action e extended p primary partition (1-4) p {enter} Partition number (1-4):1 {enter} First cylinder (1-888, default 1): {press enter} Using default value 1 Last cylinder ... (1-888, default 888): {press enter} Using default value 888

Now is a good time to verify that all went well by printing outthe partition details and table. Now is a good time to verify that all went well by printing out the partition details and table.

Command (m for help): p Disk /dev/sda: 131 MB, 131072000 bytes 9 heads, 32 sectors/track, 888 cylinders Units = cylinders of 288 * 512 = 147456 bytes Device Boot Start End Blocks Id System /dev/sda1 1 888 127856 83 Linux

Make sure that the partition is not set up as bootable. Thereshould not be an asterisk under the heading "Boot" in thepartition table. Furthermore, the headings "Id" and "System"should be as they are in the printout shown above. That indicatesthat you can format the device in ext2 (next section). These arethe defaults. If they are not, they can be changed by Make sure that the partition is not set up as bootable. There should not be an asterisk under the heading "Boot" in the partition table. Furthermore, the headings "Id" and "System" should be as they are in the printout shown above. That indicates that you can format the device in ext2 (next section). These are the defaults. If they are not, they can be changed by

Command (m for help): a [toggle a bootable flag] Command (m for help): t [change a partition's system id]

If (or when) the partition table is correct, you can conclude the procedure with

Command (m for help): w [write table to disk and exit]

Making an ext2 device

Having completed the partition part, we go straight on to"formatting" the device in ext2. For this we use the toolmke2fs. This is straightforward. Having completed the partition part, we go straight on to "formatting" the device in ext2. For this we use the tool mke2fs. This is straightforward.

#mke2fs /dev/sda1

You might like to watch the led on your memory stick flickerwhile this is happening. When it stops, the job is done. You might like to watch the led on your memory stick flicker while this is happening. When it stops, the job is done.

Tests - ext2

To see if you were successful, essentially repeat the proceduresdescribed inandwith two exceptions of detail. In the first place the mountcommand should be To see if you were successful, essentially repeat the procedures described inand with two exceptions of detail. In the first place the mount command should be

#mount -t ext2 /dev/sda1 /mnt/memstick

The second exception is that the permissions on your favouritetext file should not change anymore. The second exception is that the permissions on your favourite text file should not change anymore.

Setting up a dual partition

Partitioning

The procedures described here will partition the flash memory devicein two pieces -- one of the partitions will be formatted in ext2 andthe other in vfat. This may be handy when the device is to be usedto transport data between two (or more) Linux machines (ext2) and between Linuxand Windows machines (vfat) The procedures described here will partition the flash memory device in two pieces -- one of the partitions will be formatted in ext2 and the other in vfat. This may be handy when the device is to be used to transport data between two (or more) Linux machines (ext2) and between Linux and Windows machines (vfat) In this section we describe how to set up a dual partition forthe memory stick: One half of the device will bedevoted to the vfat filesystem, and the other will be ext2. Inthis way your device will be more versatile, but also a littleshattered. The procedure will be given in somewhat less detailedthan in. Note that the procedureof the previous section is repeated and that the hex code foreach file type is explicitly entered (lines following the entry"t": Win95 = b; Linux = 83). When the partition will only be forLinux, this is unnecessary -- Linux is the default. Also note thatthe vfat partition goes on first -- that is the way Windows wouldhave it. In this section we describe how to set up a dual partition for the memory stick: One half of the device will be devoted to the vfat filesystem, and the other will be ext2. In this way your device will be more versatile, but also a little shattered. The procedure will be given in somewhat less detailed than in. Note that the procedure of the previous section is repeated and that the hex code for each file type is explicitly entered (lines following the entry "t": Win95 = b; Linux = 83). When the partition will only be for Linux, this is unnecessary -- Linux is the default. Also note that the vfat partition goes on first -- that is the way Windows would have it.

#fdisk /dev/sdaCommand (m for help):dSelected partition 1Command (m for help):nCommand action e extended p primary partition (1-4)pPartition number (1-4):1First cylinder (1-888, default 1): Using default value 1Last cylinder or +size or +sizeM or +sizeK (1-888, default 888):444Command (m for help):tSelected partition 1Hex code (type L to list codes):b Changed system type of partition 1 to b (Win95 FAT32) Command (m for help):nCommand action e extended p primary partition (1-4)pPartition number (1-4):2First cylinder (445-888, default 445): Using default value 445 Last cylinder or +size or +sizeM or +sizeK (445-888, default 888): Using default value 888Command (m for help):tPartition number (1-4):2Hex code (type L to list codes):83Command (m for help):p Disk /dev/sda: 131 MB, 131072000 bytes 9 heads, 32 sectors/track, 888 cylinders Units = cylinders of 288 * 512 = 147456 bytes Device Boot Start End Blocks Id System /dev/sda1 1 444 63920 b Win95 FAT32 /dev/sda2 445 888 63936 83 Linux Command (m for help):w

The choice of splitting the device right in the middle (1--444;445--888 for a 128 MB memory stick) is arbitrary. Any otherwell-conceived splitting would be fine. The choice of splitting the device right in the middle (1--444; 445--888 for a 128 MB memory stick) is arbitrary. Any other well-conceived splitting would be fine. If you had more than one partition to begin with, the "delete"part of the procedure would have to be repeated. If you had more than one partition to begin with, the "delete" part of the procedure would have to be repeated.

Making a dual ext2/vfat system

This step is a repetition of . The first step makes the dos partition, the second makes the Linux ext2 partition. This step is a repetition of . The first step

#mkdosfs -F 32 /dev/sda1#mke2fs /dev/sda2

Tests - ext2 and vfat

Simply carry out the tests ofandon the two devices separately. The mount commandswould be Simply carry out the tests ofandon the two devices separately. The mount commands would be

#mount -t vfat /dev/sda1 /mnt/fatstick # mount -t ext2 /dev/sda2 /mnt/memstick

Configuring Linux for the dual partition

/dev/sda1 . This is, perhaps, a shortcoming

#fdisk /dev/sdaChoose p; check partition table If satisfied, choose w#less /proc/partitions

Making the memory stick accessible to users

What we have described so far, are simply the procedures of settingup the system and formatting memory sticks. We have to make themounting of the USB filesystem more permanent, and we have to makemounting of such a device easier for non-root users. There areonly a few things to be done. What we have described so far, are simply the procedures of setting up the system and formatting memory sticks. We have to make the mounting of the USB filesystem more permanent, and we have to make mounting of such a device easier for non-root users. There are only a few things to be done. The procedure described below is for the more complex situation ofa memory stick with a dual partition (). Ifthe partition is a single ext2, the procedure should be simplifiedby leaving out the line referring to the vfat part and changingsda2 to sda1. The procedure described below is for the more complex situation of a memory stick with a dual partition (). If the partition is a single ext2, the procedure should be simplified by leaving out the line referring to the vfat part and changing

sda2

sda1 . Suppose that the mount points/mnt/memstickand/mnt/fatstick have been created. You donot have to use those words and they don't even have to besub-directories of/mnt Suppose that the mount points /mnt/memstick

and /mnt/fatstick

not have to use those words and they don't even have to be sub-directories of /mnt To make remembering easier, create the following symbolic links: To make remembering easier, create the following symbolic links:

#ln -s /dev/sda1 /dev/fatflash#ln -s /dev/sda2 /dev/flash

For smoother and easier mounting add the following lines to/etc/fstab : ( See NOTE at the end of) For smoother and easier mounting add the following lines to

/etc/fstab

none /proc/bus/usb usbfs defaults 0 0 /dev/flash /mnt/memstick ext2,vfat rw,user,noauto 0 0 /dev/fatflash /mnt/fatstick vfat rw,user,noauto 0 0

The middle line enables mounting either in ext2 or vfat.The last two lines make it possible for any user to mount thedevice by the command The middle line enables mounting either in ext2 or vfat. The last two lines make it possible for any user to mount the device by the command

$ mount /dev/flash [for ext2 or vfat] $ mount /dev/fatflash [for vfat]

It is possible to mount both partitions simultaneously. See . In the case of an ext2-formatted device it does not seem possiblefor a user (other than root) to mount in read-write mode. Thesolution is to create a directory on the memory stick owned by theparticular user with complete permissions. Root should do that: In the case of an ext2-formatted device it does not seem possible for a user (other than root) to mount in read-write mode. The solution is to create a directory on the memory stick owned by the particular user with complete permissions. Root should do that:

#mount /dev/flash # mkdir -m 777 /mnt/memstick/superdir # chown charles:charles /mnt/memstick/superdir

If the device is in the vfat format, this step is unnecessary. If the device is in the vfat format, this step is unnecessary. In conclusion, here is a small script I use to mount and unmountmy (ext2) flash drive: In conclusion, here is a small script I use to mount and unmount my (ext2) flash drive:

I call this from an FVWM menu byExec exec xterm-geometry 43x2+1250+0 -e /home/nikos/bin/flashdrivewhich places it on a tiny x-term in one corner of the screen. Thehorizontal displacement of 1250 may have to be modified accordingto the horizontal resolution of your screen. I call this from an FVWM menu by which places it on a tiny x-term in one corner of the screen. The horizontal displacement of 1250 may have to be modified according to the horizontal resolution of your screen.

Maintenance

Filesystem inspection

It is a virtuous habit to inspect your ext2 filesystem on theflash memory regularly. To do this, the tooldumpe2fs may be used in the following way:# dumpe2fs -h /dev/sda1 (you must be root andthe device should not be mounted). The result should be similar tothe following: It is a virtuous habit to inspect your ext2 filesystem on the flash memory regularly. To do this, the tool dumpe2fs may be used in the following way: (you must be root and the device should not be mounted). The result should be similar to the following:

Filesystem volume name: <none> Last mounted on: <not available> Filesystem UUID: c42a6963-5e6a-4cd2-b7d7-c8f09dca6c52 Filesystem magic number: 0xEF53 Filesystem revision #: 1 (dynamic) Filesystem features: dir_index filetype sparse_super Default mount options: (none) Filesystem state: clean Errors behavior: Continue Filesystem OS type: Linux Inode count: 32000 Block count: 127856 Reserved block count: 6392 Free blocks: 116456 Free inodes: 31922 First block: 1 Block size: 1024 Fragment size: 1024 Blocks per group: 8192 Fragments per group: 8192 Inodes per group: 2000 Inode blocks per group: 250 Filesystem created: Sat Sep 20 12:43:00 2003 Last mount time: Tue Oct 28 14:13:03 2003 Last write time: Tue Oct 28 14:28:27 2003 Mount count: 13 Maximum mount count: 35 Last checked: Sat Oct 18 11:28:26 2003 Check interval: 15552000 (6 months) Next check after: Thu Apr 15 11:28:26 2004 Reserved blocks uid: 0 (user root) Reserved blocks gid: 0 (group root) First inode: 11 Inode size: 128 Default directory hash: tea Directory Hash Seed: 118bee0a-efa5-4771-967e-41a0badd0355

A few important aspects need to be pointed out. A few important aspects need to be pointed out. You might get warnings about these things when you mount thedevice or when you try the read files from the device. You might get warnings about these things when you mount the device or when you try the read files from the device.

And then?

When the usage allocation has been spent, or there is evidence offile corruption, the thing to do is to run #fsck.ext2 /dev/sda1 with the device unmounted. After that, usageparameters will be freshly allocated and bad blocks will be gone. When the usage allocation has been spent, or there is evidence of file corruption, the thing to do is to runwith the device unmounted. After that, usage parameters will be freshly allocated and bad blocks will be gone.

The Linux-2.6 kernel

What is new?

Setting up your system as described above for the Linux-2.4.x kernels will be perfectly good, but with the Linux-2.6.x kernels (x = 0, 1, 2, 3 at the time of this writing) much more is possible:

USB Hotplugging of devices has been improved immensely. Thismeans that it has become possible to attach and remove adevice from the USB port with full cognisance of thesystem. If a device is attached, it will show in the /procfilesystem. Once it is removed, it will not show anymore. USB Hotplugging of devices has been improved immensely. This means that it has become possible to attach and remove a device from the USB port with full cognisance of the system. If a device is attached, it will show in the /proc filesystem. Once it is removed, it will not show anymore.

Thesystem has been introduced. By using this facility, a very tight grip on attached devices, their attributes and nodes of attachment is possible.

Utilities for handling hotplugging have been improved or became available. With the aid of these it is possible to easily distinguish between attached devices irrespective of the order in which they were attached.

Kernel options

Kernel configuration options are very much the same as described in . The configuration menu (# make menuconfig) has been organized differently and more systematically. Here are the relevant choices:

Bus options

Device drivers

File systems

Notes

It may be in your interest to consult Migrating to Linux Kernel 2.6,but it is not necessary to follow steps that cannot be followed. It may be in your interest to consult

Migrating to Linux Kernel 2.6

Migrating to Linux Kernel 2.6, but it is not necessary to follow steps that cannot be followed.

Hotplugging and Naming

In the situation of two USB devices plugged in at the same time was discussed. The difficulty is that it becomes hard for the user to distinguish the two devices. Progress recently made in the Linux Hotplug Project(search for hotplug) and implemented in the 2.6-series of kernels, has made it possible to address this problem efficiently. For implementation some utilities are needed:

Linux Hotplug Project Linux Hotplug

Installation

The sysfs filesystem is intrinsic tothe Linux-2.6 kernel series. It is used byudev andsysfstools to obtain informationabout kernel objects (devices, etc.), their attributesand mutual linkages. Activation is according to thefollowing procedure: Thefilesystem is intrinsic to the Linux-2.6 kernel series. It is used by

about kernel objects (devices, etc.), their attributes and mutual linkages. Activation is according to the following procedure:

 # cd / # mkdir sys # mount -t sysfs none /sys

If you do , a whole directory structure with entries like , , ... will be shown. You were successful. The mounting should now be made permanent by adding the following line to

/etc/fstab :

 none /sys sysfs defaults 0 0

This is installed in one of the usual ways without havingto make special configuration options. Instructions inthe README file are very clear. You should, however,first make sure that the entry/usr/local/lib is entered in/etc/ld.so.conf so that thelibsysfs.so libraries will berecognisable by the system. After installation, checkthat the utilities /usr/local/bin/lsbusand /usr/local/bin/systool have beeninstalled. Also check that the libraries mentioned abovehave been properly linked in(ldconfig -p | greplibsysfs). This is installed in one of the usual ways without having to make special configuration options. Instructions in the README file are very clear. You should, however, first make sure that the entry

/usr/local/lib

/etc/ld.so.conf

libsysfs.so

recognisable by the system. After installation, check that the utilities /usr/local/bin/lsbus

and /usr/local/bin/systool

installed. Also check that the libraries mentioned above have been properly linked in (). Next install the hotplugscripts. The first thing to do is to check thathotplugging will work on your system. Do: less /proc/sys/kernel/hotplug. Theresult should be/sbin/hotplug. If it isnot so, you should add the lineecho"/sbin/hotplug" > /proc/sys/kernel/hotplug toan appropriate initialization script,e.g. /etc/rc.d/rc.local. Thiscommand could also be given (as root) before checkingthat everything works. Installation is extremelysimple. Do: #make install. Check afterwards that thescript /sbin/hotplug exists. Next install the hotplug

scripts. The first thing to do is to check that hotplugging will work on your system. Do: . The result should be . If it is not so, you should add the line echo "/sbin/hotplug" > /proc/sys/kernel/hotplug

an appropriate initialization script, e.g. /etc/rc.d/rc.local . This command could also be given (as root) before checking that everything works. Installation is extremely simple. Do: . Check afterwards that the script /sbin/hotplug

This is the last installation. Study the README file and follow the instructions necessary to be able to run . If you have compiled the Linux-2.6.x kernel, this should not be too difficult. A quick check would be to find out if

/sbin/udev

/sbin/udevinfo

other occurences as well, a very important one being

/etc/udev/udev.rules . If you have a working setup for memory sticks (as described above) and a memory stick plugged in you could try , you should see the device (sda, sda1, ...). If it is there, unplug the device and list again. It should be gone!

Naming memory sticks

With the udev utility in place, itis possible to identify a memory stick on the basis ofsome built-in attribute. One that works, is thevendor identity. To find this for anattached device, do systool -vb scsi | grep vendor. Thisshould give a list of attached SCSI devices (rememberthat your memory stick poses as a SCSI device ). Let us suppose that you have twomemory sticks plugged in at the same time and found (as Idid) two vendor Id's, namely "UFD" and "STORAGE". Thenadd to the file /etc/udev/udev.rulesthe following lines (at the top if you like): With theutility in place, it is possible to identify a memory stick on the basis of some built-in attribute. One that works, is the

attached device, do . This should give a list of attached SCSI devices (remember that your memory stick poses as a SCSI device ). Let us suppose that you have two memory sticks plugged in at the same time and found (as I did) two vendor Id's, namely "UFD" and "STORAGE". Then add to the file /etc/udev/udev.rules

the following lines (at the top if you like):

 ## Flash Memory 1 BUS="scsi", SYSFS_vendor="UFD*", NAME="namib%n" ## Flash Memory 2 BUS="scsi", SYSFS_vendor="STORAGE*", NAME="kalahari%n"

The vendor-identities may have trailing blank spaces, andtherefore the wildcard asterisks have a purpose. Do notignore them.Remove and replace the memory sticks, thendo ls /udev. Instead of the impersonalsda, sda1,etc. there should now be kalahari,kalahari1,namib andnamib1. The devices have been namedaccording to the "vendor" information in them. Thus wecan identify the device, no matter where it wasmapped. All that remains to be done is to creatappropriate mount points like/mnt/namib andkalahari and make entries like The vendor-identities may have trailing blank spaces, and therefore the wildcard asterisks have a purpose. Do not ignore them.Remove and replace the memory sticks, then do . Instead of the impersonal

sda , sda1 , etc. there should now be kalahari ,

kalahari1 ,

namib

namib1 . The devices have been named according to the "vendor" information in them. Thus we can identify the device, no matter where it was mapped. All that remains to be done is to creat appropriate mount points like

/mnt/namib

kalahari

 /udev/namib1 /mnt/namib vfat,ext2 user,noauto,rw 0 0 /udev/kalahari1 /mnt/kalahari vfat,ext2 user,noauto,rw 0 0

in /etc/fstab to enable mountingfrom other places. in /etc/fstab

from other places. This is great fun ... and remember, there are otherdeserts on the planet. This is great fun ... and remember, there are other deserts on the planet.

Formalities

This document is copyrighted (c) 2003 Niko Sauer and isdistributed under the terms of the Linux Documentation Project(LDP) license, stated below. This document is copyrighted (c) 2003 Niko Sauer and is distributed under the terms of the Linux Documentation Project (LDP) license, stated below. Unless otherwise stated, Linux HOWTO documents arecopyrighted by their respective authors. Linux HOWTO documents maybe reproduced and distributed in whole or in part, in any mediumphysical or electronic, as long as this copyright notice isretained on all copies. Commercial redistribution is allowed andencouraged; however, the author would like to be notified of anysuch distributions. Unless otherwise stated, Linux HOWTO documents are copyrighted by their respective authors. Linux HOWTO documents may be reproduced and distributed in whole or in part, in any medium physical or electronic, as long as this copyright notice is retained on all copies. Commercial redistribution is allowed and encouraged; however, the author would like to be notified of any such distributions. All translations, derivative works, or aggregate worksincorporating any Linux HOWTO documents must be covered under thiscopyright notice. That is, you may not produce a derivative workfrom a HOWTO and impose additional restrictions on itsdistribution. Exceptions to these rules may be granted undercertain conditions; please contact the Linux HOWTO coordinator atthe address given below. All translations, derivative works, or aggregate works incorporating any Linux HOWTO documents must be covered under this copyright notice. That is, you may not produce a derivative work from a HOWTO and impose additional restrictions on its distribution. Exceptions to these rules may be granted under certain conditions; please contact the Linux HOWTO coordinator at the address given below. In short, we wish to promote dissemination of thisinformation through as many channels as possible. However, we dowish to retain copyright on the HOWTO documents, and would like tobe notified of any plans to redistribute the HOWTOs. In short, we wish to promote dissemination of this information through as many channels as possible. However, we do wish to retain copyright on the HOWTO documents, and would like to be notified of any plans to redistribute the HOWTOs. If you have any questions, please <contactnikos AT friedrichs DOT up DOT ac DOT za> If you have any questions, please contact

Email: <nikos AT friedrichs DOT up DOT ac DOT za>

Disclaimer

No liability for the contents of this documents can be accepted.Use the concepts, examples and other content at your own risk.As this is a new document, there may be errorsand inaccuracies, that may of course be damaging to your system.Proceed with caution, and although this is highly unlikely,the author do not take any responsibility for that. No liability for the contents of this documents can be accepted. Use the concepts, examples and other content at your own risk. As this is a new document, there may be errors and inaccuracies, that may of course be damaging to your system. Proceed with caution, and although this is highly unlikely, the author do not take any responsibility for that. All copyrights are held by their by their respective owners, unlessspecifically noted otherwise.Use of a term in this documentshould not be regarded as affecting the validity of any trademarkor service mark. All copyrights are held by their by their respective owners, unless specifically noted otherwise.Use of a term in this document should not be regarded as affecting the validity of any trademark or service mark. Naming of particular products or brands should not be seenas endorsements. Naming of particular products or brands should not be seen as endorsements. You are strongly recommended to take a backup of your systembefore major installation and backups at regular intervals. You are strongly recommended to take a backup of your system before major installation and backups at regular intervals.

Credits

I wish to express my deep gratitude towards my sons Paul andPhilip who in 1995 persuaded me to move to Linux. Philip gave meprofound advice and insights which made this project a pleasantlearning experience. I wish to express my deep gratitude towards my sons Paul and Philip who in 1995 persuaded me to move to Linux. Philip gave me profound advice and insights which made this project a pleasant learning experience. Emma Jane Hogbin, in her review, raised a number of importantquestions which served to improve this document immensely. Mysincerest thanks to her. Emma Jane Hogbin, in her review, raised a number of important questions which served to improve this document immensely. My sincerest thanks to her.

Feedback

Users of this document are most welcome to email me withsuggestions on the improvement of the presentation and possibleadditions to the scope of it. Users of this document are most welcome to email me with suggestions on the improvement of the presentation and possible additions to the scope of it.


CategoryHowto CategoryDraft

USB_Flash_Memory_HOWTO (last edited 2008-08-31 21:58:09 by RobertSpencer)