HOWTO: Increase write speed by 'aligning' FAT32(通过对齐 FAT32 提高U盘访问速度, 转载)
I like many who visit this sub-forum, do so because they, or a program they used, formatted their USB stick at some point - afterwards noticing a substantial decrease in write performance on the drive. You can fix this by aligning the FAT32 file system to the 128K "blocks" or "cells" that flash memory uses. After much trial and error, and not being an expert in file systems, I was able to read up on the subject and figure out how to align my file system on the drive. The tools you need to perform these operations should be readily available in most any Linux distribution - don't let that scare you away, you can burn a Linux install CD and boot off of it to perform all of these commands without even needing to install Linux on your computer, for any that support a "Live CD" or "Try First before Install" feature, for instance Ubuntu is a popular distro that supports this feature. People who already understand aligning partitions, but want to align a FAT32 format to the aligned partition, can learn how in Step 3.
Following this procedure will destroy all contents on your USB stick, back up important files BEFORE starting - you have been warned.
Step One: Identify the drive and the device
Insert the stick into your computer after booting into Linux. Most distributions will automatically mount the file system currently on the drive. Mounting is like when Windows detects your drive and gives it a drive letter, same thing. Linux does not use drive letters, instead it uses a system of folders, which is much cleaner and more organised. Depending on your distro, you fill most likely find the drive mounted under /media/USBstickname or /mnt/USBstickname. Many distros will name the folder of the USB stick whatever the volume label was set to, i.e. "Patriot" for a stock formatted USB stick.
Next, open a terminal window if you are using a GUI, most modern Linux will be using a GUI, if you need this HOWTO: then you probably will be using a GUI. Find Terminal or Command Line or similar on the menus.
using the command df, which stands for 'disk free' we can see our drives free space, but it is also a convenient way to find the device name and mount point for our drive. It is important to identify the proper disk here; you don't want to format your hard drive now do you?
We will use df with a option, -h - this will produce so-called human readable information, in terms of Gigabytes and Megabytes, and not just long strings of bytes. Lets try:
username@computername:~$ df -h
Produces the following display: (simplified here)
Here we can see a few key things:Code:Filesystem Size Used Avail Use% Mounted on /dev/sda3 78G 37G 37G 50% / /dev/sdd1 15G 2.3G 13G 15% /media/MultiBoot <<< My USB DriveThis is all the information we need to move on to the next step.
- My USB device is /dev/sdd (/dev/sdd1 is the first partition of /dev/sdd)
- My drive is mounted at /media/MultiBoot
- The drive size is reported as 15G, which matches up to my Patriot Xporter XT Boost 16GB
Step 2: Unmount and prepare the partitions
First we unmount the drive, so that we don't have any files open and we can modify the drive at will.
username@computername:~$ umount /media/MultiBoot
If the command is a success, it will return you to the prompt without saying anything usually.
Note:Now we can use a cool program called fdisk to modify the partition table on the usb drive - this is sometimes referred to as the Master Boot Record or MBR for short, although technically, the partition table is only one part of the entire MBR itself. fdisk is a powerful and somewhat advanced program, but don't let that scare you: the worst that could happen is you could inadvertently destroy the contents of your entire hard disk(s) in your computer if you are not careful. As long as you pay attention to what you are typing, and don't blindly type in commands without understanding what each command does, you are in little danger.
If you had a drive window open in the GUI with the files on the USB showing, it will close, or sometimes change to another path, since you just removed the path it was using.
Why are we changing partitions? Formatting doesn't usually change partitions!Please read The Ted Ts'o article on SSD alignment before you start, so you can understand a bit of what it is we are doing here! He has more information in there about LVM and EXT4 file systems, which we won't be using on a USB stick (we could use EXT2/3/4 on a stick if it is for Linux use ONLY)
We are, because the standard partition table that Patriot has used is not 'aligned', at least not for our purposes. For instance, I checked my friends stock Patriot USB drive and it was quite 'odd' and none of the units lined up to a 128K boundary. To make our jobs easier, we will align the drive from the ground up so to speak, so that we KNOW where everything is while lining up data on our disks.
A neat trick is to make our heads and sectors per track line up by using 224 heads and 56 sectors per track. A USB stick is not a hard drive and it does not have sectors, tracks, OR heads - but it pretends to be a hard drive, and so, it has a drive geometry just like any hard drive would. This geometry business of heads/sectors/tracks and so on has no physical relationship to actual physical properties on our USB stick, it just helps to make a convenient way to talk about how data is organised on the drive. Note that there are other combinations that also make 128K alignment easy, but this is the one mentioned in several SSD and flash articles around the web. You will get different speed results by using different geometries, so if you are the inquisitive type, try several and see what you like best.Understand the "Sector"We will be using sudo along with fdisk in order to give the program 'super user' or in Windows speak 'admin' privileges. Sudo will often ask you for your password to continue.
The sector is something that you need to understand to proceed. It is the lowest or smallest size chunk of a disk that you access at the lowest levels while doing these things we are doing. A sector holds 512 bytes of information (very small). This small paragraph contains 372 bytes of information, and would just fit into one sector of a disk!
user@host:~$ sudo fdisk -H 224 -S 56 /dev/sdxxx
That command does:The command will display something similar to:
- sudo: use admin rights
- fdisk: start the fdisk partition program
- -H 224 : set the heads geometry to 224 heads
- -S 56 : use 56 sectors per track
- /dev/sdxxx : the device we wish to change, in this case you fill in the xxx with your device from step one.
[sudo] password for username:
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): p
We type 'p' at this prompt to display or 'print' the current partition table: You may want to type 'm' here so you can see a listing of all commands available in fdisk. Here's an example of 'p':
Your display will be different, this shows a drive that has already been configured for the 224/56 alignment.Code:Disk /dev/sdd: 16.0 GB, 16030629888 bytes 224 heads, 56 sectors/track, 2496 cylinders Units = cylinders of 12544 * 512 = 6422528 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x9a8001cf Device Boot Start End Blocks Id System /dev/sdd1 * 1 2496 15654784 c W95 FAT32 (LBA) Command (m for help):
You will need to perform the following steps in fdisk:I don't want to provide a step by step here, since it would be too easy to just blindly type it all in and mess something up. Read, learn and understand what you are doing before you jump in! Research fdisk (man fdisk) and get to know the program. It's easy, but it takes a little learning to grasp the concepts. The information above will guide you easily along once you know fdisk.
- Delete the existing partition
- Create a new partition that starts at cylinder 1 and ends at the end of the disk
- Mark the partition as a FAT32 (LBA) partition type, which is hex type "0x0C".
- Enter expert mode and change the beginning of data to sector 256. (Sector 256 x 512 bytes/sector = 128K (131,072 bytes)
- Return from expert mode and write the changes to disk.
- Press q to quit at any time while in fdisk before writing your changes if you feel uncomfortable or don't understand what you are doing.
Once you have written the changes, your partition table is aligned and you can begin formatting an aligned FAT32 onto the new partition! This next step is the one you probably won't find elsewhere on the Internet...
Step 3: The Final Frontier
Step 3: Making an aligned FAT32 formathttp://www.patriotmemory.com/forums/showthread.php?3696-HOWTO-Increase-write-speed-by-aligning-FAT32Anyone can figure out alignment of the partition from many sources already posted on the web. One thing I could not find was a guide on how to align that pesky FAT32 format so that each cluster (allocation unit) is aligned inside the boundaries of erase blocks. Well, the Linuxmkfs.vfat utility has all the options we need to make this aligned format possible!Final Thoughts
Please view this primer to FAT32 to get an idea of how the system is laid out on your disk. You normally have the first sector in the partition which is your Volume ID, which sits in the reserved space which is normally the first 32 sectors in the partition. This is followed by (2) copies of the File Allocation Tables, which vary in length when created depending on a variety of factors, including the chosen allocation unit size. The FAT size remains constant after creation. Please keep in mind for this discussion the physical size of a sector, which is 512 bytes.The Golden NuggetFirst we will format our disk using FAT32 paying no mind to reserved sectors. This will report to us our FAT size as so:
We will be changing the reserved sectors from the default value of 32, to a number that we will calculate from the reported size of the FAT tables after formatting. The goal will be to make the FAT tables end right at a 128K boundary, so each cluster of the file system will fall neatly within erase blocks on our disk!
user@host:~$ sudo mkfs.vfat -F 32 -n MultiBoot -s 32 -v /dev/sdd1
mkfs.vfat 3.0.7 (24 Dec 2009)
/dev/sdd1 has 224 heads and 56 sectors per track,
logical sector size is 512,
using 0xf8 media descriptor, with 31309312 sectors;
file system has 2 32-bit FATs and 32 sectors per cluster.
FAT size is 7641 sectors, and provides 977937 clusters.
Volume ID is 40c250bd, volume label MultiBoot .
The juicy bits are that 2nd to last line, it tells you the size on disk of 1 FAT table.
7641 sector FAT x 2 FATs x 512 bytes/sector = 7,824,384 bytes
The above formula shows you the exact amount of space the FAT tables are using at the beginning of your disk. This number is not usually going to be evenly divisible by 128K (131,072 bytes) as you can see 7,824,384 / 131072 = 59.695 erase block sized chunks. What we need to do is force the end of those FAT tables to end right at 60 blocks to do so we:
131,072 x 60 = 7,864,320 bytes in 60 erase blocks
7,864,320 - 7,824,384 = 39,936 bytes remainder
39,936 / 512 = 78 sectors remain
New reserved sector count for alignment = 78
Those are all the fundamentals required to align a FAT32 partition, so that clusters on the disk fall in line with the erase blocks of the physical cell medium.
An example of the format command required:
sudo mkfs.vfat -F 32 -R 78 -n MultiBoot -s 32 -v /dev/sddx
Breakdown:
- sudo - super user privledges
- mkfs.vfat - create a FAT file system
- -F 32 - 32 bit FAT (FAT32)
- -R 78 - Use 78 Reserved Sectors (instead of 32)
- -n MultiBoot - drive label, up to 11 characters
- -s 32 - 32 sectors per cluster 32 x 512 - 16K allocation unit size
- -v /dev/sddx - Device to format
Larger allocation units seem to make for better large file write speeds, I had the best performance at 64K allocation unit size, at around 15-16MB/second. As long as the calculations are all correct, you will notice much better write speeds on your newly aligned drive. They may not reach the speeds of a brand new drive, I don't have a new drive to compare side by side with this one. I went from about 6MB/sec write speed to about 12MB/second speeds on a drive using 32K cluster sizes. Read speeds are always around, or slightly above 30MB/second.Have fun,
If a utility for Windows could be found that allows for advanced partition alignment that fdisk has, and the formatting options that mkfs.vfat allows for, a user could avoid having to use an alternate OS for this setup.
John C.