做过kickstart或者cobbler的同学应该都知道,在系统安装结束后可以对系统作一些定制化的配置,比如ip,分区,硬盘,包等。
下面讲讲我在写%post时遇到的一些关于硬盘分区的问题。
废话不多说,先贴上处理硬盘的脚本,这里只是处理非系统盘的其他硬盘。
echo "Dealing with data disks now..."
NUM=01
DATA_DISK=""
for DEVICE in `awk '$NF ~ /sd/ && $NF !~ /[0-9]$/ {print $NF}' /proc/partitions | sort`; do
DEVICE="/dev/$DEVICE"
FSTYPE=""
LABEL=""
FSTYPE=`parted -s ${DEVICE}1 print 2>&1 | awk '/^ / {print $5}'`
LABEL=`e2label ${DEVICE}1 2>/dev/null`
NEW_LABEL="/hadoop$NUM"
# Skip 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
# Skip "/" LABEL
if [ "$LABEL" = "/" ]; then
echo "Device ${DEVICE}1 has label '/', skipping..."
continue
fi
# Skip label "/var/log/hadoop"
if [ "$LABEL" = "/var/log/hadoop" ]; then
echo "Device ${DEVICE}1 has label /var/log/hadoop, skipping..."
continue
fi
# If LABEL is null , create partition ,then label and filesystem
if [ -z "$LABEL" ]; then
FSTYPE=ext4
echo "Creating gpt "
parted -s $DEVICE mklabel gpt
echo "Creating new partition on $DEVICE with label $NEW_LABEL..."
parted -s $DEVICE mkpart primary $FSTYPE 0% 100% 2>&1
echo "Formatting filesystem ${DEVICE}1..."
partprobe
sleep 3
mkfs.$FSTYPE -q -N 61050880 -L $NEW_LABEL -m 1 -O sparse_super ${DEVICE}1 2>&1 &
# If LABEL is not null , rewrite label
else
echo "Rewrite Label of ${DEVICE}1 to $NEW_LABEL"
e2label ${DEVICE}1 $NEW_LABEL
fi
echo "Adding entry for $NEW_LABEL into /etc/fstab..."
grep -w "LABEL=$NEW_LABEL $NEW_LABEL" /etc/fstab > /dev/null
if [ $? -eq 0 ]; then
perl -pi -e "s|LABEL=$NEW_LABEL $NEW_LABEL .*$|LABEL=$NEW_LABEL $NEW_LABEL $FSTYPE defaults,noatime,nodiratime 0 0|" /etc/fstab 2>&1
else
echo "LABEL=$NEW_LABEL $NEW_LABEL $FSTYPE defaults,noatime,nodiratime 0 0" >> /etc/fstab
fi
# Create a mount point
if [ ! -d $NEW_LABEL ]; then
echo "Creating mount point $NEW_LABEL ..."
mkdir -p $NEW_LABEL
fi
# Increase NUM by 1
NUM=`expr $NUM + 1 | awk '{printf("%02s",$1)}'`
# Put all partitions of data disks into string list
DATA_DISK="$DATA_DISK ${DEVICE}1"
done
echo "Waiting for all mkfs processes to finish (if any)..."
wait
if [ -n "$DATA_DISK" ]; then
echo "Tuning newly formatted filesystems..."
FS=""
for FS in $DATA_DISK; do
tune2fs -i 0 -c 0 $FS 2>&1
done
fi
echo "mount all disks now"
mount -a
parted -s $DEVICE mkpart primary $FSTYPE 0% 100% 2>&1
代表在DEVICE上创建分区,分区的跨度是从0%开始到100%结束,单位默认是MB,也可以用其他写法,比如百分比的方式:
parted -s $DEVICE mkpart primary $FSTYPE 0 200000 2>&1
或者采用扇区的方式,如下命令表示起始扇区是34sectors
parted -s $DEVICE mkpart primary $FSTYPE 34s 100% 2>&1
上面脚本中有partprobe这个命令,它是用来告诉kernel重新读取硬盘的扇区信息。
如果不加这条命令,在安装完系统后你会发现有些硬盘没有被正确格式化,也就是mkfs命令没有正确执行。
相关报错信息如下:
Could not stat/dev/sdi1 --- No such file or directory
The deviceapparently does not exist; did you specify it correctly?
你想知道怎么看到日志信息的?
请在你的postinstall脚本中把日志都打到一个文件中。
以上报错信息不总是出现,只是有时会出现,原因就是因为在划分好扇区后,kernel没有及时读取对应硬盘的扇区信息,所以在格式化硬盘的时候就找不到对应的扇区,跑一下partprobe来告诉kernel再重新读取下硬盘扇区信息,这样就不会出现上面的报错信息了。
通常你会在日志中看到如下信息
Warning: The resulting partition is not properly aligned for best performance.
你不用担心硬盘性能会受到影响,大部分硬盘的logical size 和physical size都是512B,有些硬盘的physical size是4KB,对于这种硬盘就需要在划分分区的时候注意下,分区起始要是1MB,parted工具会帮你处理这些的,所以不用担心,不放心的话,可以用下面介绍的命令查看下分区情况。
parted /dev/sdb print free 2>&1
parted /dev/sdb unit mb print free 2>&1
parted /dev/sdb rm 1
parted -s /dev/sdb mklabel gpt
mkfs.ext4 -q -N 61050880 -L /hadoop01 -m 1 -O sparse_super /dev/sdb1 2>&1