任务:
- 上次内容回顾
- 本次内容
- 定时任务
3.1 简介
3.2 cron分类
3.3 cron使用指南
3.4 定时任务规则
3.5 定时任务的特殊符号
3.6 定时任务故障 - 书写定时任务的时候 尽量使用脚本
- 脚本或命令 在定时任务中要定向到空或追加到文件
- 环境变量
- 定时任务案例
- 定时任务备份项目
3.7 总结 - 磁盘管理
4.1 raid 级别及应用场景
4.2 磁盘分区格式化与挂载
4.3 磁盘相关命令
5.总结
任务:
[面试题]老男孩教育-三剑客笔试题集合
https://www.jianshu.com/p/61d8883719ac
用户相关面试题
- 定时任务
- 磁盘管理体系
1. 上次内容回顾
- 把文件中的回车替换为空格
[root@oldboy64-yl ~]# seq 10 > num.txt
[root@oldboy64-yl ~]# cat num.txt
1
2
3
4
5
6
7
8
9
10
[root@oldboy64-yl ~]# tr '\n' ' ' < num.txt
1 2 3 4 5 6 7 8 9 10 [root@oldboy64-yl ~]#
- sed直接替换失败原因
[root@oldboy64-yl ~]# sed 's#\n# #g' num.txt
1
2
3
4
5
6
7
8
9
10
# sed命令是1行1行处理 sed会把每一行读取到内存中(不会存入回车)
[root@oldboy64-yl ~]# sed ':label $!N;s#\n# #g;t label' num.txt
1 2 3 4 5 6 7 8 9 10
[root@oldboy64-yl ~]#
- 【一题试水平】 利用sed命令将test.txt中所有的回车替换成空格?
- https://www.jianshu.com/p/25aab4d9f1ea
2. 本次内容
- 定时任务 服务
定时执行命令/脚本
定时进行备份 - 磁盘管理体系
raid级别
磁盘分区 格式化 挂载
命令
3.定时任务
3.1 简介
- 闹钟--->定时任务
定时任务服务 crond(服务名称)cronie 企业必备 atd 一次性的服务 anoncron 服务器非7*24小时运行
3.2 cron分类
crond分类 | |
---|---|
系统的定时任务 | 1.系统中毒 2.定时日志切割(轮询) |
用户的定时任务 |
[root@oldboy64-yl ~]# ll -d /etc/cron*
drwxr-xr-x. 2 root root 36 Jul 14 12:55 /etc/cron.d
drwxr-xr-x. 2 root root 57 Jul 11 23:45 /etc/cron.daily #里面的内容每天1次
-rw------- 1 root root 0 Apr 11 2018 /etc/cron.deny #拒绝 黑名单
drwxr-xr-x. 2 root root 22 Jun 30 17:48 /etc/cron.hourly #每小时
drwxr-xr-x. 2 root root 6 Jun 10 2014 /etc/cron.monthly #每个月
-rw-r--r-- 1 root root 451 Jun 10 2014 /etc/crontab #系统定时任务配置
drwxr-xr-x. 2 root root 6 Jun 10 2014 /etc/cron.weekly #每周
[root@oldboy64-yl ~]#
- 系统定时任务+logrotate 日志切割(轮询)
[root@oldboy64-yl ~]# ll /var/log/messages* /var/log/secure*
-rw------- 1 root root 151983 Aug 11 21:30 /var/log/messages
-rw------- 1 root root 67232 Jul 21 09:19 /var/log/messages-20190721
-rw------- 1 root root 83605 Jul 28 09:25 /var/log/messages-20190728
-rw------- 1 root root 85992 Aug 4 10:40 /var/log/messages-20190804
-rw------- 1 root root 723550 Aug 11 09:47 /var/log/messages-20190811
-rw------- 1 root root 2716 Aug 11 21:02 /var/log/secure
-rw------- 1 root root 21620 Jul 21 08:28 /var/log/secure-20190721
-rw------- 1 root root 7328 Jul 28 09:22 /var/log/secure-20190728
-rw------- 1 root root 26373 Aug 4 09:10 /var/log/secure-20190804
-rw------- 1 root root 65170 Aug 11 09:43 /var/log/secure-20190811
[root@oldboy64-yl ~]# ls /etc/cron.daily/
logrotate man-db.cron mlocate
[root@oldboy64-yl ~]# cat /etc/cron.daily/logrotate
#!/bin/sh
/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
[root@oldboy64-yl ~]# file /etc/cron.daily/logrotate
/etc/cron.daily/logrotate: POSIX shell script, ASCII text executable
[root@oldboy64-yl ~]# cat /etc/logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly
# keep 4 weeks worth of backlogs
rotate 4
# create new (empty) log files after rotating old ones
create
# use date as a suffix of the rotated file
dateext
# uncomment this if you want your log files compressed
#compress
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d
# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
monthly
create 0664 root utmp
minsize 1M
rotate 1
}
/var/log/btmp {
missingok
monthly
create 0600 root utmp
rotate 1
}
# system-specific logs may be also be configured here.
[root@oldboy64-yl ~]# cat /etc/logrotate.d/syslog
/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
{
missingok
sharedscripts
postrotate
/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
endscript
}
[root@oldboy64-yl ~]#
3.3 cron使用指南
命令和文件 | 假设当前root用户 | |
---|---|---|
crontab | cron定时任务table(表) | |
-e 编辑(与vi类似) | vi /var/spool/cron/root | |
-l 显示定时任务 | cat /var/spool/cron/root | |
/var/spool/cron/ | 用户定时任务的配置文件目录 | |
/var/log/cron | 定时任务的日志文件 |
3.4 定时任务规则
- 定时任务格式:什么时间 做什么(命令或脚本)
[root@oldboy64-yl ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
[root@oldboy64-yl ~]#
第一个位置(min) | 第二个位置(hour) | 第三个位置 | 第4个位置 | 第五个位置 | 最后位置 |
---|---|---|---|---|---|
* | * | * | * | * | 命令或脚本 |
分钟*相当于是0-59 每分钟 | 小时 * 0-23 | 天 1-31 | 月份 1-12 | 周几(1-6)0或7周日 |
- 分时日月周
- 每周日早上9:30 来上学 (go to school)
30 09 * * 00 go to school
- 周日的半夜12点,去 dbj
* 00 * * 00 dbj
书写定时任务的时候 每个小时执行 分钟位置一般是整点00
00 00 * * 00 dbj
- 注意: 不要日期和周一起写
- 练习题
写一条计划任务,在每天晚上的3:05分执行脚本/usr/script/tongji.sh
05 03 * * * sh /usr/script/tongji.sh
crontab里0 2 * * 1 /data/postmail/shell/a.sh表示什么
含义?
每周一凌晨2点执行脚本/data/postmail/shell/a.sh
执行脚本注意事项:
[root@oldboy64-yl ~]# /etc/init.d/network
Usage: /etc/init.d/network {start|stop|status|restart|force-reload}
[root@oldboy64-yl ~]# sh /etc/init.d/network
Usage: /etc/init.d/network {start|stop|status|restart|force-reload}
[root@oldboy64-yl ~]# ll `which sh bash`
-rwxr-xr-x. 1 root root 964608 Oct 31 2018 /usr/bin/bash
lrwxrwxrwx. 1 root root 4 Jun 30 17:47 /usr/bin/sh -> bash
[root@oldboy64-yl ~]# #对这个脚本有读权限就行
[root@oldboy64-yl ~]# #不需要执行权限
3.5 定时任务的特殊符号
符号 | ||
---|---|---|
* | 每 | * * * * * |
xx/n 每nxx执行1次 | */2 * * * * cmd | */2 每2分钟 每隔2分钟 一样的 |
- | 7-11 | |
, | 1,3,4 |
- 每隔xxx分钟
每分钟
* * * * * date >> /root/time.log
*/1 * * * * CMD
每个小时的第1分钟
01 * * * * CMD3
- 每2分钟把自己的名字写入到/root/name.log
定时任务书写流程
1.命令行测试命令OK?
2.书写脚本 测试脚本
3.书写定时任务规则 每分钟
4.书写定时任务规则 要求 检查
1,检查结果(追加 打包备份)
2,定时任务的日志
1.命令行
[root@oldboy64-yl ~]# echo lidao
lidao
[root@oldboy64-yl ~]# echo lidao `hostname`
lidao oldboy64-yl
[root@oldboy64-yl ~]# echo lidao `hostname`>> /root/name.log
[root@oldboy64-yl ~]# cat /root/name.log
lidao oldboy64-yl
#2.书写定时任务
[root@oldboy64-yl ~]# crontab -l
#print name to file by yl at 20190812
*/2 * * * * echo lidao `hostname` >> /root/name.log
[root@oldboy64-yl ~]# tail -f /root/name.log
lidao oldboy64-yl
lidao oldboy64-yl
- 每天晚上12点打包备份 /etc/ 目录到/backup下面 (保证每天的备份不
同) - 打包备份 命令行
[root@oldboy64-yl ~]# #1.命令行
[root@oldboy64-yl ~]# mkdir -p /backup/
[root@oldboy64-yl ~]# tar zcf /backup/etc.tar.gz /etc/
tar: Removing leading `/' from member names
[root@oldboy64-yl ~]# date +%F
2019-08-12
[root@oldboy64-yl ~]# date +%F_%H
2019-08-12_00
[root@oldboy64-yl ~]# date +%F_%Y-%m-%d
2019-08-12_2019-08-12
[root@oldboy64-yl ~]# date +%T_%H:%M:%S
00:12:36_00:12:36
[root@oldboy64-yl ~]# date +%w
1
[root@oldboy64-yl ~]# tar zcf /backup/etc-`date +%F`.tar.gz /etc/
tar: Removing leading `/' from member names
[root@oldboy64-yl ~]# ll /backup/
total 19984
-rw-r--r-- 1 root root 10228098 Aug 12 00:15 etc-2019-08-12.tar.gz
-rw-r--r-- 1 root root 10228098 Aug 12 00:15 etc.tar.gz
- 脚本与测试
[root@oldboy64-yl ~]# cat /server/scripts/bak.sh
#!/bin/bash/
cd / &&\
tar zcf /backup/etc-`date +%F`.tar.gz /etc/
[root@oldboy64-yl ~]# sh /server/scripts/bak.sh
tar: Removing leading `/' from member names
[root@oldboy64-yl ~]# ll /backup/
total 19984
-rw-r--r-- 1 root root 10228098 Aug 12 00:23 etc-2019-08-12.tar.gz
-rw-r--r-- 1 root root 10228098 Aug 12 00:15 etc.tar.gz
[root@oldboy64-yl ~]#
- 书写定时任务 每分钟执行 测试
[root@oldboy64-yl ~]# crontab -l
#*/2 * * * * echo lidao `hostname` >> /root/name.log
#backup etc by yl at 20190812
* * * * * sh /server/scripts/bak.sh
[root@oldboy64-yl ~]#
[root@oldboy64-yl ~]# tail -f /var/log/cron
Aug 12 00:05:01 oldboy64-yl crond[6438]: (root) RELOAD (/var/spool/cron/root)
Aug 12 00:10:01 oldboy64-yl CROND[11574]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Aug 12 00:20:01 oldboy64-yl CROND[11637]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Aug 12 00:29:12 oldboy64-yl crontab[11688]: (root) BEGIN EDIT (root)
Aug 12 00:30:01 oldboy64-yl CROND[11691]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Aug 12 00:30:22 oldboy64-yl crontab[11688]: (root) REPLACE (root)
Aug 12 00:30:22 oldboy64-yl crontab[11688]: (root) END EDIT (root)
Aug 12 00:30:41 oldboy64-yl crontab[11695]: (root) LIST (root)
Aug 12 00:31:01 oldboy64-yl crond[6438]: (root) RELOAD (/var/spool/cron/root)
Aug 12 00:31:01 oldboy64-yl CROND[11697]: (root) CMD (sh /server/scripts/bak.sh)
Aug 12 00:32:01 oldboy64-yl CROND[11744]: (root) CMD (sh /server/scripts/bak.sh)
[root@oldboy64-yl ~]# ll /backup/
total 19984
-rw-r--r-- 1 root root 10228098 Aug 12 00:33 etc-2019-08-12.tar.gz
- 测试完成 修改时间为要求时间
[root@oldboy64-yl ~]# crontab -l
#*/2 * * * * echo lidao `hostname` >> /root/name.log
#backup etc by yl at 20190812
00 00 * * * sh /server/scripts/bak.sh
- 有一个/opt/backup.sh脚本,如何在10月的每天早上06到18每3小时
执行并在13点单独执行一次。
\00 06-18/3,13 * 10 * sh /opt/bcakup.sh
3.6 定时任务故障
1. 书写定时任务的时候 尽量使用脚本
- %
- 显示脚本执行过程
[root@oldboy64-yl ~]# sh -x /server/scripts/bak.sh
+ cd /
++ date +%F
+ tar zcf /backup/etc-2019-08-12.tar.gz /etc/
tar: Removing leading `/' from member names
[root@oldboy64-yl ~]#
# 带+号行的 表示执行过程
# +越多表示优先执行
# 没有+ 号表示输出 显示到屏幕的内容
2.脚本或命令 在定时任务中要定向到空或追加到文件
符号 | ||
---|---|---|
定向到空 | >/dev/null 2>&1 &>/dev/null | 1>/dev/null 2>/dev/null 错误正确信息都写入到文件中 /dev/null 黑洞 命令或脚本 |
追加到文件 | >>/tmp/cron.log 2>&1 &>>/tmp/cron.log | 文件相当于是 日志 |
#print name to file by yl at 20190812
#*/2 * * * * echo lidao `hostname` >> /root/name.log 2>&1
#backup etc by yl at 20190812
#00 00 * * * sh /server/scripts/bak.sh >/dev/null 2>&1
[root@oldboy64-yl ~]#
- 没有定向到空或追加到文件
1,系统会有大量的你有1个新邮件的提示(postfix 邮件服务开启)
您在 /var/spool/mail/root 中有新邮件
2,postfix服务关闭了,邮件无法发出 在邮件缓存目录中生成大量小文件/var/spool/postfix/maildrop/
3. 环境变量
- 每分钟显示当前系统ip地址和主机名,时间 追加到/tmp/ip.log中
- 命令行执行脚本OK,定时任务运行脚本PATH只有/bin和/usr/bin无法识别
/sbin/ip a
hostname
date
- 小结:
定时任务执行脚本坑:定时任务运行脚本PATH只有/bin和/usr/bin其他的路径无法识别
写备份脚本
4. 定时任务案例
把/etc打包备份到/backup/10.0.0.200/etc-2018-11-11.tar.gz
[root@oldboy64-yl ~]# ipadd=10.0.0.200
[root@oldboy64-yl ~]# echo $ipadd
10.0.0.200
[root@oldboy64-yl ~]# touch $ipadd.txt
[root@oldboy64-yl ~]# ll
total 180420
-rw-r--r-- 1 root root 0 Aug 11 15:25 10.0.0.200
[root@oldboy64-yl ~]# ipadd=`hostname -I|awk '{print $1}'`
[root@oldboy64-yl ~]# echo $ipadd
10.0.0.200
[root@oldboy64-yl ~]# echo $ipadd.log
10.0.0.200.log
[root@oldboy64-yl ~]# ipadd=`hostname -I`
[root@oldboy64-yl ~]# echo $ipadd.log
10.0.0.200 .log
- 脚本内容及测试
[root@oldboy64-yl ~]# cat /server/scripts/bak-etc-v2.sh
#!/bin/bash
ipaddr=`hostname -I|awk '{print $1}'`
dtime=`date +%F`
mkdir -p /backup/$ipaddr
tar zcf /backup/$ipaddr/etc-$dtime.tar.gz /etc/
[root@oldboy64-yl ~]# ll /backup/
total 19984
-rw-r--r-- 1 root root 10228109 Aug 12 17:35 etc-2019-08-12.tar.gz
-rw-r--r-- 1 root root 10228098 Aug 12 00:15 etc.tar.gz
[root@oldboy64-yl ~]# \rm -f /backup/*
[root@oldboy64-yl ~]# ll /backup/
total 0
[root@oldboy64-yl ~]# sh /server/scripts/bak-etc-v2.sh
tar: Removing leading `/' from member names
[root@oldboy64-yl ~]# ll /backup/10.0.0.200/
total 9992
-rw-r--r-- 1 root root 10228109 Aug 12 19:26 etc-2019-08-12.tar.gz
[root@oldboy64-yl ~]# tree /backup/
/backup/
└── 10.0.0.200
└── etc-2019-08-12.tar.gz
1 directory, 1 file
[root@oldboy64-yl ~]#
[root@oldboy64-yl ~]# crontab -l |tail -3
#backup etc
* * * * * sh /server/scripts/bak-etc-v2.sh >/dev/null 2>&1
[root@oldboy64-yl ~]#
#日志
[root@oldboy64-yl ~]# tail -f /var/log/cron
Aug 12 19:34:27 oldboy64-yl crontab[12915]: (root) END EDIT (root)
Aug 12 19:35:01 oldboy64-yl crond[6438]: (root) RELOAD (/var/spool/cron/root)
Aug 12 19:35:01 oldboy64-yl CROND[12934]: (root) CMD (sh /server/scripts/bak-etc-v2.sh >/dev/null 2>&1)
Aug 12 19:36:01 oldboy64-yl CROND[12949]: (root) CMD (sh /server/scripts/bak-etc-v2.sh >/dev/null 2>&1)
Aug 12 19:37:01 oldboy64-yl CROND[12974]: (root) CMD (sh /server/scripts/bak-etc-v2.sh >/dev/null 2>&1)
Aug 12 19:38:01 oldboy64-yl CROND[12990]: (root) CMD (sh /server/scripts/bak-etc-v2.sh >/dev/null 2>&1)
Aug 12 19:39:01 oldboy64-yl CROND[13004]: (root) CMD (sh /server/scripts/bak-etc-v2.sh >/dev/null 2>&1)
Aug 12 19:39:19 oldboy64-yl crontab[13014]: (root) LIST (root)
Aug 12 19:40:01 oldboy64-yl CROND[13018]: (root) CMD (sh /server/scripts/bak-etc-v2.sh >/dev/null 2>&1)
#打包备份成功
[root@oldboy64-yl ~]# tree /backup/
/backup/
└── 10.0.0.200
└── etc-2019-08-12.tar.gz
1 directory, 1 file
[root@oldboy64-yl ~]# ll /backup/10.0.0.200/etc-2019-08-12.tar.gz
-rw-r--r-- 1 root root 8126464 Aug 12 19:42 /backup/10.0.0.200/etc-2019-08-12.tar.gz
[root@oldboy64-yl ~]#
- 修改定时任务时间为要求的时间
[root@oldboy64-yl ~]# crontab -l
#print name to file by yl at 20190812
#*/2 * * * * echo lidao `hostname` >> /root/name.log 2>&1
#backup etc by yl at 20190812
#00 00 * * * sh /server/scripts/bak.sh >/dev/null 2>&1
#backup etc
00 00 * * * sh /server/scripts/bak-etc-v2.sh >/dev/null 2>&1
- 打包备份 /etc/rc.local /etc/fstab /var/spool/cron 到/backup
[root@oldboy64-yl ~]# tar zchf /backup/conf.tar.gz /etc/rc.local /etc/fstab /var/spool/cron/
tar: Removing leading `/' from member names
[root@oldboy64-yl ~]# ll /mnt/
total 0
[root@oldboy64-yl ~]# tar xf /backup/conf.tar.gz -C /mnt/
[root@oldboy64-yl ~]# ll /mnt/etc/
total 8
-rw-r--r-- 1 root root 568 Aug 11 18:04 fstab
-rwxr-xr-x 1 root root 473 Oct 31 2018 rc.local
[root@oldboy64-yl ~]# ll /mnt/var/spool/cron
total 4
-rw------- 1 root root 260 Aug 12 19:44 root
[root@oldboy64-yl ~]#
5. 定时任务备份项目
要求:
- 把/etc打包备份到/backup/10.0.0.200/etc-2018-11-11.tar.gz
- 把这个目录下面 /backup/ip命名目录下面 7天之前的备份删除
- 删除7天之前的备份但是要保留每周1的备份
[root@oldboy64-yl ~]# cat /server/scripts/bak-etc-v3.sh
#!/bin/bash
ipaddr=`hostname -I|awk '{print $1}'`
dtime=`date +%F_%w`
mkdir -p /backup/$ipaddr
#backup
tar zcf /backup/$ipaddr/etc-$dtime.tar.gz /etc/
#del
#find /backup/$ipaddr -type f -name "*.tar.gz" -mtime +7|xargs rm -f
find /backup/$ipaddr -type f -name "*.tar.gz" ! -name "*_1.tar.gz" |xargs rm -f
[root@oldboy64-yl ~]#
3.7 总结
- 格式 特殊符号
- 命令和脚本
- 变量 检查脚本执行过程
- 备份 定时执行命令或脚本
4. 磁盘管理
raid 级别及应用场景
磁盘分区格式化与挂载
no space left on device 故障
磁盘相关命令
4.1 raid 级别及应用场景
- raid 磁盘冗余阵列
获取更高的容量
获取更高的性能
获取更高的冗余(安全) - raid级别
raid | 至少需要几块硬盘 | 性能 | 容量 | 冗余 | 应用场景 |
---|---|---|---|---|---|
raid 0 条带 | 1(安装系统的时候必须要做raid) | 最快的 | 所有硬盘综合 | 只要坏一块都丢了 | |
raid 1 镜像 | 2 | 写入慢 读取相当于是1块硬盘 | 所有硬盘容量的一半 | 可以坏一半 | |
raid 5 | 3 | 写入慢 读取OK | 损失一块硬盘容量 | 坏一块硬盘 | |
raid 10 | 4块 增加硬盘两倍 | 读写性能很高 | 所有硬盘容量的一半 | 坏一半 |
4.2 磁盘分区格式化与挂载
- 环境准备
[root@oldboy64-yl /]# fdisk -l |grep 'sd[a-z]'
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
/dev/sda1 * 2048 411647 204800 83 Linux
/dev/sda2 411648 2508799 1048576 82 Linux swap / Solaris
/dev/sda3 2508800 41943039 19717120 83 Linux
Disk /dev/sdb: 213 MB, 213909504 bytes, 417792 sectors
Disk /dev/sdc: 213 MB, 213909504 bytes, 417792 sectors
[root@oldboy64-yl /]#
磁盘相关词汇 |
---|
主分区 |
扩展分区 |
逻辑分区 |
-
磁盘开头512字节
446 MBR 主引导记录 引导系统启动
- 分区命令
fdisk
parted - /dev/sda sd(sas/sata/scsi)hd(ide)
- /dev/vdxx
- sas
serial (sata)
attached (+)
scsi
fdisk | |
---|---|
p | 显示磁盘分区信息 |
n | 创建新磁盘分区 |
d | 删除分区 |
w | 保存并退出 |
q | 不保存退出 |
单词记录 | |
primary | 主分区 |
extended | 扩展分区 |
logical | 逻辑分区 |
default | 默认 |
[root@oldboy64-yl ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-417791, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-417791, default 417791): +10M
Partition 1 of type Linux and of size 10 MiB is set
Command (m for help): p
Disk /dev/sdb: 213 MB, 213909504 bytes, 417792 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x758985a6
Device Boot Start End Blocks Id System
/dev/sdb1 2048 22527 10240 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@oldboy64-yl ~]# ll /dev/sdb*
brw-rw---- 1 root disk 8, 16 Aug 12 20:49 /dev/sdb
brw-rw---- 1 root disk 8, 17 Aug 12 20:49 /dev/sdb1
[root@oldboy64-yl ~]#
- 磁盘分区 格式化 挂载流程
[root@oldboy64-lnb ~]# #给/dev/sdb 进行分区 只分1个分区
所有容量都给这个分区
[root@oldboy64-lnb ~]# #把/dev/sdb1 挂载到/mnt
#1.磁盘分区
[root@oldboy64-yl ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): d
Selected partition 1
Partition 1 is deleted
Command (m for help): p
Disk /dev/sdb: 213 MB, 213909504 bytes, 417792 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x758985a6
Device Boot Start End Blocks Id System
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-417791, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-417791, default 417791):
Using default value 417791
Partition 1 of type Linux and of size 203 MiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
#2.磁盘格式化
[root@oldboy64-yl ~]# mkfs.xfs -f /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=12992 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=51968, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=855, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@oldboy64-yl ~]#
# xfs 文件系统类型 centos 7 默认的
# ext4 centos 6 默认的
# ext3 5
#3.磁盘挂载
[root@oldboy64-yl ~]# mount /dev/sdb1 /mnt
[root@oldboy64-yl ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 2.3G 17G 12% /
devtmpfs 980M 0 980M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 9.6M 981M 1% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sda1 197M 105M 93M 54% /boot
tmpfs 199M 0 199M 0% /run/user/0
/dev/sdb1 200M 11M 190M 6% /mnt
[root@oldboy64-yl ~]#
#4. 永久挂载
#方法1./etc/rc.local
mount /dev/sdb1 /data
#方法2./etc/fstab
#开机自动挂载列表
/etc/fstab 第一列 | 第二列 | 第三列 | 第四列 | 第五列 | 第六列 |
---|---|---|---|---|---|
UUID或设备名称 | 挂载点(目录或swap) | 文件系统类型(xfs或ext4或swap) | 挂载参数(default) | 是否备份(dump) | 是否开机自动检查(fsck) |
- 如何查询设备的uuid
[root@oldboy64-yl ~]# ll /dev/sdb1
brw-rw---- 1 root disk 8, 17 Aug 12 21:04 /dev/sdb1
[root@oldboy64-yl ~]# blkid
/dev/sda1: UUID="daff218d-4ecc-484c-a479-efabd96c0467" TYPE="xfs"
/dev/sda2: UUID="f8a73e41-694e-4e02-bbb0-3dbc06d90f25" TYPE="swap"
/dev/sda3: UUID="47ec2d05-84dd-4131-86ec-426c706e9c01" TYPE="xfs"
/dev/sdb1: UUID="fe74cc6c-00b2-4c1d-9be2-7aac764412b8" TYPE="xfs"
/dev/sr0: UUID="2018-11-25-23-54-16-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
[root@oldboy64-yl ~]#
[root@oldboy64-yl ~]# tail -1 /etc/fstab
/dev/sdb1 /data xfs defaults 0 0
[root@oldboy64-yl ~]# mount -a
[root@oldboy64-yl ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 2.3G 17G 12% /
devtmpfs 980M 0 980M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 9.6M 981M 1% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sdb1 200M 11M 190M 6% /data
/dev/sda1 197M 105M 93M 54% /boot
tmpfs 199M 0 199M 0% /run/user/0
[root@oldboy64-yl ~]#
- parted使用指南
parted(支持tab键) | |
---|---|
p | 显示磁盘信息 |
mklabel(mktable) | 创建磁盘分区表:dos(mbr)gpt |
mkpart | 创建分区 |
rm | 删除分区 |
q | 退出 |
mbr | 最多支持4个主分区 | 不支持2tb以上硬盘 |
---|---|---|
gpt | 最多支持70 80个主分区 | 支持大硬盘 |
[root@oldboy64-yl ~]# parted /dev/sdc
GNU Parted 3.1
Using /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of
commands.
(parted) mklabel gpt
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 214MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
(parted) mkpart
Partition name? []? primary
File system type? [ext2]? xfs
Start? 0
End? 10
Warning: The resulting partition is not properly aligned for
best performance.
Ignore/Cancel? I
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 214MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 17.4kB 10.0MB 9983kB primary
(parted) mkpart primary 10 20
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 214MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 17.4kB 10.0MB 9983kB primary
2 10.5MB 19.9MB 9437kB primary
(parted) q
Information: You may need to update /etc/fstab.
4.3 磁盘相关命令
磁盘命令集合 | |
---|---|
fdisk | |
parted | |
mount | |
unmount | |
dd | 创建指定大小文件 或测试磁盘性能(连续写入) |
fsck | 检查磁盘 -a |
fio | 随机的读写速度 |
dd | if | of | bs | count |
---|---|---|---|---|
input file | output file | block size | 次数 | |
/dev/zero 白洞 | 每次复制多少 | |||
1k 100 1M(大写) |
5.总结
定时任务
https://www.jianshu.com/p/d902ef6d3cf5
【面试题】定时任务磁盘
raid级别
磁盘分区格式化及挂载
测试磁盘性能 dd fio hdparm