Linux磁盘分区创建及bash 简单应用

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

(1)要求其block大小为2048,预留空间百分比为2,卷标为MYDATA,默认挂载属性包含acl;
(2)挂载至/data/mydata目录,要求挂载是禁止程序自动运行,并且更新文件的访问时间戳;
[root@danry ~]# fdisk /dev/sdb 
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xb74243b2.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extende
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +10G
Partition 1 of type Linux and of size 10 GiB is set

Command (m for help): P

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xb74243b2

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20973567    10485760   83  Linux

Command (m for help): W
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@danry ~]# mke2fs -t ext4 -L MYDATA -m 2 -b 2048 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=MYDATA
OS type: Linux
Block size=2048 (log=1)
Fragment size=2048 (log=1)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 5242880 blocks
104857 blocks (2.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=273678336
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

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

[root@danry ~]# tune2fs -o acl /dev/sdb1
tune2fs 1.42.9 (28-Dec-2013)
[root@danry ~]# blkid /dev/sdb1
/dev/sdb1: LABEL="MYDATA" UUID="fa82387d-d7ab-41ec-be8b-54b25b4c57ac" TYPE="ext4" 
[root@danry ~]# 

[root@danry ~]# mount -o acl,noexec,noatime /dev/sdb1 /data/Mydata

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

[root@danry ~]# fdisk /dev/sdb 
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 2
First sector (20973568-41943039, default 20973568): 
Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-41943039, default 41943039): +1G
Partition 2 of type Linux and of size 1 GiB is set

Command (m for help): P

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xb74243b2

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20973567    10485760   83  Linux
/dev/sdb2        20973568    23070719     1048576   83  Linux

Command (m for help): W
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

[root@danry ~]# partx  -a /dev/sdb
partx: /dev/sdb: error adding partition 1
[root@danry ~]# mkswap /dev/sdb2
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=ab39844d-56fd-412e-bdb8-08703c3dbf81
[root@danry ~]# swapo
swapoff  swapon   
[root@danry ~]# swapo
swapoff  swapon   
[root@danry ~]# swapon  /dev/sdb2 

[root@danry ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1823         282        1169           9         371        1344
Swap:          3071           0        3071
[root@danry ~]# 

3、写一个脚本

(1)获取并列出当前系统上的所有磁盘设备;
(2)显示每个磁盘设备上每个分区相关的空间使用信息;
[root@localhost Desktop]# cat lsdisk.sh 
#!/bin/bash
#Author:Danry
#Description: Display disk partition information!
#
display=`ls /dev/[s,h]d[a-z]`
fdisk -l $display
[root@localhost Desktop]# 
~
[root@localhost Desktop]# bash lsdisk.sh 
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0008508b

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    41943039    20458496   8e  Linux LVM

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

[root@localhost Desktop]# 

4、总结Raid的哥哥级别及其组合方式和性能不同;

Raid2、Raid3、Raid4现在几乎已经很少用到了
Raid0:至少两块硬盘,一个数据打散后写入到不同的硬盘当中,其增加读写性能,降低了数据的安全性,磁盘可用率100%。
Raid1:至少两块硬盘,一块硬盘写数据,一块硬盘做备份,其增加数据安全性,降低了数据的读写性能,磁盘可用率50%。
Raid4:至少三块硬盘,其中两块硬盘存放数据,一块硬盘存放校验值,增加了数据的读写性,也增加了数据的安全性,磁盘可用率(磁盘数-1)
Raid5:至少三块硬盘,其中两块硬盘存放数据,一块硬盘存放校验值,其每次数据校验盘是轮流交替的,增加了数据的读写性,也增加了数据的安全性,磁盘可用率(磁盘数-1)
Raid10:至少4块硬盘,其组合先引用Raid1的安全特性,再引用Raid0的读写特,若4快盘则:俩俩为一组,每组最多可坏一块硬盘,磁盘可用率50%性。
Raid01:至少4块硬盘,其组合是先引用Raid0的读写特性,再引用Raid1安全特性,若4快盘则:俩俩为一组,每组最多可坏一块硬盘,磁盘可用率50%。

5、创建一个大小为10G的Raid1,要求一个空闲盘,而且CHUN大小为128K;

[root@localhost Desktop]# mdadm -C /dev/md1 -n 2 -x 1 -c 128 -l 1 /dev/sdb1 /dev/sdb2 /dev/sdb3
mdadm: /dev/sdb1 appears to contain an ext2fs file system
       size=10485760K  mtime=Sat Mar  4 13:38:10 2017
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
Continue creating array? 
Continue creating array? (y/n) y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md1 started.
[root@localhost scripts]# mdadm -D /dev/md1 
/dev/md1:
        Version : 1.2
  Creation Time : Sat Mar  4 16:27:42 2017
     Raid Level : raid1
     Array Size : 5238784 (5.00 GiB 5.36 GB)
  Used Dev Size : 5238784 (5.00 GiB 5.36 GB)
   Raid Devices : 2
  Total Devices : 3
    Persistence : Superblock is persistent

    Update Time : Sat Mar  4 16:27:58 2017
          State : clean, resyncing 
 Active Devices : 2
Working Devices : 3
 Failed Devices : 0
  Spare Devices : 1

  Resync Status : 64% complete

           Name : localhost.localdomain:1  (local to host localhost.localdomain)
           UUID : 345d614a:1c8921d7:4a62d97c:6d9555cc
         Events : 10

    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、创建一个大小为4G的Raid5设备,chunk大小为256K,格式化ext4文件系统,要求自动挂载至、backup目录,而且不更新访问时间戳,切支持acl功能;

[root@localhost Desktop]# mdadm -C /dev/md5 -n 4 -c 256 -l 5 /dev/sdb1 /dev/sdb2 /dev/sdb3 /dev/sdb4
mdadm: /dev/sdb1 appears to contain an ext2fs file system
       size=10485760K  mtime=Sat Mar  4 13:38:10 2017
mdadm: /dev/sdb1 appears to be part of a raid array:
       level=raid1 devices=2 ctime=Sat Mar  4 16:27:42 2017
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md5 started.
[root@localhost scripts]# mdadm -D /dev/md5 
/dev/md5:
        Version : 1.2
  Creation Time : Sat Mar  4 16:33:41 2017
     Raid Level : raid5
     Array Size : 3142656 (3.00 GiB 3.22 GB)
  Used Dev Size : 1047552 (1023.17 MiB 1072.69 MB)
   Raid Devices : 4
  Total Devices : 4
    Persistence : Superblock is persistent

    Update Time : Sat Mar  4 16:33:47 2017
          State : clean 
 Active Devices : 4
Working Devices : 4
 Failed Devices : 0
  Spare Devices : 0

         Layout : left-symmetric
     Chunk Size : 256K

           Name : localhost.localdomain:5  (local to host localhost.localdomain)
           UUID : afc5cf68:aa1f4f34:f3047c47:d0b6f61c
         Events : 18

    Number   Major   Minor   RaidDevice State
       0       8       17        0      active sync   /dev/sdb1
       1       8       18        1      active sync   /dev/sdb2
       2       8       19        2      active sync   /dev/sdb3
       4       8       20        3      active sync   /dev/sdb4
[root@localhost Desktop]# echo "/dev/md5 /backup ext4 defaults,acl,noatime,nodiratime 0 0" >> /etc/fstab [root@localhost scripts]# mount -a

7、写一个脚本

(1)、接受一个以上字符串当做用户名;
(2)、显示每个文件拥有的行数;
(3)、总结说明本次共为几个文件统计了其行数;
#!/bin/bash
#Author:Danry
#Description:
#
#
#
if [ $# -le 1 ]
    then
        echo "please input someting" && exit 2
fi

for i in $*
    do 
        echo "$i line:$(cat $i | wc -l)"
    done 
        echo "there are $# files"
~
[root@danry Desktop]# bash test.sh abc.txt 
abc.txt line:6
there are 1 files


[root@danry Desktop]# cat abc.txt 
hao 
nihao 
hello 
world
good
morning`
[root@danry Desktop]# 

8、写一个脚本

(1)、传递两个以上字符串当作用户名;
(2)、创建这些用户;且密码通用户名;
(3)、总结说明共创建了几个用户;
#!/bin/bash
#Author:Danry
#Description:
#
if [ $# -le 1 ]
    then 
        echo "Must be more than two characters" && exit 2 
fi

sum=0
for user in $@
do
    if id $user &> /dev/null
        then
            echo "user $user already exists!"
            continue
    else
        useradd $user 
        echo "$user" | passwd --stdin $user  &>/dev/null
        echo "$user has been created."
        let sum++
    fi
done

echo "$sum users created."
~
[root@danry Desktop]# bash test2.sh  nihao nibuhao
nihao has been created.
nibuhao has been created.
2 users created.
[root@danry Desktop]# 

9、写一个脚本,新增20个用户,vistitor1-visitor20;计算它们的ID之和;

#!/bin/bash
#
#
for n in `seq 20`
    do
        username=visitor$n
        useradd $username
        uid=`id -u $username`
        let sumuserid+=$uid

    done
    echo "The users id SUM:$sumuserid"
~
[root@localhost Desktop]# bash test.sh 
Creating mailbox file: File exists
Creating mailbox file: File exists
Creating mailbox file: File exists
Creating mailbox file: File exists
Creating mailbox file: File exists
Creating mailbox file: File exists
Creating mailbox file: File exists
Creating mailbox file: File exists
Creating mailbox file: File exists
Creating mailbox file: File exists
Creating mailbox file: File exists
Creating mailbox file: File exists
Creating mailbox file: File exists
Creating mailbox file: File exists
Creating mailbox file: File exists
Creating mailbox file: File exists
Creating mailbox file: File exists
Creating mailbox file: File exists
Creating mailbox file: File exists
Creating mailbox file: File exists
The users id SUM:20210
[root@localhost Desktop]#

[root@localhost Desktop]# tail -20 /etc/passwd
visitor1:x:1001:1001::/home/visitor1:/bin/bash
visitor2:x:1002:1002::/home/visitor2:/bin/bash
visitor3:x:1003:1003::/home/visitor3:/bin/bash
visitor4:x:1004:1004::/home/visitor4:/bin/bash
visitor5:x:1005:1005::/home/visitor5:/bin/bash
visitor6:x:1006:1006::/home/visitor6:/bin/bash
visitor7:x:1007:1007::/home/visitor7:/bin/bash
visitor8:x:1008:1008::/home/visitor8:/bin/bash
visitor9:x:1009:1009::/home/visitor9:/bin/bash
visitor10:x:1010:1010::/home/visitor10:/bin/bash
visitor11:x:1011:1011::/home/visitor11:/bin/bash
visitor12:x:1012:1012::/home/visitor12:/bin/bash
visitor13:x:1013:1013::/home/visitor13:/bin/bash
visitor14:x:1014:1014::/home/visitor14:/bin/bash
visitor15:x:1015:1015::/home/visitor15:/bin/bash
visitor16:x:1016:1016::/home/visitor16:/bin/bash
visitor17:x:1017:1017::/home/visitor17:/bin/bash
visitor18:x:1018:1018::/home/visitor18:/bin/bash
visitor19:x:1019:1019::/home/visitor19:/bin/bash
visitor20:x:1020:1020::/home/visitor20:/bin/bash
[root@localhost Desktop]# 

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/86957

(1)
上一篇 2017-09-11 21:04
下一篇 2017-09-11 22:06

相关推荐

  • 修改文件的权限

        linux中一切皆文件,文件有权限,所有者,所属组,大小等属性。文件所有者是指创建文件的用户,所属组是指创建文件的用户属于哪一个主要的组(用户的主组只能有一个)。     用户对文件进行各种操作的前提是有相应的权限,所以有些文件我们只能读,不能写,而有些文件既可以读写,还可以更改内容,下面就…

    2017-07-30
  • raid5创建

    创建分区并改类型 创建raid5   创建把文件系统 生成配置文件    

    2017-12-17
  • centos7 搭建SVN服务器

    运维常见的工作就是日常软件的安装和维护,SVN虽然被Git侵占了市场份额,但是仍然是高效简捷的源码管理工具。从日常软件的安装部署开始,熟悉一些Linux的常用命令。

    Linux干货 2018-03-26
  • 马哥教育网络20期+第7周练习博客

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

    Linux干货 2016-07-24
  • 正则表达式 小结

    听老师和学姐都说,正则表达式很重要,所以这次我总结一下,同时加强一下记忆。 目前我们学的正则表达式有:字符匹配;匹配字数;位置锚定。 :. 匹配任意单个字符 [] 匹配指定范围内的任意单个字符 [^] 匹配指定范围外的任意单个字符 [:alnum:] 或 [0-9a-zA-Z] [:alpha:] 或 [a-zA-Z]…

    2017-07-31
  • 有证说话硬–实现CA和证书申请

    centos下利用openssl来实现证书的颁发 直接进入正题,细节坑就不说了,自己解决起来更有挑战性不是 步骤流程: 我是拿的7.3版本做CA主机,6.8版本做客户端 1.创建CA 2.生成私钥 3.生成自签名证书 4.到客服端 5.生成私钥 6.生成证书申请文件 7.将请求发送给-CA主机 8.CA主机-验证签署 9.拷回给客户端使用 用法:openss…

    2017-04-11

评论列表(1条)

  • h
    h 2017-09-13 10:58

    作业内容注意排版,不需要把过程全部记录下来,可以把重要知识点写下来。