[root@localhost ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xfdff3257.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
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): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2610, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +50M
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (8-2610, default 8):
Using default value 8
Last cylinder, +cylinders or +size{K,M,G} (8-2610, default 2610): +500M
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xfdff3257
Device Boot Start End Blocks Id System
/dev/sdb1 1 7 56196 83 Linux
/dev/sdb2 8 72 522112+ 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
格式化磁盘
[root@localhost ~]# mke2fs -j /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
14056 inodes, 56196 blocks
2809 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=57671680
7 block groups
8192 blocks per group, 8192 fragments per group
2008 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 28 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]# mke2fs -j /dev/sdb2
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
130560 inodes, 522112 blocks
26105 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
64 block groups
8192 blocks per group, 8192 fragments per group
2040 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 38 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
创建两个目录,进行文件系统挂载
[root@localhost ~]# mount /dev/sdb1 /tye/boot
[root@localhost ~]# mount /dev/sdb2 /tye/sysroot
安装grub到目标磁盘
[root@localhost ~]# grub-install --root-directory=/tye /dev/sdb
Probing devices to guess BIOS drives. This may take a long time.
Installation finished. No error reported.
This is the contents of the device map /tye/boot/grub/device.map.
Check if this is correct or not. If any of the lines is incorrect,
fix it and re-run the script `grub-install'.
(fd0) /dev/fd0
(hd0) /dev/sda
(hd1) /dev/sdb
[root@localhost ~]# cd /tye/boot
[root@localhost boot]# ls
grub lost+found
[root@localhost boot]# cd grub
[root@localhost grub]# ls
device.map fat_stage1_5 iso9660_stage1_5 minix_stage1_5 stage1 ufs2_stage1_5 xfs_stage1_5
e2fs_stage1_5 ffs_stage1_5 jfs_stage1_5 reiserfs_stage1_5 stage2 vstafs_stage1_5
[root@localhost grub]# vim grub.conf
default=0
timeout=3
title tye linux
kernel /vmlinuz-2.6.32-754.el6.x86_64 ro root=/dev/sda2 selinux=0 init=/bin/bash
initrd /initramfs-2.6.32-754.el6.x86_64
创建一级目录,并复制文件
[root@localhost sysroot]# mkdir home opt sys proc bin sbin usr/{bin,sbin} etc/rc.d dev tmp media mnt -pv
mkdir: created directory `home'
mkdir: created directory `opt'
mkdir: created directory `sys'
mkdir: created directory `proc'
mkdir: created directory `bin'
mkdir: created directory `sbin'
mkdir: created directory `usr'
mkdir: created directory `usr/bin'
mkdir: created directory `usr/sbin'
mkdir: created directory `etc'
mkdir: created directory `etc/rc.d'
mkdir: created directory `dev'
mkdir: created directory `tmp'
mkdir: created directory `media'
mkdir: created directory `mnt'
通过脚本拷贝命令文件和库文件
#!/bin/bash
# 拷贝命令
destdir=/tye/sysroot
logfile=/root/scripts/copylog.txt
#拷贝命令
function copy_cmd() {
#获得路径
local cmd_dir=`dirname $1`
local cmd_name=`basename $1`
local cmd_destdir=$destdir$cmd_dir/$cmd_name
cp $1 $cmd_destdir &>/dev/null
#echo -e "\t $1 copyto $cmd_destdir successfully!!"
#记录复制日志
echo -e "\t $1 copyto $cmd_destdir successfully!!" >> $logfile
}
#拷贝依赖库文件
function copy_lib() {
#通过ldd获取命令所依赖的库文件列表
local lib_list=`ldd $1 | egrep -o "/.* "`
for i in $lib_list;
do
local lib_destdir=$destdir$i
if [ -e $destdir$i ];then
continue
elif [ -d `dirname $lib_destdir` ];then
cp $i $lib_destdir
#记录复制日志
echo -e "\t $i copyto $lib_destdir successfully!!" >> $logfile
else
mkdir -pv $lib_destdir &> /dev/null
cp $i $lib_destdir
#记录复制日志
echo -e "\t $i copyto $lib_destdir successfully!!" >> $logfile
fi
done
}
if [ -d $destdir ];then
mkdir -pv $destdir &>/dev/null
fi
#死循环,清空屏幕,然后输入命令
while true ;do
#tput clear
cat << EOF
###########################################################
*** 命令复制脚本 ***
*** 输入一个命令 ***
*** 按q,Q退出脚本 ***
###########################################################
EOF
read -p "请输入一个可执行命令[/bin/bash]:" cmd
if [ $cmd == 'q' -o $cmd == 'Q' ];then
unset cmd
break
fi
#判断输入的命令是否存在
if [ -n $cmd ];then
if type $cmd ;then
copy_cmd $cmd
copy_lib $cmd
else
echo "cmd is not existed"
fi
fi
done