#/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
胖胖~生活日記
紀錄生活雜事,與學習日記!
Tuesday, September 24, 2013
This program will auto mount USB device and count number when systme power on.
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.
Thursday, July 11, 2013
Linux command tar
常用tar指令整理:
壓縮: tar -jcv -f filename.tar.bz2 要被壓縮的檔案或目錄名稱。
[root@mylinux tmp]# tar -jcv -f /tmp/vitest.tar.bz2 /tmp/vitest
=======================================================================
查詢: tar -jtv -f filename.tar.bz2。
[root@mylinux tmp]# tar -jtv -f /tmp/vitest.tar.bz2
=======================================================================
解壓縮: tar -jxv -f filename.tar.bz2 -C 欲解壓縮的目錄。
[root@mylinux tmp]# tar -jxv -f vitest.tar.bz2 -C /
壓縮: tar -jcv -f filename.tar.bz2 要被壓縮的檔案或目錄名稱。
[root@mylinux tmp]# tar -jcv -f /tmp/vitest.tar.bz2 /tmp/vitest
Note: 此案例是給定絕對位置所以,壓縮後檔案位置會放到/tmp裡面,並觀察到解壓縮路徑會包括/tmp/vitest。
如果只要打包到/vitest資料夾則需要移除/tmp,並且在tmp目錄裡面做打包動作。
如果只要打包到/vitest資料夾則需要移除/tmp,並且在tmp目錄裡面做打包動作。
=======================================================================
查詢: tar -jtv -f filename.tar.bz2。
[root@mylinux tmp]# tar -jtv -f /tmp/vitest.tar.bz2
Note: 解壓縮前先觀察路徑。
=======================================================================
解壓縮: tar -jxv -f filename.tar.bz2 -C 欲解壓縮的目錄。
[root@mylinux tmp]# tar -jxv -f vitest.tar.bz2 -C /
Note:給定解壓縮目錄的路徑,須注意所在目錄,此案例是在/tmp裡面解壓縮所以只要給定/,如果所在位置不同請給絕對位置。
Linux command wget
An sample example to used wget to download an file from HTTP.
[root@mylinux /]# wget -P /tmp/vitest/ http://linux.vbird.org/linux_basic/0310vi/vi.big5
[Parameter]:
-P: Save file to directory.
[root@mylinux /]# wget -P /tmp/vitest/ http://linux.vbird.org/linux_basic/0310vi/vi.big5
[Parameter]:
-P: Save file to directory.
Wednesday, June 26, 2013
An 'array' with 5 elements which is a pointer
期望印出Array[5]中的數值:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int n =5;
int *Array,*aptr;
Array = (int*)malloc(n*sizeof(int));
memset(Array,0,5);
aptr = Array;
for(n = 0; n < 5; n++)
{
*aptr = n;
aptr++;
}
aptr = &Array[0];
for(n = 0; n < 5; n++)
{
printf("show array vaule = [%d]\n", (*aptr)++;
}
free(Array);
system("PAUSE");
return 0;
}
![]() |
| Result of this program. |
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int n =5;
int *Array,*aptr;
Array = (int*)malloc(n*sizeof(int));
memset(Array,0,5);
aptr = Array;
for(n = 0; n < 5; n++)
{
*aptr = n*2;
aptr++;
}
aptr = &Array[0];
for(n = 0; n < 5; n++)
{
printf("show array vaule = [%d]\n", (*aptr)++;
}
free(Array);
system("PAUSE");
return 0;
}
![]() |
那要如何列印出Array中的值[0][2][4][6][8]請看下例:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int n =5;
int *Array,*aptr;
Array = (int*)malloc(n*sizeof(int));
memset(Array,0,5);
aptr = Array;
for(n = 0; n < 5; n++)
{
*aptr = n*2;
aptr++;
}
aptr = &Array[0];
for(n = 0; n < 5; n++)
{
printf("show array vaule = [%d]\n", *aptr++;
}
free(Array);
system("PAUSE");
return 0;
}
取消括號以後才是對aptr位址做增加,順利得到我們要的值。
接下來再嘗試將++換到aptr前面看看會發生甚麼事情。
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int n =5;
int *Array,*aptr;
Array = (int*)malloc(n*sizeof(int));
memset(Array,0,5);
aptr = Array;
for(n = 0; n < 5; n++)
{
*aptr = n*2;
aptr++;
}
aptr = &Array[0];
for(n = 0; n < 5; n++)
{
printf("show array vaule = [%d]\n", ++*aptr);
}
free(Array);
system("PAUSE");
return 0;
}
將程式改成++*aptr,代表Array[0]裡面的值先做遞增再輸出,測試結果列印出來為[1][2][3][4][5]。
另外如果加上括號++(*aptr),測試輸出結果列印出來與上例相同。:
Friday, March 17, 2006
Thursday, December 22, 2005
Tuesday, December 20, 2005
English Daily - Learn American idioms, English conversation
English Daily - Learn American idioms, English conversation
This web has many different part for english learner like conversation and vocabulary and common mistakes in english.
This web has many different part for english learner like conversation and vocabulary and common mistakes in english.
Subscribe to:
Posts (Atom)








