准备环境
升级内核
- 升级完成后需要重启 ```bash
__kernel_update() {
#导入公钥
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
#添加YUM源
yum install -y https://www.elrepo.org/elrepo-release-7.0-5.el7.elrepo.noarch.rpm
#安装更新长期支持版内核
yum install -y --enablerepo=elrepo-kernel kernel-lt kernel-lt-devel
#更新GRUB配置使用新内核启动
#查看配置中的所有内核版本
awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg
_modify=$(awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg | head -1 | grep "$(uname -r)" -c)
if ((_modify != 1)); then
#设置新内核为默认启动项,默认情况下新内核序号为0
grub2-set-default 0
#更新配置
grub2-mkconfig -o /boot/grub2/grub.cfg
# 设置开机免输入密码
sed -i 's,^ExecStart=.*$,ExecStart=-/sbin/agetty --autologin root --noclear %I,' /etc/systemd/system/getty.target.wants/getty@tty1.service
# 修改系统启动等待时间
sed -i 's,timeout=5$,timeout=2,' /boot/grub2/grub.cfg 2>/dev/null
sed -i 's,timeout=5$,timeout=2,' /boot/efi/EFI/centos/grub.cfg 2>/dev/null
sync
# reboot
echo "安装完成, 手动执行 reboot 重启设备"
fi
} __kernel_update
<a name="r5XXe"></a>
## 安装 bcache 模块
- 执行就完事了
```bash
__install_bcache() {
yum -y install git gcc make automake autoconf libtool libuuid libuuid-devel libblkid-devel
if (($(which docker | grep 'docker$' -c) != 1)); then
git clone http://evilpiepirate.org/git/bcache-tools.git
else
docker run -it --rm -v "$(pwd)/bcache-tools":/dest --pull=always registry.cn-hangzhou.aliyuncs.com/lwmacct/file:bcache-tools-t2022012901
fi
_this_path=$(pwd)
cd bcache-tools || exit 0
make
sudo make install
modprobe bcache
if (("$(lsmod | grep '^bcache' -c)" == 1)); then
echo "bcache 模块已安装"
else
echo "bcache 模块不存在"
fi
cd "$_this_path" || exit
rm -rf bcache-tools
# 开机加载模块
cat >/etc/sysconfig/modules/bcache.modules <<"EOF"
#!/usr/bin/env bash
/sbin/modinfo -F filename bcache > /dev/null 2>&1
if [ $? -eq 0 ]; then
/sbin/modprobe bcache
fi
EOF
chmod +x /etc/sysconfig/modules/bcache.modules
}
__install_bcache
设置 bcache 加速磁盘
指定 SSD加速盘 和 被加速的机械盘
- 这一步需要手动按需要进行
使用 lsblk
命令查看磁盘列表
[root@vm ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
nvme0n1 259:0 0 510G 0 disk
sdd 8:48 0 1.3T 0 disk
sdb 8:16 0 1.1T 0 disk
nvme0n2 259:1 0 520G 0 disk
sde 8:64 0 1.4T 0 disk
sdc 8:32 0 1.2T 0 disk
sda 8:0 0 32G 0 disk
├─sda2 8:2 0 500M 0 part /boot
├─sda3 8:3 0 31.5G 0 part /
└─sda1 8:1 0 2M 0 part
[root@vm ~]#
从以上命令的反馈可以看得出, 我们有 两块 SSD, 分别是 nvme0n1 nvme0n2
现在我要使用
nvme0n1 给机械盘 ssd 和 ssb 加速
nvme0n2 给机械盘 sde 和 sdc 加速
命令示范
make-bcache -B /dev/sdd /dev/sdb -C /dev/nvme0n1
make-bcache -B /dev/sde /dev/sdc -C /dev/nvme0n2
好了, 以上命令仅仅是为了,让你了解 bcache 加速对应关系,
使用以下脚本来自动完成吧, 均匀分配机械盘和ssd
- 如果只有 1 块 ssd, 那么这块 ssd 将会给所有机械盘加速
- 如果有 4 块 hhd, 2块ssd, 那么, 每块 ssd 将会加速, 2块机械
- 如果有 6 块 hhd, 2块ssd, 那么, 每块 ssd 将会加速, 3块机械
以此类推, 如不符合需求,再手动进行, 指派
__bcache_disk() {
_rootfs_disk=$(lsblk 2>/dev/null | grep '\s/$' -B10 | tac | grep '\sdisk' | head -1 | awk '{print $1}')
_blkid=$(blkid)
_disk_sdx=$(lsblk | grep '\sdisk\s' | grep 'sd[a-z]{1,2}' -P | grep -v "$_rootfs_disk" | awk '{print $1}')
_disk_nvme=$(lsblk | grep '\sdisk\s' | grep 'nvme' -P | grep -v "$_rootfs_disk" | awk '{print $1}')
_nvme_array=(x ${_disk_nvme//\S/ })
_num=$(echo "${#_nvme_array[@]}")
# 自动 make_bcache
_path_make_bcache_tmp="/data/kuaicdn/conf/bcache/make_bcache.tmp"
_path_make_bcache_sh="/data/kuaicdn/conf/bcache/make_bcache.sh"
mkdir -p ${_path_make_bcache_sh%/*}
rm -rf $_path_make_bcache_tmp $_path_make_bcache_sh
i=1
_count=0
for _hhd in $_disk_sdx; do
# wipefs -a "/dev/$_hhd"
if ((i >= _num)); then i=1; fi
_ssd=${_nvme_array[i]}
_cmd="$_hhd $_ssd"
echo "$_cmd" >>$_path_make_bcache_tmp
((i++))
((_count++))
done
# _disk_nvme=$(lsblk | grep '\sdisk\s' | grep 'nvme' -P | grep -v "$_rootfs_disk" | awk '{print $1}')
_str=$(cat "$_path_make_bcache_tmp")
for _ssd in $_disk_nvme; do
_cmd=$(echo "$_str" | grep "$_ssd" | awk '{print "/dev/" $1}' | xargs -n99 | xargs -I{} echo "make-bcache -B {} -C /dev/$_ssd --wipe-bcache")
# _cmd=$(echo "$_str" | grep "$_ssd" | awk '{print "/dev/" $1}' | xargs -n99)
echo "$_cmd" >>$_path_make_bcache_sh
done
# make-bcache -B /dev/$_hhd -C /dev/$_ssd --wipe-bcache
rm -rf $_path_make_bcache_tmp
bash $_path_make_bcache_sh
echo "bcache 加速规则位于文件 $_path_make_bcache_sh"
}
__bcache_disk
格式化 bcache 块, 并挂载
- 每次 “指定 SSD加速盘 和 被加速的机械盘”, 后执行一遍命令, 可重复执行不影响
```bash
__mkfs_bcache() {
格式化 bcache, 已格式化的不会重复格式化
_bcache=$(lsblk | grep ‘bcache[0-9]{1,3}’ -Po | sort -u) _blkid=$(blkid) for item in $_bcache; do
done } __mkfs_bcacheif (($(echo "$_blkid" | grep "$item" -c) == 0)); then
mkfs.xfs -f "/dev/$item"
xfs_admin -L data "/dev/$item" >/dev/null 2>&1
fi
__mount() {
# 挂载磁盘并写入开机挂载
sed -in-place -e '\/disk.*/d' /etc/fstab
sed -in-place -e '\/data[0-9]\{1,2\}.*/d' /etc/fstab
blkid -s "LABEL" -s "UUID" -s 'TYPE' | grep -E "kuaicdn|data" | grep -Eo '[0-9a-z-]{36}.*' | sed 's/"//g' | sed 's/TYPE=//g' | awk -F "-| " '{print "echo \"UUID=" $1"-"$2"-"$3"-"$4"-"$5 " /disk/"$1" "$6" defaults,noatime,nodiratime 0 0\" >> /etc/fstab; mkdir -p /disk/"$1}' | sh
mount -a
} __mount
```