本週作業內容:
1、創建一個10G分區,並格式爲ext4文件系統;
(1)要求其block大小爲2048, 預留空間百分比爲2, 卷標爲MYDATA, 默認掛載屬性包含acl;
(2)掛載至/data/mydata目錄,要求掛載時禁止程序自動運行,且不更新文件的訪問時間戳;
$ fdisk -l /dev/sd* $ fdisk /dev/sdb Command (m for help): p Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-3263, default 1): 1 Last cylinder, +cylinders or +size{K,M,G} (1-3263, default 3263): +10G Command (m for help): p Command (m for help): w $ partx -a -n 1:1 /dev/sdb $ cat /proc/partitions major minor #blocks name 8 0 31457280 sda 8 1 512000 sda1 8 2 30944256 sda2 8 16 26214400 sdb 8 17 10490413 sdb1 $ mke2fs -t ext4 -b 2048 -m 2 -L 'MYDATA' /dev/sdb1 $ tune2fs -o acl /dev/sdb1 tune2fs 1.41.12 (17-May-2010) $ dumpe2fs -h /dev/sdb1 ...
$ mkdir /data/mydata $ mount -o noexec,noatime,acl,defaults /dev/sdb1 /data/mydata $ cat /proc/mounts | grep '^/dev/sdb1' /dev/sdb1 /data/mydata ext4 rw,seclabel,noexec,noatime,barrier=1,data=ordered 0 0
2、創建一個大小爲1G的swap分區,並創建好文件系統,並啓用之;
$ fdisk /dev/sdb Command (m for help): p Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (1307-3263, default 1307): 1307 Last cylinder, +cylinders or +size{K,M,G} (1307-3263, default 3263): +1G Command (m for help): t Partition number (1-4): 2 Hex code (type L to list codes): 82 Changed system type of partition 2 to 82 (Linux swap / Solaris) Command (m for help): p Command (m for help): w $ partx -a -n 2 /dev/sdb BLKPG: Device or resource busy error adding partition 2 $ cat /proc/partitions | grep 'sdb[0-9]$' 8 17 10490413 sdb1 8 19 1060290 sdb3
$ mkswap /dev/sdb3 Setting up swapspace version 1, size = 1060284 KiB no label, UUID=418ed5bb-f1d9-427c-946f-f614c815618e $ swapon /dev/sdb3 $ swapon -s Filename Type Size Used Priority /dev/dm-1 partition 2031612 0 -1 /dev/sdb3 partition 1060284 0 -2
3、寫一個腳本
(1)、獲取並列出當前系統上的所有磁盤設備;
(2)、顯示每個磁盤設備上每個分區相關的空間使用信息;
$ cat /tmp/test.sh #!/bin/bash # the script file named getdiskinfo is located /tmp/test.sh echo 'All of the disks devices information is:' blkid | grep '^/dev/sd[a-z]' echo echo 'all of the disks partitions is :' fdisk -l | grep -e '^Disk /dev/sd[a-z]' -e '^/dev/sd[a-z]' echo echo 'the partitions used information is:' df -lh $ chmod +x /tmp/test.sh $ bash /tmp/test.sh
4、總結RAID的各個級別及其組合方式和性能的不同;
Raid:Redundant Arrays of Inexpensive(Independent) Disks,獨立磁盤冗餘陣列,由多塊廉價的(或獨立的)磁盤組成的磁盤陣列。根據磁盤組織方式的不同,分爲不同的磁盤陣列級別,常用的Raid Level有Raid0,Raid1,Raid5,Raid6,Raid10,Raid50,其組合方式及特點如下:
Raid0,也稱爲Stripe,條帶狀。
工作原理:把N塊磁盤通過Arrays卡串聯在一起創建一個大的卷集,選擇合理的帶區來創建帶區集,將數據分割到所有的N塊硬盤中同時進行讀寫。一般只用在對數據安全性沒要求的場合
讀寫性能大幅提升
容量:N*min(S1,S2,...)
冗餘:無冗餘能力
磁盤:2,2+
Raid1,也稱mirrors,鏡像卷
工作原理:把一個磁盤的數據完全鏡像到另一個磁盤,即數據在寫入一塊磁盤的同時,會在另一塊閒置的磁盤上生成鏡像文件,最大限度的保證系統的可靠性和可修復性,多用在保存關鍵性的重要數據的場合。
讀性能提升,寫性能略微下降
容量:N*min(S1,S2,...)/2
冗餘:最多可以損壞一半磁盤
磁盤:2,2+
Raid5,分佈式奇偶校驗的獨立磁盤結構。
Raid5的奇偶校驗碼在不同時刻分佈在不同的磁盤上。Raid4的奇偶校驗碼固定存放在一塊磁盤上。
讀性能大幅提升,寫性能略提升
容量:N*min(S1,S2,...) -1
冗餘:最多可以損壞一塊磁盤
磁盤:3,3+
Raid6,帶有兩種分佈存儲的奇偶校驗碼的獨立磁盤結構。
與Raid5不同的是,Raid6在各個時刻都使用了不同的兩塊磁盤作爲奇偶校驗碼。是對RAID5的擴展,主要是用於要求數據絕對不能出錯的場合
讀性能大幅提升,寫性能下降
容量:N*min(S1,S2,...) -2
冗餘:最多可以損壞兩塊磁盤
磁盤:4,4+
Raid10,高可靠性與高效磁盤結構
由下往上,先做Raid1,再做Raid0,兩種結構的優缺點相互補充,達到既高效又高速的目的。這種新結構的價格高,可擴充性不好。主要用於數據容量不大,但要求速度和差錯控制的數據庫中。
讀性能大幅提升,寫一般
容量:N*min(S1,S2,...)/2
冗餘:最多可以損壞兩塊磁盤
磁盤:4,4+
5、創建一個大小爲10G的RAID1,要求有一個空閒盤,而且CHUNK大小爲128k;
$ fdisk /dev/sdb Command (m for help): p Device Boot Start End Blocks Id System Command (m for help): n Command action e extended p primary partition (1-4) e Partition number (1-4): 1 First cylinder (1-6527, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-6527, default 6527): +35G Command (m for help): n Command action l logical (5 or over) p primary partition (1-4) l First cylinder (1-4570, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-4570, default 4570): +10G Command (m for help): n Command action l logical (5 or over) p primary partition (1-4) l First cylinder (1307-4570, default 1307): Using default value 1307 Last cylinder, +cylinders or +size{K,M,G} (1307-4570, default 4570): +10G Command (m for help): n Command action l logical (5 or over) p primary partition (1-4) l First cylinder (2613-4570, default 2613): Using default value 2613 Last cylinder, +cylinders or +size{K,M,G} (2613-4570, default 4570): +10G Command (m for help): p Command (m for help): t Partition number (1-7): 5 Hex code (type L to list codes): fd Changed system type of partition 5 to fd (Linux raid autodetect) Command (m for help): t Partition number (1-7): 6 Hex code (type L to list codes): fd Changed system type of partition 6 to fd (Linux raid autodetect) Command (m for help): t Partition number (1-7): 7 Hex code (type L to list codes): fd Changed system type of partition 7 to fd (Linux raid autodetect) Command (m for help):p Command (m for help):w $ partx -a /dev/sdb $ cat /proc/partitions $ cat /proc/mdstat $ mdadm -C /dev/md0 -a yes -n 2 -x 1 -l 1 -c 128 /dev/sdb{5,6,7}
6、創建一個大小爲4G的RAID5設備,chunk大小爲256k,格式化ext4文件系統,要求可開機自動掛載至/backup目錄,而且不更新訪問時間戳,且支持acl功能;
$ fdisk /dev/sdb Command (m for help):p major minor #blocks name /dev/sdb8 1570 1832 2104484+ fd Linux raid autodetect Partition 8 does not start on physical sector boundary. /dev/sdb9 1832 2094 2104484+ fd Linux raid autodetect Partition 9 does not start on physical sector boundary. /dev/sdb10 2094 2355 2104480+ fd Linux raid autodetect Partition 10 does not start on physical sector boundary. $ mdadm -C /dev/md5 -l 5 -n 3 -c 256 /dev/sdb{8,9,10} mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md0 started. $ cat /proc/mdstat cat /proc/mdstat Personalities : [raid1] [raid6] [raid5] [raid4] md5 : active raid5 sdb10[3] sdb9[1] sdb8[0] 4204544 blocks super 1.2 level 5, 256k chunk, algorithm 2 [3/3] [UUU $ mkdir -p /backup $ mkfs.ext4 /dev/md5 $ mount -o auto,noatime,acl /dev/md5 /backup
7、寫一個腳本
(1)接受一個以上文件路徑作爲參數;
(2)顯示每個文件擁有的行數;
(3)總結說明本次共爲幾個文件統計了其行數;
$ vim /tmp/work77.sh
#!/bin/bash # declare -i sumlines=0 declare -i totallines=0 case "$#" in 0) echo 'please input more than one argment,such as "/tmp/test/work7_7 /PATH/FROM/FILE1 /PATH/FROM/FILE2 ..."' exit 1 ;; *) for i in $@;do if [ ! -e $i ];then echo 'this file $i not exist' else sumlines=$( wc -l $i | cut -d' ' -f1 ) echo "this file $i lines total is : $sumlines" fi totallines+=$sumlines done ;; esac echo "the total files are : $#" echo "the total lines of all files are : $totallines"
$ bash -n /tmp/work77.sh $ bash /tmp/work77.sh please input more than one argment,such as "/tmp/test/work7_7 /PATH/FROM/FILE1 /PATH/FROM/FILE2 ..." $ bash /tmp/work77.sh /etc/fstab /etc/mtab /proc/mounts this file /etc/fstab lines total is : 15 this file /etc/mtab lines total is : 9 this file /proc/mounts lines total is : 16 the total files are : 3 the total lines of all files are : 40
8、寫一個腳本
(1)傳遞兩個以上字符串當作用戶名;
(2)創建這些用戶;且密碼同用戶名;
(3)總結說明共創建了幾個用戶;
$ vim /tmp/work78.sh
#!/bin/bash # creat user\ declare -i i=0 for userid in "$@" ;do if [ $( wc -c<<<$userid ) -gt 2 ] ;then if id $userid &> /dev/null ;then echo "$userid exist!!!" else useradd $userid && echo "created new user: $userid" echo $userid | passwd --stdin $userid &> /dev/null let i++ fi else echo "username length less 2 charaters" fi done echo "the users created total is : $i"
$ bash -n /tmp/work78.sh $ bash /tmp/work78.sh 0 username length less 2 charaters the users created total is : 0 $ bash /tmp/work78.sh a5 a55 created new user: a5 created new user: a55 the users created total is : 2 $ bash /tmp/work78.sh a55 a55 exist!!! the users created total is : 0
9、寫一個腳本,新建20個用戶,visitor1-visitor20;計算他們的ID之和;
$ vim /tmp/work79.sh
#!/bin/bash # creat 20 user and sum total of all user ID declare -i i=1 declare -i sum=0 for i in {1..20};do if id visitor$i &> /dev/null ;then echo "user visitor$i exist" else useradd visitor$i sum=$(id visitor$i | cut -d'=' -f2 | cut -d'(' -f1) sum+=$sum fi done echo "the total of all new user ID is : $sum"
$ bash -n /tmp/work79.sh $ bash !$ bash /tmp/work79.sh creat new user visitor1 creat new user visitor2 creat new user visitor3 creat new user visitor4 creat new user visitor5 creat new user visitor6 creat new user visitor7 creat new user visitor8 creat new user visitor9 creat new user visitor10 creat new user visitor11 creat new user visitor12 creat new user visitor13 creat new user visitor14 creat new user visitor15 creat new user visitor16 creat new user visitor17 creat new user visitor18 creat new user visitor19 creat new user visitor20 the total of all new user ID is : 10250
10、寫一腳本,分別統計/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#號開頭的行數之和,以及總的空白行數;
#!/bin/bash # this script is located /tmp/work710.sh # this one function count the blank lines of some files. declare -i lines1=0 # lines1 is total of # lines declare -i lines2=0 # lines2 is total of bank lines for i in /etc/rc.d/sysinit /etc/rc.d/init.d/functions /etc/fstab do x='grep -e '^#' $i | wc -l' y='grep -e '^[[:space:]]' $i | wc -l' lines1=$x+$lines1 lines2=$y+$lines2 done echo "the total of the # head lines is : $lines1" echo "the total of the blank head lines is : $lines2"
$ bash /tmp/work710.sh the total of the # head lines is : 94 the total of the blank head lines is : 1128
11、寫一個腳本,顯示當前系統上所有默認shell爲bash的用戶的用戶名、UID以及此類所有用戶的UID之和;
$ cat 11.sh #!/bin/bash # declare -i sumuid=0 declare -a uid=$( grep 'bash$' /etc/passwd | cut -d: -f3 ) grep 'bash$' /etc/passwd | cut -d: -f1,3 for i in $uid;do let sumuid+=$i done echo "UID total is : $sumuid" $ bash 11.sh root:0 admin:500 magedu:501 UID total is : 1001
12、寫一個腳本,顯示當前系統上所有,擁有附加組的用戶的用戶名;並說明共有多少個此類用戶;
$ vim 12.sh #!/bin/bash declare -i i=0 userlist=$( gre '[^:]$' /etc/group | cut -d: -f4 ) usersum=$( cat /etc/passwd | wc -l ) for i in $(seq 1 1 $usersum) ;do username=$( head -$i /etc/passwd | cut -d: -f1 ) getuser=$(echo $userlist | grep -o $username 2> /dev/null) echo "user: $getuser have append group" if [ -z $getuser ];then let i++ fi echo "total user is : $i"
13、創建一個由至少兩個物理卷組成的大小爲20G的卷組;要求,PE大小爲8M;而在卷組中創建一個大小爲5G的邏輯卷mylv1,格式化爲ext4文件系統,開機自動掛載至/users目錄,支持acl;
$ pvcreate /dev/sdb $ pvcreate /dev/sdc $ pvs $ vgcreat -s 8M myvg /dev/sdb /dev/sdc $ lvcreate -L 5G -n mylv1 /dev/myvg $ mke2fs -t ext4 /dev/myvg/mylv1 $ mkdir -p /users $ mount -a -o auto,acl /dev/myvg/mylv1 /users 在/etc/fstab中增加一行 /dev/myvg/mylv1 /users auto,acl 0 0
14、新建用戶magedu;其家目錄爲/users/magedu,而後su切換至此用戶,複製多個文件至家目錄;
$ mkdir -p /users $ useradd -d /users/magedu magedu $ su - magedu $ cp /etc/fstab /etc/mtab /proc/mounts /users/magedu
15、擴展mylv1至9G,確保擴展完成後原有數據完全可用;
$ lvextend -L 9G /dev/myvg/mylv1 $ resize2fs /dev/myvg/mylv1
16、縮減mylv1至7G,確保縮減完成後原有數據完全可用;
$ umount /dev/myvg/mylv1 $ e2fsck -f /dev/myvg/mylv1 $ resize2fs /dev/mhvg/mylv1 $ lvreduce -L 7G /dev/myvg/mylv1 $ mount -a -o auto,acl /dev/myvg/mylv1 /users
17、對mylv1創建快照,並通過備份數據;要求保留原有的屬主屬組等信息;
$ lvcreate -L 2g -p r -s -n snapshot_mylv1 /dev/myvg/mylv1 $ lvdisplay $ mkdir /snapshot $ mount /dev/myvg/snapshot_mylv1 /snapshot