Linux磁盤分區創建及bash 簡單應用

1、創建一個10G分區,并格式為ext4文件系統;

(1)要求其block大小為2048,預留空間百分比為2,卷標為MYDATA,默認掛載屬性包含acl;
(2)掛載至/data/mydata目錄,要求掛載是禁止程序自動運行,并且更新文件的訪問時間戳;
[[email protected] ~]# 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.
[[email protected] ~]# 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   

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

[[email protected] ~]# mount -o acl,noexec,noatime /dev/sdb1 /data/Mydata

2、創建一個大小為1G的swap分區,并創建好文件系統,并啟用之;

[[email protected] ~]# 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.

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

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

3、寫一個腳本

(1)獲取并列出當前系統上的所有磁盤設備;
(2)顯示每個磁盤設備上每個分區相關的空間使用信息;
[[email protected] Desktop]# cat lsdisk.sh 
#!/bin/bash
#Author:Danry
#Description: Display disk partition information!
#
display=`ls /dev/[s,h]d[a-z]`
fdisk -l $display
[[email protected] Desktop]# 
~
[[email protected] 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

[[email protected] 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;

[[email protected] 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.
[[email protected] 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功能;

[[email protected] 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.
[[email protected] 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
[[email protected] Desktop]# echo "/dev/md5 /backup ext4 defaults,acl,noatime,nodiratime 0 0" >> /etc/fstab [[email protected] 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"
~
[[email protected] Desktop]# bash test.sh abc.txt 
abc.txt line:6
there are 1 files


[[email protected] Desktop]# cat abc.txt 
hao 
nihao 
hello 
world
good
morning`
[[email protected] 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 [email protected]
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."
~
[[email protected] Desktop]# bash test2.sh  nihao nibuhao
nihao has been created.
nibuhao has been created.
2 users created.
[[email protected] 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"
~
[[email protected] 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
[[email protected] Desktop]#

[[email protected] 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
[[email protected] Desktop]# 

本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.npownk.tw/86957

評論列表(1條)

  • h
    h 2017-09-13 10:58

    作業內容注意排版,不需要把過程全部記錄下來,可以把重要知識點寫下來。

聯系我們

400-080-6560

在線咨詢

工作時間:周一至周五,9:30-18:30,節假日同時也值班

QR code
安徽十一选五分布走势