1、创建一个10G分区,并格式为ext4文件系统;

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

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

答:
#用fdisk命令进入磁盘sdb进行分区管理;
[root@ilinux ~]# 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').
 
Command (m for help): p     #显示已有分区情况,sdb还没有创建分区;
 
Disk /dev/sdb: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054cylinders
Units = cylinders of 16065 * 512 = 8225280bytes
Sector size (logical/physical): 512 bytes /512 bytes
I/O size (minimum/optimal): 512 bytes / 512bytes
Disk identifier: 0xd8114f48
 
  Device Boot      Start         End      Blocks  Id  System
 
Command (m for help): n         #输入n,创建分区;
Command action
  e   extended
  p   primary partition (1-4)
p                    #输入p,创建主分区(e是创建扩展分区);
Partition number (1-4): 1
First cylinder (1-13054, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G}(1-13054, default 13054): +10G
 
Command (m for help): w         #输入w,写入磁盘并退出
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
 
WARNING: Re-reading the partition tablefailed with error 16: Device or resource busy.
The kernel still uses the old table. Thenew table will be used at
the next reboot or after you runpartprobe(8) or kpartx(8)
Syncing disks.
 
#通知内核重新读取硬盘分区表;
[root@ilinux ~]# partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition 1
 
#格式化为ext4文件系统,block大小为2048, 预留空间百分比为2, 卷标为MYDATA;
[root@ilinux ~]# mkfs.ext4 -b 2048 -m 2 -L"MYDATA" /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
#提示/dev/sdb1正在被使用。/dev/sdb5 正在被DM管理,所以创建文件系统时提示报错,我们手工的移除;
/dev/sdb1 is apparently in use by thesystem; will not make a filesystem here!
 
[root@ilinux ~]# dmsetup status
myvg-mylv--snap-cow: 0 1048576 linear
myvg-mylv-real: 0 6291456 linear
myvg-mylv: 0 6291456 snapshot-origin
myvg-mylv--snap: 0 6291456 snapshot312/1048576 16
[root@ilinux ~]# dmsetup remove_all
[root@ilinux ~]# dmsetup status
No devices found
 
#再一次执行,格式化为ext4文件系统;
[root@ilinux ~]# 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
657408 inodes, 5245206 blocks
104904 blocks (2.00%) reserved for thesuper user
First data block=0
Maximum filesystem blocks=543162368
321 block groups
16384 blocks per group, 16384 fragments pergroup
2048 inodes per group
Superblock backups stored on blocks:
       16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816, 1327104,
       2048000, 3981312
 
Writing inode tables: done                           
Creating journal (32768 blocks): done
Writing superblocks and filesystemaccounting information: done
 
This filesystem will be automaticallychecked every 29 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
 
#创建目录/
[root@ilinux ~]# mkdir -p /data/mydata
 
#挂载至/data/mydata目录,默认挂载属性:acl,挂载时禁止程序自动运行,且不更新文件的访问时间戳;
[root@ilinux ~]# mount -o acl,noexec,noatime /dev/sdb1 /data/mydata/
 
#查看文件系统的大小;
[root@ilinux ~]# df -h
Filesystem      Size Used Avail Use% Mounted on
/dev/sda3       156G 1.8G  146G   2% /
tmpfs           242M     0 242M   0% /dev/shm
/dev/sda1       190M  32M  149M  18% /boot
/dev/sdb1       9.8G  13M  9.6G   1% /data/mydata
 
#查看分区的属性信息;
[root@ilinux ~]# mount
. . . . . .
/dev/sdb1 on /data/mydata type ext4(rw,noexec,noatime,acl)


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

答:
#查看当前系统swap分区的大小为2G;
[root@ilinux ~]# free -m
             total       used       free    shared    buffers     cached
Mem:        482       203       279          0        26        39
-/+ buffers/cache:          137      345
Swap:        1999         0      1999
#创建一个1G的分区作为swap分区;
[root@ilinux ~]# fdisk /dev/sdb
Command (m for help): n     #输入n,创建分区;
Command action
  e   extended
  p   primary partition (1-4)
p           #输入p,创建主分区;
Partition number (1-4): 1            #输入1,创建的是第一个分区;
First cylinder (1-7832, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G}(1-7832, default 7832): +1G       #分区大小为1G;
Command (m for help): t       #调整分区id,此步骤不做也可创建swap分区;
Selected partition 1
Hex code (type L to list codes): 82     #输入82,为swap分区id;
Command (m for help): w              #输入w,保存并退出;
[root@ilinux ~]# fdisk -l /dev/sdb       #查看分区情况;
. . . . . .
 Device Boot          Start         End      Blocks   Id  System
/dev/sdb1               1         132    1060258+  82  Linux swap / Solaris
 
#查看内核是否已经识别新的分区;
[root@ilinux ~]# cat /proc/partitions 
major minor #blocks  name
 
  8        0  167772160 sda
  8        1     204800 sda1
  8        2    2048000 sda2
  8        3  165518336 sda3
  8       16   62914560 sdb
  8       17    1060258 sdb1
 
#通知内核重新读取硬盘分区表
[root@ilinux ~]# partx -a /dev/sdb        #也可以使用kpartx-a或partprobe命令;
BLKPG: Device or resource busy
error adding partition 1
 
[root@ilinux ~]# mkswap /dev/sdb
sdb  sdb1 
[root@ilinux ~]# mkswap /dev/sdb1      #创建交换分区sdb1;
Setting up swapspace version 1, size =1060252 KiB
no label,UUID=a8864cdf-9278-4124-974c-025ba996d589
[root@ilinux ~]# free -m       #查看交换分区,还是2G,新创建的交换分区还没加进来;
          total       used       free    shared    buffers     cached
Mem:     482        147        334          0        9         46
-/+ buffers/cache:        92        390
Swap:     1999          0       1999
[root@ilinux ~]# swapon /dev/sdb1      #激活交换分区sdb1,加 -a 激活所有交换分区;
[root@ilinux ~]# free -m              #再次查看,交换分区已变成3G了;
          total       used       free    shared    buffers     cached
Mem:       482        148        334          0        9         46
-/+ buffers/cache:     92        389
Swap:     3035          0       3035
说明:swapoff /dev/sdb1      #禁用交换分区sdb1;


3、写一个脚本

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

答:
[root@ilinux ~]# vim  alldisk.sh
         #!/bin/bash
         #获取并列出当前系统上的所有磁盘设备;
         #
         fdisk -l/dev/[sh]d[a-z] | grep -o "^Disk /dev/[sh]d[a-z]"
[root@ilinux ~]# bash -nalldisk.sh 
[root@ilinux ~]# bashalldisk.sh   
Disk /dev/sda
Disk /dev/sdb

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

答:
[root@ilinux ~]# vim part.sh
#!/bin/bash
#
for i in `fdisk -l | grep   '^/dev/[sh]d[a-z]' | awk '{print $1}'`
do
    df -h $i | grep -C2'^/dev/'
done
[root@ilinux ~]# bash -n part.sh     #测试是否有语法错误;
[root@ilinux ~]# bash part.sh
Filesystem      Size    Used   Avail  Use% Mounted on
/dev/sda1      190M     32M    149M   18%   /boot
Filesystem      Size    Used   Avail  Use% Mounted on
/dev/sda3      156G     1.8G   146G   2%   /


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

答:
RAID实现方式:外接式RAID、内接式RAID、SoftwareRAID;
RAID的级别(level):不是用来标识性能高低之分的,仅是标识其磁盘组织形式上的不同;
下面来说说常见的RAID级别:
RAID-0 :
         读、写性能提升;
         可用空间:N*min(S1,S2,. . . )
         无容错能力;
         最少磁盘数:2,2+;
 
RAID-1 :
         读性能提升,写性能略有下降;
         可用空间:1*min(S1,S2,. . . );
         有冗余能力;
         最少磁盘数:2,2+;
 
RAID-4 :
         磁盘A(数据) 磁盘B(数据) 磁盘C(校验码)
         1101            0110           1011
         校验码是前面两个数据做异或运算,数字相同则为0,数字不同才为1;
         如果有一块硬盘坏了,也可以读取到数据;由其他两块硬盘做异或运算可计算得出;
         每次都要进行计算,读取速度下降;出问题的机率提高了;
 
                   降级模式:坏了一块硬盘,其他两块也可以继续工作;缺陷:单块盘作为校验盘,访问压力较大,容易成为性能瓶颈;但是RAID-4允许坏掉一块盘,可以以降级方式工作,也可以在它出现错误时,尽早拿一块硬盘更换;
                   很多RAID设备允许多加一块盘,用于做热备;只是平时不用,是空闲盘,若哪块硬盘坏了,就立即顶上去;
 
RAID-5 :
         读、写性能提升;
         可用空间:(N-1)*min(S1,S2,. . . );
         有容错能力:仅允许坏1块磁盘;
         最少磁盘数:3,3+;
 
RAID6 :
         读、写性能提升;
         可用空间:(N-2)*min(S1,S2,. . . );
         有容错能力:允许坏2块磁盘;
         最少磁盘数:4,4+;
 
混合类型:
         RAID-10:
                   读、写性能提升;
                   可用空间:N*min(S1,S2,. . . )/2;
                   有容错能力:每组镜像最多只能坏一块;
                   最少磁盘数:4,4+;
 
         RAID-01:
                  读、写性能提升;
                  可用空间:N*min(S1,S2, . . . )/2;
                  有容错能力:最多只能坏一块;
                  最少磁盘数:4,4+;
 
                   RAID-50 :
                            RAID5 + RAID 0;
                            读、写性能提升;
                            可用空间:(N-1)*min(S1,S2, . . . )/2;
                            有容错能力:每组镜像最多只能坏一块;
                            最少磁盘数:6,6+;
 
         RAID-7:某公司的独有技术,IO能力非常好,价格非常高;
 
         JBOD:Just a Bunch OF Disks;
                            功能:将多块磁盘的空间合并为一个大的连续空间使用;
                            可用空间:sum(S1,S2,. . . );


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

答:
这里我们创建3个10G分区代替3个磁盘来做RAID1;
[root@ilinux ~]# fdisk /dev/sdb
Command (m for help): n       #创建分区;
Command action
  e   extended
  p   primary partition (1-4)
p                        #分区类型为主分区;
Partition number (1-4): 1      #创建的是第一个分区
First cylinder (1-7832, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G}(1-7832, default 7832): +10G    #分区大小为10G;
 
同以上方法,再创建2个10G分区;
Command (m for help): n        #创建分区;
p                            #主分区;
Partition number (1-4): 2        #第二个分区;
Last cylinder, +cylinders or +size{K,M,G}(1307-7832, default 7832): +10G #分区大小为10G;
 
Command (m for help): n       #创建分区;
p                           #主分区;
Partition number (1-4): 3        #第三个分区;
Last cylinder, +cylinders or +size{K,M,G}(2613-7832, default 7832): +10G    #分区大小为10G;
 
Command (m for help): l         #查看分区id列表,可知RAID分区id为fd;
. . . . . .
18 AST SmartSleep  70  DiskSecure Mult b8  BSDI swap       fd  Linux raidauto
. . . . . .
       
Command (m for help): t        #调整分区id为fd;
Partition number (1-4): 1
Hex code (type L to list codes): fd
Changed system type of partition 1 to fd(Linux raid autodetect)
 
Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): fd
Changed system type of partition 2 to fd(Linux raid autodetect)
 
Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): fd  
Changed system type of partition 3 to fd(Linux raid autodetect)
 
Command (m for help): p
 
  Device Boot      Start         End      Blocks  Id  System
/dev/sdb1               1        1306   10490413+  fd  Linux raid autodetect
/dev/sdb2            1307        2612   10490445   fd  Linux raid autodetect
/dev/sdb3            2613        3918   10490445   fd  Linux raid autodetect
 
Command (m for help): w
 
[root@ilinux ~]# cat /proc/partitions      #查看是否已经识别新的分区;
major minor #blocks  name
. . . . .
  8       17   10490413 sdb1
  8       18   10490445 sdb2
  8       19   10490445 sdb3
 
#创建RAID1,-C 创建模式,-l 1 指明创建为raid1,-n 2 使用2块设备来创建此RAID,-x 1 指明空闲盘的个数为1,-c 128 指明块大小为128k;
[root@ilinux ~]# mdadm -C /dev/md0 -l 1 -n2 -x 1 -c 128 /dev/sdb{1,2,3}
 
#显示raid的详细信息;
[root@ilinux ~]# mdadm -D /dev/md0 
. . . . . .
    Number   Major  Minor   RaidDevice  State
      0       8       17       0      active sync   /dev/sdb1
      1       8       18       1      active sync   /dev/sdb2
 
      2       8       19       -      spare   /dev/sdb3



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

答:
这里用创建三个分区代替三个磁盘,用于创建RAID5;
按照上题的方法先创建三个分区,如下:
Device Boot           Start       End      Blocks     Id System
/dev/sdb5            3919        4180    2104483+  fd  Linux raid autodetect
/dev/sdb6            4181        4442    2104483+  fd  Linux raid autodetect
/dev/sdb7            4443        4704    2104483+  fd  Linux raid autodetect
 
[root@ilinux ~]# partx -a /dev/sdb      #通知内核重新读取硬盘分区表;
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@ilinux ~]# cat /proc/partitions      #查看内核是否已经识别到新的分区;
major minor #blocks  name
 
  8        0  167772160 sda
  8        1     204800 sda1
  8        2    2048000 sda2
  8        3  165518336 sda3
  8       16   62914560 sdb
  8       17   10490413 sdb1
  8       18   10490445 sdb2
  8       19   10490445 sdb3
  8       20         31 sdb4
  8       21   2104483 sdb5
  8       22    2104483 sdb6
  8       23    2104483 sdb7
  9        0   10482176 md0
 
#创建RAID5,chuck为256k; 
[root@ilinux ~]# mdadm -C /dev/md1 -l 5 -n 3 -c 256 -a yes /dev/sdb{5..7} 
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md1 started.
 
#显示raid的详细信息;
[root@ilinux ~]# mdadm -D /dev/md1
   Number   Major   Minor  RaidDevice  State
      0       8       21       0      active sync   /dev/sdb5
      1       8       22       1      active sync   /dev/sdb6
       3      8       23        2     active sync   /dev/sdb7
[root@ilinux ~]# mkfs.ext4 /dev/md1     #格式化为ext4文件系统;
#创建目录 /backup
[root@ilinux ~]# mkdir /backup
 
#修改配置文件,设置开机自动挂载至/backup;
[root@ilinux ~]# echo "/dev/md1 /backup ext4 defaults,noatime,acl 0 0">>/etc/fstab


7、写一个脚本

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

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

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

答:
#!/bin/bash
#
if [ $# -lt 2 ];then
   echo "Please input more than one parameters."
   exit 1
fi
 
for i in $@;do
   if [ -f $i &> /dev/null ];then
       wc -l $i
   else
       echo "Plese input correct parameters."
   fi
done
echo "本次共为$#个文件统计了行数."


8、写一个脚本

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

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

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

答:
[root@ilinux ~]# vim users.sh
#!/bin/bash
#  $#是传给脚本的参数个数,$@ 是传给脚本的所有参数的列表
#  $*是以一个单字符串显示所有向脚本传递的参数
#
if [ $# -lt 2 ];then
   echo "Please enter at least 2 parameters."
else
   num=0
   for i in $@;do
       if id $i &> /dev/null;then
           echo "$i is exists."
       else
           useradd $i && echo $i | passwd --stdin $i &>/dev/null 
           echo "user: $i created successfuily!"
           let num=$num+1
       fi
   done
   echo "Created a total of $num users."
fi
[root@ilinux ~]# bash -n users.sh
[root@ilinux ~]# bash users.sh 111 222 333 
user: 111 created successfuily!
user: 222 created successfuily!
user: 333 created successfuily!
Created a total of 3 users.


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

答:
[root@ilinux ~]# vim usersid.sh
#!/bin/bash
#
sum=0
if [ $UID -eq 0];then
    echo "You are root,you can createusers."
fi
for i in{1..20};do
    if id visitor$i &> /dev/null;then
        echo "visitor$i is exists."
    else
        useradd visitor$i
        userid=`id -u visitor$i`
        sum=$[ $sum + $userid ]
    fi
done
echo "Thesum fo uid: $sum"
[root@ilinux ~]# bash -n usersid.sh
[root@ilinux ~]# bash usersid.sh   
You are root,you can create users.
The sum fo uid: 11010
 
扩展:
这20个用户的任务完成了,下面来看下如何批量的删除用户;
#!/bin/bash
#
for i in {1..20}
do
    users=visitor$i
    userdel -r $users
done


10、写一脚本,分别统计/etc/rc.d/rc.sysinit/etc/rc.d/init.d/functions/etc/fstab文件中以#号开头的行数之和,以及总的空白行数;

答:
[root@ilinux ~]# vim lines.sh   
  #!/bin/bash
  #
  sys1=$(grep "^#" /etc/rc.d/rc.sysinit | wc -l)
  sys2=`grep "^[[:space:]]*$" /etc/rc.d/rc.sysinit | wc -l`
  fun1=$(grep "^#" /etc/rc.d/init.d/functions | wc -l)
  fun2=`grep "^[[:space:]]*$" /etc/init.d/functions | wc -l`
  fs1=`grep "^#" /etc/fstab | wc -l`
  fs2=`grep "^[[:space:]]*$" /etc/fstab | wc -l`
  let sum1=$sys1+$fun1+$fs1
  let sum2=$sys2+$fun2+$fs2
  echo "Begin with #, the sum of lines is: $sum1"
  echo "The total number blank lines is: $sum2"

[root@ilinux ~]# bash -n lines.sh
[root@ilinux ~]# bash  lines.sh 
Begin with #, the sum of lines is: 91
The total number blank lines is: 179


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

答:
[root@ilinux ~]# vim shell.sh
   #!/bin/bash
   #
   printf "user  uid\n"
   printf"==========================\n"
   grep "bash$" /etc/passwd | awk-F: '{print $1,$3}'
   sum=0
   for i in `grep "bash$"/etc/passwd | awk -F: '{print $3}'`;do
      let sum=$sum+$i
   done
   printf"==========================\n"
   echo "The sum of user's uid:$sum"
[root@ilinux ~]# bash -n shell.sh
[root@ilinux ~]# bash  shell.sh
user uid
==========================
root 0
tom 500
jerry 501
. . . . . .
centos 522
user11 523
user12 524
visitor19 559
visitor20 560
==========================
The sum of user's uid: 30812


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

答:
[root@ilinux ~]# vim count.sh
#!/bin/bash
#
sum=0
for uname in `awk -F: '{print $1}' /etc/passwd`;do
    group=(`groups $uname |awk -F: '{print $2}'`)
    count=${#group[@]}
    if [ $count -ge 2 ];then
        echo"$uname"
        let sum=$sum+1
    fi
done
echo "The num of users: $sum"

[root@ilinux ~]# bash -n count.sh
[root@ilinux ~]# bash  count.sh
bin
daemon
adm
postfix
visitor20
The num of users: 5


13、创建一个由至少两个物理卷组成的大小为20G的卷组;要求,PE大小为8M;而在卷组中创建一个大小为5G的逻辑卷mylv1,格式化为ext4文件系统,开机自动挂载至/users目录,支持acl

答:
先创建两个分区如下:
/dev/sdb8            4705        6010   10490413+  8e  Linux LVM
/dev/sdb9            6011        7316   10490413+  8e  Linux LVM
#创建物理卷,并显示详细信息;
[root@ilinux ~]# pvcreate -v /dev/sdb{8,9}    
   DEGRADED MODE. Incomplete RAID LVs will be processed.
   Wiping cache of LVM-capable devices
   Set up physical volume for "/dev/sdb8" with 20980827 availablesectors
   Zeroing start of device /dev/sdb8
   Writing physical volume data to disk "/dev/sdb8"
 Physical volume "/dev/sdb8" successfully created
   Wiping cache of LVM-capable devices
   Set up physical volume for "/dev/sdb9" with 20980827 availablesectors
   Zeroing start of device /dev/sdb9
   Writing physical volume data to disk "/dev/sdb9"
 Physical volume "/dev/sdb9" successfully created
# pvs显示简要pv信息,pvdisplay 显示pv详细信息;
[root@ilinux ~]# pvs          
 PV         VG   Fmt Attr PSize  PFree
 /dev/sdb8       lvm2 ---  10.00g 10.00g
 /dev/sdb9       lvm2 ---  10.00g 10.00g
#创建卷组myvg0,指定PE大小为8M;
[root@ilinux ~]# vgcreate -s 8 myvg0 /dev/sdb{8,9}
 Volume group "myvg0" successfully created
#显示卷组简要信息,myvg0为20G;
[root@ilinux ~]# vgs
 VG    #PV #LV #SN Attr   VSize VFree
 myvg0   2   0   0wz--n- 20.00g 20.00g
#创建逻辑卷mylv1,-L 指定大小为5G,-n 指定名称为mylv0;
[root@ilinux ~]# lvcreate -L 5G -n mylv1 myvg0
 Logical volume "mylv1" created
# lvs简要显示逻辑卷信息;
[root@ilinux ~]# lvs
 LV    VG    Attr      LSize Pool Origin Data% Meta%  Move Log Cpy%Sync Convert
 mylv1 myvg1 -wi-a----- 5.00g 
#将mylv1格式化为ext4文件系统;
[root@ilinux ~]# mkfs.ext4 /dev/myvg0/mylv1
#创建/users目录;
[root@ilinux ~]# mkdir /users
#先把mylv0直接挂载到/users
[root@ilinux ~]# mount -o acl /dev/myvg0/mylv1 /users
#让mylv0开机自动挂载至/users;
[root@ilinux ~]# echo "/dev/myvg0/mylv1  /users ext4  defaults,acl  0 0" >>/etc/fstab


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

答:
[root@ilinux ~]# useradd magedu -d /users/magedu
[root@ilinux ~]# su - magedu
Welcome 561,your home is/users/magedu.Note:...
[magedu@ilinux ~]$ cp /tmp/passwd.txt /tmp/user.log ~
[magedu@ilinux ~]$ ll /users/magedu/                 
total 8                         
-rw-r--r--. 1 magedu magedu 36 Sep 26 08:07passwd.txt
-rw-r--r--. 1 magedu magedu 72 Sep 26 08:07user.log


15、扩展mylv19G,确保扩展完成后原有数据完全可用;

答:
#扩展前,查看数据可用;
[magedu@ilinux ~]$ cat /users/magedu/user.log 
centos logged on.
centos logged on.
centos logged on.
centos logged on.
[magedu@ilinux ~]$ df -h
Filesystem            Size    Used  Avail  Use%  Mounted on
/dev/sda3                156G     1.8G  146G   2%   /
tmpfs                   242M     0   242M   0%   /dev/shm
/dev/sda1                190M      32M 149M    18%  /boot
/dev/mapper/myvg0-mylv1       4.8G     11M   4.6G    1%  /users
#切换到root用户,将mylv1扩展到9G;
[root@ilinux ~]# lvextend -L +4G /dev/myvg0/mylv1     #可用+4G,也可直接指定9G;
 Size of logical volume myvg0/mylv1 changed from 5.00 GiB (640 extents)to 9.00 GiB (1152 extents).
 Logical volume mylv1 successfully resized
#从新格式化为ext4文件系统;
[root@ilinux ~]# resize2fs /dev/myvg0/mylv1 
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/myvg0/mylv1 is mountedon /users; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of/dev/myvg0/mylv1 to 2359296 (4k) blocks.
The filesystem on /dev/myvg0/mylv1 is now2359296 blocks long.
 
[root@ilinux ~]# lvs
 LV    VG    Attr      LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
 mylv0 myvg1 -wi-ao---- 9.00g                                                   
[root@ilinux ~]# cat /users/magedu/user.log 
centos logged on.
centos logged on.
centos logged on.
centos logged on.


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

答:
#先卸载分区/users
[root@ilinux ~]# umount /users/
umount: /users: device is busy.
       (In some cases useful info about processes that use
        the device is found by lsof(8) or fuser(1))
#这里提示device is busy.
#查看有哪些进程在使用;
[root@ilinux ~]# fuser -m /users/
/users/:              1551c  1587c
 [root@ilinux~]# ps aux | grep 1551
magedu   1551  0.0  0.3 108340 1848 pts/0    S    02:23  0:00 -bash
root     3248  0.0  0.1 103252  828 pts/0    S+   03:22  0:00 grep 1551
[root@ilinux ~]# kill -9 1551
# kill后,终端会logout,再从新连接,这时卸载就可以了。
[root@ilinux ~]# umount /users/
#强制检查文件系统;
[root@ilinux ~]# e2fsck -f /dev/myvg0/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/myvg0/mylv1: 18/589824 files (0.0%non-contiguous), 72677/2359296 blocks
#缩减文件系统大小至7G;
[root@ilinux ~]# resize2fs /dev/myvg0/mylv1 7G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/myvg0/mylv1to 1835008 (4k) blocks.
The filesystem on /dev/myvg0/mylv1 is now1835008 blocks long.
#缩减逻辑卷大小至7G,也可用lvresize命令;
[root@ilinux ~]# lvreduce -L 7G /dev/myvg0/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 myvg0/mylv1 changed from 9.00 GiB (1152 extents)to 7.00 GiB (896 extents).
 Logical volume mylv1 successfully resized
#重新挂载/users
[root@ilinux ~]# mount  /dev/myvg0/mylv1  /users
#检查数据的可用性;
[root@ilinux ~]# cat /users/magedu/user.log 
centos logged on.
centos logged on.
centos logged on.
centos logged on.


17、对mylv1创建快照,并通过备份数据;要求保留原有的属主属组等信息;

答:
#创建快照snap_lv1,-p r 表示只读;
[root@ilinux ~]# lvcreate -L 7G -p r -s -n snap_lv1 /dev/myvg0/mylv1             
 Logical volume "snap_lv1" created
[root@ilinux ~]# ll /dev/myvg0/
total 0
lrwxrwxrwx. 1 root root 7 Oct  4 03:52 mylv1 -> ../dm-0
lrwxrwxrwx. 1 root root 7 Oct  4 03:52 snap_lv1 -> ../dm-1
[root@ilinux ~]# lvs
 LV       VG    Attr    LSize Pool Origin Data% Meta%  Move Log Cpy%Sync Convert
 mylv1    myvg0 owi-aos---7.00g                                                   
 snap_lv1  myvg0 sri-a-s---  7.00g     mylv1  0.00
#挂载快照逻辑卷snap_lv1;
[root@ilinux ~]# mkdir /snapusers
[root@ilinux ~]# mount /dev/myvg0/snap_lv1/snapusers/
mount: block device /dev/mapper/myvg0-snap_lv1is write-protected, mounting read-only
#查看逻辑卷及其快照的属性信息;
[root@ilinux ~]# df -hP
Filesystem                  Size  Used Avail Use% Mounted on
/dev/sda3                   156G  1.8G 146G   2% /
tmpfs                       242M     0 242M   0% /dev/shm
/dev/sda1                   190M   32M 149M  18% /boot
/dev/mapper/myvg0-mylv1     6.8G  12M  6.5G   1% /users
/dev/mapper/myvg0-snap_lv1   6.8G  12M  6.5G   1% /snapusers
#在逻辑卷复制一个100M的文件;
[root@ilinux users]# dd if=/dev/zero of=zero.test bs=1024k count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 0.579345s, 181 MB/s
#再次对比逻辑卷和快照的大小情况,逻辑卷;
[root@ilinux ~]# df -hP
Filesystem                  Size  Used Avail Use% Mounted on
/dev/sda3                   156G  1.9G 146G   2% /
tmpfs                       242M     0 242M   0% /dev/shm
/dev/sda1                   190M   32M 149M  18% /boot
/dev/mapper/myvg0-mylv1     6.8G 112M  6.4G   2% /users
/dev/mapper/myvg0-snap_lv1  6.8G  12M  6.5G   1% /snapusers