马哥教育网络班19期第七周课程练习

第七周作业

1.创建一个10G分区,并格式化为ext4文件系统:

  1)要求甘block大小为2048,预留空间百分比为2,卷标为MYDATA,默认挂载属性包含acl

  2)挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳;

[root@nginx ~]# fdisk /dev/sdd

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel

Building a new DOS disklabel with disk identifier 0xf6cff7e3.

Changes will remain in memory only, until you decide to write them.

After that, of course, the previous content won't be recoverable.

 

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

 

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)

1

Invalid partition number for type `1'

Command action

   e   extended

   p   primary partition (1-4)

p

Partition number (1-4): 1

First cylinder (1-1305, default 1):

Using default value 1

Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): 10G

 

Command (m for help): p

 

Disk /dev/sdd: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 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: 0xf6cff7e3

 

   Device Boot      Start         End      Blocks   Id  System

/dev/sdd1               1          10       80293+  83  Linux

 

Command (m for help): w

The partition table has been altered!

 

Calling ioctl() to re-read partition table.

Syncing disks.

 

 

[root@nginx ~]# mke2fs -t ext4 -b 2048 -m 2 -L 'MYDATA'  /dev/sdd1

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

20112 inodes, 40146 blocks

802 blocks (2.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=41943040

3 block groups

16384 blocks per group, 16384 fragments per group

6704 inodes per group

Superblock backups stored on blocks:

        16384

 

Writing inode tables: done                           

Creating journal (4096 blocks): done

Writing superblocks and filesystem accounting information: done

 

This filesystem will be automatically checked every 36 mounts or

180 days, whichever comes first.  Use tune2fs -c or -i to override.

[root@nginx ~]#

 

[root@nginx mydata]# mount -o noexec,nodiratime /dev/sdd1 /data/mydata

 

 

2.创建一个大小为1Gswap分区,并创建好文件系统,并启用之;

[root@nginx mydata]# fdisk /dev/sde

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel

Building a new DOS disklabel with disk identifier 0x81838cae.

Changes will remain in memory only, until you decide to write them.

After that, of course, the previous content won't be recoverable.

 

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

 

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-1305, default 1):

Using default value 1

Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): 1G

 

Command (m for help): t

Selected partition 1

Hex code (type L to list codes): 1

Changed system type of partition 1 to 1 (FAT12)

 

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.

 

WARNING: If you have created or modified any DOS 6.x

partitions, please see the fdisk manual page for additional

information.

Syncing disks.

[root@nginx mydata]# mkswap /dev/sde1

Setting up swapspace version 1, size = 7996 KiB

no label, UUID=77e8c9dc-d0c3-48d5-8871-01a819d88979

[root@nginx mydata]# swapon

 

Usage:

 swapon -a [-e] [-v] [-f]             enable all swaps from /etc/fstab

 swapon [-p priority] [-v] [-f] <special>  enable given swap

 swapon -s                            display swap usage summary

 swapon -h                            display help

 swapon -V                            display version

 

The <special> parameter:

 {-L label | LABEL=label}             LABEL of device to be used

 {-U uuid  | UUID=uuid}               UUID of device to be used

 <device>                             name of device to be used

 <file>                               name of file to be used

 

[root@nginx mydata]# swapon -a

[root@nginx mydata]# free -m

             total       used       free     shared    buffers     cached

Mem:           980        204        776          0         19         68

-/+ buffers/cache:        117        863

Swap:         1983          0       1983

[root@nginx mydata]#

 

 

 

3.写一个脚本

 (1)获取并列出当前系统上的所有磁盘设备;

 (2)显示每个磁盘设备上每个分区相关的空间使用信息

#/bin/bash

#

#

fdisk -l | grep "Disk \/dev\/[sh]d[a-z]"

fdisk -l | grep [sh]d[a-z]

 

4.总结RAID的各个级别及其组合方式和性能的不同

RAID级别

RAID0

RAID1

RAID5

RAID6

RAID01

RAID10

容错

冗余

镜像

奇偶校验

奇偶校验

镜像

镜像

读性能

有所提高

有所提高

有所提高

有所提高

有所提高

有所提高

写性能

有所提高

稍降

有所提高

有所提高

有所提高

有所提高

至少磁盘数量

2

2

3

4

4

4

允许磁盘最多损坏数量

0

1

1

2

每组各1

每组各1

可用容量

100%

50%

n-1/n

n-2/n

50%

50%

 

 

 

 

 

5.创建一个大小为10GRAID1,要求有一个空闲盘,而且chunk大小为128k

[root@nginx ~]# mdadm -C /dev/md0 -l 1 -c 128 -x 1 -n 3 /dev/sdd /dev/sde /dev/sdf

mdadm: You haven't given enough devices (real or missing) to create this array

[root@nginx ~]# mdadm -C /dev/md0 -l 1 -c 128 -x 1 -n 3 /dev/sdd1 /dev/sde1 /dev/sdf1

mdadm: You haven't given enough devices (real or missing) to create this array

[root@nginx ~]# mdadm -C /dev/md0 -l 1 -c 128 -x 1 -n 2 /dev/sdd1 /dev/sde1 /dev/sdf1

mdadm: /dev/sdd1 appears to contain an ext2fs file system

       size=9446188K  mtime=Thu Jan  1 08:00:00 1970

mdadm: Note: this array has metadata at the start and

    may not be suitable as a boot device.  If you plan to

    store '/boot' on this device please ensure that

    your boot-loader understands md/v1.x metadata, or use

    –metadata=0.90

mdadm: /dev/sde1 appears to contain an ext2fs file system

       size=9446188K  mtime=Thu Jan  1 08:00:00 1970

Continue creating array? y

mdadm: Defaulting to version 1.2 metadata

mdadm: array /dev/md0 started.

[root@nginx ~]#

 

 

 

6.创建一个大小为4GRAID5设备,chunk大小为256k,格式化ext4文件系统,要求可开机自动挂载至/backup目录,而且不更新访问时间戳,且支持acl功能

 

[root@nginx ~]# mdadm -C /dev/md1 -c 256 -n 3 -l 5 /dev/sdb1 /dev/sdc1 /dev/sdd1 

mdadm: Defaulting to version 1.2 metadata

mdadm: array /dev/md1 started.

[root@nginx ~]# mke2fs -t etx4 /dev/md1

mke2fs 1.41.12 (17-May-2010)

Filesystem label=

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

Stride=64 blocks, Stripe width=128 blocks

525200 inodes, 2098432 blocks

104921 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=2151677952

65 block groups

32768 blocks per group, 32768 fragments per group

8080 inodes per group

Superblock backups stored on blocks:

        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

 

Writing inode tables: done                           

Writing superblocks and filesystem accounting information: done

 

This filesystem will be automatically checked every 37 mounts or

180 days, whichever comes first.  Use tune2fs -c or -i to override.

 

 

#

# /etc/fstab

# Created by anaconda on Fri Dec 25 05-55-39 2015

#

# Accessible filesystems, by reference, are maintained under '/dev/disk'

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#

/dev/mapper/vg_centos-lv_root /                       ext4    defaults        1 1

UUID=19e69dcd-00ea-471e-b368-1e21d77c548f /boot                   ext4    defaults        1 2

/dev/mapper/vg_centos-lv_swap swap                    swap    defaults        0 0

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

#UUID=d44bb1f3-d8a552ed-ffd3527d-138ecff8       /backup         ext4    defaults,acl    0 0

#UUID=9f0645ef-1a1f8086-ee568336-0c8097d4       /mydata         ext4    defaults        0 0

#/dev/md126             /backup                 ext4    defaults        0 0

/dev/md127              /mydata                 ext4    defaults        0 0

/dev/md1                /backup                 ext4    defaults,acl,nodiratime 0 0

 

7.写一个脚本

  (1)接受一个以上文件路径作为参数

  (2)显示每个文件拥有的行数

  (3)总结说明本次共为几个文件统计了其行数

 

#/bin/bash

#

#

declare -i lines

declare -i files

files=0

 

if [ $# == 0 ]; then

  echo "please give a file name for me"

  exit 1

fi

for i in $* ; do

  lines=0

  lines=`wc -l $i | cut -f1 -d" "`

  echo "fle $i total lines is: $lines"

  files=files+1

done

 

echo "Total count file: $files"

 

8.写一个脚本

  (1)传递两个以上字符串当作用户名

  (2)创建这些用户,且密码同用户名

  (3)总结说明共创建了几个用户

#/bin/bash

#

#

declare -i totalusers

totalusers=0

 

 

for i in $* ; do

  if ! id $i &> /dev/null ; then

     useradd $i

     echo $i | passwd $i &> /dev/null

     totalusers=totalusers+1

  fi

done

echo "TotalUser is : $totalusers"

 

 

 

9.写一个脚本,新建20个用户,visitor1-visitor20,计算他们的ID之和

#/bin/bash

#

#

declare -i userno

declare -i totaluid

declare -i iuid

 

userno=1

totaluid=0

iuid=0

 

while [ $userno -lt 21 ] ; do

  useradd visitor$userno &> /dev/null

  iuid=`id visitor$userno -u`

  totaluid=$totaluid+$iuid

  userno=$userno+1

done

 

 

10.写一脚本,分别统计/etc/rc.d/rc.sysinit/etc/rc.d/init.d/functions/etc/fstab文件中以

#号开头的行数之和,以及总的空白行数

#/bin/bash

#

#

declare -i lines

declare -i spacelines

file1=/etc/rc.d/rc.sysinit

file2=/etc/rc.d/init.d/functions

file3=/etc/fstab

 

lines=0

spacelines=0

 

lines=`grep ^# $file1 | wc -l`

echo "$file1 include '#' lines total is: $lines "

 

#echo `grep ^[[:space:]]*$ $file1 | wc -l`

spacelines=$spacelines+`grep ^[[:space:]]*$ $file1 | wc -l`

 

 

lines=0

lines=`grep ^# $file2 | wc -l`

echo "$file2 include '#' lines total is: $lines "

spacelines=$spacelines+`grep ^[[:space:]]*$ $file2 | wc -l`

 

lines=0

lines=`grep ^# $file3 | wc -l`

echo "$file3 include '#' lines total is: $lines "

spacelines=$spacelines+`grep ^[[:space:]]*$ $file3 | wc -l`

 

echo "Total sapcelines is: $spacelines."

 

 

 

11.写一个脚本,显示当前系统上所有默认shellbash的用户的用户名、UID以及此类所有用户的UID之和

#/bin/bash

#

#

declare -i uids=0

declare -i totaluid=0

#cat /etc/passwd | grep /bin/bash$ | cut -f1 -d:

bashusers=`cat /etc/passwd | grep /bin/bash$ | cut -f1 -d:`

for i in $bashusers; do

  uids=`id -u $i`

  echo "user name : $i"

  echo "uid is : $uids"

  totaluid=$totaluid+$uids

  echo ""

done

echo "Total UID is: $totaluid."

 

 

12.写一个脚本,显示当前系统上所有,拥有附加组的用户的用户名,并说明共有多少个此类用户

#/bin/bash

#

#

declare -i countusers=0

 

users=`cat /etc/passwd | cut -f1 -d:`

 

for u in $users ; do

  id $u | cut -f3 -d" " | grep "," &> /dev/null

  if [ $? -eq 0 ] ; then

    echo "$u"

    countusers=$countusers+1

  fi

done

  echo "Total User for GID is : $countusers."

 

 

13.创建一个由至少两个物理卷组成的大小为20G的卷组,要求:PE大小为8M,而在卷组中创建一个大小为5G的逻辑卷my1v1,

格式化为ext4文件系统,开机自动挂载至/users目录,支持acl

 

14.新建用户magedu,其家目录为/users/magedu,而后su切换至此用户,复制多个文件至家目录

[root@nginx tmp]# useradd -d /users/magedu magedu

[root@nginx tmp]# su – magedu

Welcom 538,You home is: /users/magedu

[magedu@nginx ~]$ pwd

/users/magedu

[magedu@nginx ~]$ ls

[magedu@nginx ~]$ cp /tmp/* ./

cp: omitting directory `/tmp/install'

cp: omitting directory `/tmp/magedu.com'

cp: omitting directory `/tmp/users'

cp: omitting directory `/tmp/vmware-root'

cp: cannot open `/tmp/yum_save_tx-2016-06-10-16-25Lt9uQC.yumtx' for reading: Permission denied

[magedu@nginx ~]$ ls

countfiles.sh  createuser2.sh  delusers.sh  test.sh

counts.sh      createuser.sh   guiduser.sh  userinfo.sh

[magedu@nginx ~]$

 

15.扩展mylvl9G,确保扩展完成后原有数据完全可用

[root@nginx ~]# pvcreate /dev/sde mypv

[root@nginx ~]# vgcreate myvg mypv

[root@nginx ~]# lvcreate -L 5G -n mylv1 myvg

[root@nginx /]# mount /dev/mapper/myvg-mylv1 /data -t ext4

[root@nginx data]# cp /tmp/* ./

cp: omitting directory `/tmp/install'

cp: omitting directory `/tmp/magedu.com'

cp: omitting directory `/tmp/users'

cp: omitting directory `/tmp/VMwareDnD'

cp: omitting directory `/tmp/vmware-root'

[root@nginx data]# ls

countfiles.sh  createuser2.sh  delusers.sh  lost+found  userinfo.sh

counts.sh      createuser.sh   guiduser.sh  test.sh

 

[root@nginx data]# lvs

  LV      VG        Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert

  mylv1   myvg      -wi-ao—-  5.00g                                                   

  lv_root vg_centos -wi-ao—- 17.57g                                                    

  lv_swap vg_centos -wi-ao—-  1.94g                                                   

[root@nginx data]# lvextend -L 9G /dev/myvg/mylv1

  Size of logical volume myvg/mylv1 changed from 5.00 GiB (1280 extents) to 9.00 GiB (2304 extents).

  Logical volume mylv1 successfully resized

[root@nginx data]# lvs

  LV      VG        Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert

  mylv1   myvg      -wi-ao—-  9.00g                                                   

  lv_root vg_centos -wi-ao—- 17.57g                                                   

  lv_swap vg_centos -wi-ao—-  1.94g                                                   

[root@nginx data]# ls

countfiles.sh  createuser2.sh  delusers.sh  lost+found  userinfo.sh

counts.sh      createuser.sh   guiduser.sh  test.sh

[root@nginx data]#

 

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

[root@nginx data]# fuser -km /dev/myvg/mylv1

/dev/myvg/mylv1:      2343c

[root@nginx ~]# umount /dev/myvg/mylv1

[root@nginx ~]# e2fsck -f /dev/myvg/mylv1

e2fsck 1.41.12 (17-May-2010)

Pass 1: Checking inodes, blocks, and sizes

Pass 2: Checking directory structure

Pass 3: Checking directory connectivity

Pass 4: Checking reference counts

Pass 5: Checking group summary information

/dev/myvg/mylv1: 19/327680 files (0.0% non-contiguous), 55910/1310720 blocks

[root@nginx ~]#

[root@nginx ~]# lvreduce -L 7G /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 mylv1? [y/n]: y

  Size of logical volume myvg/mylv1 changed from 9.00 GiB (2304 extents) to 7.00 GiB (1792 extents).

  Logical volume mylv1 successfully resized

[root@nginx ~]# lvs

  LV      VG        Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert

  mylv1   myvg      -wi-a—–  7.00g                                                   

  lv_root vg_centos -wi-ao—- 17.57g                                                   

  lv_swap vg_centos -wi-ao—-  1.94g

 

[root@nginx ~]# mount /dev/myvg/mylv1 /data

[root@nginx ~]# cd data

-bash: cd: data: No such file or directory

[root@nginx ~]# ls

anaconda-ks.cfg  Documents  install.log         Music     Public     Videos

Desktop          Downloads  install.log.syslog  Pictures  Templates

[root@nginx ~]# cd /data

[root@nginx data]# ls

countfiles.sh  createuser2.sh  delusers.sh  lost+found  userinfo.sh

counts.sh      createuser.sh   guiduser.sh  test.sh

[root@nginx data]#

 

17.mylv1创快照,并通过备份数据,要深圳市保留原有的属主属组等信息.

 

对mylv1创建快照卷snap-mylv1,并通过其cp内部的数据至/backups/目录中,要求保留原有属主属组等信息;        

[root@nginx backup]# lvcreate -L 100M -n snap-mylv1 -p r -s /dev/myvg/mylv1

  Logical volume "snap-mylv1" created

[root@nginx backup]# mkdir /snapbackup

[root@nginx backup]# mount /dev/myvg/

mylv1       snap-mylv1 

[root@nginx backup]# mount /dev/myvg/snap-mylv1  /snapbackup/

mount: block device /dev/mapper/myvg-snap–mylv1 is write-protected, mounting read-only

[root@nginx backup]# cp -a /snapbackup/ ./

[root@nginx backup]# ls

etc.20151224.tar.gz  snapbackup

[root@nginx backup]# cd snapbackup/

[root@nginx snapbackup]# ls

countfiles.sh  createuser2.sh  delusers.sh  lost+found  userinfo.sh

counts.sh      createuser.sh   guiduser.sh  test.sh

[root@nginx snapbackup]#   

 

 

原创文章,作者:马哥Net19_小斌斌,如若转载,请注明出处:http://www.178linux.com/19076

(0)
上一篇 2016-06-23 11:02
下一篇 2016-06-23 11:04

相关推荐

  • Linux基础之权限管理(含SUID\SGID\STICKY和ACL)

    概述     Linux系统是一个多人多任务的操作系统,系统上同时可能有很多人登录,每个人都会利用系统上的各类资源完成一定的操作。那么如何合理的规范这些用户的行为,保证资源的合理分配,则就需要用到权限管理的相关内容了。权限管理是Linux系统上最基础,也是最重要的一部分内容,本章就简要说明下权限管理的相关内容。内容主要…

    Linux干货 2016-08-04
  • 一起学DNS系列(十四)DNS查询工具之DIG的使用(2)

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://jeffyyko.blog.51cto.com/28563/240243    上节谈到了利用DIG的+trace参数追踪域名解析的过程,其实除此之外,DIG还有一些其他的参数,这节会涉及到这…

    2015-03-17
  • LVM组件详解&创建实验流程

       前言:根据前面的学习我们知道,一个磁盘一旦分完区并且格式化后,其空间大小是没有办法进行调整了。因此推出了LVM(Logical Volume Manager)逻辑卷管理机制,LVM重点在于可以弹性的调整文件系统的容量!并非在于性能和数据保全方面,需要文件的读写性能或者数据的可靠性,需要用到RAID。而LVM可以整合多个物理…

    Linux干货 2016-08-30
  • linux一周学习总结

          对于linux,之前也完全没有接触过,完全零基础小白。来到马哥以后,进入学习也有一周时间 ,一周里老师带我们学习了很多指令,下面,我就自己的理解和老师讲授的内容对linux中的一些指令做一个简单的小总结。 一、关于SHELL Shell     查询当前系统使用了哪个shell   &n…

    2017-07-15
  • 文本处理工具sed、vim

    文本处理工具sed、vim 一、sed的使用 1、sed的概念 sed简单地说sed是一种行处理工具。 sed 是一种流性的行编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space ),接着用sed 命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末…

    Linux干货 2016-08-11
  • apache服务器

    一、改变网页的默认位置 1、修改DocumentRoot和Directory,修改新的存放路径为/data/htdocs/ 2、创建/data/htdocs/index.html,注意权限问题 3、注意报错You don't have permission to access /index.html,此时去查看SElinux的状态。命令selinu…

    Linux干货 2016-10-09

评论列表(1条)

  • 马哥教育
    马哥教育 2016-06-23 14:17

    写的很棒,但是有的脚本有点小瑕疵,在仔细测试一下