1、創建一個10G分區,并格式為ext4文件系統;
[root@localhost ~]# fdisk /dev/sda 命令(輸入 m 獲取幫助):n All primary partitions are in use 添加邏輯分區 5 起始 扇區 (41945088-83886079,默認為 41945088): 將使用默認值 41945088 Last 扇區, +扇區 or +size{K,M,G} (41945088-83886079,默認為 83886079):+10G 分區 5 已設置為 Linux 類型,大小設為 10 GiB 命令(輸入 m 獲取幫助):w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: 設備或資源忙. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) 正在同步磁盤。
(1) 要求其block大小為2048, 預留空間百分比為2, 卷標為MYDATA, 默認掛載屬性包含acl;
[root@localhost ~]# partprobe [root@localhost ~]# mke2fs -t ext4 -b 2048 -m 2 -L MYDATA /dev/sda5
(2) 掛載至/data/mydata目錄,要求掛載時禁止程序自動運行,且不更新文件的訪問時間戳;
[root@localhost ~]# mount -o noexec,noatime /dev/sda5 /data/mydata/
2、創建一個大小為1G的swap分區,并創建好文件系統,并啟用之;
[root@localhost ~]# fdisk /dev/sda 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 All primary partitions are in use Adding logical partition 6 First sector (62918656-83886079, default 62918656): Using default value 62918656 Last sector, +sectors or +size{K,M,G} (62918656-83886079, default 83886079): +1G Partition 6 of type Linux and of size 1 GiB is set Command (m for help): t Partition number (1-6, default 6): 6 Hex code (type L to list all codes): 82 Changed type of partition 'Linux' to 'Linux swap / Solaris' Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks. [root@localhost ~]# partprobe [root@localhost ~]# mkswap /dev/sda6 Setting up swapspace version 1, size = 1048572 KiB no label, UUID=6ae279f8-d9b5-44c1-9896-386ec7f088db [root@localhost ~]# swapon /dev/sda6 [root@localhost ~]# swapon NAME TYPE SIZE USED PRIO /dev/sda2 partition 2G 0B -1 /dev/sda6 partition 1024M 0B -2
3、寫一個腳本
(1)、獲取并列出當前系統上的所有磁盤設備;
(2)、顯示每個磁盤設備上每個分區相關的空間使用信息;
#!/bin/bash # fdisk -l | grep "^Disk /dev/[sh]d[a-z]" fdisk -l | grep "^/dev/[sh]d[a-z]"
4、總結RAID的各個級別及其組合方式和性能的不同;
RAID 0稱為條帶:可以把多塊硬盤連成一個容量更大的硬盤群,可以提高磁 盤的性能和吞吐量。RAID 0沒有冗余或錯誤修復能力,成本低,要求至少兩個磁盤,一般只是在那些對數 據安全性要求不高的情況下才被使用。 RAID 1稱為磁盤鏡像:把一個磁盤的數據鏡像到另一個磁盤上,在不影響性能情況下最大限度的保證系統的可靠性和可修復性上,具有很高的數據冗余能力,但磁盤利用 率為50%,故成本最高,多用在保存關鍵性的重要數據的場合。RAID 1的操作方式是把用戶寫入硬盤的數據百分之百地自動復制到另外一個硬盤上。 RAID 5可以理解為是RAID 0和RAID 1的折衷方案。RAID 5可以為系統提供數據安全保障,但保障程度要比Mirror低而磁盤空間利用率要比Mirror高。RAID 5具有和RAID 0相近似的數據讀取速度,只是多了一個奇偶校驗信息,寫入數據的速度比對單個磁盤進行寫入操作稍慢。同時由于多個數據對應一個奇偶校驗信息,RAID 5的磁盤空間利用率要比RAID 1高,存儲成本相對較低。 RAID10也被稱為鏡象陣列條帶。象RAID0一樣,數據跨磁盤抽??;象RAID1一樣,每個磁盤都有一個鏡象磁盤, 所以RAID 10的另一種會說法是 RAID 0+1。RAID10提供100%的數據冗余,支持更大的卷尺寸,但價格也相對較高。對大多數只要求具有冗余度而不必考慮價格的應用來說,RAID10提 供最好的性能。使用RAID10,可以獲得更好的可靠性,因為即使兩個物理驅動器發生故障(每個陣列中一個),數據仍然可以得到保護。
5、創建一個大小為10G的RAID1,要求有一個空閑盤,而且CHUNK大小為128k;
[root@localhost ~]# mdadm -C /dev/md/raid1 -n 2 -l mirror -c 128 -x 1 /dev/sdb1 /dev/sdb2 /dev/sdb3 [root@localhost ~]# cat /proc/mdstat Personalities : [raid1] md127 : active raid1 sdb3[2](S) sdb2[1] sdb1[0] 10477568 blocks super 1.2 [2/2] [UU] unused devices: <none>
6、創建一個大小為4G的RAID5設備,chunk大小為256k,格式化ext4文件系統,要求可開機自動掛載至/backup目錄,而且不更新訪問時間戳,且支持acl功能;
[root@localhost ~]# mdadm -C /dev/md/raid5 -n 3 -l raid5 -c 256 /dev/sdb5 /dev/sdb6 /dev/sdb7 mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md/raid5 started. [root@localhost ~]# mke2fs -t ext4 /dev/md/raid5 mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=64 blocks, Stripe width=128 blocks 262144 inodes, 1047552 blocks 52377 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1073741824 32 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 Allocating group tables: done Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done [root@localhost ~]# vim /etc/fstab /dev/md/raid5 /backup ext4 noatime,acl 0 0
7、寫一個腳本
(1) 接受一個以上文件路徑作為參數;
(2) 顯示每個文件擁有的行數;
(3) 總結說明本次共為幾個文件統計了其行數;
#!/bin/bash # declare -i cnt=0 if [ $# -lt 1 ] then echo "at least input one file" exit 1 fi for i in $* do if [ -e $i ] then wc -l $i cnt+=1 else echo "$i isn't exist" fi done echo "$cnt files have been counted"
8、寫一個腳本
(1) 傳遞兩個以上字符串當作用戶名;
(2) 創建這些用戶;且密碼同用戶名;
(3) 總結說明共創建了幾個用戶;
#!/bin/bash # declare -i count=0 if [ $# -lt 2 ] then echo "input at least two usernames" exit 1 fi for username in $* do id $username &> /dev/null if [ $? -eq 0 ] then echo "$username existed" else useradd $username echo "$username"|passwd --stdin $username &>/dev/null count+=1 fi done echo "$count users have been created"
9、寫一個腳本,新建20個用戶,visitor1-visitor20;計算他們的ID之和;
#!/bin/bash # declare -i count=0 for i in {1..20} do useradd visitor$i &> /dev/null uid=$(id -u visitor$i) count+=$uid done echo "the sum of uid is $count"
10、寫一腳本,分別統計/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#號開頭的行數之和,以及總的空白行數;
#!/bin/bash # declare -i sum1=0 declare -i sum2=0 for file in /etc/rc.d/rc.sysinit /etc/rc.d/init.d/functions /etc/fstab do num1=$(grep "^#" $file | wc -l) num2=$(grep "^[[:space:]]*$" $file | wc -l) sum1+=num1 sum2+=num2 done echo "total # started lines:$sum1" echo "total blank lines:$sum2"
11、寫一個腳本,顯示當前系統上所有默認shell為bash的用戶的用戶名、UID以及此類所有用戶的UID之和;
#!/bin/bash # grep "/bin/bash" /etc/passwd | cut -d: -f 1,3 declare -i sum=0 for i in $(grep "/bin/bash" /etc/passwd | cut -d: -f 3) do sum+=$i done echo "sum of uid is $sum"
12、寫一個腳本,顯示當前系統上所有,擁有附加組的用戶的用戶名;并說明共有多少個此類用戶;
#!/bin/bash # declare -i count=0 for username in $(cat /etc/passwd | cut -d: -f 1) do id $username | cut -d ' ' -f 3 |grep "," &> /dev/null if [ $? -eq 0 ] then echo "$username" count+=1 fi done echo "the sum of users is $count"
13、創建一個由至少兩個物理卷組成的大小為20G的卷組;要求,PE大小為8M;而在卷組中創建一個大小為5G的邏輯卷mylv1,格式化為ext4文件系統,開機自動掛載至/users目錄,支持acl;
[root@localhost ~]# pvcreate /dev/sdb1 [root@localhost ~]# pvcreate /dev/sdb2 [root@localhost ~]# vgcreate -s 8M myvg /dev/sdb[12] [root@localhost ~]# lvcreate -L 5G -n mylv1 myvg [root@localhost ~]# mke2fs -t ext4 /dev/myvg/mylv1 [root@localhost ~]# vim /etc/fstab /dev/myvg/mylv1 /users ext4 acl 0 0
14、新建用戶magedu;其家目錄為/users/magedu,而后su切換至此用戶,復制多個文件至家目錄;
[root@localhost ~]# useradd magedu -d /users/magedu [root@localhost ~]# su - magedu [magedu@localhost ~]$ cp -r /etc/rc.d/* ~
15、擴展mylv1至9G,確保擴展完成后原有數據完全可用;
[root@localhost ~]# lvextend -L 9G /dev/myvg/mylv1 Size of logical volume myvg/mylv1 changed from 5.00 GiB (640 extents) to 9.00 GiB (1152 extents). Logical volume mylv1 successfully resized. [root@localhost ~]# resize2fs /dev/myvg/mylv1 9G resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/myvg/mylv1 to 2359296 (4k) blocks. The filesystem on /dev/myvg/mylv1 is now 2359296 blocks long.
16、縮減mylv1至7G,確??s減完成后原有數據完全可用;
[root@localhost ~]# umount /dev/myvg/mylv1 [root@localhost ~]# e2fsck -f /dev/myvg/mylv1 e2fsck 1.42.9 (28-Dec-2013) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/myvg/mylv1: 11/589824 files (0.0% non-contiguous), 75551/2359296 blocks [root@localhost ~]# resize2fs /dev/myvg/mylv1 7G resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/myvg/mylv1 to 1835008 (4k) blocks. The filesystem on /dev/myvg/mylv1 is now 1835008 blocks long. [root@localhost ~]# lvreduce -L 7G /dev/myvg/mylv1 WARNING: Reducing active logical volume to 7.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce mylv1? [y/n]: y Size of logical volume myvg/mylv1 changed from 9.00 GiB (1152 extents) to 7.00 GiB (896 extents). Logical volume mylv1 successfully resized. [root@localhost ~]# mount -a
17、對mylv1創建快照,并通過備份數據;要求保留原有的屬主屬組等信息;
[root@localhost ~]# lvcreate -s -L 30M -n snapmylv1 -p r /dev/myvg/mylv1 Rounding up size to full physical extent 32.00 MiB Logical volume "snapmylv1" created.
原創文章,作者:zhangxiaola,如若轉載,請注明出處:http://www.www58058.com/49982
出于腳本編程嚴謹角度,第8題知道判斷用戶是否存在,其實第九題也需要做個判斷,除非你的系統本身沒有此用戶。