Stories
Slash Boxes
Comments

News for nerds, stuff that matters

Slashdot Log In

Log In

Create Account  |  Retrieve Password

Optimizing Linux Use On a USB Flash Drive?

Posted by timothy on Wed Dec 03, 2008 05:18 PM
from the right-here-in-my-pocket dept.
Buckbeak writes "I like to carry my Linux systems around with me, on USB flash drives. Typically, SanDisk Cruzers or Kingston HyperX. I encrypt the root partition and boot off the USB stick. Sometimes, the performance leaves something to be desired. I want to be able to do an 'apt-get upgrade' or 'yum update' while surfing but the experience is sometimes painful. What can I do to maximize the performance of Linux while running off of a slow medium? I've turned on 'noatime' in the mount options and I don't use a swap partition. Is there any way to minimize drive I/O or batch it up more? Is there any easy way to run in memory and write everything out when I shut down? I've tried both EXT2 and EXT3 and it doesn't seem to make much difference. Any other suggestions?"
+ -
story

Related Stories

This discussion has been archived. No new comments can be posted.
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More
Loading... please wait.
  • by bluefoxlucid (723572) on Wednesday December 03 2008, @05:22PM (#25981599) Journal
    Get a swap partition so that you can free up some memory for disk cache, for one. Enable laptop mode too.
    • Re: (Score:3, Interesting)

      Well, laptop mode's an obvious. However, I would not enable swap at all, or at least put it on the FS somewhere. If you do that, you wont be able to hibernate properly. Nor will hibernation work properly if you encrypt swap (partition).

      You're looking at standby or poweroff events due to encrypted partitions and systems. That's the pain you pay for. There is another way... TCPA, and IBM has writen the requisite linux tools to utilize it properly. Just everybody's too scared to use it properly.

    • Just to clarify for anyone who misunderstood this, I believe bluefoxlucid was referring to a swap partition *on a local hard drive* and not on the USB flash drive. For example, if you're booting on what typically is a Linux machine there is a very good chance that there is already a local swap partitioned off. Just be wary about what you may leave behind if you're not careful.
      • No, put it on flash. It'll be faster than a local hard drive and won't leave crap behind; though in practice, the seek speed probably won't make a difference unless you're about 2 seconds away from resetting the system for not responding anymore.
        • Re: (Score:3, Informative)

          by Anonymous Coward

          No, put it on flash. It'll be faster

          No, it will most certainly be much slower. The memory page size is 4K, whereas the typical flash block size is much bigger. Flash can only be erased in blocks, so to write one small 4K page to flash memory, the controller has to read a whole block, erase the flash block and write back the modified block. That's why most flash storage devices suck so badly at storing many small files, even though the continuous write speed isn't too bad.

          The performance penalty could be hidden by the OS: Instead of swapping o

      • Re: (Score:2, Interesting)

        Or on a mostly-Windows machine, you can always use a local-drive-based swapfile (there's likely no swap partition). Mount NTFS drive, create a "myswapfile" somewhere innocuous, mount it as a loopback swap partition (-o loop). Delete it on unmount (as part of your shutdown process) -- if you're truly paranoid you can even take the time to scrub the sectors your swap was using.

        Don't swap to the flashdrive -- you'll just hog USB bandwidth that you need for reading & writing real files off your root parti

      • by bluefoxlucid (723572) on Wednesday December 03 2008, @05:57PM (#25982083) Journal
        FUD. Learn wear leveling.
        • Re: (Score:2, Informative)

          no matter, you STILL don't want to be swapping to flash! better to load up on RAM and forget swap.
          • The file system you use to store your files is way more intensive than your swap partition; OR your mouse isn't moving anymore, you can't close windows, and you have to unplug it and plug it back in.
        • by Mr Z (6791) on Thursday December 04 2008, @01:57AM (#25985737) Homepage Journal

          Apparently wear leveling is no panacea. Some devices wear-level only within a zone, and so a lot of activity in a hot zone can still burn that zone out. I guess such an algorithm only really works well when writes are distributed across zones evenly.

          See page 5 in this document [micron.com] for a description of zone-based wear leveling.

          • by SanityInAnarchy (655584) <ninja@slaphack.com> on Thursday December 04 2008, @12:35PM (#25991215) Journal

            Method doesn't work when improperly implemented. News at 11.

            You're basically saying that wear-leveling is no panacea, because not all devices have a good wear-leveling algorithm. Wouldn't the solution, then, to be to demand devices which have a good algorithm, and to reward manufacturers who solve this problem with dollars and word of mouth?

            Of course, the sad part is, the devices would probably be simpler, and cheaper to manufacture, if they didn't pretend to be block devices. Only problem is, then OSes (probably filesystems) would have to implement wear-leveling on their own, and Linux is the only one I know of which currently does that.

            Still, it would be nice if we could fix that problem in software, rather than letting manufacturers get it wrong.

            • by Mr Z (6791) on Thursday December 04 2008, @12:52PM (#25991485) Homepage Journal

              Method doesn't work when improperly implemented. News at 11.

              My point is that this seems to be more prevalent than people realize.

              You're basically saying that wear-leveling is no panacea, because not all devices have a good wear-leveling algorithm. Wouldn't the solution, then, to be to demand devices which have a good algorithm, and to reward manufacturers who solve this problem with dollars and word of mouth?

              The problem with short product lifecycles is that you might not realize you bought a turd until after it eats your data, and there's no time for word-of-mouth to spread. And with so many no-name USB sticks out there, how can you really tell which ones are the turds and which ones aren't? There really isn't a good feedback mechanism here. You have some hope paying high dollar for a name brand, but I wouldn't put too much stock in it.

              Also, there are conflicting product goals: Dynamic wear leveling, as described in the PDF I linked above, gives faster write performance. Static wear leveling gives longer silicon lifetime. Which algorithm will generate better word of mouth? It's not clear, although I suspect faster write performance will generate better word-of-mouth while the product is still on store shelves.

              Of course, the sad part is, the devices would probably be simpler, and cheaper to manufacture, if they didn't pretend to be block devices. Only problem is, then OSes (probably filesystems) would have to implement wear-leveling on their own, and Linux is the only one I know of which currently does that.

              You would have to expose a number of low-level details, such as erase block size and other aspects of physical organization to the OS. And, you'd have to teach many OSes about how to talk to you, such as "block erase takes 5ms; device is unresponsive during erasure". Until there's a standardized way of doing this across platforms, faking yourself as a block device is an easy way to get broad compatibility with anything that understands the USB storage device (or SD card, or whatever format you're working with) profile.

              Still, it would be nice if we could fix that problem in software, rather than letting manufacturers get it wrong.

              You can still implement software wear leveling over the abstracted interface. Unless the device actively tries to work against you, it should help.

      • Hey guess what? Every access to FAT writes to the same area of the file system (the FAT), and this is what destroys old-style floppy disks. Do you see Windows chewing up USB drives like crazy? No? Do you think you swap that much? Readyboost is more intensive than swap partitions (unless your system has decided to grind to a halt due to swap thrashing, and it's time to press the big red button).
        • Re: (Score:2, Funny)

          by Anonymous Coward

          Does anyone actually use Readyboost??

          • Re: (Score:3, Informative)

            Yes and it works quiet well in real world performance testing.

            Even a 1gb or 2gb stick can improve overall responsiveness enough that makes a tight XP installation feel slow. This is especially true with gaming and applications that push the limits of your RAM shoving the HD cache down.

            There are a few crap Flash drives, that just don't perform well, avoid them and it works like promised.

              • Re: (Score:3, Informative)

                Nowhere does op claim to have used ReadyBoost on XP, he compared Vista w/ ReadyBoost to a 'tight XP installation'

                Meaning Vista+ReadyBoost is apparently 'faster' as far as responsiveness goes than a clean and properly tweaked windows XP installation.

                I still think pics or it didn't happen, but never was ReadyBoost on XP mentioned.
        • Re: (Score:3, Informative)

          Every access to FAT writes to the same area of the file system (the FAT), and this is what destroys old-style floppy disks.

          That's why people use a flash file system http://en.wikipedia.org/wiki/File_system#Flash_file_systems [wikipedia.org]. http://en.wikipedia.org/wiki/YAFFS [wikipedia.org] is a good choice; it seems to be used in Android.

          • Re: (Score:3, Informative)

            Flash file systems are for raw NAND, not NAND behind a microcontroller handling wear leveling. FAT would kill the chip on the XO board, but the same chip in a USB drive would be fine.
            • Those chips are about as intelligent as a rubber band. There's currently just no way to implement sane wear filesystem-independent wear levelling in a tiny chip like that. In fact, ignoring the hints of the filesystem in general is a terrible approach, since the filesystem knows best how it can safely move its data around, and a flash-optimized file system is well worth using.

        • Not every access... every WRITE.
      • The best thing to do is make a ram disk and use that as swap space.

        So the OS can use part of the memory as swap, rather than memory, and when the part it is using as memory is full it can copy it to the part it is using as disk? (Except it won't, because if I recall correctly the RAM disk, along with disk buffers, are all dynamically assigned in memory, so when you need to swap there's nowhere to swap to.)

        Were you trying to be funny? Or just unintentionally managed it?

        • Obviously that won't do any good. You need to create a RAM disk using virtual memory mapped to space on the USB drive.
              • Yert: It is not clear that the original post was a joke.

                "Whoooosh" is fun for playing elitist games. But I'm more interested in helping people avoid being "whoooosh"ed into a lot of lost time, effort, and perhaps compromised data because they missed something that another poster thought was obvious and funny.

  • USB has high cpu use and encrypted does not help a firewire based flash drive will be a lot faster. USB 3.0 may help but it too may still the limit of usb 2.0 high cpu use as well.

  • Hrm. (Score:5, Insightful)

    by Creepy Crawler (680178) on Wednesday December 03 2008, @05:25PM (#25981653)

    Well, the sucky thing about USB is it requires an inordinate amount of CPU. Normally this isnt a worry, but if you're using a encrypted loopback.. well ouch.

    One thing you could instead use is the SD card slot and a USB loader. If you choose a 8GB class 6 SD card, you could have plenty of room for whatever, and 6MB/s minimum speed. You're still going to take the CPU hit for encryption, but that is your choice. The big thing is to stay off the USB.

    • The internal SD card reader is typically connected to the USB bus so it probably won't change performance all that much.

      • Re:Hrm. (Score:4, Informative)

        by Creepy Crawler (680178) on Wednesday December 03 2008, @06:07PM (#25982199)

        True, but some laptop and desktop designs have went away from USB connectedness.

        For example, on my Thinkpad the SD reader is its own bus. The Bluetooth is a USB 1.1 (grr) device, so I need to rmmod the bt modules and remove usb old modules to be power efficient.

        It really is a crapshoot on what the computer maker put on the USB bus. I just lucked out.

    • Usually SD readers use a USB interface inside... So, it will also have a high CPU usage. The 6MB/s are a real deal, I have a class 6 card that does it, but... in large files you can see periodic freezes during transfer, I think, because of the buffer system used by the SD.
  • by Paradigm_Complex (968558) on Wednesday December 03 2008, @05:29PM (#25981711)
    One thing I've found really, really helps is to use smaller programs et al. While the difference in how long it takes to start up gnome-terminal vs rxvt or nautilus vs pcmanfm is minimal on a normal/modern desktop or laptop, the difference is substantial on a cheap USB flash drive. There's plenty of lists for lightweight applications, window managers etc for linux around online. In fact, I'll often just stick with terminal applications (moc, for instance).

    Another option, if you're booting on a box which has a good internet connection, is to ssh -X things over a network. Not only does this save a large amount of space, but I've found it's often faster to have a program like Firefox start on my snazzy box at home and ssh -X over than waiting for it to load off of my crappy usb drive.
  • USB1 vs USB2 (Score:5, Interesting)

    by Mendy (468439) on Wednesday December 03 2008, @05:30PM (#25981715)

    It might be that the poor performance occurs when you're on a computer that only has USB1 support. On Dells this was added later than you might expect.

    You might find you got better performance if you were to use a CD to hold most of the static software and the USB for just your home directory.

  • I know a little bit about this because I am one of the developers for TurnKey Linux [turnkeylinux.org], a new opensource project which builds small installable live CDs (we're up to 9) optimized for various mostly server-related tasks. I've been investigating supporting live USB mode.

    Your generic run-of-the-mill USB drive has about fourth-half the read/write performance of your hard drive nowadays (10-15MB/s). Since there are no moving parts (spinning platters), usually the seek times are very good.

    There are several things you can do to optimize the performance of an operating system running live from a USB drive:

    1) buy a faster USB drive: a good USB drive (e.g., Lexar JumpDrive [lexar.com]) can have 2-3 times the performance of a generic.

    2) Use a Linux distribution with a smaller footprint such as DSL [damnsmalllinux.org] (50MB) or Puppy Linux [puppylinux.org] (standard edition is 68MB): the smaller the footprint, the less your drive has to read, the faster your system will load.

    3) Try loading the operating system system into a ramdisk: many live USB distributions have the ability to load themselves into RAM. With some you have to add a cheatcode in the bootloader. Others do it by default if there is enough memory (usually not a problem with small distributions and modern computers).

    4) Try turning on readahead: many distributions which are designed to run from a live CD or live USB have a feature that reads ahead various files important to the boot sequence sequentially. Whether or not this helps depends on the characteristics of the storage medium you are using, but you should investigate it.

  • by agristin (750854) on Wednesday December 03 2008, @05:42PM (#25981899) Journal

    make sure you are on USB 2.0- interface can kill you.

    Also did you check the faq-

    No seriously:

    http://www.linux-usb.org/FAQ.html#i5 [linux-usb.org]

    especially the section on:

    Q: What is max_sectors and how should I use it?

    A:For USB Mass Storage devices (that is, devices which use the usb-storage driver) max_sectors controls the maximum amount of data that will be transferred to or from the device in a single command. As the name implies this transfer length is measured in sectors, where a sector is 512 bytes (that's a logical sector size, not necessarily the same as the size of a physical sector on the device). Thus for example, max_sectors = 240 means that a single command will not transfer more than 120 KB of data.

  • by nightfire-unique (253895) on Wednesday December 03 2008, @05:58PM (#25982103)

    Most USB flash drives are very slow, and rely on heavy cacheing to make them usable. That doesn't help when you need to write large amounts of data (ie. in an apt-get update/install).

    Some flash drives that advertise themselves as 10-15MiB/sec write capable peak out at only 1 or 2, and even less with small-block random I/O (since the erase-write cycle operates on relatively large blocks).

    Several vendors make specialized flash drives that are somewhat more expensive (ie. 20-50% over average), but perform much better.

    One is here: OCZ Turbo USB 2.0 [ocztechnology.com]

    • Re: (Score:3, Informative)

      You could also skip flash entirely and buy a very small hard drive [apricorn.com]. I've got a 60-gig USB drive from Apricorn that I carry around in my pocket, with an AES-encrypted root filesystem. Performance isn't spectacular, but it's certainly usable.

  • I/O to RAM is really fast compared to I/O to any block device (most USB keys appear to the host PC as block devices, because they have a little ARM7 or other low-power 16-bit MCU on them that emulates the IDE interface). So, maybe you could get a speedup by mounting any I/O intensive parts of your filesystem on a ramdisk. It might also save wear and tear on the flash (though MLC NAND is never going to be all that reliable). Here's an fstab excerpt showing one technique:
    /dev/ram0 /tmp tmpfs defaults,nodev,
    • On Debian-based distros like Ubuntu, /dev/shm is a ramdisk which will automatically resize, compared to /dev/ramX which gets a bit weird about changing size.
    • Re: (Score:3, Informative)

      /dev/ram0 /tmp tmpfs defaults,nodev,nosuid 0 3
      /dev/ram1 /var/run tmpfs defaults,nodev,nosuid 0 3
      /dev/ram2 /var/log tmpfs defaults,nodev,nosuid 0 3

      Just to let you know, tmpfs ignores the device path, you can put whatever you want in it (and you aren't actually using /dev/ram* with that).

      -:sigma.SB

  • Distros like Damn Small Linux know a mode where all frequent writes go to a RAM disk. Current flash hardware (especially disks - lesser cheap usb sticks) is already a lot smarter at "wear levelling", but a standard distro genereates a whole lot of small writing activity. It would be nice if there was a out-of-the-box way to make a server distro like Ubuntu Server USB ready. My file server could shut down its 5 disks completly until I access the files over the network. This could save quite some energy (and
  • by mbyte (65875) on Wednesday December 03 2008, @06:16PM (#25982309) Homepage

    Try out different filesystems, NILFS [nilfs.org] seems to be optimized for FLASH usage.

    Brtfs could also be worth a try.

    use the "noop" IO/Scheduler with nilfs:
    echo noop > /sys/block/sdX/queue/scheduler

    Postmark benchs on an usb-stick (shameless copied from here [forumdeluxx.de]:
    ext3 (mount -o noatime,noadirtime, normale Partition, scheduler cfq): 49 Transactions/s
    nilfs2 (Partition aligned 128k, scheduler noop, protection_period 10s): 588 Transactions/s

    • Re: (Score:3, Funny)

      by Anonymous Coward

      I'm holding out for the MILFS filesystem.

  • what I do (Score:4, Insightful)

    by ILuvRamen (1026668) on Wednesday December 03 2008, @06:20PM (#25982347)
    I have an old 10GB laptop drive inside of a very low profile USB enclosure and it runs like 35x faster than my USB drive or something absurd like that. It's a little more sensitive to bumps but it's not exactly expensive for 10GB drives. You can get a 6 pack of used one on ebay for about $3-7 each. Best of all, in a good enclosure, it still fits in your pocket.
  • busybox (Score:5, Insightful)

    by nategoose (1004564) on Wednesday December 03 2008, @06:21PM (#25982357)
    You might want to try replacing many programs as you can with busybox. It's versions of utilities are less complete than the standard gnu utilities, but they are all rolled into one binary, so most likely most of that binary will get cached in ram pretty early and stay there.
    Also, for any packages you build you should try to use the -Os option for gcc, and perhaps even strip the binaries to remove unused symbols and debug info.
    Building the system as though this was an embedded system with a small disk should be a win in most cases since fewer things have to go over the wire to load a file and more of the binaries can fit into cache.
  • I'm surprised no one mentioned this (or maybe someone has and it's just under my threshold), but not using a journalling filesystem can help tremendously. Having a whole system on a flash-based USB mass storage media formatted and mounted as ext3 is a great way to make sure the only bottleneck you'll ever have is disk I/O.

  • by Mr Z (6791) on Wednesday December 03 2008, @10:38PM (#25984601) Homepage Journal

    Dave Jones recently posted elsewhere his notes for improving things on the eee900. Several of the steps focus on getting proper performance out of the flash, and so would also apply to booting from a USB thumb drive or other flash media. Here's what he had to say:

    Making the eee 900 series suck less.

    Recently I've read about or spoken with a few people using Fedora on eeepc's who have been making some fairly big blunders without realising it. I've played with a few of these now, in their various incarnations.

    The current one I've been carrying around is the 900 model with a whopping 20GB of flash. It's quite deceptive, because there are actually two SSDs in there (one 4GB, and one 16GB)

    These ssds are also pretty damn awful performance-wise compared to the newer generation of SSDs, but short of opening it up and retrofitting something, there's not much that can be done. The tips below should at least make it more bearable.

    • First off, don't use the default partitioning scheme.

      By default, anaconda will choose to use lvm, and make a contiguous volume out of the two SSDs. This idea is fail, because the two disks aren't the same, and run at different speeds.

      # hdparm -t /dev/sda

      /dev/sda:
      Timing buffered disk reads: 108 MB in 3.04 seconds = 35.57 MB/sec

      # hdparm -t /dev/sdb

      /dev/sdb:
      Timing buffered disk reads: 86 MB in 3.05 seconds = 28.20 MB/sec

      So, don't do that. Just create regular partitions, and make sure you put / on the faster of the two disks (the 4GB one), and leave the 16GB one for /home

    • Next, the default filesystem will be ext3. You really don't want this.

      Given the journal is in a fixed location on disk, scribbling to it every time a file gets written is a great way to wear out the flash. Go with ext2. (Given that you've only got a few GB of flash anyway, a fsck doesn't take that long should you need to). Additionally, not having to write to the journal means that you're doing less IO, which is obviously a win when it's on such slow media.

    • This should go without saying - no swap.

      Not only for the flash wear problem in the previous bullet, but also, because it's slow as all hell. If you find you run out of ram and get stuff oom-killed in this setup, well, you probably need to add more ram, or consider a real laptop.

    • After installing, change the fstab so that everything gets mounted with noatime.

      Writes to the disk are just painful, so minimising them is the path to success here. It's doubtful that you'll be running anything on an eee that would actually care about atimes anyway.

    With these points taken into consideration, the eee isn't a half-bad machine. I still wouldn't want to be building kernels and such on it, but it's perfectly usable for email and such whilst travelling.

  • by blanchae (965013) on Thursday December 04 2008, @01:38AM (#25985651) Homepage
    There's quite a discrepancy over the speeds on flash drives. Cheap flash drives run USB 1.1 and the transfer rates are around 1 MB/sec. USB 2.0 drives range from slow 10 MB/sec to close to 40 MB/sec. The fastest drives will cost easily over $100. The size of the drive will slow the transfer rate also. 4 GB drives are faster than 8 GB and so on. Corsair GT drives have close to 35 MB/sec read speeds, write speeds are always dramatically slower. I've installed a bootable PBX in a Flash on a 4 GB Corsair USB flash drive [cadvision.com] with very acceptable performance for teaching purposes. I can see other LAMP bootable installations popping up. Each student can have their own server to configure and boot-up then take shut it down and take it home.