N25-week7 作業

1. 創建一個10G的分區,并格式為ext4文件系統
  (1) 要求block大小為2048, 預留空間百分比為2, 卷標為MYDATA, 默認掛載屬性包含acl;
  (2) 掛載至/data/mydata目錄,要求掛載時禁止程序自動運行,且不更新文件的訪問時間戳

#創建文件系統,首先使用fdisk對磁盤進行分區操作

[root@localhost ~]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u').
#fdisk下n命令創建新的分區
     Command (m for help): n
#選擇p創建主分區
Command action
  e   extended
     p   primary partition (1-4)
#選擇1創建1號分區
Partition number (1-4): 1
#起始柱面默認選1
First cylinder (1-261, default 1):
Using default value 1
#結尾柱面默認為最后一個柱面
Last cylinder, +cylinders or +size{K,M,G} (1-261, default 261):
Using default value 261
#使用t命令來修改分區的system id
Command (m for help): t
Selected partition 1
#83為ext系列文件系統的system id
Hex code (type L to list codes): 83
#使用w命令將操作保存
Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

#之后使用重讀分區表

[root@localhost ~]# partx -a /dev/sdb

#查看/proc/partitions文件

[root@localhost ~]# cat /proc/partitions
major minor  #blocks  name

   8        0   20971520 sda
   8        1     512000 sda1
   8        2   20458496 sda2
   8       16    2097152 sdb
   8       17    2096451 sdb1
   8       32    1048576 sdc
 253        0   18423808 dm-0
 253        1    2031616 dm-1

#到此,分區創建完畢,下面來對分區進行ext文件系統的格式化

#使用mkfs.ext4命令來格式化/dev/sdb1

[root@localhost ~]# mkfs.ext4 -b 2048 -m 2 -L MYDATA /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=MYDATA
OS type: Linux
Block size=2048 (log=1)
Fragment size=2048 (log=1)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 1048224 blocks
20964 blocks (2.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=537919488
64 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

Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 34 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

#然后創建/data/mydata目錄

[root@localhost ~]# mkdir -p /data/mydata

#之后使用mount命令將/dev/sdb1掛載到/data/mydata上

[root@localhost ~]# mount -a -t ext4  -o noexec -o noatime -o acl /dev/sdb1 /dat
[root@localhost ~]# cd /data/mydata/
[root@localhost mydata]# ll
total 16
drwx------. 2 root root 16384 Feb 18 15:39 lost+found
[root@localhost mydata]#

2. 創建一個大小為10G的swap分區,并創建好文件系統,并啟用之;
#使用fdisk命令在sdc硬盤上新建分區

[root@localhost ~]# fdisk /dev/sdc

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-130, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130):
Using default value 130

#調整system type為82(swap)
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 82
Changed system type of partition 1 to 82 (Linux swap / Solaris)

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

#partx /dev/sdc重讀分區表

[root@localhost ~]# partx /dev/sdc
# 1:        63-  2088449 (  2088387 sectors,   1069 MB)
# 2:         0-       -1 (        0 sectors,      0 MB)
# 3:         0-       -1 (        0 sectors,      0 MB)
# 4:         0-       -1 (        0 sectors,      0 MB)

#查看系統是否認出/dev/sdc1分區

[root@localhost ~]# cat /proc/partitions
major minor  #blocks  name

   8        0   20971520 sda
   8        1     512000 sda1
   8        2   20458496 sda2
   8       16    2097152 sdb
   8       17    2096451 sdb1
   8       32    1048576 sdc
   8       33    1044193 sdc1
 253        0   18423808 dm-0
 253        1    2031616 dm-1
[root@localhost ~]#

#為/dev/sdc1創建swap文件系統

[root@localhost ~]# mkswap /dev/sdc1
Setting up swapspace version 1, size = 1044188 KiB
no label, UUID=80f20b38-b6e2-48b1-902c-995f3cbad9da

#使用free命令查看當前swap文件系統的大小

[root@localhost ~]# free -h
             total       used       free     shared    buffers     cached
Mem:          980M       240M       740M       204K        22M        92M
-/+ buffers/cache:       126M       854M
Swap:         1.9G         0B       1.9G

#使用swapon命令啟用/dev/sdc1 swap分區

[root@localhost ~]# swapon /dev/sdc1

#再使用free命名查看swap文件系統的大小, 由1.9G增加到2.9G, 說明/dev/sdc1 swap分區成功啟用

[root@localhost ~]# free -h
             total       used       free     shared    buffers     cached
Mem:          980M       240M       739M       204K        22M        92M
-/+ buffers/cache:       126M       854M
Swap:         2.9G         0B       2.9G

3. 寫一個腳本
(1) 獲取并列出當前系統上的所有磁盤設備
(2) 顯示每個磁盤設備上每個分區相關的空間使用信息

#腳本內容

[root@localhost ~]# cat week7.sh
#!/bin/bash

#獲取并列出當前系統上的所有磁盤設備
#顯示每個磁盤設備上每個分區相關的空間使用信息

declare i #聲明變量i, 用于控制for循環體
declare disk_list #聲明變量 disk_lis,保存系統上磁盤設備列表

disk_list=$(ls /dev/{hd,sd}* 2> /dev/null)
echo -e  "System disk device list:\n$disk_list"

echo -e  "\nDetails of Used/Free space:"

for i in $disk_list ; do
        df -h -T $i
done

#腳本執行結果

[root@localhost ~]# bash week7.sh
System disk device list:
/dev/sda
/dev/sda1
/dev/sda2
/dev/sdb
/dev/sdb1
/dev/sdc
/dev/sdc1

Details of Used/Free space:
Filesystem     Type  Size  Used Avail Use% Mounted on
-              -     480M  200K  480M   1% /dev
Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/sda1      ext4  477M   28M  425M   7% /boot
Filesystem     Type  Size  Used Avail Use% Mounted on
-              -     480M  200K  480M   1% /dev
Filesystem     Type  Size  Used Avail Use% Mounted on
-              -     480M  200K  480M   1% /dev
Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/sdb1      ext4  2.0G  9.1M  1.9G   1% /data/mydata
Filesystem     Type  Size  Used Avail Use% Mounted on
-              -     480M  200K  480M   1% /dev
Filesystem     Type  Size  Used Avail Use% Mounted on
-              -     480M  200K  480M   1% /dev

4. 總結RAID的各個級別及其組合方式和性能的不同

5. 創建一個大小為10G的RAID1,要求有一個空閑盤,而且CHUNK大小為 128K

6. 創建一個大小為4G的RAID5設備,chunk大小為256K,格式化為ext4文件系統,要求可開機自動掛載至/backup目錄,而且不更新訪問時間戳,且支持acl功能

7. 寫一個腳本
(1) 接受一個以上文件路徑作為參數
(2) 顯示每個文件擁有的行數
(3) 總結說明本次共為幾個文件統計了其行數

#腳本內容
#!/bin/bash

#接受一個以上的文件路徑作為參數
#顯示每個文件擁有的行數
#總結說明本次共為幾個文件統計了行數

#聲明變量i,用于控制for循環體
declare i
declare File_List
declare File_Count=0

read -p "Please input file name here, this script could count lines number of the file you typed. Warnning:you should type at least 1 file:" File_List

if [[ -z $File_List ]]; then
   echo "At least provide one file to run this script"
   exit 2
else
   for i in $File_List; do
        if [ ! -e $i ];then
             echo "No such file: $i"
        elif [ ! -f $i ];then
             echo "File $i is not a normal file and canot count lines number"
        else
             echo "line number of file $i is $(wc -l $i|cut -f1 -d' ')"
             let File_Count=$File_Count+1
        fi
   done
fi

echo "Totaly count line number of $File_Count files."

unset i
unset File_List
unset File_Count

#運行結果
#(1)未給出參數的運行結果

[root@localhost ~]# bash week7_2.sh
Please input file name here, this script could count lines number of the file you typed. Warnning:you should type at least 1 file:
At least provide one file to run this script
[root@localhost ~]#

#(2)給出1個不存在的文件,一個設備文件,一個普通文件的運行結果

[root@localhost ~]# bash week7_2.sh
Please input file name here, this script could count lines number of the file you typed. Warnning:you should type at least 1 file:a
No such file: a
Totaly count line number of 0 files.
[root@localhost ~]#

#(3)給出N個普通文件的運行結果

[root@localhost ~]# !b
bash week7_2.sh
Please input file name here, this script could count lines number of the file you typed. Warnning:you should type at least 1 file:/etc/fstab /etc/rc.d/rc.sysinit /proc/partitins /root/week7.sh /root/week7_2.sh
line number of file /etc/fstab is 16
line number of file /etc/rc.d/rc.sysinit is 691
No such file: /proc/partitins
line number of file /root/week7.sh is 19
line number of file /root/week7_2.sh is 34
Totaly count line number of 4 files.

#(4)給出的文件中沒有普通文件的運行結果

[root@localhost ~]# !b
bash week7_2.sh
Please input file name here, this script could count lines number of the file you typed. Warnning:you should type at least 1 file:/roott/week7.sh /ect/fstab /dev/sda /dev/sdc1
No such file: /roott/week7.sh
No such file: /ect/fstab
File /dev/sda is not a normal file and canot count lines number
File /dev/sdc1 is not a normal file and canot count lines number
Totaly count line number of 0 files.
[root@localhost ~]#

8 寫一個腳本
(1) 傳遞兩個以上的字符串當做用戶名
(2) 創建這些用戶,且密碼同用戶名
(3) 總結說明共創建了幾個用戶
#腳本內容

[root@localhost ~]# cat week7_3.sh
#!/bin/bash

#傳遞兩個以上的字符串當做用戶名
#創建這些用戶,且密碼同用戶名
#總結說明共創建了幾個用戶

declare i #用于控制for循環體
declare user_list
declare user_count

read -p "please type username which need to be created:" user_list
echo $user_list
if [[ -z $user_list ]]; then
   echo "Invaild parameter"
   exit 2
else
   for i in $user_list; do
      if id $i &> /dev/null;then
         echo "$i exists"
      elif [[ $(echo ${#i}) -le 2 ]];then
         echo "Length of username should be more than 2 charactors"
      else
         useradd $i &> /dev/null && echo $i | passwd $i --stdin &> /dev/null && echo "add user $i sucessfully. Password is the same as username"
         let user_count=$user_count+1
      fi
   done
fi

echo "Totally add $user_count users."

unset i
unset user_list
unset user_count
[root@localhost ~]#

#執行結果
#(1) 未給出參數的執行結果

[root@localhost ~]# bash week7_3.sh
please type username which need to be created:

Invaild parameter
[root@localhost ~]#

#(2) 傳遞的參數字符數小于2的執行結果

[root@localhost ~]# !b
bash week7_3.sh
please type username which need to be created:a b cd de c1
a b cd de c1
Length of username should be more than 2 charactors
Length of username should be more than 2 charactors
Length of username should be more than 2 charactors
Length of username should be more than 2 charactors
Length of username should be more than 2 charactors
Totally add 0 users.
[root@localhost ~]#

#(3) 傳遞的參數已經存在于系統的執行結果

[root@localhost ~]# !b
bash week7_3.sh
please type username which need to be created:root bin daemon adm lp
root bin daemon adm lp
root exists
bin exists
daemon exists
adm exists
lp exists
Totally add 0 users.
[root@localhost ~]#

#(4) 正常添加用戶名的執行結果

[root@localhost ~]# !b
bash week7_3.sh
please type username which need to be created:week7 week7_1 week7_3
week7 week7_1 week7_3
add user week7 sucessfully. Password is the same as username
add user week7_1 sucessfully. Password is the same as username
add user week7_3 sucessfully. Password is the same as username
Totally add 3 users.
[root@localhost ~]#

9. 寫一個腳本,新建20個用戶visitor1-vistor20, 計算他們的ID之和

#腳本內容

[root@localhost ~]# cat week7_4.sh
#! /bin/bash

#寫一個腳本,新建20個用戶visitor1-vissitor20,計算他們的ID之和

declare i
declare user_count=0
declare id_sum=0

for i in {1..20}; do
   if id visitor$i &> /dev/null;then
      echo "user visitor$i exists."
   else
      useradd visitor$i &> /dev/null && echo "Add visitor$i" && let id_sum=$(id -u visitor$i)+$id_sum && let user_count=$user_count+1
   fi
done

echo "Total add $user_count users and sum id number is $id_sum"

unset i
unset user_count
unset id_sum
[root@localhost ~]#

#執行結果
#(1) 第一次執行結果,visitor1-visitor20全部添加,并計算出id之和

[root@localhost ~]# bash week7_4.sh
Add visitor1
Add visitor2
Add visitor3
Add visitor4
Add visitor5
Add visitor6
Add visitor7
Add visitor8
Add visitor9
Add visitor10
Add visitor11
Add visitor12
Add visitor13
Add visitor14
Add visitor15
Add visitor16
Add visitor17
Add visitor18
Add visitor19
Add visitor20
Total add 20 users and sum id number is 10210
[root@localhost ~]#

#(2) 第二次執行結果,visitor1-visitor20全部已經存在,沒有添加用戶,沒有計算id之和

[root@localhost ~]# bash week7_4.sh
user visitor1 exists.
user visitor2 exists.
user visitor3 exists.
user visitor4 exists.
user visitor5 exists.
user visitor6 exists.
user visitor7 exists.
user visitor8 exists.
user visitor9 exists.
user visitor10 exists.
user visitor11 exists.
user visitor12 exists.
user visitor13 exists.
user visitor14 exists.
user visitor15 exists.
user visitor16 exists.
user visitor17 exists.
user visitor18 exists.
user visitor19 exists.
user visitor20 exists.
Total add 0 users and sum id number is 0
[root@localhost ~]#

#(3) 第三次執行結果,刪除奇數結尾的visitor用戶之后,再次執行,添加了10個用戶,并計算了id之和

[root@localhost ~]# for i in $(seq 1 2 20);do userdel -r visitor$i
> done
[root@localhost ~]# tail /etc/passwd
visitor2:x:502:502::/home/visitor2:/bin/bash
visitor4:x:504:504::/home/visitor4:/bin/bash
visitor6:x:506:506::/home/visitor6:/bin/bash
visitor8:x:508:508::/home/visitor8:/bin/bash
visitor10:x:510:510::/home/visitor10:/bin/bash
visitor12:x:512:512::/home/visitor12:/bin/bash
visitor14:x:514:514::/home/visitor14:/bin/bash
visitor16:x:516:516::/home/visitor16:/bin/bash
visitor18:x:518:518::/home/visitor18:/bin/bash
visitor20:x:520:520::/home/visitor20:/bin/bash
[root@localhost ~]# bash week7_4.sh
Add visitor1
user visitor2 exists.
Add visitor3
user visitor4 exists.
Add visitor5
user visitor6 exists.
Add visitor7
user visitor8 exists.
Add visitor9
user visitor10 exists.
Add visitor11
user visitor12 exists.
Add visitor13
user visitor14 exists.
Add visitor15
user visitor16 exists.
Add visitor17
user visitor18 exists.
Add visitor19
user visitor20 exists.
Total add 10 users and sum id number is 5255
[root@localhost ~]#

原創文章,作者:JL,如若轉載,請注明出處:http://www.www58058.com/69800

(0)
JLJL
上一篇 2017-02-22 14:45
下一篇 2017-02-22 21:00

相關推薦

  • 用PHP編寫Hadoop的MapReduce程序

    Hadoop流 雖然Hadoop是用java寫的,但是Hadoop提供了Hadoop流,Hadoop流提供一個API, 允許用戶使用任何語言編寫map函數和reduce函數.Hadoop流動關鍵是,它使用UNIX標準流作為程序與Hadoop之間的接口。因此,任何程序只要可以從標準輸入流中讀取數據,并且可以把數據寫入標準輸出流中,那么就可以通過Hadoop流使…

    Linux干貨 2015-04-13
  • 少走冤枉路!帶你走過SNMP的那些坑

    SNMP(Simple Network Management Protocol)即簡單網絡管理協議,是在網絡與系統監控領域中,最常使用的一種數據采集技術。盡管這個協議非常簡單,但在大規模IT環境監測中,還是經常會碰到各種坑,因此優云開源了一套友好的SNMPAPI,并通過本文簡單介紹這套API中的一些特點,希望幫助各位運維同仁提前規避一些問題。 特點[0].&…

    2016-06-22
  • Linux入門知識

    了解計算機的構造和Linux的基本知識,掌握相關的內容

    2017-09-10
  • dns主從

    一、前言 Dns全稱domain name system,當我們訪問一個網站時,在網站輸入一個網址。但是網絡是靠ip地址這個邏輯地址來標識地址的。而一個網址是如何轉換為ip地址的?下面我們將簡單講解下dns的原理。 二、dns查詢過程 在了解dns查詢過程時,我們先了解一些有關dns的專業名詞     根域:用來管理互…

    Linux干貨 2015-10-01
  • H3C 設備監測命令大全 (v3)

    H3C 設備監測命令大全  display aaa unsent-h323-call-record  display acl      display alarm   urgent   display…

    Linux干貨 2016-06-01
  • find文件:就是這么簡單

    概述 由于Linux一切皆文件,我們的日常運維工作其實就是與文件打交道的事,如何能夠快速而有效地找到我們需要的文件呢?這是個令人頭疼的問題。幸運是,Linux為用戶提供了強大的查找工具——find。find通過遍歷指定路徑完成文件查找,它的的工作特點: 精確查找——多查詢條件組合,精確匹配; 實時查找——遍歷指定路徑; 查找速度稍慢——由于需要遍歷路徑,速度…

    Linux干貨 2016-08-18

評論列表(1條)

  • 馬哥教育
    馬哥教育 2017-03-15 01:28

    贊~給出詳細的實現方式也給出了實驗結果~比較不錯~繼續加油!

欧美性久久久久