Tuesday, September 24, 2013

This program will auto mount USB device and count number when systme power on.

#/bin/bash
# Program:
#       This program will auto mount USB device and count number when systme power on.
# History:
# 2013/09/24 Mace First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

#test file path
file_path="/tmp"

# Open an file to save fdisk information.
touch /mnt/information

# Store fdisk -l info to file information.
fdisk -l | grep "FAT32" > /mnt/information

# cut dev path to dev_path.
dev_path="`head /mnt/information | tr -s ' ' | cut -f1 -d' '` "

# Judge USB device is on plug.
if [ "$dev_path" == " " ];then
  echo "Can't found USB device." 
  exit 0
else
  echo "Found USB device at: "$dev_path
fi

# mount USB to /tmp folder.
mount -t vfat $dev_path $file_path

if [ -f "$file_path"/test ];then
  count_num="`head $file_path/test`"
  echo "$count_num"
else
  echo "file not found. Create test file init number to 0."
  touch $file_path/test
fi

if [ "$count_num" == " " ];then
  count_num=0
  echo "test file is empty. init count_unm=0"
else
  count_num=$(($count_num + 1))
  #echo "$count_num"
  echo $count_num > $file_path/test
fi 

#umount USB device.
umount "$file_path"
 
exit 0

Monday, September 23, 2013

How to make linux run on RamDisk


        This topic is talking about making Linux run on Ramdisk and create your own file system.
        Also we can run script after system initialed.
We need prepare some tool and device as below:
1.      USB device with FAT format. We can use format tool “HPUSBFW”.
2.      Syslinux: Help us to loader Linux kernel and file system and also kernel boot up parameter, like: Console, Kernel file name..
3.      Busybox: Busybox include Linux most common use command and help you to generate file system.

========================================================================
There 4 part in this topic:
First: Create a bootable Linux USB device.
1.      Download syslinux(v6.01) tool and unzip to USB device. URL: https://www.kernel.org/pub/linux/utils/boot/syslinux/
2.      Open cmd with administer.


3.      Find your USB device disc number my example is “F:”
4.      Enter “FF:\syslinux-6.01\bios\win32” folder and type command: “syslinx.exe –ma F:”

       For the DOS and Windows installers, the -m and -a options can be used on hard drives to write a Master Boot Record (MBR), and to mark the specific partition active. On boot time, by default, the kernel will be loaded from the image named LINUX on the boot floppy.
5.      Now finished the first part. We can found more information on syslinux.
6.      Create and configure syslinux.cfg.
TIMEOUT 200
serial 0 115200
default linux (Auto search kernel file: “linux”, if can’t find linux we need enter file name.)
LABEL linux
KERNEL linux (*Kernel file name must same as KERNEL “linux”)
APPEND ro initrd=root.gz rootfstype=ext2 root=/dev/ram0 rw init=/linuxrc console=tty0 console=tty0,115200n8 acpi=off raid=noautodetect
========================================================================
Second: Configure Kernel and build image.
1.      Check Linux system version and download kernel source.
2.      For example:”uname –a” get the kernel version. Like: Linux localhost.localdomain 2.6.32-358.el6.i686 #1..
3.      Download Kernel source: “wget https://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.32.18.tar.gz /usr/src/kernels”
4.      tar kernel source: “tar -xvjf linux-2.6.32.18.tar.gz .”
5.      Enter folder configure make menu: “make menuconfig”.
è Enter Genernal setup: Set 
[*] enable deprecated sysfs features which may confuse old userspace tools
[*] Initial RAM filesystem and RAM disk (initramfs/initrd) support
è Enter Device Drivers 
---> Generic Driver Options  --->
[*] Create a kernel maintained /dev tmpfs (EXPERIMENTAL)
[*]   Automount devtmpfs at /dev
--->[*] Block devices  --->
           <*>   RAM block device support 
           (16)    Default number of RAM disks     
(16384) Default RAM disk size (kbytes)
è Enter Executable file formats / Emulations  --->
            [*] Kernel support for ELF binaries
            [*] Write ELF core dumps with partial segments   
            <*> Kernel support for a.out and ECOFF binaries  
<*> Kernel support for MISC binaries  
è Enter  File systems  --->  
       <*> Second extended fs support                 
         [*]   Ext2 extended attributes                
         [*]   Ext2 POSIX Access Control Lists          
         [*]   Ext2 Security Labels        
         [*]   Ext2 execute in place support 
       DOS/FAT/NT Filesystems  --->
         <*> MSDOS fs support
         <*> VFAT (Windows-95) fs support      
         (437) Default codepage for FAT        
         (ascii) Default iocharset for FAT        
         < > NTFS file system support 
6.      Exit & Save config
7.      make clean
8.      make bzImage.
9.      Find bzImage in the /arch/x86/boot/bzImage
10.  Change file name to “linux” and copy to USB device.

========================================================================
Third: Create Ext2 Block space to generator filesystem.
1.      Download Busybox tool. Example: busybox-1.20.2, URL: http://www.busybox.net/
2.      tar busybox file to linux. /root/Desktop/
3.      Create an empty file that you can format as a filesystem:
   cd /tmp/
          mkdir initrd | chmod 755 initrd                   ß Create empty folder under /tmp/.
          dd if=/dev/zero of=rootfs bs=4k count=4000       ß Create 16M empty file that you can format as a filesystem
      mke2fs -i 4096 -m 0 -F /tmp/rootfs               ß Format the rootfs file with a filesystem
      mount -o loop rootfs initrd/                  ß Mount the file on a mountpoint so we can place files in it
      cd initrd/ 
mkdir dev etc etc/rc.d bin proc mnt tmp var     ß Making necessary folder
chmod 755 dev etc  etc/rc.d bin mnt tmp var
chmod 555 proc
4.      Enter Busybox folder and find /examples/bootfloppy/bootfloppy.txt to start making a root file system. You may find the tools at /examples/bootfloppy/
5.      Make device files in /dev: This can be done by running the 'mkdevs.sh' script. If you want the gory
        details, you can read the script.
6.      Make necessary files in /etc: For this, just cp -a the etc/ directory onto rootfs. Again, if you want all the details, you can just look at the files in the dir.
7.      Modify /etc/inittab as below:
    ::sysinit:/etc/init.d/rcS
::respawn:-/bin/sh
::restart:/sbin/init
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a –r
8.      Modify /etc/fstab
    poc             /proc    proc    defaults    0   0
/dev/ram0       /        ext2    defaults    0   0

========================================================================
Fourth: busybox compiler and install.
1.      Enter Busybox folder: make menuconfig
Busybox Settings  --->
  General Configuration  --->
     [*] Don't use /usr
Build Options  ---> 
   [*] Build BusyBox as a static binary (no shared libs)
      [*] Build with Large File Support (for accessing files > 2 GB)  
   ()  Cross Compiler prefix (*If you use another compiler tool please assign the path. )
Coreutils  ---> Select all items.
Debian Utilities  ---> Select all items.
Editors  --->  Don’t need
   [ ]   Allow vi to display 8-bit chars (otherwise shows dots)
   [ ]     Enable regex in search and replace
Finding Utilities  ---> Select all items.
Init Utilities  --->
     [*]   Support reading an inittab file
Linux System Utilities >
[*] mount
[*] umount
[*] Support loopback mounts
[*] Support for the old /etc/mtab file
       Miscellaneous Utilities  --->
            [*] devfsd (obsolete)
       Shells  --->
            [*] ash
      
2.      Exit & Save config.
3.      “make clean”
4.      “make CONFIG_PREFIXED=/tmp/initrd install”
5.      “umount initrd”
6.      “gzip -9 –c rootfs > root.gz”
7.      Copy root.gz to USB device
8.      Check USB device include files: linux, root.gz, syslinux.cfg
=================================================================================================
Troubleshooting:
1.      Make menuconfig Error !!
yum -y install ncurses ncurses-devel
2.      Need compiler busybox in static, we need install gcc compiler patch for this.
yum -y install glibc-devel.i686 glibc-devel
sudo yum install glibc-static libstdc++-static
3.      Kernel panic problem: can’t mount root unknow device.
   Please make sure kernel configure is correct and kernel image file name must be same as syslinux.cfg item: KERNEL XXXX.
4.      Kernel panic problem: can’t access init=/linuxrc.

   Please use same compiler to build kernel and busybox.