1. 程式人生 > 其它 >第七週練習

第七週練習

技術標籤:作業linux

一、破壞mbr表並修復

1.先備份MBR分割槽表

[[email protected] ~]# dd if=/dev/sda of=/data/dpt.img bs=1 count=64 skip=446
64+0 records in
64+0 records out
64 bytes copied, 0.0013962 s, 45.8 kB/s
[[email protected] ~]# scp /data/dpt.img 10.0.0.17:/data/
[[email protected] data]# du -h dpt.img 
4.0K	dpt.img

2.破壞MBR分割槽表,並重啟

[[email protected] ~]# dd if=/dev/zero of=/dev/sda bs=1 count=64 seek=446
[[email protected] ~]#reboot

3.更改啟動項,用光碟啟動,選擇troubleshooting,進入rescue a centos linux,選擇專案3 skip to shell.

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片儲存下來直接上傳(img-BHSNphnI-1611149444155)(C:\Users\MSI-\AppData\Roaming\Typora\typora-user-images\image-20210120152240100.png)]

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片儲存下來直接上傳(img-wPxWM2jf-1611149444165)(C:\Users\MSI-\AppData\Roaming\Typora\typora-user-images\image-20210120152424951.png)]

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片儲存下來直接上傳(img-gbBdzThx-1611149444167)(C:\Users\MSI-\AppData\Roaming\Typora\typora-user-images\image-20210120152521570.png)]

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片儲存下來直接上傳(img-OA7nngts-1611149444174)(C:\Users\MSI-\AppData\Roaming\Typora\typora-user-images\image-20210120152734371.png)]

4.配置網路並修復,最後更改啟動項。

sh-4.4#ifconfig ens160 10.0.0.18/24
sh-4.4#scp 10.0.0.17:/data/dpt.img .

sh-4.4#dd if=dpt.img of=/dev/sda bs=1 seek=446
sh-4.4#exit
sh-4.4#reboot
二、總結RAID的各個級別及其組合方式和效能的不同。

RAID: Redundant Arrays of Inexpensive(Independent) Disks 廉價(獨立)磁碟冗餘陣列

由多塊磁碟合成一個陣列來提供更好的效能,如提高磁碟的並行讀寫能力,磁碟的耐用性和冗餘性等。

RAID的級別有:raid0、raid1、raid4、raid5、raid6、raid10、raid01、raid50等

RAID0: 以 chunk 單位,讀寫資料,讀、寫效能提升,可用空間:N*min(S1,S2,...),沒有容錯能力,最少磁碟數:2, 2+

RAID1:讀效能提升、寫效能略有下降,可用空間:1*min(S1,S2,...),有容錯能力,最少磁碟數:2, 2N

RAID4:多塊資料盤異或運算值存於專用校驗盤,磁碟利用率 (N-1)/N,有冗餘能力,至少3塊磁碟盤才可以實現

RAID5:讀、寫效能提升,可用空間:(N-1)*min,有容錯能力:允許最多1塊磁碟損壞,最少磁碟數:3, 3+

RAID6:讀、寫效能提升,可用空間:(N-2)*min,有容錯能力:允許最多2塊磁碟損壞,最少磁碟數:4, 4+

RAID10:讀、寫效能提升,可用空間:N*min(S1,S2,...)/2,有容錯能力:每組映象最多隻能壞一塊,最少磁碟數:4, 4+

RAID01:多塊磁碟先實現RAID0,再組合成RAID1

RAID50:多塊磁碟先實現RAID5,再組合成RAID0
三、建立一個2G的檔案系統,塊大小為2048byte,預留1%可用空間,檔案系統 ext4,卷標為TEST,要求此分割槽開機後自動掛載至/test目錄,且預設有acl掛載選項

1、先新增一塊20G的硬碟

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片儲存下來直接上傳(img-PIO3n03o-1611149444178)(C:\Users\MSI-\AppData\Roaming\Typora\typora-user-images\image-20210120160724296.png)]

#掃描新增加的20G硬碟
[[email protected] ~]# echo "---" > /sys/class/scsi_host/host16/scan
[[email protected] ~]# cat /proc/partitions 
major minor  #blocks  name

   8        0  209715200 sda
   8        1    1048576 sda1
   8        2  104857600 sda2
   8        3    4194304 sda3
   8        4          1 sda4
   8        5   52428800 sda5
   8       16   20971520 sdb
  11        0    8038400 sr0
[[email protected] ~]# lsblk 
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0  100G  0 part /
├─sda3   8:3    0    4G  0 part [SWAP]
├─sda4   8:4    0    1K  0 part 
└─sda5   8:5    0   50G  0 part /data
sdb      8:16   0   20G  0 disk 
sr0     11:0    1  7.7G  0 rom  

#檢視硬碟資訊
[[email protected] ~]# fdisk -l
Disk /dev/sda: 200 GiB, 214748364800 bytes, 419430400 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
Disklabel type: dos
Disk identifier: 0xce9920ed

Device     Boot     Start       End   Sectors  Size Id Type
/dev/sda1  *         2048   2099199   2097152    1G 83 Linux
/dev/sda2         2099200 211814399 209715200  100G 83 Linux
/dev/sda3       211814400 220203007   8388608    4G 82 Linux swap / Solaris
/dev/sda4       220203008 419430399 199227392   95G  5 Extended
/dev/sda5       220205056 325062655 104857600   50G 83 Linux


Disk /dev/sdb: 20 GiB, 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

2、#將sdb作為操作目標,在sdb磁碟上建立大小為2G的分割槽

#將sdb作為操作目標,在sdb磁碟上建立大小為2G的分割槽
[[email protected] ~]# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.32.1).
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.
Created a new DOS disklabel with disk identifier 0x40d160c9.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039): +2G

Created a new partition 1 of type 'Linux' and of size 2 GiB.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
Command (m for help): p
Disk /dev/sdb: 20 GiB, 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
Disklabel type: dos
Disk identifier: 0x40d160c9

#檢視增加的2G裝置

[[email protected] ~]# lsblk 
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0  100G  0 part /
├─sda3   8:3    0    4G  0 part [SWAP]
├─sda4   8:4    0    1K  0 part 
└─sda5   8:5    0   50G  0 part /data
sdb      8:16   0   20G  0 disk 
└─sdb1   8:17   0    2G  0 part 
sr0     11:0    1  7.7G  0 rom  

3、設定格式為ext4,塊大小2048,卷標為TEST

#格式為ext4,塊大小2048,卷標為TEST
[[email protected] ~]# mkfs.ext4 -b 2048 -m 1 -L TEST /dev/sdb1
mke2fs 1.45.4 (23-Sep-2019)
/dev/sdb1 contains a ext4 file system
	created on Wed Jan 20 17:43:33 2021
Proceed anyway? (y,N) Y
Creating filesystem with 1048576 2k blocks and 131072 inodes
Filesystem UUID: b20fb569-49b6-4cf5-9a27-8461ca7ee6c0
Superblock backups stored on blocks: 
	16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816

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

[[email protected] ~]# blkid 
/dev/sdb1: LABEL="TEST" UUID="b20fb569-49b6-4cf5-9a27-8461ca7ee6c0" TYPE="ext4" PARTUUID="40d160c9-01"
/dev/sda1: UUID="e4ed5ac2-1b02-4cc3-8fd1-ebd299d321b8" TYPE="ext4" PARTUUID="ce9920ed-01"
/dev/sda2: UUID="812abf4b-5d32-4380-a980-a3809d13fffe" TYPE="xfs" PARTUUID="ce9920ed-02"
/dev/sda3: UUID="ccc25742-8377-4ae6-9f90-3f4ec73f37f2" TYPE="swap" PARTUUID="ce9920ed-03"
/dev/sda5: UUID="b5bf8c09-2c50-483d-8ecd-0be7222b9cb3" TYPE="xfs" PARTUUID="ce9920ed-05"
/dev/sr0: UUID="2020-06-08-22-08-25-00" LABEL="CentOS-8-2-2004-x86_64-dvd" TYPE="iso9660" PTUUID="545ce9a4" PTTYPE="dos"

4、設定掛載點,並開機自動掛載到/test,預設選項acl

#設定掛載點,並開機自動掛載到/test,預設選項acl
[[email protected] ~]# mkdir /test
[[email protected] ~]# echo "/dev/sdb1 /test ext4 acl 0 0 " >> /etc/fstab 
[[email protected] ~]# mount -a
[[email protected] ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        964M     0  964M   0% /dev
tmpfs           981M     0  981M   0% /dev/shm
tmpfs           981M  8.7M  972M   1% /run
tmpfs           981M     0  981M   0% /sys/fs/cgroup
/dev/sda2       100G  2.5G   98G   3% /
/dev/sda5        50G  778M   50G   2% /data
/dev/sda1       976M  104M  806M  12% /boot
tmpfs           197M     0  197M   0% /run/user/0
/dev/sdb1       2.0G  9.1M  2.0G   1% /test
[[email protected] ~]# cat /etc/fstab 
UUID=812abf4b-5d32-4380-a980-a3809d13fffe /                       xfs     defaults        0 0
UUID=e4ed5ac2-1b02-4cc3-8fd1-ebd299d321b8 /boot                   ext4    defaults        1 2
UUID=b5bf8c09-2c50-483d-8ecd-0be7222b9cb3 /data                   xfs     defaults        0 0
UUID=ccc25742-8377-4ae6-9f90-3f4ec73f37f2 swap                    swap    defaults        0 0
/dev/sdb1                                /test                    ext4    acl             0 0 

#重啟測試
WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Wed Jan 20 17:22:26 2021 from 192.168.2.102
[[email protected] ~]# lsblk 
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0  100G  0 part /
├─sda3   8:3    0    4G  0 part [SWAP]
├─sda4   8:4    0    1K  0 part 
└─sda5   8:5    0   50G  0 part /data
sdb      8:16   0   20G  0 disk 
└─sdb1   8:17   0    2G  0 part /test
sr0     11:0    1  7.7G  0 rom  

四、建立一個至少有兩個PV組成的大小為20G的名為testvg的VG;要求PE大小 為16MB, 而後在卷組中建立大小為5G的邏輯卷testlv;掛載至/users目錄

1、先新增一塊20G的硬碟,sdc

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片儲存下來直接上傳(img-BACyrZjn-1611149444181)(C:\Users\MSI-\AppData\Roaming\Typora\typora-user-images\image-20210120203644843.png)]

[[email protected] ~]# echo "- - -" > /sys/class/scsi_host/host32/scan
[[email protected] ~]# cat /proc/partitions 
major minor  #blocks  name

   8       16   20971520 sdb
   8       17    2097152 sdb1
   8        0  209715200 sda
   8        1    1048576 sda1
   8        2  104857600 sda2
   8        3    4194304 sda3
   8        4          1 sda4
   8        5   52428800 sda5
  11        0    8038400 sr0
   8       32   20971520 sdc
[[email protected] ~]# lsblk 
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0  100G  0 part /
├─sda3   8:3    0    4G  0 part [SWAP]
├─sda4   8:4    0    1K  0 part 
└─sda5   8:5    0   50G  0 part /data
sdb      8:16   0   20G  0 disk 
└─sdb1   8:17   0    2G  0 part /test
sdc      8:32   0   20G  0 disk 
sr0     11:0    1  7.7G  0 rom  

2、規劃使用sdb和sdc兩塊磁碟來操作,分別取兩個10G大小的空間。sdb2、sdc1

[[email protected] ~]# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.32.1).
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 (container for logical partitions)
Select (default p): 

Using default response p.
Partition number (2-4, default 2): 
First sector (4196352-41943039, default 4196352): 
Last sector, +sectors or +size{K,M,G,T,P} (4196352-41943039, default 41943039): +10G

Created a new partition 2 of type 'Linux' and of size 10 GiB.

Command (m for help): p
Disk /dev/sdb: 20 GiB, 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
Disklabel type: dos
Disk identifier: 0x40d160c9

Device     Boot   Start      End  Sectors Size Id Type
/dev/sdb1          2048  4196351  4194304   2G 83 Linux
/dev/sdb2       4196352 25167871 20971520  10G 83 Linux

Command (m for help): w
The partition table has been altered.
Syncing disks.


[[email protected] ~]# fdisk /dev/sdc 

Welcome to fdisk (util-linux 2.32.1).
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.
Created a new DOS disklabel with disk identifier 0x433afee5.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): 

Using default response p.
Partition number (1-4, default 1): 
First sector (2048-41943039, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039): +10G

Created a new partition 1 of type 'Linux' and of size 10 GiB.

Command (m for help): p
Disk /dev/sdc: 20 GiB, 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
Disklabel type: dos
Disk identifier: 0x433afee5

Device     Boot Start      End  Sectors Size Id Type
/dev/sdc1        2048 20973567 20971520  10G 83 Linux

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

[[email protected] ~]# lsblk 
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0  100G  0 part /
├─sda3   8:3    0    4G  0 part [SWAP]
├─sda4   8:4    0    1K  0 part 
└─sda5   8:5    0   50G  0 part /data
sdb      8:16   0   20G  0 disk 
├─sdb1   8:17   0    2G  0 part /test
└─sdb2   8:18   0   10G  0 part 
sdc      8:32   0   20G  0 disk 
└─sdc1   8:33   0   10G  0 part 
sr0     11:0    1  7.7G  0 rom 

3、建立PV,並檢視建立的PV

[[email protected] ~]#yum install lvm2 -y

[[email protected] ~]# pvcreate /dev/sdb
sdb   sdb1  sdb2  
[[email protected] ~]# pvcreate /dev/sdb
sdb   sdb1  sdb2  
[[email protected] ~]# pvcreate /dev/sdb2
  Physical volume "/dev/sdb2" successfully created.
[[email protected] ~]# pvcreate /dev/sdc1
  Physical volume "/dev/sdc1" successfully created.
[[email protected] ~]# pvs
  PV         VG Fmt  Attr PSize  PFree 
  /dev/sdb2     lvm2 ---  10.00g 10.00g
  /dev/sdc1     lvm2 ---  10.00g 10.00g

4、建立名為testvg的VG,PE大小 為16MB,

#建立VG
[[email protected] ~]# vgcreate testvg -s 16M /dev/sdb2 /dev/sdc1
  Volume group "testvg" successfully created

#檢視VG, PE Size               16.00 MiB                              
[[email protected] ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree  
  testvg   2   0   0 wz--n- <19.97g <19.97g
[[email protected] ~]# vgdisplay testvg
  --- Volume group ---
  VG Name               testvg
  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.97 GiB
  PE Size               16.00 MiB                               #pe大小16M 
  Total PE              1278
  Alloc PE / Size       0 / 0   
  Free  PE / Size       1278 / <19.97 GiB
  VG UUID               ZJd68K-KSS5-inC2-MAZi-6LQt-203d-hl9UGQ
   

5、在卷組中建立大小為5G的邏輯卷testlv;掛載至/users目錄

#建立大小為5G的邏輯卷testlv
[email protected] ~]# lvcreate -L 5G -n testvg testvg
  Logical volume "testvg" created.
[[email protected] ~]# lvs
  LV     VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  testvg testvg -wi-a----- 5.00g                                                    
[[email protected] ~]# lvcreate -L 5G -n testlv testvg
  Logical volume "testlv" created.
[[email protected] ~]# lvs
  LV     VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  testlv testvg -wi-a----- 5.00g                                                    
  testvg testvg -wi-a----- 5.00g                                                    
[[email protected] ~]# lvremove /dev/testvg/testvg 
Do you really want to remove active logical volume testvg/testvg? [y/n]: y
  Logical volume "testvg" successfully removed
[[email protected] ~]# lvs
  LV     VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  testlv testvg -wi-a----- 5.00g       
  
#檢視卷組資訊,  LV Size                5.00 GiB
  [[email protected] ~]# lvdisplay /dev/testvg/testlv 
  --- Logical volume ---
  LV Path                /dev/testvg/testlv
  LV Name                testlv
  VG Name                testvg
  LV UUID                eqfOys-mhxg-3Uce-1KWv-MLax-CoGL-ni9gYG
  LV Write Access        read/write
  LV Creation host, time n51c8, 2021-01-20 21:00:27 +0800
  LV Status              available
  # open                 0
  LV Size                5.00 GiB
  Current LE             320
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

#格式化卷組/dev/testvg/testlv  為ext4格式 
[[email protected] ~]# mkfs.ext4 /dev/testvg/testlv 
mke2fs 1.45.4 (23-Sep-2019)
Creating filesystem with 1310720 4k blocks and 327680 inodes
Filesystem UUID: c47c3e5e-44cc-4353-a499-69205e01c9b0
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 

#建立掛載點/users,並開機自動掛載
[[email protected] ~]# 
[[email protected] ~]# mkdir /users

[[email protected] ~]# echo "/dev/testvg/testlv /users ext4  defaults 0 0"  >> /etc/fstab 
[[email protected] ~]# mount -a
[[email protected] ~]# df -h
Filesystem                 Size  Used Avail Use% Mounted on
devtmpfs                   964M     0  964M   0% /dev
tmpfs                      981M  8.0K  981M   1% /dev/shm
tmpfs                      981M  8.7M  972M   1% /run
tmpfs                      981M     0  981M   0% /sys/fs/cgroup
/dev/sda2                  100G  2.5G   98G   3% /
/dev/sda5                   50G  778M   50G   2% /data
/dev/sdb1                  2.0G  9.1M  2.0G   1% /test
/dev/sda1                  976M  104M  806M  12% /boot
tmpfs                      197M     0  197M   0% /run/user/0
/dev/mapper/testvg-testlv  4.9G   20M  4.6G   1% /users
[[email protected] ~]# lsblk 
NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                 8:0    0  200G  0 disk 
├─sda1              8:1    0    1G  0 part /boot
├─sda2              8:2    0  100G  0 part /
├─sda3              8:3    0    4G  0 part [SWAP]
├─sda4              8:4    0    1K  0 part 
└─sda5              8:5    0   50G  0 part /data
sdb                 8:16   0   20G  0 disk 
├─sdb1              8:17   0    2G  0 part /test
└─sdb2              8:18   0   10G  0 part 
sdc                 8:32   0   20G  0 disk 
└─sdc1              8:33   0   10G  0 part 
  └─testvg-testlv 253:1    0    5G  0 lvm  /users
sr0                11:0    1  7.7G  0 rom  

#
[[email protected] ~]# cat /etc/fstab | grep -v "#"

UUID=812abf4b-5d32-4380-a980-a3809d13fffe /                       xfs     defaults        0 0
UUID=e4ed5ac2-1b02-4cc3-8fd1-ebd299d321b8 /boot                   ext4     defaults        1 2
UUID=b5bf8c09-2c50-483d-8ecd-0be7222b9cb3 /data                   xfs       defaults        0 0
UUID=ccc25742-8377-4ae6-9f90-3f4ec73f37f2 swap                     swap    defaults        0 0
/dev/sdb1                           /test                       ext4     acl         0 0 
/dev/testvg/testlv /users ext4  defaults 0 0