N25第七周RAID LV 概述及示例,swap创建,read 特殊用法以及bash脚本示例

1、创建一个10G分区,并格式为ext4文件系统; (1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl; (2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳;

mke2fs -t ext4 -b 2048 -m 2 -L MYDATA /dev/sdc1
tune2fs -o acl /dev/sdc1

[root@localhost ~]# tune2fs -l /dev/sdc1  | head -n 20
tune2fs 1.42.9 (28-Dec-2013)
Filesystem volume name:   MYDATA
Last mounted on:          <not available>
Filesystem UUID:          07bffbb5-3c74-4be1-9d1e-9ebe41c82f34
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype extent 64bit flex_bg sparse_super huge_file uninit_bg dir_nlink extra_isize
Filesystem flags:         signed_directory_hash 
Default mount options:    user_xattr acl
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              655360
Block count:              5242880
Reserved block count:     104857
Free blocks:              5121266
Free inodes:              655349
First block:              0
Block size:               2048
Fragment size:            2048

[root@localhost ~]# mount -o noatime,noauto -L MYDATA  /data/mydata
[root@localhost ~]# mount | tail -1
/dev/sdc1 on /data/mydata type ext4 (rw,noatime,seclabel,data=ordered)

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

[root@localhost ~]# mkswap /dev/sdb2
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=b74adcbd-fe54-48bf-947f-a0396cb0319f
[root@localhost ~]# swapon /dev/sdb2        #
[root@localhost ~]# free
              total        used        free      shared  buff/cache   available
Mem:        1001336      194708      537976        7216      268652      606452
Swap:       3146744           0     3146744
[root@localhost ~]#

3、写一个脚本 (1)、获取并列出当前系统上的所有磁盘设备; (2)、显示每个磁盘设备上每个分区相关的空间使用信息;

[root@localhost tmp]# cat ./disfree.sh
#!/bin/bash
echo " all disks informations:"
fdisk -l | grep "^Disk /dev/[hs]d[abcd]"

echo "each partition informations of each disk"
fdisk -l | grep "^/dev/[hs]d[abcd][1-9]"
[root@localhost tmp]# bash ./disfree.sh
 all disks informations:
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Disk /dev/sdc: 21.5 GB, 21474836480 bytes, 41943040 sectors
Disk /dev/sdd: 21.5 GB, 21474836480 bytes, 41943040 sectors
Disk /dev/sda: 128.8 GB, 128849018880 bytes, 251658240 sectors
each partition informations of each disk
/dev/sdb1            2048    10487807     5242880    5  Extended
/dev/sda1   *        2048      616447      307200   83  Linux
/dev/sda2          616448     4812799     2098176   82  Linux swap / Solaris
/dev/sda3         4812800   251658239   123422720   83  Linux
[root@localhost tmp]#

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

N25第七周RAID LV 概述及示例,swap创建,read 特殊用法以及bash脚本示例N25第七周RAID LV 概述及示例,swap创建,read 特殊用法以及bash脚本示例

5、创建一个大小为10G的RAID1,要求有一个空闲盘,而且CHUNK大小为128k; 首先要建立分区—-》 调整分区类型fd—->然后让系统重读分区信息kpartx -a /path/to/device

  建立RAID1:mdadm -C /dev/md0 -a yes -n 2 -l 1 -c 128 -x 1 /dev/sd[b-d]1

  [root@localhost ~]# mdadm -D /dev/md127
    /dev/md127:
            Version : 1.2
      Creation Time : Sun Jan  1 13:27:16 2017
         Raid Level : raid1
         Array Size : 10482112 (10.00 GiB 10.73 GB)
      Used Dev Size : 10482112 (10.00 GiB 10.73 GB)
       Raid Devices : 2
      Total Devices : 3
        Persistence : Superblock is persistent

        Update Time : Mon Jan  2 00:29:12 2017
              State : clean 
     Active Devices : 2
    Working Devices : 3
     Failed Devices : 0
      Spare Devices : 1

           Name : localhost.localdomain:0  (local to host localhost.localdomain)
           UUID : fd6aa856:f594ccf5:bcb8b1fa:b666a0ff
         Events : 18

    Number   Major   Minor   RaidDevice State
       0       8       17        0      active sync   /dev/sdb1
       1       8       33        1      active sync   /dev/sdc1

       2       8       49        -      spare   /dev/sdd1

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

  首先要建立分区----》 调整分区类型fd---->然后让系统重读分区信息kpartx -a /path/to/device

  建立RAID1:mdadm -C /dev/md0 -a yes -n 2 -l 5 -c 256  /dev/sd[b-d]1

  格式化ext4文件系统:

    [root@localhost ~]# mke2fs -t ext4 -L 'mddata' /dev/md0
    mke2fs 1.42.9 (28-Dec-2013)
    Filesystem label=mddata
    OS type: Linux
    Block size=4096 (log=2)
    Fragment size=4096 (log=2)
    Stride=64 blocks, Stripe width=128 blocks
    262144 inodes, 1047552 blocks
    52377 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=1073741824
    32 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

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


  [root@localhost ~]# mdadm -D /dev/md0
    /dev/md0:
            Version : 1.2
      Creation Time : Sun Jan  1 18:48:28 2017
         Raid Level : raid5
         Array Size : 4190208 (4.00 GiB 4.29 GB)
      Used Dev Size : 2095104 (2046.34 MiB 2145.39 MB)
       Raid Devices : 3
      Total Devices : 3
        Persistence : Superblock is persistent

        Update Time : Sun Jan  1 18:59:36 2017
              State : clean 
     Active Devices : 3
    Working Devices : 3
     Failed Devices : 0
      Spare Devices : 0

             Layout : left-symmetric
         Chunk Size : 256K

               Name : localhost.localdomain:0  (local to host localhost.localdomain)
               UUID : 9a2cade4:b8237d53:1ddcb8ce:b25afdfd
             Events : 18

        Number   Major   Minor   RaidDevice State
           0       8       17        0      active sync   /dev/sdb1
           1       8       33        1      active sync   /dev/sdc1
           3       8       49        2      active sync   /dev/sdd1
    [root@localhost ~]# blkid /dev/md0
    /dev/md0: LABEL="mddata" UUID="19d7ccc2-2c2a-4480-9578-8557356f2058" TYPE="ext4" 
    [root@localhost ~]# cat /etc/fstab

    #
    # /etc/fstab
    # Created by anaconda on Tue Nov  8 20:12:18 2016
    #
    # 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
    #
    UUID=e4326591-c5be-42b7-9a3f-080fdef925a7 /                       xfs     defaults        0 0
    UUID=8096d939-e581-466a-b8b4-f77b509922a0 /boot                   xfs     defaults        0 0
    UUID=bf72d293-8677-41c5-8a2f-607f3b4b0e14 swap                    swap    defaults        0 0
    LABEL=mddata                 /backup              ext4    defaults,noatime,acl 0 0

    [root@localhost ~]# mount -a

7、写一个脚本 (1) 接受一个以上文件路径作为参数; (2) 显示每个文件拥有的行数; (3) 总结说明本次共为几个文件统计了其行数;

[root@localhost ~]# cat /tmp/lines_counter.sh
#!/bin/bash
 echo -n " please input paths of documents:"; read -a doc
 wc -l ${doc[*]}
 echo "total ${#doc[*]} pieces documents are count"

[root@localhost ~]# bash /tmp/lines_counter.sh
 please input paths of documents:/etc/fstab /etc/issue
 15 /etc/fstab
  3 /etc/issue
 18 total
total 2 pieces documents are count

8、写一个脚本 (1) 传递两个以上字符串当作用户名; (2) 创建这些用户;且密码同用户名; (3) 总结说明共创建了几个用户;

[root@localhost ~]# cat /tmp/useradd
#!/bin/bash
 echo -n " please input username:"; read -a name
 for i in $(seq 0 $[${#name[*]}-1]); do
       useradd ${name[$i]} && echo "${name[$i]}"| passwd ${name[$i]} --stdin 
       done
 echo "total ${#name[*]} users are added"

[root@localhost ~]# bash /tmp/useradd
 please input username:jerr1 jerr2 jerr3 jerr4
Changing password for user jerr1.
passwd: all authentication tokens updated successfully.
Changing password for user jerr2.
passwd: all authentication tokens updated successfully.
Changing password for user jerr3.
passwd: all authentication tokens updated successfully.
Changing password for user jerr4.
passwd: all authentication tokens updated successfully.
total 4 users are added
[root@localhost ~]#

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

[root@localhost ~]# cat /tmp/idcount
#!/bin/bash
  declare -i sum=0
  for ((i=1;i<=20;i++));do
        useradd visitor$i 
        echo "user visitor$i added complete"
        let  id1=$(id -u visitor$i)
        let sum+=$id1
        done
  echo " total SUM of these id: $sum"


[root@localhost ~]#

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

[root@localhost ~]# cat /tmp/blank_counter.sh
#!/bin/bash
 echo -n " please input paths of documents:"; read -a doc
 declare -i total_lines=0
 declare -i totla_blank=0
 for i in $(seq 0 $[${#doc[*]}-1]);do
       lines1=$(grep -E "^#.*" ${doc[$i]}| wc -l)
       lines_blank=$(grep -E "^[[:space:]]*$" ${doc[$i]}| wc -l)
    echo " ${doc[$i]} total lines begin with # : $lines1,total blank lines:$lines_blank"
       let  total_lines+=$lines1
       let  total_blank+=$lines_blank
       done
 echo "All documents total lines:$total_lines,total blank: $total_blank"

[root@localhost ~]# bash /tmp/blank_counter.sh
 please input paths of documents:/etc/rc.d/rc.sysinit /etc/rc.d/init.d/functions /etc/fstab
 /etc/rc.d/rc.sysinit total lines begin with # : 44,total blank lines:99
 /etc/rc.d/init.d/functions total lines begin with # : 40,total blank lines:75
 /etc/fstab total lines begin with # : 7,total blank lines:1
All documents total lines:91,total blank: 175
[root@localhost ~]#

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

[root@localhost ~]# cat  /tmp/UID_counter
#!/bin/bash
declare -i sum=0 
  while read line;do
        username=$(echo $line | cut -d: -f1)
        useruid=$(echo $line | cut -d: -f3)
        shell=$(echo $line | cut -d: -f7)
        if [ "$shell" == "/bin/bash" ];then
           echo "bash user: $username,$useruid"
           let sum+=$useruid
       fi
  done < /etc/passwd
echo "total Sum of uid: $sum"

    [root@localhost ~]# bash /tmp/UID_counter | tail
    bash user: visitor12,544
    bash user: visitor13,545
    bash user: visitor14,546
    bash user: visitor15,547
    bash user: visitor16,548
    bash user: visitor17,549
    bash user: visitor18,550
    bash user: visitor19,551
    bash user: visitor20,552
    total Sum of uid: 27375
    [root@localhost ~]#

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

    [root@localhost ~]# cat /tmp/usercounter
    #!/bin/bash
    declare -i sum=0
    while read line;do
            username=$(echo $line | cut -d: -f1)
            id $username | grep ',' &> /dev/null
            if [ $? -eq 0 ];then
               echo "$username has secondary group"
               let sum+=1
            fi
    done < /etc/passwd
    echo " total $sum users have secondary group"

    [root@localhost ~]# bash /tmp/usercounter
    qemu has secondary group
    postfix has secondary group
    mix has secondary group
    gesrrt has secondary group
    fedora has secondary group
    jow has secondary group
    magedu has secondary group
     total 7 users have secondary group
    [root@localhost ~]#

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

[root@localhost ~]# vgcreate -s 8M myvg /dev/sda5 /dev/sda6
  Volume group "myvg" successfully created
[root@localhost ~]# 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               19.98 GiB
  PE Size               8.00 MiB
  Total PE              2558
  Alloc PE / Size       0 / 0   
  Free  PE / Size       2558 / 19.98 GiB
  VG UUID               os7kzA-jngy-32xq-D0tE-Ey7S-Xgj9-tmpgnn

root@localhost ~]# lvcreate -L 5G -n mylv1 myvg
  Logical volume "mylv1" created.
[root@localhost ~]# mke2fs -t ext4 /dev/myvg/mylv1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=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

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done 

[root@localhost ~]# blkid /dev/myvg/mylv1
[root@localhost ~]# tune2fs -L mylv1 /dev/myvg/mylv1
tune2fs 1.42.9 (28-Dec-2013)
[root@localhost ~]# blkid /dev/myvg/mylv1
/dev/myvg/mylv1: LABEL="mylv1" UUID="60c02ab3-e5f8-4629-b884-dd1a17f247b0" TYPE="ext4"

[root@localhost ~]# cat /etc/fstab

    #
    # /etc/fstab
    # Created by anaconda on Tue Nov  8 20:12:18 2016
    #
    # 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
    #
UUID=e4326591-c5be-42b7-9a3f-080fdef925a7 /                       xfs     defaults        0 0
UUID=8096d939-e581-466a-b8b4-f77b509922a0 /boot                   xfs     defaults        0 0
UUID=bf72d293-8677-41c5-8a2f-607f3b4b0e14 swap                    swap    defaults        0 0
LABEL=mylv1                              /users                   ext4     defaults,acl   0 0

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

[root@localhost ~]# useradd -d /users/magedu magedu
[root@localhost magedu]# su magedu
[magedu@localhost ~]$ cp /etc/{fstab,issue} /user/magedu

15、扩展mylv1至9G,确保扩展完成后原有数据完全可用;

[root@localhost ~]# lvextend -L 9G /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@localhost ~]# resize2fs /dev/myvg/mylv1
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/myvg/mylv1 is mounted on /users; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 2
The filesystem on /dev/myvg/mylv1 is now 2359296 blocks long.

16、缩减mylv1至7G,确保缩减完成后原有数据完全可用;

[root@localhost ~]# umount /dev/myvg/mylv1
[root@localhost ~]# e2fsck -f /dev/myvg/mylv1
e2fsck 1.42.9 (28-Dec-2013)
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
mylv1: 26/589824 files (0.0% non-contiguous), 75566/2359296 blocks
root@localhost ~]# resize2fs /dev/myvg/mylv1 2G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/myvg/mylv1 to 524288 (4k) blocks.
The filesystem on /dev/myvg/mylv1 is now 524288 blocks long.
[root@localhost ~]# 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 (1152 extents) to 7.00 GiB (896 extents).
  Logical volume mylv1 successfully resized.

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

[root@localhost ~]# lvcreate -s -L 512M -p r -n mylv-snap /dev/myvg/mylv1
 Logical volume "mylv-snap" created.
[root@localhost ~]# mount /dev/myvg/mylv-snap /mnt
mount: /dev/mapper/myvg-mylv--snap is write-protected, mounting read-only
 [root@localhost magedu]# cp -a ./ /users/magedu
cp: overwrite ‘/users/magedu/./.bashrc’? y   
cp: overwrite ‘/users/magedu/./.bash_profile’? 
cp: overwrite ‘/users/magedu/./.bash_history’? 
cp: overwrite ‘/users/magedu/./.bash_logout’? 
cp: overwrite ‘/users/magedu/./fstab’? 
cp: overwrite ‘/users/magedu/./issue’? 
cp: overwrite ‘/users/magedu/./.cache/abrt/lastnotification’? 
[root@localhost magedu]#

原创文章,作者:diglinux,如若转载,请注明出处:http://www.178linux.com/65814

(0)
上一篇 2017-01-04 00:05
下一篇 2017-01-04 17:18

相关推荐

  • 如何实现本地yum源的共享

    上一篇关于Linux yum客户端的配置的博客,介绍了如何通过修改本机yum配置文件来访问网络或者本地的yun sever, 本文就来介绍一下,如何把我们的本地RPM包像yum sever一样通过http或ftp网络方式共享出去。 一、准备好发行版光盘,包,元数据    CentOS-6.8-x86_64-bin-DVD1.iso二、准备…

    2017-06-17
  • lvs

    1、什么是LVS LVS是Linux Virtual Server的简写,以为Linux虚拟服务器,是一个虚拟服务器集群,其具有很好的可伸缩性、可靠性、可管理性。LVS集群采用IP负载均衡技术和基于内容请求分发技术。调度器具有很好的吞吐率,将请求均衡地转移到不同的服务器上执行,且调度器自动屏蔽掉服务器的故障,从而将一组服务器构成一个高性能的、高可用的虚拟服务…

    Linux干货 2016-10-29
  • RAID(重要)

    RAID   RAID(Redundent Array of Independent Disk):独立的冗余磁盘阵列,目的是为了结合多个物理驱动器组成单个单元,提高了磁盘的性能或保证数据冗余。RAID能够防止硬件的故障导致数据丢失,但是不能防止人为操作,软件故障、恶意软件感染等造成的数据丢失,因此不能替代备份。 分类有:硬件实现RAID(一是外接式磁盘阵列柜…

    Linux干货 2016-08-30
  • Linxu系统的启动过程

    Linxu系统的启动过程 启动流程 1、引导Linux启动是从BIOS中的地址0xFFFF0处开始的,BIOS由两部分组成:POST代码和运行时服务,运行时服务是为操作系统提供一些接口,如温度检测等。 BIOS的第一个步骤是加电自检(POST),完成对硬件的的检测,如某些硬件出现错误无法通过检测就导致系统无法启动,POST完成之后将被清出内存; BIOS的第…

    Linux干货 2016-09-13
  • ☞RAID使用手册

    RAID使用手册 RAID演示

    Linux干货 2016-09-02