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.www58058.com/86957

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

相關推薦

  • 基礎腳本編程練習題

        Shell腳本其實就是以一系列命令組合起來的文本文件,這些命令組合起來完成一個或者一項功能。因為Shell似乎是各UNIX系統之間通用的功能,并且經過POSIX(表示可移植操作系統接口Portable Operating System Interface of UNIX,縮寫為 POSIX)的標準化。因此,Sh…

    2017-08-05
  • 馬哥教育網絡班21期-第四周課程練習

    Do one thing at a time,and do well. 小僧近期忙的去尿尿的時間都要擠出來…..! 無人能理解 zZzz 復制/etc/skel目錄為/home/tuser1,要求/home/tuser1及其內部文件的屬組和其它用戶均沒有任何訪問權限。 編輯/etc/group文件,添加組hadoop。 手動編輯/etc/pass…

    Linux干貨 2016-07-29
  • 重定向、管道——Linux基本命令(9)

    1.     輸出重定向 Linux默認輸入是鍵盤,輸出是顯示器。可以用重定向來改變這些設置。比如用wc命令的時候本來是要手動輸入一篇文字來計算字符數的,可以直接把一個已經寫好的文件用‘<’指向這條命令,就直接可以統計這個文件的字符數等了。   STDOUT(標準輸出)和STDERR(標準錯誤)可以被重…

    2017-07-20
  • vsftpd+pam+MySQL—->實現虛擬用戶認證

    一、安裝所需要程序 1、安裝vsftpd yum -y install vsftpd 2、安裝MySQL yum -y install  mysql-server mysql-devel pam_mysql 二、創建虛擬用戶賬號 1.準備數據庫及相關表 首先請確保mys…

    Linux干貨 2016-09-19
  • Linux發展史

    Linux發展史 操作系統出現前:   1946年第一臺計算機誕生–20世紀50年代中期,還未出現操作系統,計算機工作采用手工操作方式。程序員將對應于程序和數據的已穿孔的紙帶(或卡片)裝入輸入機,然后啟動輸入機把程序和數據輸入計算機內存,接著通過控制臺開關啟動程序針對數據運行;計算完畢,打印機輸出計算結果;用戶取走結果并卸下紙帶(或卡片…

    Linux干貨 2016-10-14
  • ACL權限實例詳解

    CentOS7當中,無論是操作系統安裝時還是之后手工創建的文件系統(xfs、ext4)均會開啟ACL功能。 CentOS6及之前的版本,僅操作系統安裝時創建的文件系統才會默認開啟ACL,手工創建的文件系統,需要手工開啟ACL 功能。 Acl如何設置 創建分區 mount -o acl /dev/sda7  取消的方式,重新掛載時不指定即可 tune…

    2017-07-29

評論列表(1條)

  • h
    h 2017-09-13 10:58

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

欧美性久久久久