脚本写到手抽筋

为运行于虚拟机上的CentOS 6添加一块新硬件,提供两个主分区

(1) 为硬盘新建两个主分区;并为其安装grub;
(2) 为硬盘的第一个主分区提供内核和ramdisk文件; 为第二个分区提供rootfs;
(3) 为rootfs提供bash、ls、cat程序及所依赖的库文件;
(4) 为grub提供配置文件;
(5) 将新的硬盘设置为第一启动项并能够正常启动目标主机;

为硬盘新建两个主分区

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
[root@centos ~]# fdisk -l  #先来看看磁盘情况啊,你不看,一上来直接搞,小心搞出事情!!

Disk /dev/sda: 171.8 GB, 171798691840 bytes
255 heads, 63 sectors/track, 20886 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: 0x00098b32

Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 20887 167259136 8e Linux LVM

Disk /dev/sdb: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 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: 0x00000000

Disk /dev/mapper/vg_centos-lv_root: 170.2 GB, 170196467712 bytes
255 heads, 63 sectors/track, 20691 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: 0x00000000

Disk /dev/mapper/vg_centos-lv_swap: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x00000000

不错不错,看到sdb了, 分的40G

开始分区

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
[root@centos ~]# fdisk /dev/sdb

Command (m for help): p

Disk /dev/sdb: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 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: 0x86662f67

Device Boot Start End Blocks Id System

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-5221, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-5221, default 5221): +20G

Command (m for help): p

Disk /dev/sdb: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 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: 0x86662f67

Device Boot Start End Blocks Id System
/dev/sdb1 1 2612 20980858+ 83 Linux

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (2613-5221, default 2613):
Using default value 2613
Last cylinder, +cylinders or +size{K,M,G} (2613-5221, default 5221):
Using default value 5221

Command (m for help): p

Disk /dev/sdb: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 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: 0x86662f67

Device Boot Start End Blocks Id System
/dev/sdb1 1 2612 20980858+ 83 Linux
/dev/sdb2 2613 5221 20956792+ 83 Linux

Command (m for help): w
The partition table has been altered!

分区完了就开始格式化了啊

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
[root@centos ~]# mkfs.ext4 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1313760 inodes, 5245214 blocks
262260 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=4294967296
161 block groups
32768 blocks per group, 32768 fragments per group
8160 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000

正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 21 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@centos ~]# mkfs.ext4 /dev/sdb2
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 5239198 blocks
261959 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=4294967296
160 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000

正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

挂载分区sdb1到/mnt/boot下

1
2
[root@centos mnt]#mkdir boot
[root@centos mnt]#mount /dev/sdb1 /mnt/boot

安装grub

1
2
3
4
5
6
7
8
9
[root@centos mnt]# grub-install --root-de
[root@centos mnt]# grub-install --root-directory=/mnt /dev/sdb
Probing devices to guess BIOS drives. This may take a long time.
/dev/mapper/vg_centos-lv_root does not have any corresponding BIOS drive.
[root@centos mnt]# ll
总用量 8
drwxr-xr-x 3 root root 4096 2月 20 10:09 boot
drwxr-xr-x 2 root root 4096 10月 18 10:41 hgfs
[root@centos mnt]#

复制内核及ramdisk文件到sdb1上

1
2
[root@centos mnt]# cp /boot/vmlinuz-2.6.32-642.13.1.el6.x86_64 /mnt/boot/vmlinuz
[root@centos mnt]# cp /boot/initramfs-2.6.32-642.13.1.el6.x86_64.img /mnt/boot/initramfs.img

在/mnt/boot/grub目录下编辑新建grub.conf文件

内容如下:

1
2
3
4
5
6
default=0
timeout=5
title CentOS6(test)
root (hd0,0)
kernel /vmlinuz ro root=/dev/sda2 selinux=0 init=/bin/bash
initrd /initramfs.img

挂载分区/dev/sdb2到/mnt/sysroot下

1
2
mkdir /mnt/sysroot
mount /dev/sdb2 /mnt/sysroot/

建立FHS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@centos sysroot]#  mkdir -pv /mnt/sysroot/{bin,dev,etc/{rc.d/init.d,sysconfig/network-scripts},lib/modules,lib64,proc,sbin,sys,tmp,usr/local/{bin,sbin},var/{lock,log,run}}
mkdir: 已创建目录 "/mnt/sysroot/bin"
mkdir: 已创建目录 "/mnt/sysroot/dev"
mkdir: 已创建目录 "/mnt/sysroot/etc"
mkdir: 已创建目录 "/mnt/sysroot/etc/rc.d"
mkdir: 已创建目录 "/mnt/sysroot/etc/rc.d/init.d"
mkdir: 已创建目录 "/mnt/sysroot/etc/sysconfig"
mkdir: 已创建目录 "/mnt/sysroot/etc/sysconfig/network-scripts"
mkdir: 已创建目录 "/mnt/sysroot/lib"
mkdir: 已创建目录 "/mnt/sysroot/lib/modules"
mkdir: 已创建目录 "/mnt/sysroot/lib64"
mkdir: 已创建目录 "/mnt/sysroot/proc"
mkdir: 已创建目录 "/mnt/sysroot/sbin"
mkdir: 已创建目录 "/mnt/sysroot/sys"
mkdir: 已创建目录 "/mnt/sysroot/tmp"
mkdir: 已创建目录 "/mnt/sysroot/usr"
mkdir: 已创建目录 "/mnt/sysroot/usr/local"
mkdir: 已创建目录 "/mnt/sysroot/usr/local/bin"
mkdir: 已创建目录 "/mnt/sysroot/usr/local/sbin"
mkdir: 已创建目录 "/mnt/sysroot/var"
mkdir: 已创建目录 "/mnt/sysroot/var/lock"
mkdir: 已创建目录 "/mnt/sysroot/var/log"
mkdir: 已创建目录 "/mnt/sysroot/var/run"
[root@centos sysroot]#

拷贝bash、ls、cat程序及其依赖库

1
2
3
4
5
[root@centos sysroot]# cp /bin/{bash,ls,cat} /mnt/sysroot/bin

[root@centos bin]# cp `ldd /bin/{bash,ls,cat} |grep -oe "/lib.*[[:space:]]"|sort -u` /mnt/sysroot/lib64/

[root@centos bin]# chroot /mnt/sysroot

chroot先测试一下

1
2
3
4
5
6
[root@centos bin]# chroot /mnt/sysroot
bash-4.1# ls
bin dev etc lib lib64 proc sbin sys tmp usr var
bash-4.1# exit
exit
[root@centos bin]#

OK,接下来挂载到另外一台虚拟机上测试一下,亲测成功。

制作一个kickstart文件以及一个引导镜像。

思路

  1. 找到一个已安装好的centos的ks文件,一般位于/root/anaconda-ks.cfg,修修改改,生成一个自定义的ks.cfg文件。
  2. 放到解压的原版IOS到某目录,添加ks文件。
  3. 修改光盘的isolinux.cfg文件,在 append 指令后附加 ks 设置。
  4. 再生成自定义的ISO文件。
  5. 最后用此ISO去安装启动一台裸虚拟机测试是否能自动安装。

找到一个已安装好的centos的ks文件,一般位于/root/anaconda-ks.cfg,修修改改,生成一个自定义的ks.cfg文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#platform=x86, AMD64, 或 Intel EM64T
#version=DEVEL
# Firewall configuration
firewall --disabled
# Install OS instead of upgrade
install
# Use CDROM installation media
cdrom
# Root password
rootpw --iscrypted $1$Qo3PJbQH$GyTUtGgr1U.th4KTIrvfT1
# System authorization information
auth --useshadow --passalgo=sha512
# Use graphical install
graphical
firstboot --disable
# System keyboard
keyboard us
# System language
lang zh_CN
# SELinux configuration
selinux --disabled
# Installation logging level
logging --level=info

# System timezone
timezone Asia/Shanghai
# Network information
network --bootproto=dhcp --device=eth1 --onboot=on
# System bootloader configuration
bootloader --location=mbr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype="ext4" --size=500
part swap --fstype="swap" --size=2000
part / --fstype="ext4" --grow --size=1

%packages
@base
@compat-libraries
@graphical-admin-tools
@legacy-unix
@network-tools
@xfce-desktop
NetworkManager-openswan
arptables_jf
arpwatch
audit-viewer
authd
cups-lpd
dbench
dropwatch
dump
ebtables
ettercap
finger
finger-server
firstaidkit-gui
ipset
iptraf
iptstate
krb5-appl-servers
ksh
lksctp-tools
lshw-gui
mipv6-daemon
mksh
mrtg
ncompress
netlabel_tools
nmap
openvpn
policycoreutils-gui
qstat
rsh
rsh-server
rusers
rusers-server
rwho
sabayon
setroubleshoot
stunnel
system-config-kickstart
system-config-lvm
talk
talk-server
tcp_wrappers
telnet
telnet-server
tftp
vtun
wireshark
wireshark-gnome
yumex
-cpuspeed
-irqbalance
-mdadm

%end

放到解压的原版IOS到某目录,添加ks文件

我这里是用软碟通直接提取修改,所以就省略了

还有一个坑就是ks配置文件里面用cdrom参数制定本地光盘安装会报错,建议用url方式。这个坑踩得我受伤了,就不写了。

应用服务服务脚本

(1) 能接受四个参数:start, stop, restart, status
start: 输出“starting 脚本名 finished.”

(2) 其它任意参数,均报错退出;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/bash
#
case $1 in
start)
echo "starting $0 finished"
;;

stop)
echo "stopping $0 finished"
;;

restart)
echo "restarting $0 finished"
;;

status)
echo " $0 status"
;;

*)
echo "Usage: $prog {start|stop|restart|status}"
exit 1
;;
esac

判断给定的用户是否登录了当前系统

(1) 如果登录了,则显示用户登录,脚本终止;

(2) 每3秒钟,查看一次用户是否登录;

1
2
3
4
5
#!/bin/bash
#
until who | grep "^logstash\>" &> /dev/null; do
sleep 3
done

显示用户选定要查看的信息;

cpu) display cpu info

mem) display memory info

disk) display disk info

quit) quit

非此四项选择,则提示错误,并要求用户重新选择,只到其给出正确的选择为止;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/bin/bash
#
cat << EOF
Display information as following shown selection
cpu) display cpu info
mem) display memory info
disk) display disk info
quit) quit
************************************************
EOF

while true; do
read -p "Please input your selection: " selection

case $selection in
cpu)
lscpu
;;
mem)
free -m
;;
disk)
fdisk -l /dev/vd[a-z]
;;
quit)
echo "Bye Bye"
exit 0
;;
*)
echo "Please input valid selection"
;;
esac
done

尝试用shell函数

(1) 用函数实现返回一个用户的UID和SHELL;用户名通过参数传递而来;

(2) 提示用户输入一个用户名或输入“quit”退出;

当输入的是用户名,则调用函数显示用户信息;

当用户输入quit,则退出脚本;进一步地:显示键入的用户相关信息后,再次提醒输出用户名或quit:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/bin/bash
#

userInfo() {
local userLine=$(grep $1 /etc/passwd)
local userSHELL=${userLine##*:}
local userID=$(echo $userLine | cut -f3 -d:)
echo "$userSHELL and $userID"

}

quit() {
if [ "$1" == "quit" ]; then
echo "Bye Bye"
exit 2
fi
}

while true; do
read -p "Please input your username or input 'quit': " choice

if [ "$choice" == "quit" ]; then
quit $choice
break
fi

if id $choice >& /dev/null; then
userInfo $choice
fi
done

完成如下功能(使用shell函数)

(1) 提示用户输入一个可执行命令的名字;获取此命令依赖的所有库文件;

(2) 复制命令文件至/mnt/sysroot目录下的对应的rootfs的路径上,例如,如果复制的文件原路径是/usr/bin/useradd,则复制到/mnt/sysroot/usr/bin/目录中;

(3) 复制此命令依赖的各库文件至/mnt/sysroot目录下的对应的rootfs的路径上;规则同上面命令相关的要求;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/bin/bash
read -p "Please input a command: " cmd

cmdPath=$(which $cmd | grep bin)

cpCmd() {

cp $cmdPath /mnt/sysroot$cmdpath
echo "Copy $cmdPath to path /mnt/sysroot/"

}

cpFile() {

libFile=$(ldd $cmdPath | grep -o "/[^[:space:]]\{1,\}")
for i in $libFile; do

cp $lib /mnt/sysroot$cmdPath
echo "Copy $libFile to path/mnt/sysroot/$cmdPath"
done
}

cpCmd

cpFile
0%