1. 程式人生 > >磁盤管理和腳本交互小練習

磁盤管理和腳本交互小練習

linux練習

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、顯示所有磁盤塊及分區信息
[[email protected] scripts]# fdisk -l /dev/[sh]d[a-z]
2、根據 2610 cylinders 和End 判斷能否分區,並確認分區

3、管理分區
[[email protected] scripts]# fdisk /dev/sdb
    n,p, , +20G, w

4、更新內核識別分區表
[[email protected] scripts]# partx -a /dev/sdb
[[email protected] scripts]# partx -a /dev/sdb

5、格式化分區
[[email protected]
/* */ scripts]# mke2fs -t ext4 -b 2048 -L ‘TEST‘ /dev/sdb2 6、查看默認掛載屬性 [[email protected] scripts]# dumpe2fs -h /dev/sdb2 Default mount options: (none) 7、調整默認掛載屬性acl [[email protected] scripts]# tune2fs -o acl /dev/sdb2 8、查看默認掛載屬性 [[email protected] scripts]# dumpe2fs -h /dev/sdb2 Default mount options: acl ##嘿嘿


2、創建5G文件系統,卷標HUGE,要求此分區開機自動掛載至/mogdata,文件系統類型ext3

1、查看sdb的分區表
[[email protected] scripts]# kpartx -l /dev/sdb
sdb1 : 0 20980827 /dev/sdb 63
sdb2 : 0 224910 /dev/sdb 20980890
2、破壞分區表
[[email protected] 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
[[email protected] scripts]# kpartx -l /dev/sdb
[[email protected] scripts]# 

3、管理sdb
[[email protected] scripts]# fdisk /dev/sdb
n,p,1, ,+5G,w

4、讓內核重讀分區表
[[email protected] scripts]# partx -a /dev/sdb

5、格式化
[[email protected] scripts]# mkfs.ext2 -L ‘HUGE‘ /dev/sdb1
[[email protected] scripts]# tune2fs -O has_journal /dev/sdb1
[[email protected] scripts]# blkid /dev/sdb1
/dev/sdb1: LABEL="HUGE" UUID="1b4a4a93-20a1-439e-b40b-50989cf21fed" SEC_TYPE="ext2" TYPE="ext3" 

6、開機掛載至/mogdata
1)創建目錄
[[email protected] scripts]# mkdir /mogdata

2)查看是否創建
[[email protected] scripts]# ls -ld /mogdata
drwxr-xr-x 2 root root 4096 Aug 10 16:29 /mogdata

3)配置fstab
[[email protected] 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、寫腳本
[[email protected] 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、測試語法(沒有信息就是最好的信息)
[[email protected] scripts]# bash -n test1.sh

3、給予執行權限
[[email protected] scripts]# chmod a+x test1.sh

4、確認執行結果
[[email protected] scripts]# ls -ld test1.sh   
-rwxr-xr-x 1 root root 450 Aug 10 16:44 test1.sh

5、運行腳本(顯示的為最後一個)
[[email protected] 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、寫腳本
[[email protected] 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)給空白
[[email protected] scripts]# bash test2.sh 
Enter a special file: あなたははしごそれを取る方法を知っていますか?
2)不空白,非設備
[[email protected] scripts]# bash test2.sh 
Enter a special file: lala   
Vitis vinifera L.
3)不空白,設備
[[email protected] 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


本文出自 “Reading” 博客,請務必保留此出處http://sonlich.blog.51cto.com/12825953/1955194

磁盤管理和腳本交互小練習