1. 程式人生 > >Linux系統管理第四周作業【Linux微職位】

Linux系統管理第四周作業【Linux微職位】

filesystem raid swap

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

(1) 要求其block大小為2048, 預留空間百分比為2, 卷標為MYDATA, 默認掛載屬性包含acl;

創建分區

[[email protected] Desktop]# 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 0xd1fd27d7.
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 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): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[[email protected]
/* */ Desktop]# partx -a /dev/sdb partx: /dev/sdb: error adding partition 1


指定block大小,預留空間百分比及卷標

[[email protected] Desktop]# mke2fs -t ext4 -b 2048 -m 2 -L ‘MYDATA‘ /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


添加acl掛載屬性

[[email protected] Desktop]# tune2fs -o acl /dev/sdb1
tune2fs 1.42.9 (28-Dec-2013)



(2) 掛載至/data/mydata目錄,要求掛載時禁止程序自動運行,且不更新文件的訪問時間戳;

[[email protected] Desktop]# mkdir -p /data/mydata
[[email protected] Desktop]# mount -o noexec,noatime /dev/sdb1 /data/mydata/



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

創建分區

[[email protected] Desktop]# 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): 
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): 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] Desktop]# partx -a /dev/sdb
partx: /dev/sdb: error adding partition 1


寫入文件系統fstab

[[email protected] Desktop]# mkswap /dev/sdb2
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=2f3ba372-bac4-4846-b833-636eb3d97303
[[email protected] Desktop]# vim /etc/fstab
[[email protected] Desktop]# cat /etc/fstab | grep swap
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
UUID=2f3ba372-bac4-4846-b833-636eb3d97303swapswap defaults0 0
[[email protected] Desktop]# mount -a


啟用swap分區

[[email protected] Desktop]# swapon -a
[[email protected] Desktop]# free -m
             total       used       free     shared    buffers     cached
Mem:          1826        868        958          9          1        283
-/+ buffers/cache:        583       1243
Swap:         3071          0       3071



3、寫一個腳本

(1) 獲取並列出當前系統上的所有磁盤設備;

(2) 顯示每個磁盤設備上每個分區相關的空間使用信息;

#!/bin/bash
echo "disk info:"
fdisk -l /dev/[sh]d[a-z]
echo
echo "partition info:"
for disk in $(fdisk -l | grep -o "^Disk /dev/[sh]d[a-z]" | cut -d‘ ‘ -f2); do
   echo $disk
   declare -i i=1
   while [ $i -le $(fdisk -l $disk | grep "^/dev/[sh]d[a-z][0-9]" | wc -l) ]; do
       df -Th ${disk}${i} | tail -1
       let i++
   done
   echo
done



4、總結RAID的各個級別及其組合方式和性能的不同;

RAID-0: 條帶卷,strip

特點:讀、寫性能提升;無容錯能力

可用空間:N*min(S1,S2,...)

最少磁盤數:2


RAID-1:鏡像卷,mirror

優點:讀性能提升、寫性能略有下降;有冗余能力

可用空間:1*min(S1,S2,...)

最少磁盤數:2


RAID-5:

特點:讀、寫性能提升;有容錯能力:1塊磁盤

可用空間:(N-1)*min(S1,S2,...)

最少磁盤數:3


RAID-6:

特點:讀、寫性能提升;有容錯能力:2塊磁盤

可用空間:(N-2)*min(S1,S2,...)

最少磁盤數:4


混合類型

RAID-10:讀、寫性能提升;有容錯能力:每組鏡像最多只能壞1塊

可用空間:N*min(S1,S2,...)/2

最少磁盤數:4


JBOD:Just a Bunch Of Disks

功能:將多塊磁盤的空間合並一個大的連續空間使用

可用空間:sum(S1,S2,...)



5、創建一個大小為10G的RAID1,要求有一個空閑盤,而且CHUNK大小為128k;

[[email protected] Desktop]# 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 0xba3edeaa.
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 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): t
Selected partition 1
Hex code (type L to list all codes): fd
Changed type of partition ‘Linux‘ to ‘Linux raid autodetect‘
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[[email protected] Desktop]# fdisk /dev/sdc
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 0x2b6abd80.
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 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): t
Selected partition 1
Hex code (type L to list all codes): fd
Changed type of partition ‘Linux‘ to ‘Linux raid autodetect‘
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[[email protected] Desktop]# fdisk /dev/sdd
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 0xde9307db.
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 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): t
Selected partition 1
Hex code (type L to list all codes): fd
Changed type of partition ‘Linux‘ to ‘Linux raid autodetect‘
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[[email protected] Desktop]# partprobe
[[email protected] Desktop]# mdadm -C /dev/md1 -a yes -c 128K -l 1 -n 2 -x 1 /dev/sdb1 /dev/sdc1 /dev/sdd1
[[email protected] Desktop]# mdadm -D /dev/md1
/dev/md1:
        Version : 1.2
  Creation Time : Wed May 31 22:46:16 2017
     Raid Level : raid1
     Array Size : 10477440 (9.99 GiB 10.73 GB)
  Used Dev Size : 10477440 (9.99 GiB 10.73 GB)
   Raid Devices : 2
  Total Devices : 3
    Persistence : Superblock is persistent
    Update Time : Wed May 31 22:47:08 2017
          State : active 
 Active Devices : 2
Working Devices : 3
 Failed Devices : 0
  Spare Devices : 1
           Name : localhost.localdomain:1  (local to host localhost.localdomain)
           UUID : d627c232:7ff80074:285fe4c7:6fc0a6bf
         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功能;

[[email protected] Desktop]# 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): 
First sector (20973568-41943039, default 20973568): 
Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-41943039, default 41943039): +4G
Partition 2 of type Linux and of size 4 GiB is set
Command (m for help): t
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): fd
Changed type of partition ‘Linux‘ to ‘Linux raid autodetect‘
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] Desktop]# fdisk /dev/sdc
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): 
First sector (20973568-41943039, default 20973568): 
Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-41943039, default 41943039): +4G
Partition 2 of type Linux and of size 4 GiB is set
Command (m for help): t
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): fd
Changed type of partition ‘Linux‘ to ‘Linux raid autodetect‘
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] Desktop]# fdisk /dev/sdd
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):  
First sector (20973568-41943039, default 20973568): 
Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-41943039, default 41943039): +4G
Partition 2 of type Linux and of size 4 GiB is set
Command (m for help): t
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): fd
Changed type of partition ‘Linux‘ to ‘Linux raid autodetect‘
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] Desktop]# partprobe
[[email protected] Desktop]# mdadm -C /dev/md5 -a yes -c 256K -l 5 -n 3 /dev/sdb2 /dev/sdc2 /dev/sdd2
[[email protected] Desktop]# mdadm -D /dev/md5
/dev/md5:
        Version : 1.2
  Creation Time : Wed May 31 23:23:25 2017
     Raid Level : raid5
     Array Size : 8384000 (8.00 GiB 8.59 GB)
  Used Dev Size : 4192000 (4.00 GiB 4.29 GB)
   Raid Devices : 3
  Total Devices : 3
    Persistence : Superblock is persistent
    Update Time : Wed May 31 23:23:46 2017
          State : clean 
 Active Devices : 3
Working Devices : 3
 Failed Devices : 0
  Spare Devices : 0
         Layout : left-symmetric
     Chunk Size : 256K
           Name : localhost.localdomain:5  (local to host localhost.localdomain)
           UUID : 2dd21526:ded34170:a50dd479:4324cab9
         Events : 20
    Number   Major   Minor   RaidDevice State
       0       8       18        0      active sync   /dev/sdb2
       1       8       34        1      active sync   /dev/sdc2
       3       8       50        2      active sync   /dev/sdd2
[[email protected] Desktop]# mke2fs -t ext4 /dev/md5
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=64 blocks, Stripe width=128 blocks
524288 inodes, 2096000 blocks
104800 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2147483648
64 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, 1605632
Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
[[email protected] Desktop]# mkdir /backup
[[email protected] Desktop]# vim /etc/fstab 
[[email protected] Desktop]# tail -1 /etc/fstab 
/dev/md5/backupext4defaults,acl,noatime 0 0
[[email protected] Desktop]# mount -a


Linux系統管理第四周作業【Linux微職位】