本周作业内容:

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、创建一个大小为1Gswap分区,并创建好文件系统,并启用之;

$ 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 LevelRaid0,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+

wKioL1fp3JCDnpzaAAHq0NvuNvE145.jpg-wh_50

5、创建一个大小为10GRAID1,要求有一个空闲盘,而且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、创建一个大小为4GRAID5设备,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、写一个脚本,显示当前系统上所有默认shellbash的用户的用户名、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、扩展mylv19G,确保扩展完成后原有数据完全可用;

$ lvextend -L 9G /dev/myvg/mylv1
$ resize2fs /dev/myvg/mylv1

16、缩减mylv17G,确保缩减完成后原有数据完全可用;

$ 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