1、創建一個10G分區,并格式為ext4文件系統;
[root@localhost xuc]# cat /proc/partitions major minor #blocks name 8 0 104857600 sda 8 1 524288 sda1 8 2 20971520 sda2 8 3 10485760 sda3 8 4 10485235 sda4 //新建分區10G (1) 要求其block大小為2048, 預留空間百分比為2, 卷標為MYDATA, 默認掛載屬性包含acl; [root@localhost xuc]# mke2fs -t ext4 -b 2048 -m 2 -L MYDATA /dev/sda4 mke2fs 1.41.12 (17-May-2010) 文件系統標簽=MYDATA 操作系統:Linux 塊大小=2048 (log=1) 分塊大小=2048 (log=1) Stride=0 blocks, Stripe width=0 blocks 655360 inodes, 5242616 blocks 104852 blocks (2.00%) reserved for the super user 第一個數據塊=0 Maximum filesystem blocks=542113792 320 block groups 16384 blocks per group, 16384 fragments per group 2048 inodes per group Superblock backups stored on blocks: 16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816, 1327104, 2048000, 3981312 正在寫入inode表: 完成 Creating journal (32768 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 This filesystem will be automatically checked every 28 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. (2) 掛載至/data/mydata目錄,要求掛載時禁止程序自動運行,且不更新文件的訪問時間戳; [root@localhost xuc]# mkdir -pv /data/mydata mkdir: 已創建目錄 "/data" mkdir: 已創建目錄 "/data/mydata" [root@localhost xuc]# mount -o noexec,noatime /dev/sda4 /data/mydata/ [root@localhost xuc]# mount /dev/sda2 on / type ext4 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0") /dev/sda1 on /boot type ext4 (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) /dev/sda4 on /data/mydata type ext4 (rw,noexec,noatime)
2、創建一個大小為1G的swap分區,并創建好文件系統,并啟用之;
//由于內存4G,因此這次創建的swap是內存2倍10G,而不是題目要求的1G Device Boot Start End Blocks Id System /dev/sda1 * 1 66 524288 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 66 1371 10485760 83 Linux Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 3 First cylinder (1371-10443, default 1371): Using default value 1371 Last cylinder, +cylinders or +size{K,M,G} (1371-10443, default 10443): +10G Command (m for help): m Command action a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition l list known partition types m print this menu n add a new partition o create a new empty DOS partition table p print the partition table q quit without saving changes s create a new empty Sun disklabel t change a partition's system id u change display/entry units v verify the partition table w write table to disk and exit x extra functionality (experts only) Command (m for help): t Partition number (1-4): 3 Hex code (type L to list codes): 82 Changed system type of partition 3 to 82 (Linux swap / Solaris) Command (m for help): p Disk /dev/sda: 85.9 GB, 85899345920 bytes 255 heads, 63 sectors/track, 10443 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: 0x0006bb86 Device Boot Start End Blocks Id System /dev/sda1 * 1 66 524288 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 66 1371 10485760 83 Linux /dev/sda3 1371 2676 10483898 82 Linux swap / Solaris Command (m for help): w The partition table has been altered! [root@localhost xuc]# partx -a /dev/sda BLKPG: Device or resource busy error adding partition 1 BLKPG: Device or resource busy error adding partition 2 BLKPG: Device or resource busy error adding partition 3 [root@localhost xuc]# cat /proc/partitions major minor #blocks name 8 0 83886080 sda 8 1 524288 sda1 8 2 10485760 sda2 8 3 10483898 sda3 [root@localhost xuc]# mkswap /dev/sda3 Setting up swapspace version 1, size = 10483892 KiB no label, UUID=a382c6e1-521f-4142-99f4-cb67146809fe [root@localhost xuc]# swapon /dev/sda3
3、寫一個腳本
(1)、獲取并列出當前系統上的所有磁盤設備; (2)、顯示每個磁盤設備上每個分區相關的空間使用信息; //通過函數調用來實現,第一個用cut,第二個用awk #!/bin/bash # diskinfo(){ echo -e "Disks on the deivce:" fdisk -l |egrep -o '^Disk /dev/sd[a-z]'|cut -d" " -f2 } usage(){ df -h |egrep '/dev/sd' |awk -F' ' 'BEGIN{print "Usage is :"}{print $1,$3}' } diskinfo echo usage [root@localhost test-scripts]# bash diskinfo Disks on the deivce: /dev/sda Usage is : /dev/sda2 2.8G /dev/sda1 38M
4、總結RAID的各個級別及其組合方式和性能的不同;
-
RAID:Redundant Arrays of Inexpensive Disks廉價磁盤冗余陣列 主要為了提高I/O能力和容錯率
-
常用級別分為RAID-0,RADI-1,RAID-2…RAID-5,RAID-6,組合方式RAID10,RAID01
-
下面具體介紹一下具體實現和性能差異:
5、創建一個大小為10G的RAID1,要求有一個空閑盤,而且CHUNK大小為128k;
//正好有兩個擴展分區磁盤為10G的 就做了RAID [root@localhost test-scripts]# mdadm -C /dev/md0 -n 2 -l 1 -c 128 /dev/sda{5,6} [root@localhost test-scripts]# mdadm -D /dev/md0 /dev/md0: Version : 1.2 Creation Time : Sat Oct 8 18:05:03 2016 Raid Level : raid1 Array Size : 10482176 (10.00 GiB 10.73 GB) Used Dev Size : 10482176 (10.00 GiB 10.73 GB) Raid Devices : 2 Total Devices : 2 Persistence : Superblock is persistent Update Time : Sat Oct 8 18:05:26 2016 State : clean, resyncing Active Devices : 2 Working Devices : 2 Failed Devices : 0 Spare Devices : 0 Resync Status : 44% complete Name : localhost.localdomain:0 (local to host localhost.localdomain) UUID : bee8c695:a50dfdfc:b3021feb:c3cafbba Events : 7 Number Major Minor RaidDevice State 0 8 5 0 active sync /dev/sda5 1 8 6 1 active sync /dev/sda6
6、創建一個大小為4G的RAID5設備,chunk大小為256k,格式化ext4文件系統,要求可開機自動掛載至/backup目錄,而且不更新訪問時間戳,且支持acl功能;
[root@localhost test-scripts]# mdadm -C /dev/md1 -n 3 -l 5 -c 256 /dev/sda{7,8,9} mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md1 started. [root@localhost test-scripts]# mke2fs -t ext4 /dev/md1 mke2fs 1.41.12 (17-May-2010) 文件系統標簽= 操作系統:Linux 塊大小=4096 (log=2) 分塊大小=4096 (log=2) Stride=64 blocks, Stripe width=128 blocks 262944 inodes, 1051136 blocks 52556 blocks (5.00%) reserved for the super user 第一個數據塊=0 Maximum filesystem blocks=1077936128 33 block groups 32768 blocks per group, 32768 fragments per group 7968 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 正在寫入inode表: 完成 Creating journal (32768 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 This filesystem will be automatically checked every 22 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@localhost ~]# vim /etc/fstab tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 /dev/md1 /backup ext4 noatime,acl 0 0
7、寫一個腳本
(1) 接受一個以上文件路徑作為參數; (2) 顯示每個文件擁有的行數; (3) 總結說明本次共為幾個文件統計了其行數; #!/bin/bash # if [ $# -lt 1 ]; then echo "no parament,retry plz" exit 1 else for i in $*;do echo -n "$i file line is :" cat $i |wc -l done fi echo "Total file number is $#" [root@localhost test-scripts]# bash fileline.sh /etc/passwd /etc/fstab /etc/passwd file line is :32 /etc/fstab file line is :15 Total file number is 2 [root@localhost test-scripts]# bash fileline.sh no parament,retry plz
8、寫一個腳本
(1) 傳遞兩個以上字符串當作用戶名; (2) 創建這些用戶;且密碼同用戶名; (3) 總結說明共創建了幾個用戶; #!/bin/bash # declare -i count=0 if [ $# -le 1 ]; then echo "no parament,retry plz" exit 1 fi for i in $*;do id $i &>/dev/null if [ $? -eq 0 ];then echo "$i is already exist" else useradd $i echo "$i" |passwd --stadin $i &>/dev/null let count+=1 fi done echo "Total parament is $#,create username is $count" [root@localhost test-scripts]# bash adduser.sh tom mike tom is already exist mike is already exist Total parament is 2,create username is 0 [root@localhost test-scripts]# bash adduser.sh tom jerry tom is already exist Total parament is 2,create username is 1
9、寫一個腳本,新建20個用戶,visitor1-visitor20;計算他們的UID之和;
#!/bin/bash # declare -i uid_sum=0 declare -i uid=0 for i in {1..20};do useradd visitor$i id visitor$i &>/dev/null uid=`id visitor$i |cut -d" " -f1 |egrep -o '[0-9]{3}'` let uid_sum+=uid done echo "visitor1~visitor20 UID_SUM is $uid_sum" [root@localhost test-scripts]# bash UID_SUM.sh uid=501(visitor1) gid=501(visitor1) 組=501(visitor1) uid=502(visitor2) gid=502(visitor2) 組=502(visitor2) uid=503(visitor3) gid=503(visitor3) 組=503(visitor3) uid=504(visitor4) gid=504(visitor4) 組=504(visitor4) uid=505(visitor5) gid=505(visitor5) 組=505(visitor5) uid=506(visitor6) gid=506(visitor6) 組=506(visitor6) uid=507(visitor7) gid=507(visitor7) 組=507(visitor7) uid=508(visitor8) gid=508(visitor8) 組=508(visitor8) uid=509(visitor9) gid=509(visitor9) 組=509(visitor9) uid=510(visitor10) gid=510(visitor10) 組=510(visitor10) uid=511(visitor11) gid=511(visitor11) 組=511(visitor11) uid=512(visitor12) gid=512(visitor12) 組=512(visitor12) uid=513(visitor13) gid=513(visitor13) 組=513(visitor13) uid=514(visitor14) gid=514(visitor14) 組=514(visitor14) uid=515(visitor15) gid=515(visitor15) 組=515(visitor15) uid=516(visitor16) gid=516(visitor16) 組=516(visitor16) uid=517(visitor17) gid=517(visitor17) 組=517(visitor17) uid=518(visitor18) gid=518(visitor18) 組=518(visitor18) uid=519(visitor19) gid=519(visitor19) 組=519(visitor19) uid=520(visitor20) gid=520(visitor20) 組=520(visitor20) visitor1~visitor20 UID_SUM is 10210
10、寫一腳本,分別統計/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#號開頭的行數之和,以及總的空白行數;
#!/bin/bash # declare -i u1=0 declare -i u2=0 for i in $*;do p=`grep '^#.*' $i |wc -l` q=`grep '^[[:space:]]*$' $i |wc -l` let u1+=p let u2+=q done echo "# head line sum is $u1" echo "space line sum is $u2" [root@localhost test-scripts]# bash file_line.sh /etc/rc.d/rc.sysinit /etc/rc.d/init.d/functions /etc/fstab # head line sum is 94 space line sum is 210
11、寫一個腳本,顯示當前系統上所有默認shell為bash的用戶的用戶名、UID以及此類所有用戶的UID之和;
#!/bin/bash # declare -i uid=0 awk -F: 'BEGIN{print "usershellname is bash"}$NF=="/bin/bash"{print $1,$NF}' /etc/passwd p=`awk -F: '$NF=="/bin/bash"{print $3}' /etc/passwd` for i in $p;do let uid+=i done echo "uid_sum is :" echo $uid [root@localhost test-scripts]# bash showshell.sh usershellname is bash root /bin/bash xuc /bin/bash uid_sum is : 500
12、寫一個腳本,顯示當前系統上所有,擁有附加組的用戶的用戶名;并說明共有多少個此類用戶;
#!/bin/bash # echo "attach group users list :" while read line;do user_sum=$(echo $line |cut -d: -f1) p=$(id -G $user_sum |wc -w) if [ $p -ge 2 ];then echo "$user_sum" let sum+=1 fi done < /etc/passwd echo "The number of ownerattach user : $sum" [root@localhost test-scripts]# bash ownerattach.sh attach group users list : bin daemon adm postfix xuc The number of ownerattach user : 5
13、創建一個由至少兩個物理卷組成的大小為20G的卷組;要求,PE大小為8M;而在卷組中創建一個大小為5G的邏輯卷mylv1,格式化為ext4文件系統,開機自動掛載至/users目錄,支持acl;
[root@localhost xuc]# pvcreate /dev/sda{10,11} Physical volume "/dev/sda10" successfully created Physical volume "/dev/sda11" successfully created [root@localhost xuc]# pvs PV VG Fmt Attr PSize PFree /dev/sda10 lvm2 ---- 10.00g 10.00g /dev/sda11 lvm2 ---- 10.00g 10.00g [root@localhost xuc]# vgcreate -s 8M myvg /dev/sda{10,11} Volume group "myvg" successfully created [root@localhost xuc]# vgs VG #PV #LV #SN Attr VSize VFree myvg 2 0 0 wz--n- 20.00g 20.00g [root@localhost xuc]# vgdisplay --- Volume group --- VG Name myvg System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 1 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 2 Act PV 2 VG Size 20.00 GiB PE Size 8.00 MiB Total PE 2560 Alloc PE / Size 0 / 0 Free PE / Size 2560 / 20.00 GiB VG UUID VkgZVT-aKpf-6sg2-kkIE-0Utc-npvJ-LJPvSO [root@localhost xuc]# lvcreate -L 5G -n mylv1 myvg Logical volume "mylv1" created. [root@localhost xuc]# lvdisplay --- Logical volume --- LV Path /dev/myvg/mylv1 LV Name mylv1 VG Name myvg LV UUID 3pPfn1-tsw5-yPa0-yLg9-2xhB-iajm-HoH11e LV Write Access read/write LV Creation host, time localhost.localdomain, 2016-10-11 18:56:45 +0800 LV Status available # open 0 LV Size 5.00 GiB Current LE 640 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 [root@localhost xuc]# cd [root@localhost ~]# mkdir users [root@localhost ~]# vim /etc/fstab tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 /dev/md1 /backup ext4 noatime,acl 0 0 /dev/myvg/mglv1 /users ext4 acl 0 0 [root@localhost myvg]# mke2fs -t ext4 /dev/myvg/mylv1 mke2fs 1.41.12 (17-May-2010) 文件系統標簽= 操作系統:Linux 塊大小=4096 (log=2) 分塊大小=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 327680 inodes, 1310720 blocks 65536 blocks (5.00%) reserved for the super user 第一個數據塊=0 Maximum filesystem blocks=1342177280 40 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 正在寫入inode表: 完成 Creating journal (32768 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 This filesystem will be automatically checked every 35 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
14、新建用戶magedu;其家目錄為/users/magedu,而后su切換至此用戶,復制多個文件至家目錄;
[root@localhost myvg]# useradd -d /users/magedu magedu [root@localhost myvg]# su magedu [magedu@localhost myvg]$ cd /users/magedu/ [magedu@localhost ~]$ cp /etc/fstab /etc/passwd ./ [magedu@localhost ~]$ ls fstab passwd
15、擴展mylv1至9G,確保擴展完成后原有數據完全可用;
[root@localhost myvg]# lvextend -L +4G /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@test users]# resize2fs /dev/myvg/mylv1 [root@localhost myvg]# lvdisplay --- Logical volume --- LV Path /dev/myvg/mylv1 LV Name mylv1 VG Name myvg LV UUID 3pPfn1-tsw5-yPa0-yLg9-2xhB-iajm-HoH11e LV Write Access read/write LV Creation host, time localhost.localdomain, 2016-10-11 18:56:45 +0800 LV Status available # open 0 LV Size 9.00 GiB Current LE 1152 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0
16、縮減mylv1至7G,確??s減完成后原有數據完全可用;
[root@localhost myvg]# lvreduce -L -2G /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 myvg/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@test users]# resize2fs /dev/myvg/mylv1 [root@localhost myvg]# lvdisplay --- Logical volume --- LV Path /dev/myvg/mylv1 LV Name mylv1 VG Name myvg LV UUID 3pPfn1-tsw5-yPa0-yLg9-2xhB-iajm-HoH11e LV Write Access read/write LV Creation host, time localhost.localdomain, 2016-10-11 18:56:45 +0800 LV Status available # open 0 LV Size 7.00 GiB Current LE 896 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256
17、對mylv1創建快照,并通過備份數據;要求保留原有的屬主屬組等信息;
[root@test users]# lvcreate -L 7G -p r -s -n mylv1_snapshot /dev/myvg/mylv1
原創文章,作者:N22_熊寶,如若轉載,請注明出處:http://www.www58058.com/50611