做过kickstart/cobbler的同学应该都知道postinstall,它是用来在自动化安装操作系统时执行的脚本,比如配置ip,分区,格式化硬盘,安装包等。
下面讲讲我在写postinstall时遇到的一些关于硬盘分区的问题。
废话不多说,先贴上处理硬盘的脚本,这里只是处理非系统盘的其他硬盘。
echo "Dealing with internal disks now..."
NUM=01
for DEVICE in `lsscsi | awk ‘$2=/disk/ && $NF ~ /dev/ {print $NF}‘`; do
FSTYPE=""
LABEL=""
echo "Checking filesystem type and label on ${DEVICE}1..."
FSTYPE=`parted -s ${DEVICE}1 print 2>&1 | awk ‘/^ / {print $5}‘`
# Let‘s make sure this is NOT a swap partition, which wouldn‘t have a label
echo $FSTYPE | grep swap > /dev/null
if [ $? -eq 0 ]; then
echo "Partition ${DEVICE}1 is a SWAP, skipping..."
continue
fi
# Don‘t do anything if LABEL is "/"
if [ "$LABEL" = "/" ]; then
echo "Device ${DEVICE}1 either has or root label ‘/‘, skipping..."
continue
fi
LABEL=`e2label ${DEVICE}1`
if [ -z "$LABEL" -a -z "$FSTYPE" ]; then
echo "No filesystem and no label found on $DEVICE..."
LABEL=/hadoop$NUM
FSTYPE=ext4
# parted create gpt label
parted -s $DEVICE mklabel gpt
TOTAL=`parted -s $DEVICE unit mb print free 2>&1 | awk ‘/Free Space/ {print $3}‘ | awk -FMB ‘{print $1}‘`
echo "Creating new partition on $DEVICE with label $LABEL..."
parted -s $DEVICE mkpart primary $FSTYPE 0 $TOTAL 2>&1
if [ $? -eq 0 ]; then
NUM=`expr $NUM + 1 | awk ‘{printf("%02s",$1)}‘`
echo "Formatting filesystem ${DEVICE}1..."
sleep 3
mkfs.$FSTYPE -q -N 61050880 -L $LABEL -m 1 -O sparse_super ${DEVICE}1 2>&1 &
NEW_FS="${DEVICE}1 $NEW_FS"
else
echo "Failed to create new partition on $DEVICE! Skipping it..."
continue
fi
fi
echo "Adding entry for $LABEL into /etc/fstab..."
grep -w "LABEL=$LABEL $LABEL" /etc/fstab > /dev/null
if [ $? -eq 0 ]; then
perl -pi -e "s|LABEL=$LABEL $LABEL .*$|LABEL=$LABEL $LABEL $FSTYPE defaults,noatime,nodiratime 0 0|" /etc/fstab 2>&1
else
echo "LABEL=$LABEL $LABEL $FSTYPE defaults,noatime,nodiratime 0 0" >> /etc/fstab
fi
# Create a mount point
if [ ! -d $LABEL ]; then
echo "Creating mount point $LABEL..."
mkdir -p $LABEL
fi
done
echo "Waiting for all mkfs processes to finish (if any)..."
wait
if [ -n "$NEW_FS" ]; then
echo "Tuning newly formatted filesystems..."
FS=""
for FS in $NEW_FS; do
tune2fs -i 0 -c 0 $FS 2>&1
done
fi
我着重说说
parted -s $DEVICE mkpart primary $FSTYPE 0 $TOTAL 2>&1
代表在DEVICE上创建分区,分区的跨度是从0开始到TOTAL结束,单位默认是MB,也可以用其他写法,比如百分比的方式:
parted -s $DEVICE mkpart primary $FSTYPE 0% 100% 2>&1
或者采用扇区的方式,如下命令表示起始扇区是34sectors
parted -s $DEVICE mkpart primary $FSTYPE 34s 100% 2>&1
但通过测试发现,第二种写法在安装操作系统的过程中会出现各种问题,比如某些硬盘在这条命令的执行时间太长或不能正确执行,导致mkfs命令不能正确的执行格式化硬盘操作。
报错信息如下:
filesystem and no label found on/dev/sdi...
Creating new partition on /dev/sdiwith label /hadoop08...
Formatting filesystem /dev/sdi1...
Adding entry for /hadoop08 into/etc/fstab...
Could not stat/dev/sdi1 --- No such file or directory
The deviceapparently does not exist; did you specify it correctly?
通常你会在日志中看到如下信息
Warning: The resulting partition is not properly aligned for best performance.
你不用担心硬盘性能会受到影响,大部分硬盘的logical size 和physical size都是512B,有些硬盘的physical size是4KB,对于这种硬盘就需要在划分分区的时候注意下,分区起始要是1MB,parted工具会帮你处理这些的,所以不用担心,不放心的话,可以用下面介绍的命令查看下分区情况。
附,硬盘操作相关的一些命令
显示硬盘/dev/sdb的分区情况
parted /dev/sdb print free 2>&1
parted /dev/sdb unit mb print free 2>&1
删除分区/dev/sdb1
parted /dev/sdb rm 1
创建gpt表
parted -s /dev/sdb mklabel gpt
格式化分区/dev/sdb1
mkfs.ext4 -q -N 61050880 -L /hadoop01 -m 1 -O sparse_super /dev/sdb1 2>&1
IO-limit相关的文章链接
http://people.redhat.com/msnitzer/docs/io-limits.txt