這一週把磁碟管理與腳本基本功一起練:從 fdisk 分區、mkfs 格式化、mount 參數,一路到 RAID、LVM 與十幾道 shell 題目。
要求 block 2048、預留 2%、卷標 MYDATA、預設掛載屬性含 acl。
$ fdisk -l /dev/sd*
$ fdisk /dev/sdb
Command (m for help): n
Partition number (1-4): 1
Last cylinder, +size{K,M,G}: +10G
Command (m for help): w
$ partx -a -n 1:1 /dev/sdb
$ mke2fs -t ext4 -b 2048 -m 2 -L 'MYDATA' -o acl,Defaults /dev/sdb1
$ tune2fs -o acl /dev/sdb1
$ dumpe2fs -h /dev/sdb1
掛載時禁止執行與不更新存取時間:
$ mkdir /data/mydata $ mount -o noexec,noatime /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
$ fdisk /dev/sdb # 新增 partition 2,type 改為 82 $ partx -a -n 2 /dev/sdb $ mkswap /dev/sdb3 Setting up swapspace version 1, size = 1060284 KiB $ swapon /dev/sdb3 $ swapon -s Filename Type Size Used Priority /dev/dm-1 partition 2031612 0 -1 /dev/sdb3 partition 1060284 0 -2
$ cat /tmp/test.sh #!/bin/bash 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 -h $ chmod +x /tmp/test.sh && bash /tmp/test.sh
RAID 各級別的取捨大致是:0 效能最高但無冗餘,1 鏡像安全但成本最高,5 兼顧讀取效能與空間利用率(需 3 顆以上),10 則是效能與安全並重。
建立含一顆熱備的 10G RAID1,chunk 128k:
mdadm -C /dev/md0 -n 2 -x 1 -c 128 -l 1 /dev/sdb{2,3,4}
建立 4G RAID5、chunk 256k,格式化後開機自動掛載至 /backup,且不更新 atime、支援 acl:
mdadm -C /dev/md1 -n 3 -c 256 -l 5 -a yes /dev/sdb{5,6,7}
mke2fs -t ext4 /dev/md1
mkdir /backup
mount -o noatime -o acl /dev/md1 /backup/
# /etc/fstab 補上:/dev/md1 /backup ext4 acl,noatime 0 0
$ cat /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 ..."' ; 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"
$ cat /tmp/work78.sh
#!/bin/bash
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 /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 a55 exist!!! the users created total is : 0
$ cat /tmp/work79.sh
#!/bin/bash
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"
#!/bin/bash declare -i lines1=0 declare -i lines2=0 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
列出所有預設 shell 為 bash 的使用者、UID 及其總和;以及列出所有擁有附加群組的使用者並統計數量。這兩題的核心都是從 /etc/passwd 切欄位,再搭配 id、cut、grep 做條件過濾。
以至少兩個實體卷組成 20G 卷組、PE 8M,再切出 5G 邏輯卷 mylv1 格式化 ext4、開機自動掛 /users 並支援 acl。
接著新建 magedu 使用者、家目錄設為 /users/magedu,切換後複製多檔進去。
擴展至 9G:
lvextend -L +4G /dev/myvg0/mylv1 resize2fs /dev/myvg0/mylv1 # 線上擴容可直接執行
縮減至 7G 則必須先卸載、檢查、縮小檔案系統,再縮邏輯卷:
umount /dev/myvg0/mylv1 e2fsck -f /dev/myvg0/mylv1 resize2fs /dev/myvg0/mylv1 7G lvreduce -L -2G /dev/myvg0/mylv1 mount /dev/myvg0/mylv1 /users/
最後建立快照並以唯讀掛載備份,屬主屬組等資訊自然完整保留:
lvcreate -L 7G -p r -s -n sn_mylv1 /dev/myvg0/mylv1 mount /dev/myvg0/sn_mylv1 /snmylv/ # 會自動以唯讀掛載
順序是這週最該記牢的部分:縮容一定要「先卸載 → 修檔案系統 → 縮 fs → 縮 LV」,顛倒任何一步都可能毀掉資料。