马哥教育N22期第七周作业

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

  • 下面具体介绍一下具体实现和性能差异:

    RAID_xuc.jpg

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,确保缩减完成后原有数据完全可用;

[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.178linux.com/50611

(0)
上一篇 2016-10-24 09:11
下一篇 2016-10-24 09:12

相关推荐

  • Linux Services and Security–part2

    一、请描述一次完整的http请求处理过程 (1).建立或处理连接:接收请求或拒绝请求; (2).接收请求:接收来自于网络上的主机请求报文中对某特定资源的一次请求的过程; 接收请求的模型: 并发访问响应模型: 单进程I/O模型:启动一个进程处理用户请求;这意味着,一次只能处理一个请求,多个请求被串行响应; 多进程I/O结构:并行启动多个进程,每个进程响应一个请…

    Linux干货 2017-11-13
  • YUM工具的工作原理、用法及源 码包的编译安装

    YUM工具的工作原理,用法及源码包的编译安装 _ueditor_page_break_tag_ 一、 CentOS: yum, dnf       1.YUM: (Yellow dogUpdate Modifier),rpm的前端程序,用来解决软件包相关依赖性,可以在多个库之间定位软件包,up2date的替代工具   …

    Linux干货 2016-09-01
  • LVS DR模型演示

    LVS DR模型演示 环境介绍: Directory     DIP eth0    10.1.249.158     VIP eth0:0  10.1.249.111 rs1  &nbsp…

    2016-10-30
  • 基础网络配置

    配置文件: /etc/ude /proc/sys/net/ipv4/ip_forward /etc/sysconfig/network-scripts/ifcfg-IFACE 网络配置文件 /etc/sysconfig/network-scripts/route-IFACE 路由配置文件 配置文件里的设置: DEVICE:此配置文件应用到的设备 HWADDR…

    Linux干货 2017-05-08
  • Linux基础知识总结

    Linux基础知识 计算机系统 计算机硬件组成 1.控制器:计算机系统的控制中心,协调各部分工作,保证计算机按照预先规定的目标和步骤进行操作及处理。 2.运算器:对数据进行算数运算,逻辑判断以及数据的比较、移位等操作。 3.存储器:存取程序和各种数据信息。  4.输入设备:把信息,如数字、文字、图像等转换为数字形式的“编码”的设备(键盘,扫描仪) …

    2017-05-18
  • Linux网络属性配置

      一台主机想要接入网络就必须配置相关的网络属性,如:主机名、IP地址、子网掩码、网关、DNS等,下面总结了网络属性的配置方法。 一、配置主机名   主机名配置有两种方式:命令和配置文件。   1、hostname命令    可以使用hostname命令来配置主机名,不过这样配置的主机名不会永久生效,只能在当…

    Linux干货 2016-01-06