Tag Archives: USB

OpenWRT Boot from USB device

http://www.nslu2-linux.org/wiki/OpenWRT/BootFromUsbDevice

This guide covers fast (12mbit/s) USB and ext2 filesystem only. Make sure you install package kmod-usb2 and load module ehci-hcd or your disk will be (very, hdparm -t < 12,5mbit/s) slow. This tutorial is for OpenWRT (kamikaze, backfire) with 2.6 kernel, but it will probably work for other devices too running OpenWRT on a 2.6 kernel. If you are unsure what kernel you have use: uname -a which will give you something like this:

Linux OpenWrt 2.6.21.6 #1 Fri Dec 28 11:04:49 UTC 2007 armv5teb unknown
First of all make sure you have all the modules required:
ipkg update
ipkg install kmod-usb-core kmod-usb-uhci kmod-scsi-core kmod-usb-storage kmod-fs-ext2 kmod-fs-ext3
Then load these modules:
insmod ext2
insmod jbd
insmod ext3
Install e2fsprogs and fdisk: ipkg install e2fsprogs fdisk Partition your disk. I have a 1GB Verbatim and used 700 MB for ext2 and 300 MB for swap. fdisk /dev/sdX <- change this to your device Then format your partition: mke2fs /dev/sdX1 <- change this to your device Mount it and copy the filesystem to your USB-device:
mount -t ext2 /dev/sda1 /mnt
mkdir /tmp/root
mount -o bind /rom /tmp/root
cp /tmp/root/* /mnt -a
umount /tmp/root
umount /mnt
vi /etc/init.d/pivotroot
copy and paste this into /etc/init.d/pivotroot
#!/bin/sh
# change this to your boot device
boot_dev="/dev/sda1"
/sbin/hotplug2 --override --persistent --max-children 1 --no-coldplug &
for module in usbcore uhci scsi_mod sd_mod usb-storage jbd ext2 ext3 ; do {
        insmod $module
        }; done

        # this may need to be higher if your disk is slow to initialize
        sleep 30s
        # mount the usb stick
        mount "$boot_dev" /mnt
        # if everything looks ok, do the pivot root
killall hotplug2
        [ -x /mnt/sbin/init ] && {
                mount -o move /proc /mnt/proc && \
                pivot_root /mnt /mnt/mnt && {
                mount -o move /mnt/dev /dev
                mount -o move /mnt/tmp /tmp
                mount -o move /mnt/jffs2 /jffs2 2>&-
                mount -o move /mnt/sys /sys 2>&-
          }
}
Then make it executable: chmod a+x /etc/init.d/pivotroot Now, make the symlink so it will start at boot time: ln -s /etc/init.d/pivotroot /etc/rc.d/S10pivotroot replace everything in /etc/init.d/rcS with this vi /etc/init.d/rcS
#!/bin/sh
# Copyright (C) 2006 OpenWrt.org
if test $2 == "boot" ; then
/etc/init.d/pivotroot
fi
{
 for i in /etc/rc.d/$1*; do
 $i $2 2>&1
 done
} | logger -s -p 6 -t '' &

Now reboot and telnet to your slug

Use 'passwd' to set your new root password

exit
Log in with ssh. To make sure it works type df -h. Which will give you something like this

Filesystem                Size      Used Available Use% Mounted on
/dev/sda1                 1.0M      1.0M         0 100% /mnt/rom
/dev/mtdblock5            5.4M    752.0k      4.6M  14% /mnt/jffs
mini_fo:/jffs             1.0M      1.0M         0 100% /mnt
/dev/sda1               656.1M     19.0M    603.7M   3% /
Fixup resolv.conf symlink:
rm /etc/resolv.conf
ln -s /tmp/resolv.conf.auto /etc/resolv.conf
If you created a swap partition you may want to use it:
ipkg update
ipkg install swap-utils
mkswap /dev/sda2
swapon /dev/sda2
Make it start at boot time:
echo "#!/bin/sh" >> /etc/init.d/swapspace
echo "swapon /dev/sda2" >> /etc/init.d/swapspace
chmod a+x /etc/init.d/swapspace
ln -s /etc/init.d/swapspace /etc/rc.d/S99swapspace
Use free to see if it worked: free
              total         used         free       shared      buffers
  Mem:        30472        11628        18844            0          960
 Swap:       297192            0       297192
Total:       327664        11628       316036
OpenWRT full backup
tar cv /bin /etc /home /lib /root /sbin /tmp /usr /www > openwrt.tar
scp openwrt.tar user@backup-serve:/backup

How to install Linux Mint from the USB stick

http://community.linuxmint.com/tutorial/view/744

Using the Terminal

It is really simple. Go to a Terminal and type:

sudo dd if=~/Desktop/linuxmint.iso of=/dev/sdx oflag=direct  bs=1048576

Where ‘~/Desktop/linuxmint.iso’ is the name and location of your downloaded image (located at the desktop in this example) and ‘/dev/sdx’ is the target USB drive. If your system doesn’t support ‘oflag=direct’, you can just leave it out as it is simply intended to speed up the process a bit.

If you don’t know about the target USB drive path, run this command and figure out your destination drive.

sudo fdisk -l
Warning: Make sure to set the correct device path, as this process will delete all data that was on the specified device previously!

Remember, don’t include an integer for the USB drive, e.g. ‘/dev/sdx1’, as it would refer to the existing partition on that drive and not the drive itself.

When the USB has been properly created by ‘dd’, there should be an output similar to this:

sudo dd if=~/Desktop/linuxmint.iso of=/dev/sdb oflag=direct bs=1048576
706+1 records in
706+1 records out
740601856 bytes (741 MB) copied, 91.7024 s, 8.1 MB/s

Using imageWriter

To install imageWriter, open a Terminal and run this command:

apt install usb-imagewriter

Launch imageWriter from the menu, select your ISO image and your USB device and press “Write to device”.

Note: Some early versions of imageWriter only accept .img files. If the tool won’t accept .iso files, simply rename the ISO file from .iso to .img.

Linux on USB flashdisk

http://www.cyrius.com/debian/nslu2/linux-on-flash.html

If you are running your Linux system on a USB flash key, there are a number of things you might want to do in order to reduce the wear and tear on the underlying flash device (as it only supports a limited number of writes).

The ext3 filesystem per default writes metadata changes every five seconds to disk. This can be increased by mounting the root filesystem with the commit=N parameter which tells the kernel to delay writes to every N seconds.

The kernel writes a new atime for each file that has been read which generates one write for each read. This can be disabled by mounting the filesystem with the noatime option.

Both of the above can be done by adding e.g. noatime,commit=120,… to /etc/fstab. This can also be done on an already mounted filesystem by running the command:

mount -o remount,noatime,commit=120 /

The system will run updatedb every day which creates a database of all files on the system for use with the locate command. This will also put some stress on the filesystem, so you might want to disable it by adding
exit 0
early in the /etc/cron.daily/find script.

syslogd will in the default installation sync a lot of log files to disk directly after logging some new information. You might want to change /etc/syslog.conf so that every filename starts with a – (minus) which means that writes are not synced immediately (which increases the risk that some log messages are lost if your system crashes). For example, a line such as:

kern.*                          /var/log/kern.log

would be changed to:
kern.*                          -/var/log/kern.log

You also might want to disable some classes of messages altogether by logging them to /dev/null instead, see syslog.conf(5) for details.
In addition, syslogd likes to write -- MARK -- lines to log files every 20 minutes to show that syslog is still running. This can be disabled by changing SYSLOGD in /etc/default/syslogd so that it reads

SYSLOGD="-m 0"

After you’ve made any changes, you need to restart syslogd by running
/etc/init.d/syslogd restart

If you have a swap partition or swap file on the flash device, you might want to move it to a different part of the disk every now and then to make sure that different parts of the disk gets hit by the frequent writes that it can generate. For a swap file this can be done by creating a new swap file before you remove the old one.
If you have a swap partition or swap file stored on the flash device, you can make sure that it is used as little as possible by setting /proc/sys/vm/swappiness to zero.

The kernel also has a setting known as laptop_mode, which makes it delay writes to disk (initially intended to allow laptop disks to spin down while not in use, hence the name). A number of files under /proc/sys/vm/ controls how this works:

/proc/sys/vm/laptop_mode: How many seconds after a read should a writeout of changed files start (this is based on the assumption that a read will cause an otherwise spun down disk to spin up again).

/proc/sys/vm/dirty_writeback_centisecs: How often the kernel should check if there is “dirty” (changed) data to write out to disk (in centiseconds).

/proc/sys/vm/dirty_expire_centisecs: How old “dirty” data should be before the kernel considers it old enough to be written to disk. It is in general a good idea to set this to the same value as dirty_writeback_centisecs above.

/proc/sys/vm/dirty_ratio: The maximum amount of memory (in percent) to be used to store dirty data before the process that generates the data will be forced to write it out. Setting this to a high value should not be a problem as writeouts will also occur if the system is low on memory.

/proc/sys/vm/dirty_background_ratio: The lower amount of memory (in percent) where a writeout of dirty data to disk is allowed to stop. This should be quite a bit lower than the above dirty_ratio to allow the kernel to write out chunks of dirty data in one go.

All of the above kernel parameters can be tuned by using a custom init script, such as this example script. Store it to e.g. /etc/init.d/kernel-params, make it executable with

chmod a+x /etc/init.d/kernel-params

and make sure it is executed by running
update-rc.d kernel-params defaults

  • Note
  • Most of these settings reduce the number of writes to disk by increasing memory usage. This increases the risk for out of memory situations (which can trigger the dreaded OOM-killer in the kernel). This can even happen when there is free memory available (for example when the kernel needs to allocate more than one contiguous page and there are only fragmented free pages available).

    As with any tweaks, you are advised to keep a close eye on the amount of free memory and adapt the tweaks (e.g. by using less aggressive caching and increasing the swappiness) depending on your workload.