磁盘管理和脚本交互小练习

1、创建20G的文件系统,块大小2048,文件系统ext4,卷标TEST,要求此分区开机自动挂载至/tetsing目录,且默认挂载属性为acl

2、创建5G文件系统,卷标HUGE,要求此分区开机自动挂载至/mogdata,文件系统类型ext3

3、写一个脚本,完成如下功能:

利用此前学到的if语句完成

(1) 列出当前系统识别的所有磁盘设备

(2) 如果磁盘数量为1,则显示其磁盘空间信息

否则,显示最后一个磁盘上的空间使用信息。

3、写一个脚本,完成如下功能:

利用此前学到的if语句完成

(1) 让用户输入一个磁盘

(2) 存在,则显示其磁盘空间信息

否则,显示Fool.


1、创建20G的文件系统,块大小2048,文件系统ext4,卷标TEST,要求此分区开机自动挂载至/tetsing目录,且默认挂载属性为acl

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
1、显示所有磁盘块及分区信息
[root@localhost scripts] # fdisk -l /dev/[sh]d[a-z]
2、根据 2610 cylinders 和End 判断能否分区,并确认分区
 
3、管理分区
[root@localhost scripts] # fdisk /dev/sdb
     n,p, , +20G, w
 
4、更新内核识别分区表
[root@localhost scripts] # partx -a /dev/sdb
[root@localhost scripts] # partx -a /dev/sdb
 
5、格式化分区
[root@localhost scripts] # mke2fs -t ext4 -b 2048 -L 'TEST'  /dev/sdb2
 
6、查看默认挂载属性
[root@localhost scripts] # dumpe2fs -h /dev/sdb2
Default  mount  options:    (none)
 
7、调整默认挂载属性acl
[root@localhost scripts] # tune2fs -o acl /dev/sdb2
 
8、查看默认挂载属性
[root@localhost scripts] # dumpe2fs -h /dev/sdb2
Default  mount  options:    acl   ##嘿嘿


2、创建5G文件系统,卷标HUGE,要求此分区开机自动挂载至/mogdata,文件系统类型ext3

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
1、查看sdb的分区表
[root@localhost scripts] # kpartx -l /dev/sdb
sdb1 : 0 20980827  /dev/sdb  63
sdb2 : 0 224910  /dev/sdb  20980890
2、破坏分区表
[root@localhost scripts] # dd if=/dev/zero of=/dev/sdb bs=512 count=1
1+0 records  in
1+0 records out
512 bytes (512 B) copied, 0.00428946 s, 119 kB /s
[root@localhost scripts] # kpartx -l /dev/sdb
[root@localhost scripts]
 
3、管理sdb
[root@localhost scripts] # fdisk /dev/sdb
n,p,1, ,+5G,w
 
4、让内核重读分区表
[root@localhost scripts] # partx -a /dev/sdb
 
5、格式化
[root@localhost scripts] # mkfs.ext2 -L 'HUGE' /dev/sdb1
[root@localhost scripts] # tune2fs -O has_journal /dev/sdb1
[root@localhost scripts] # blkid /dev/sdb1
/dev/sdb1 : LABEL= "HUGE"  UUID= "1b4a4a93-20a1-439e-b40b-50989cf21fed"  SEC_TYPE= "ext2"  TYPE= "ext3" 
 
6、开机挂载至 /mogdata
1)创建目录
[root@localhost scripts] # mkdir /mogdata
 
2)查看是否创建
[root@localhost scripts] # ls -ld /mogdata
drwxr-xr-x 2 root root 4096 Aug 10 16:29  /mogdata
 
3)配置fstab
[root@localhost scripts] # vim + /etc/fstab
UUID= "1b4a4a93-20a1-439e-b40b-50989cf21fed"      /mogdata   ext4    defaults, sync ,noatime,nosuid 2 3
     2:隔一天备份
     3:检测次序


3、写一个脚本,完成如下功能:

利用此前学到的if语句完成

(1) 列出当前系统识别的所有磁盘设备

(2) 如果磁盘数量为1,则显示其磁盘空间信息

否则,显示最后一个磁盘上的空间使用信息。

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
1、写脚本
[root@localhost scripts] # vim test1.sh
#!/bin/bash
# Version: 0.0.2
# Author: Lcc.org
# Desc: 利用此前学到的if语句完成    (1) 列出当前系统识别的所有磁盘设备(2) 如果磁盘数量为1,则显示其磁盘空间信息 否则,显示最后一个磁盘上的空间使用信息。
 
# bash弱类型:不用定义,直接赋值.默认为字符型.参与运算隐式转换
diskfile=` ls  /dev/ [sh]d[a-z]`
COUNT=$( ls  /dev/ [sh]d[a-z] |  wc  -l)
 
#数值判断: -eq,-ne,-lt,-le,-gt,-ge
if  [ $COUNT - eq  1 ]
then
         #显示指定设备的状态信息
     fdisk  -l $diskfile
     #自定义状态返回值:在bash脚本中,一旦遇到exit命令就会终止脚本,退出状态码为exit后的数值。
     exit  0
else
         #以空白分割,由-n 后#指定的字段作为每行字段的数量
     fdisk  -l  ` echo  $diskfile |  xargs  -n1 |  tail  -1`
     exit  0
fi
 
2、测试语法(没有信息就是最好的信息)
[root@localhost scripts] # bash -n test1.sh
 
3、给予执行权限
[root@localhost scripts] # chmod a+x test1.sh
 
4、确认执行结果
[root@localhost scripts] # ls -ld test1.sh   
-rwxr-xr-x 1 root root 450 Aug 10 16:44 test1.sh
 
5、运行脚本(显示的为最后一个)
[root@localhost scripts] # ./test1.sh
 
Disk  /dev/sdb : 21.5 GB, 21474836480 bytes
255 heads, 63 sectors /track , 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical /physical ): 512 bytes / 512 bytes
I /O  size (minimum /optimal ): 512 bytes / 512 bytes
Disk identifier: 0xf6885d69
 
    Device Boot      Start         End      Blocks   Id  System
/dev/sdb1                1         654     5253223+  83  Linux

3、写一个脚本,完成如下功能:

利用此前学到的if语句完成

(1) 让用户输入一个磁盘

(2) 存在,则显示其磁盘空间信息

否则,显示Fool.

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
1、写脚本
[root@localhost scripts] # cat test2.sh
#!/bin/bash
# Version: 0.0.3
# Author: Lcc.org
# Desc:.................
 
read  -t 5 -p  'Enter a special file: '  diskfile
  [ ! -n  "$diskfile"  ] &&  echo  "あなたははしごそれを取る方法を知っていますか?"  &&  exit  1
 
if  fdisk  -l |  fgrep  "Disk $diskfile"  &>  /dev/null 
then
     fdisk  -l $diskfile
     exit  0
else
     echo  "Vitis vinifera L."
     exit  250
fi
 
2、运行
1)给空白
[root@localhost scripts] # bash test2.sh 
Enter a special  file : あなたははしごそれを取る方法を知っていますか?
2)不空白,非设备
[root@localhost scripts] # bash test2.sh 
Enter a special  file : lala   
Vitis vinifera L.
3)不空白,设备
[root@localhost scripts] # bash test2.sh 
Enter a special  file /dev/sdb
 
Disk  /dev/sdb : 21.5 GB, 21474836480 bytes
255 heads, 63 sectors /track , 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical /physical ): 512 bytes / 512 bytes
I /O  size (minimum /optimal ): 512 bytes / 512 bytes
Disk identifier: 0xf6885d69
 
    Device Boot      Start         End      Blocks   Id  System
/dev/sdb1                1         654     5253223+  83  Linux










本文转自 lccnx 51CTO博客,原文链接:http://blog.51cto.com/sonlich/1955194,如需转载请自行联系原作者
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值