poetry

11种发行版的Linux谁最快: https://linuxstory.org/11-linux-boot/zh-tw/

机器最初配置:

Linux 3.10

Mem 4G

CPU 2

DISK 100G

本次优化从开机服务、磁盘、内核升级、网络相关等几部分考虑



先看效果

boot process.gif

最快可以达到2.55s

先看未优化的状态 :

1
2
3
$ systemd-analyze blame
Startup finished in 677ms (kernel) + 1.582s (initrd) + 5.404s (userspace) = 7.663s

优化后的状态:

1
2
3
$ systemd-analyze blame
Startup finished in 581ms (kernel) + 1.162s (initrd) + 1.037s (userspace) = 2.780s



优化分析

分析命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ systemd-analyze critical-chain
graphical.target @939ms
└─multi-user.target @939ms
└─sshd.service @929ms +9ms
└─network.target @925ms
└─network.service @688ms +236ms
└─NetworkManager.service @647ms +39ms
└─dbus.service @629ms
└─basic.target @627ms
└─sockets.target @627ms
└─dbus.socket @627ms
└─sysinit.target @627ms
................................
1
2
3
4
5
6
7
8
9
10
11
12
# 和上面不是同个系统,主要是为了下面比较用
$ systemd-analyze blame | head -n 10
2.767s tuned.service
2.287s postfix.service
1.430s lvm2-pvscan@8:2.service
1.255s NetworkManager-wait-online.service
931ms lvm2-monitor.service
925ms dev-mapper-centos\x2droot.device
880ms dracut-initqueue.service
307ms polkit.service
283ms network.service

1
2
systemd-analyze 
Startup finished in 689ms (kernel) + 1.552s (initrd) + 5.840s (userspace) = 8.083s

导出为svg图片

1
systemd-analyze plot > bootloader.svg

可以看到,一共用时7.663s,主要是集中在usespace这一块, 下面分析一下哪些开机特别耗时:



开机服务

1
2
# 查看系统开机启动项
systemctl list-unit-files | grep enable

关闭可以没有的开机服务:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
systemctl disable postfix # 邮件服务
systemctl disable firewalld # 防火墙
systemctl disable rsyslog.service # 记录登录文件服务
systemctl disable NetworkManager-dispatcher.service # 网卡守护进程
systemctl disable kdump.service # 内核崩溃信息捕获
systemctl disable crond # 定时任务
systemctl disable rhel-dmesg.service # 系统信息描述
systemctl disable runlevel4.target # 用户启动级别
systemctl disable runlevel2.target
systemctl disable NetworkManager-wait-online.service # 网络联机等待服务
systemctl disable tuned.service # 性能调优服务
systemctl disable lvm2-monitor.service # LVM逻辑卷管理及监控
systemctl disable plymouth-quit-wait.service
systemctl disable sys-kernel-debug.mount # 禁用debugfs文件系统
systemctl disable systemd-journal-flush.service # 日志刷新
setenforce 0 && sed -i 's\SELINUX=enforcing\SELINUX=disabled\' /etc/selinux/config # 禁止selinux

rsyslog.service



runlevel

runlevel

CentOS7 有6个启动级别,默认用level 3



init级别 systemctl target
0 shutdown.target 系统停机模式,系统默认运行级别不能设置为0,否则不能正常启动,机器关的;
1 emergency.target 单用户模式,root权限,用于系统维护,禁止远程登陆,就像windows下的安全模式登陆;
2 rescure.target 多用户模式,没有NFS和网络支持;
3 multi-user.target 完整的多用户文本模式,有NFS和网络,登录后进入控制台命令行模式。
4 无 系统未使用,保留一般不用,在一些特殊情况下可以用它来做一些事情。例如在笔记本电池用尽时,可以切换到这个模式来做一些设置;
5 graphical.target 图形化模式,登录后进入图形GUI模式,X windows系;
6 重启模式,默认运行级别不能设为6,否则不能正常启动。运行init6机器就会重启
systemctl 命令说明
systemctl get-default 获得当前的运行级别
systemctl set-default multi-user.target 设置默认的运行级别为mulit-user
systemctl isolate multi-user.target 在不重启的情况下,切换到运行级别mulit-user下
systemctl isolate graphical.target 在不重启的情况下,切换到图形界面下
init 3 (从init 5) 切换系统运行级别为3,重启后失效


NetworkManager

NetworkManager-wait-online.service

某些多用户环境中,部分启动过程可能来自于网络(比如NFS客户端挂载),systemd默认情况下采取某些步骤之前等待网络联机

如果故障,这个过程会很长,可以取消联机等待服务

systemctl disable NetworkManager-wait-online.service

如果不想禁用 NetworkManager-wait-online.service 联机检查,可以调整它的等待时间:

1
sed -i 's/timeout=30/timeout=1/g' /usr/lib/systemd/system/NetworkManager-wait-online.service


tuned

tuned service , tuned是CentOS / RHEL / Fedora Linux上最有用的性能调整工具之一

Tuned红帽官方说明

tuned使用

vi /etc/tuned/tuned-main.conf 将其中的daemon的值修改为0

1
2
3
4
5
# Whether to use daemon. Without daemon it just applies tuning. It is
# not recommended, because many functions don't work without daemon,
# e.g. there will be no D-Bus, no rollback of settings, no hotplug,
# no dynamic tuning, ...
daemon = 0

如果想用tuned做优化,则daemon=1

简单的tuned使用

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
[root@ym-vm1 ~]# tuned-adm list
Available profiles:

- balanced - General non-specialized tuned profile
# 优化桌面使用
- desktop - Optimize for the desktop use-case
# 优化高性能工作负载计算
- hpc-compute - Optimize for HPC compute workloads
# 优化延迟性能
- latency-performance - Optimize for deterministic performance at the cost of increased power consumption
# 优化网络延时
- network-latency - Optimize for deterministic performance at the cost of increased power consumption, focused on low latency network performance
# 优化网络吞吐
- network-throughput - Optimize for streaming network throughput, generally only necessary on older CPUs or 40G+ networks
# 低功耗
- powersave - Optimize for low power consumption
# 优化工作负载的吞吐
- throughput-performance - Broadly applicable tuning that provides excellent performance across a variety of common server workloads
# 优化virtual guest
- virtual-guest - Optimize for running inside a virtual guest
# 优化KVM
- virtual-host - Optimize for running KVM guests

[root@ym-vm1 ~]# systemctl start tuned

# 加载配置项
[root@ym-vm1 ~]# tuned-adm profile virtual-host
Trying to (re)start tuned...
Tuned (re)started, changes applied

# 激活配置项
[root@ym-vm1 ~]# tuned-adm active
Current active profile: virtual-host
Profile名称 应用场景
default 默认的节能(power-saving)配置。对系统影响最小的power-saving配置,只启动了CPU和磁盘的tuned机制;
desktop-powersave 针对桌面系统设计的节能Profile,对SATA适配器、CPU、网络和磁盘插件应用ALPM策略
server-powersave 针对服务器设计的节能Profile,对SATA适配器、禁用CD-ROM轮询、启动CPU和磁盘插件应用ALPM策略;
laptop-ac-powersave 针对运行于插入电源笔记本电脑应用,中等影响的节能策略,启用SATA的ALPM省电策略,启用WIFI省电策略,CPU、网络和磁盘插件收到节能影响;
laptop-battery-powersave 针对运行于插入电池笔记本电脑应用。激活应用所有的节能配置。对CPU、网络和IO方面,有延时缺点。
throughput-performance 禁用节能配置,启用sysctl模块设置来提升性能,改善网络和IO配置,切换到deadline switch;
latency-performance 禁用节能配置,启用sysctl模块设置来提升性能,主要在网络IO方面提升;


sys-kernel-debug

sys-kernel-debug

手动挂载 mount -t debugfs none /sys/kernel/debug



磁盘优化

引导界面的SMBusNotEnable

修改启动界面出现的 : SMBus Host Controller Not Enabled

This error is being caused because VMware doesn’t actually provide that level interface for CPU access, but CentOS try to load the kernel module anyway.

1
echo "blacklist i2c-piix4" >  /etc/modprobe.d/blacklist.conf

https://whitehatty.com/2012/09/30/vmware-fusion-5-fix-ubuntu-linux-12-04-host-smbus-controller-not-enabled/

修改ulimit最大打开文件的数量

1
echo "ulimit -SHn 102400" >> /etc/rc.local

开机不挂载/home分区, 有考虑过禁用swap, 但发现禁用交换分区之后,系统启动速度反而变慢了.

需要注意的是,磁盘挂载占用了系统启动时间的绝大部分

1
2
3
4
$ lvscan
ACTIVE '/dev/centos/swap' [2.00 GiB] inherit
ACTIVE '/dev/centos/root' [<17.00 GiB] inherit
umount /home/ && sed -i 's/^\/dev\/mapper\/centos-home/#&/' /etc/fstab

修改网卡配置项,, 考虑过禁用ipv6,但效果不大. 考虑到dhcp分配ip地址会消耗一些时间,但是虚拟机里感觉不出来, 就没有使用static静态ip.

1
2
3
4
5
6
7
cat > /etc/sysconfig/network-scripts/ifcfg-ens32 << EOF
TYPE=Ethernet
BOOTPROTO=dhcp
NAME=ens32
DEVICE=ens32
ONBOOT=yes
EOF


内核升级

内核升级

原本的内核时CentOS7自带的Linux3.10 , 现在升级到了4.4 ,开机速度快了1.7s左右, 目前并没有找到为什么4.4的内核比3.10 内核快的原因, 如有知道,希望评论告知.

1
2
3
4
5
6
7
rpm -Uvh https://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
yum list available --disablerepo='*' --enablerepo=elrepo-kernel
yum -y --enablerepo=elrepo-kernel install kernel-lt.x86_64
sed -i 's/GRUB_DEFAULT=.*$/GRUB_DEFAULT=0/' /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg
uname -r
- 4.4.230-1.el7.elrepo.x86_64

https://blog.csdn.net/zjujoe/article/details/5594961



取消引导时间

Linux的Grub引导界面

将引导时间修改为 0 ,即修改GRUB_TIMEOUT的值

1
2
sed -i 's/GRUB_TIMEOUT=.*/GRUB_TIMEOUT=0/' /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg


Todo 网络优化

研究了一下网络,但是感觉不太好优化,这里记录一下主要过程

1
2
3
4
5
6
7
8
9
10
11
12
# 这个是优化完成的结果
[root@ym-vm1 /]# systemd-analyze blame | head -n 10
381ms dev-mapper-centos\x2droot.device
236ms network.service
143ms auditd.service
135ms boot.mount
79ms tuned.service
68ms systemd-vconsole-setup.service
47ms systemd-udev-trigger.service
39ms NetworkManager.service
34ms chronyd.service
31ms systemd-logind.service

也可以看到,其中磁盘设备挂载和网络优化用时分别排第一和第二

/etc/init.d/network 这个脚本记录了网络启动过程:

脚本函数结构:

1
2
3
4
5
6
case "$1" in
start)
stop)
status)
restart|force-reload)
*)

这里只分析/etc/init.d/network start 部分,后面的stop、status、restart都有相似部分。

整体操作:

1
2
3
4
5
脚本分析: 
1、导入 /etc/init.d/functions 文件
2、判断/etc/sysconfig/network、/etc/sysconfig/pcmcia和ip命令是否存在,如果network配置文件存在,则加载
3、获取网卡设备名,我这台虚拟机网卡为ens32
4、进入case选择,选项为start|stop|restart|status|force-reload|default,类似于systemctl xxx network的操作


详解case中start操作

1
2
3
4
5
6
7
8
1、启动ipv6 - 使用 /etc/sysconfig/network-scripts/init.ipv6-global 脚本
2、nmcli重新加载ipv6配置
3、加载vlan
4、启动时加载其他接口配置,包括DEVICE、TYPE、ONBOOT
5、执行./ifup ens32 boot拉起网卡
6、拉起 xDSL和VPN网卡
7、添加静态路由,导入文件为:/etc/sysconfig/static-routes,使用route add导入
8、IPv6 hook 导入ipv6默认路由 /sbin/ip route add unreach 2002:a9fe::/32


这里调用了/etc/sysconfig/network-scripts/init.ipv6-global这个脚本,这个脚本里也有case选择

1
2
3
if [ -x /etc/sysconfig/network-scripts/init.ipv6-global ]; then
/etc/sysconfig/network-scripts/init.ipv6-global start pre
fi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/etc/sysconfig/network-scripts/init.ipv6-global 脚本结构解析: 

case $ACTION in
start)
case $POSITION in
- pre)
- post)
- *)
stop)
case $POSITION in
- pre)
- post)
- *)
*)


可以看到,网卡启动脚本执行的是init.ipv6-globalstart - pre 区域里的内容,里面主要做了ipv6参数的初始化

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
# start-pre
# IPv6 test, module loaded, exit if system is not IPv6-ready
ipv6_test || exit 1

# 配置ipv6相关参数
if [ "$IPV6FORWARDING" = "yes" ]; then
ipv6_global_forwarding=1
ipv6_global_auto=0
else
ipv6_global_forwarding=0
if [ "$IPV6_AUTOCONF" = "no" ]; then
ipv6_global_auto=0
else
ipv6_global_auto=1
fi
fi

# 配置每块网卡的ipv6参数,包括forwarding转发,redirects重定向,是否接受Router Advertisements(RA)
if [ ! "$IPV6_SET_SYSCTLS" = "no" ]; then
# Reset IPv6 sysctl switches for "all", "default" and still existing devices
for i in /proc/sys/net/ipv6/conf/* ; do
interface=${i##*/}
sinterface=${interface/.//}
# Host/Router behaviour for the interface
/sbin/sysctl -e -w net.ipv6.conf.$sinterface.forwarding=$ipv6_global_forwarding >/dev/null 2>&1

# Autoconfiguration and redirect handling for Hosts
/sbin/sysctl -e -w net.ipv6.conf.$sinterface.accept_ra=$ipv6_global_auto >/dev/null 2>&1
/sbin/sysctl -e -w net.ipv6.conf.$sinterface.accept_redirects=$ipv6_global_auto >/dev/null 2>&1
done
fi
;;


nmcli重载

1
2
3
4
5
6
7
#tell NM to reload its configuration
if [ "$(LANG=C nmcli -t --fields running general status 2>/dev/null)" = "running" ]; then
nmcli connection reload
fi

# 启动Loopback网卡接口
action $"Bringing up loopback interface: " ./ifup ifcfg-lo


判断是否支持vlan

1
2
3
4
5
6
7
case "$VLAN" in
yes)
if [ ! -d /proc/net/vlan ] && ! modprobe 8021q >/dev/null 2>&1 ; then
net_log $"No 802.1Q VLAN support available in kernel."
fi
;;
esac


配置其他网卡信息,包括设备名,类型,是否为slaver,是否桥接,是否为GRE,开机是否自启,配置完成后,用ifup拉起所有网卡

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
for i in $interfaces; do
unset DEVICE TYPE SLAVE NM_CONTROLLED
eval $(LANG=C grep -F "DEVICE=" ifcfg-$i)
eval $(LANG=C grep -F "TYPE=" ifcfg-$i)
eval $(LANG=C grep -F "SLAVE=" ifcfg-$i)
eval $(LANG=C grep -F "NM_CONTROLLED=" ifcfg-$i)

if [ -z "$DEVICE" ] ; then DEVICE="$i"; fi

if [ "$SLAVE" = "yes" ] && ( ! is_nm_running || is_false $NM_CONTROLLED ) ; then
continue
fi

if [ "${DEVICE##cipcb}" != "$DEVICE" ] ; then
vpninterfaces="$vpninterfaces $i"
continue
fi
if [ "$TYPE" = "xDSL" -o "$TYPE" = "Modem" ]; then
xdslinterfaces="$xdslinterfaces $i"
continue
fi

if [ "$TYPE" = "Bridge" ]; then
bridgeinterfaces="$bridgeinterfaces $i"
continue
fi

if [ "$TYPE" = "IPSEC" ] || [ "$TYPE" = "IPIP" ] || [ "$TYPE" = "GRE" ]; then
vpninterfaces="$vpninterfaces $i"
continue
fi

if [ "${DEVICE%%.*}" != "$DEVICE" -o "${DEVICE##vlan}" != "$DEVICE" ] ; then
vlaninterfaces="$vlaninterfaces $i"
continue
fi

if LANG=C grep -EL "^ONBOOT=['\"]?[Nn][Oo]['\"]?" ifcfg-$i > /dev/null ; then
# this loads the module, to preserve ordering
is_available $i
continue
fi

action $"Bringing up interface $i: " ./ifup $i boot
[ $? -ne 0 ] && rc=1
done




将网卡拉起后,开始配置默认路由

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
# 这里是配置ipv4的默认路由,路由条目在static-routes中定义
if [ -f /etc/sysconfig/static-routes ]; then
if [ -x /sbin/route ]; then
grep "^any" /etc/sysconfig/static-routes | while read ignore args ; do
/sbin/route add -$args
done
else
net_log $"Legacy static-route support not available: /sbin/route not found"
fi
fi

# 配置ipv6默认路由条目
# IPv6 hook (post IPv4 start)
if [ -x /etc/sysconfig/network-scripts/init.ipv6-global ]; then
/etc/sysconfig/network-scripts/init.ipv6-global start post
fi

> 转到init.ipv6-global 下查看 start post区域内的内容,这里主要配置了ipv6的默认路由和网关
post)
ipv6_test || exit 1

if [ "$IPV6_AUTOTUNNEL" = "yes" ]; then
ipv6_enable_autotunnel
fi

/sbin/ip route add unreach ::ffff:0.0.0.0/96
/sbin/ip route add unreach ::0.0.0.0/96
/sbin/ip route add unreach 2002:e000::/19
/sbin/ip route add unreach 2002:7f00::/24
/sbin/ip route add unreach 2002:0a00::/24
/sbin/ip route add unreach 2002:ac10::/28
/sbin/ip route add unreach 2002:c0a8::/32
/sbin/ip route add unreach 2002:a9fe::/32
/sbin/ip route add unreach 3ffe:ffff::/32

if [ "$IPV6_DEFAULTDEV" = "sit0" -a "$IPV6_AUTOTUNNEL" = "yes" ]; then
if [ -n "$IPV6_DEFAULTGW" ]; then
ipv6_set_default_route $IPV6_DEFAULTGW $IPV6_DEFAULTDEV sit0
elif [ -n "$IPV6_DEFAULTDEV" ]; then
ipv6_set_default_route "" $IPV6_DEFAULTDEV sit0
fi
fi
;;




最后sleep NETWORKDELAY秒,至此/etc/init.d/network start结束

1
2
3
touch /var/lock/subsys/network

[ -n "${NETWORKDELAY}" ] && /bin/sleep ${NETWORKDELAY}



Plot对比图

优化之前的状态.svg

优化之后的状态.svg

加速脚本

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
# 2020/7/21
# linux 开机加速
# Linux 4.4
# Mem 4G
# Disk 100G
# CPU 2

# 将Grub选择时间改为0
sed -i 's/GRUB_TIMEOUT=.*/GRUB_TIMEOUT=0/' /etc/default/grub

# 升级Linux内核
rpm -Uvh https://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
yum list available --disablerepo='*' --enablerepo=elrepo-kernel
yum -y --enablerepo=elrepo-kernel install kernel-lt.x86_64
sed -i 's/GRUB_DEFAULT=.*$/GRUB_DEFAULT=0/' /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg

# 修改SMBus Host Controller Not Enabled!
echo "blacklist i2c-piix4" > /etc/modprobe.d/blacklist.conf

# 修改网卡
cat > /etc/sysconfig/network-scripts/ifcfg-ens32 << EOF
TYPE=Ethernet
BOOTPROTO=dhcp
NAME=ens32
DEVICE=ens32
ONBOOT=yes
EOF

# 开机不挂在home分区
lvscan
umount /home/ && sed -i 's/^\/dev\/mapper\/centos-home/#&/' /etc/fstab
echo "ulimit -SHn 102400" >> /etc/rc.local

# 解决pvscan
sed -i 's/"\/dev"/"\/dev\/sda1"/g' /etc/lvm/lvm.conf

# 关闭不必要的服务
systemctl disable postfix
systemctl disable firewalld
systemctl disable rsyslog.service
systemctl disable NetworkManager-dispatcher.service
systemctl disable kdump.service
systemctl disable crond
systemctl disable rhel-dmesg.service
systemctl disable runlevel4.target
systemctl disable runlevel2.target
systemctl disable NetworkManager-wait-online.service
systemctl disable tuned.service
systemctl disable lvm2-monitor.service
systemctl disable plymouth-quit-wait.service
systemctl disable sys-kernel-debug.mount
systemctl disable systemd-journal-flush.service


cat > /root/boot<< 'EOF'
#!/bin/bash
# 用户登录脚本
# 2020/7/20

echo "

███████╗███████╗██╗ ███████╗ ██████╗ ██████╗ ███╗ ██╗███████╗██╗ ██████╗
██╔════╝██╔════╝██║ ██╔════╝ ██╔════╝██╔═══██╗████╗ ██║██╔════╝██║██╔════╝
███████╗█████╗ ██║ █████╗█████╗██║ ██║ ██║██╔██╗ ██║█████╗ ██║██║ ███╗
╚════██║██╔══╝ ██║ ██╔══╝╚════╝██║ ██║ ██║██║╚██╗██║██╔══╝ ██║██║ ██║
███████║███████╗███████╗██║ ╚██████╗╚██████╔╝██║ ╚████║██║ ██║╚██████╔╝
╚══════╝╚══════╝╚══════╝╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═══╝╚═╝ ╚═╝ ╚═════╝


"

file="/tmp/info-$RANDOM"
curl www.pubyun.com/dyndns/getip &> $file
publicIp=$( cat $file | sed -n '4p')
printf "%-16s %s\n" Time: "`date '+%Y-%m-%d %H:%M'`"
printf "%-16s %s\n" Ip: "`ip a | grep glo | awk '{print $2}'`"
printf "%-16s %s\n" User: "`whoami`"
printf "%-16s %s\n" PublicIp: "$publicIp"

# 下面这个可能会网速不太行
# curl cip.cc &> $file
# publicIp=`cat $file | sed -n '/IP/p' | awk -F: '{print $2}' | tr -d ' '`
# location=`cat $file | sed -n '/地址/p' | awk -F: '{print $2}' | tr -d ' '`
# printf "%-16s %s\n" PublicIp: "$publicIp"
# printf "%-16s %s\n" Location: "$location"
echo;
systemd-analyze
echo;echo;

EOF

sed -i '1,${/sh/{/root/{/boot/d}}}' ~/.bashrc
chmod +x /root/boot
echo "sh /root/boot" >> ~/.bashrc

reboot

恢复脚本

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
cat > service << EOF
#!/bin/bash
# 服务拉起脚本
# 2020/7/21

echo " service start "
systemctl start postfix
systemctl start rsyslog.service
systemctl start NetworkManager-dispatcher.service
systemctl start crond
systemctl start rhel-dmesg.service
systemctl start runlevel4.target
systemctl start runlevel2.target
systemctl start NetworkManager-wait-online.service
systemctl start tuned.service
systemctl start lvm2-monitor.service
systemctl start plymouth-quit-wait.service
systemctl start sys-kernel-debug.mount
systemctl start systemd-journal-flush.service
systemctl start NetworkManager.service
systemctl start chronyd.service

EOF
chmod +x service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cat > addisk << EOF
# 磁盘扩容脚本,需要先在vmware里扩展分区容量,默认60G
# 2020/7/21

echo " add disk +58G "
echo -e "n\n\n\n\nw" | fdisk /dev/sda
partprobe /dev/sda
mkfs.ext3 /dev/sda3
echo -e 'y\n' | pvcreate /dev/sda3
vgextend centos /dev/sda3
# 这里+58G,默认Vmware中磁盘增加60G
lvextend -L+58G /dev/centos/root /dev/sda3
lvextend /dev/centos/root /dev/sda3
xfs_growfs /dev/mapper/centos-root
EOF
chmod +x addisk
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
cat > /root/boot<< 'EOF'
#!/bin/bash
# 用户登录脚本
# 2020/7/20

echo "

███████╗███████╗██╗ ███████╗ ██████╗ ██████╗ ███╗ ██╗███████╗██╗ ██████╗
██╔════╝██╔════╝██║ ██╔════╝ ██╔════╝██╔═══██╗████╗ ██║██╔════╝██║██╔════╝
███████╗█████╗ ██║ █████╗█████╗██║ ██║ ██║██╔██╗ ██║█████╗ ██║██║ ███╗
╚════██║██╔══╝ ██║ ██╔══╝╚════╝██║ ██║ ██║██║╚██╗██║██╔══╝ ██║██║ ██║
███████║███████╗███████╗██║ ╚██████╗╚██████╔╝██║ ╚████║██║ ██║╚██████╔╝
╚══════╝╚══════╝╚══════╝╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═══╝╚═╝ ╚═╝ ╚═════╝


"

file="/tmp/info-$RANDOM"
curl www.pubyun.com/dyndns/getip &> $file
publicIp=$( cat $file | sed -n '4p')
printf "%-16s %s\n" Time: "`date '+%Y-%m-%d %H:%M'`"
printf "%-16s %s\n" Ip: "`ip a | grep glo | awk '{print $2}'`"
printf "%-16s %s\n" User: "`whoami`"
printf "%-16s %s\n" PublicIp: "$publicIp"

# 下面这个可能会网速不太行
# curl cip.cc &> $file
# publicIp=`cat $file | sed -n '/IP/p' | awk -F: '{print $2}' | tr -d ' '`
# location=`cat $file | sed -n '/地址/p' | awk -F: '{print $2}' | tr -d ' '`
# printf "%-16s %s\n" PublicIp: "$publicIp"
# printf "%-16s %s\n" Location: "$location"
echo;
systemd-analyze
echo;echo;

EOF

sed -i '1,${/sh/{/root/{/boot/d}}}' ~/.bashrc
chmod +x /root/boot
echo "sh /root/boot" >> ~/.bashrc
1
2
3
# 卸载旧内核
rpm -qa | grep kernel
yum -y remove kernel-tools-libs-3.* kernel-tools-3.* kernel-3.*