Linux磁盤及文件系統基礎操作

## Linux磁盤及文件系統基礎操作

1、破壞mbr表並修復

默認安裝的Centos系統,查看磁盤及文件系統信息:

]# fdisk -l

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: 0xf49418ad

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
/dev/sdb2         4196352     6293503     1048576   83  Linux
/dev/sdb3         6293504     8390655     1048576   83  Linux

Disk /dev/sda: 128.8 GB, 128849018880 bytes, 251658240 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: 0x000c9352

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   251658239   124779520   8e  Linux LVM

Disk /dev/mapper/centos-root: 53.7 GB, 53687091200 bytes, 104857600 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 /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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 /dev/mapper/centos-home: 71.9 GB, 71932313600 bytes, 140492800 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

# df -T
Filesystem              Type     1K-blocks    Used Available Use% Mounted on
devtmpfs                devtmpfs    448976       0    448976   0% /dev
tmpfs                   tmpfs       460992       0    460992   0% /dev/shm
tmpfs                   tmpfs       460992    7412    453580   2% /run
tmpfs                   tmpfs       460992       0    460992   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs       52403200 1282788  51120412   3% /
/dev/sda1               xfs        1038336  140080    898256  14% /boot
/dev/mapper/centos-home xfs       70212100   33004  70179096   1% /home
tmpfs                   tmpfs        92200       0     92200   0% /run/user/0

# hexdump -C -n 512 /dev/sda
00000000  eb 63 90 10 8e d0 bc 00  b0 b8 00 00 8e d8 8e c0  |.c..............|
00000010  fb be 00 7c bf 00 06 b9  00 02 f3 a4 ea 21 06 00  |...|.........!..|
00000020  00 be be 07 38 04 75 0b  83 c6 10 81 fe fe 07 75  |....8.u........u|
00000030  f3 eb 16 b4 02 b0 01 bb  00 7c b2 80 8a 74 01 8b  |.........|...t..|
00000040  4c 02 cd 13 ea 00 7c 00  00 eb fe 00 00 00 00 00  |L.....|.........|
00000050  00 00 00 00 00 00 00 00  00 00 00 80 01 00 00 00  |................|
00000060  00 00 00 00 ff fa 90 90  f6 c2 80 74 05 f6 c2 70  |...........t...p|
00000070  74 02 b2 80 ea 79 7c 00  00 31 c0 8e d8 8e d0 bc  |t....y|..1......|
00000080  00 20 fb a0 64 7c 3c ff  74 02 88 c2 52 be 05 7c  |. ..d|<.t...R..||
00000090  b4 41 bb aa 55 cd 13 5a  52 72 3d 81 fb 55 aa 75  |.A..U..ZRr=..U.u|
000000a0  37 83 e1 01 74 32 31 c0  89 44 04 40 88 44 ff 89  |[email protected]..|
000000b0  44 02 c7 04 10 00 66 8b  1e 5c 7c 66 89 5c 08 66  |D.....f..\|f.\.f|
000000c0  8b 1e 60 7c 66 89 5c 0c  c7 44 06 00 70 b4 42 cd  |..`|f.\..D..p.B.|
000000d0  13 72 05 bb 00 70 eb 76  b4 08 cd 13 73 0d 5a 84  |.r...p.v....s.Z.|
000000e0  d2 0f 83 de 00 be 85 7d  e9 82 00 66 0f b6 c6 88  |.......}...f....|
000000f0  64 ff 40 66 89 44 04 0f  b6 d1 c1 e2 02 88 e8 88  |[email protected]..........|
00000100  f4 40 89 44 08 0f b6 c2  c0 e8 02 66 89 04 66 a1  |[email protected].|
00000110  60 7c 66 09 c0 75 4e 66  a1 5c 7c 66 31 d2 66 f7  |`|f..uNf.\|f1.f.|
00000120  34 88 d1 31 d2 66 f7 74  04 3b 44 08 7d 37 fe c1  |4..1.f.t.;D.}7..|
00000130  88 c5 30 c0 c1 e8 02 08  c1 88 d0 5a 88 c6 bb 00  |..0........Z....|
00000140  70 8e c3 31 db b8 01 02  cd 13 72 1e 8c c3 60 1e  |p..1......r...`.|
00000150  b9 00 01 8e db 31 f6 bf  00 80 8e c6 fc f3 a5 1f  |.....1..........|
00000160  61 ff 26 5a 7c be 80 7d  eb 03 be 8f 7d e8 34 00  |a.&Z|..}....}.4.|
00000170  be 94 7d e8 2e 00 cd 18  eb fe 47 52 55 42 20 00  |..}.......GRUB .|
00000180  47 65 6f 6d 00 48 61 72  64 20 44 69 73 6b 00 52  |Geom.Hard Disk.R|
00000190  65 61 64 00 20 45 72 72  6f 72 0d 0a 00 bb 01 00  |ead. Error......|
000001a0  b4 0e cd 10 ac 3c 00 75  f4 c3 00 00 00 00 00 00  |.....<.u........|
000001b0  00 00 00 00 00 00 00 00  52 93 0c 00 00 00 80 20  |........R...... |
000001c0  21 00 83 aa 28 82 00 08  00 00 00 00 20 00 00 aa  |!...(....... ...|
000001d0  29 82 8e fe ff ff 00 08  20 00 00 f8 df 0e 00 00  |)....... .......|
000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200

備份MBR信息

# dd if=/dev/sda of=/root/mbr.bk bs=1 count=512
512+0 records in
512+0 records out
512 bytes (512 B) copied, 0.00280891 s, 182 kB/s

將mbr.bk保存至遠程電腦

# scp mbr.bk [email protected]:/data
The authenticity of host '172.20.200.130 (172.20.200.130)' can't be established.
ECDSA key fingerprint is SHA256:ucc5uQ4jfEPKzZniGQb2KT4NzAzFYC0wIIvgmTpjzdw.
ECDSA key fingerprint is MD5:8f:3e:26:10:15:4a:55:a1:61:2b:eb:cb:de:5a:75:63.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.20.200.130' (ECDSA) to the list of known hosts.
[email protected]'s password:
mbr.bk                       

破壞本地磁盤mbr信息

# dd if=/dev/zero of=/dev/sda bs=1 count=64 seek=446
64+0 records in
64+0 records out
64 bytes (64 B) copied, 0.000808557 s, 79.2 kB/s

查看破壞後的mbr信息

# hexdump -C -n 512 /dev/sda
00000000  eb 63 90 10 8e d0 bc 00  b0 b8 00 00 8e d8 8e c0  |.c..............|
00000010  fb be 00 7c bf 00 06 b9  00 02 f3 a4 ea 21 06 00  |...|.........!..|
00000020  00 be be 07 38 04 75 0b  83 c6 10 81 fe fe 07 75  |....8.u........u|
00000030  f3 eb 16 b4 02 b0 01 bb  00 7c b2 80 8a 74 01 8b  |.........|...t..|
00000040  4c 02 cd 13 ea 00 7c 00  00 eb fe 00 00 00 00 00  |L.....|.........|
00000050  00 00 00 00 00 00 00 00  00 00 00 80 01 00 00 00  |................|
00000060  00 00 00 00 ff fa 90 90  f6 c2 80 74 05 f6 c2 70  |...........t...p|
00000070  74 02 b2 80 ea 79 7c 00  00 31 c0 8e d8 8e d0 bc  |t....y|..1......|
00000080  00 20 fb a0 64 7c 3c ff  74 02 88 c2 52 be 05 7c  |. ..d|<.t...R..||
00000090  b4 41 bb aa 55 cd 13 5a  52 72 3d 81 fb 55 aa 75  |.A..U..ZRr=..U.u|
000000a0  37 83 e1 01 74 32 31 c0  89 44 04 40 88 44 ff 89  |[email protected]..|
000000b0  44 02 c7 04 10 00 66 8b  1e 5c 7c 66 89 5c 08 66  |D.....f..\|f.\.f|
000000c0  8b 1e 60 7c 66 89 5c 0c  c7 44 06 00 70 b4 42 cd  |..`|f.\..D..p.B.|
000000d0  13 72 05 bb 00 70 eb 76  b4 08 cd 13 73 0d 5a 84  |.r...p.v....s.Z.|
000000e0  d2 0f 83 de 00 be 85 7d  e9 82 00 66 0f b6 c6 88  |.......}...f....|
000000f0  64 ff 40 66 89 44 04 0f  b6 d1 c1 e2 02 88 e8 88  |[email protected]..........|
00000100  f4 40 89 44 08 0f b6 c2  c0 e8 02 66 89 04 66 a1  |[email protected].|
00000110  60 7c 66 09 c0 75 4e 66  a1 5c 7c 66 31 d2 66 f7  |`|f..uNf.\|f1.f.|
00000120  34 88 d1 31 d2 66 f7 74  04 3b 44 08 7d 37 fe c1  |4..1.f.t.;D.}7..|
00000130  88 c5 30 c0 c1 e8 02 08  c1 88 d0 5a 88 c6 bb 00  |..0........Z....|
00000140  70 8e c3 31 db b8 01 02  cd 13 72 1e 8c c3 60 1e  |p..1......r...`.|
00000150  b9 00 01 8e db 31 f6 bf  00 80 8e c6 fc f3 a5 1f  |.....1..........|
00000160  61 ff 26 5a 7c be 80 7d  eb 03 be 8f 7d e8 34 00  |a.&Z|..}....}.4.|
00000170  be 94 7d e8 2e 00 cd 18  eb fe 47 52 55 42 20 00  |..}.......GRUB .|
00000180  47 65 6f 6d 00 48 61 72  64 20 44 69 73 6b 00 52  |Geom.Hard Disk.R|
00000190  65 61 64 00 20 45 72 72  6f 72 0d 0a 00 bb 01 00  |ead. Error......|
000001a0  b4 0e cd 10 ac 3c 00 75  f4 c3 00 00 00 00 00 00  |.....<.u........|
000001b0  00 00 00 00 00 00 00 00  52 93 0c 00 00 00 00 00  |........R.......|
000001c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200
# reboot  #由於mbr信息被破壞,進入維護模式

Linux磁盤及文件系統基礎操作
Linux磁盤及文件系統基礎操作
Linux磁盤及文件系統基礎操作

# ip add add 172.20.200.11/24 dev ens33
# scp [email protected]:/data/mbr.bk

# dd if=mbr.bk of=/dev/sda bs=1 count=512
# reboot   #恢復完成

Linux磁盤及文件系統基礎操作
Linux磁盤及文件系統基礎操作

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

Linux磁盤及文件系統基礎操作

★標準RAID

◇RAID 0

  • 優點:使用 n 顆硬盤,即可擁有將近 n 倍的讀寫效能。
  • 缺點:數據安全性較低,同組數組中任一硬盤發生問題就會造成數據遺失。
  • 硬盤數量:最少 2 個。

Linux磁盤及文件系統基礎操作
◇RAID 1

  • 優點:安全性依照數組裏的實體硬盤數量倍數成長。
  • 缺點:空間利用率是所有 RAID 中最沒有效率的。
  • 硬盤數量:最少 2 個。
    Linux磁盤及文件系統基礎操作

在 RAID 0 追求效能極端的彼岸,存在着強調數據安全性的RAID 1,RAID 1 的數據儲存方式恰好與 RAID 0相反,欲寫入硬盤的數據被複製成與硬盤相同數量的份數,分別寫入數組中的各顆硬盤。如此一來每個硬盤內都擁有着同份數據的不同拷貝,只要數組中的其中 1 顆硬盤還可使用,數據就不會遺失,可忍受 n-1 顆硬盤同時損毀(n 爲數組中實體硬盤的個數)。

效能部分則分爲寫入和讀取 2 種不同狀況,寫入時因爲須將數據複製多份寫入各顆硬盤,寫入效能與單顆硬盤差不多;讀取時則能夠同時從各顆硬盤同時讀取數據的不同部分,所以能夠和 RAID 0 相提並論,約是單顆硬盤讀取效能乘上數組中的硬盤個數,不過也有以單顆硬盤讀取的設計。
Linux磁盤及文件系統基礎操作Linux磁盤及文件系統基礎操作

數組可使用容量是 RAID 1 最大的缺點,由於各實體硬盤中的數據幾乎是以鏡像的方式儲存,因此無論數組中放入了多少顆硬盤,可用容量都是以最小的那顆硬盤爲主;同 RAID 0,如果數組中有着硬盤容量不一的情形,大硬盤的剩餘容量能否可利用端看控制器的設計,之後的 RAID 模式也是如此,將不再贅述。

◇RAID 5

  • 優點:兼顧空間利用率與安全性。
  • 缺點:需要額外的運算資源,僅能忍受 1 個硬盤損毀。
  • 硬盤數量:至少 3 個。

Linux磁盤及文件系統基礎操作
◇RAID 6

  • 優點:容錯硬盤數量比 RAID 5 多 1 顆。
  • 缺點:運算量比 RAID 5 大、空間利用率比 RAID 5 低。
  • 硬盤數量:至少 4 個。

Linux磁盤及文件系統基礎操作

◇RAID 2、3、4 呢?

RAID2、3、4較少實際應用,它們大多隻在研究領域有實作。

★混合RAID

◇JBOD

JBOD 爲 ”Just a Bunch Of Disks” 的縮寫,意思就是「一堆硬盤」,指的是RAID 磁盤陣列以外的所有組態。

Linux磁盤及文件系統基礎操作

實作上 JBOD 有 2 種方式,第一種與我們直接將硬盤安裝在計算機中相同,各顆實體各自管理自己的容量,當然也可進行磁盤區分割。第二種則是能夠將不同實體硬盤的空間串在一起,例如 2 顆 1TB 硬盤串成 1 個 2TB 的空間運用,如此能夠放入大於 1TB 的單一檔案。

◇RAID 7

RAID 7並非公開的RAID標準,而是Storage Computer Corporation的專利硬件產品名稱。

RAID 7的效能超越了許多其他RAID標準的實做產品,但也因爲如此,在價格方面非常的高昂。

◇RAID N+N

在廠商支持的情況下,使用者甚至可以將 2 種以上的 RAID 組態放在同 1組磁盤陣列內,也就是有時可以看到的雙位數 RAID 01、10、50、60……等。

建立的方式也很好理解,首先利用前位數字的 RAID 方式建立數組,接着再將後方數字所代表的數組建立其上。

Linux磁盤及文件系統基礎操作

譬如 RAID 10 就是先建立 2 組 RAID 1,接着這 2 組 RAID 1 再組合成 RAID 0;RAID50 就是先建立 2 組 RAID 5,接着再組合成 1 組 RAID 0。

當然,這種組合等級的成本一般都非常昂貴,因此只在少數特定場合應用。

3、創建一個2G的文件系統,塊大小爲2048byte,預留1%可用空間,文件系統 ext4,卷標爲TEST,要求此分區開機後自動掛載至/test目錄,且默認有acl掛載選項

# mkdir /test  #創建掛載點
# fdisk -l    #查看磁盤信息

Disk /dev/sda: 128.8 GB, 128849018880 bytes, 251658240 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: 0x000c9352

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   251658239   124779520   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
Disk label type: dos
Disk identifier: 0xf49418ad

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
/dev/sdb2         4196352     6293503     1048576   83  Linux
/dev/sdb3         6293504     8390655     1048576   83  Linux

Disk /dev/mapper/centos-root: 53.7 GB, 53687091200 bytes, 104857600 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 /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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 /dev/mapper/centos-home: 71.9 GB, 71932313600 bytes, 140492800 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 ~]# fdisk  /dev/sdb    #將磁盤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): d
Partition number (1-3, default 3): 1
Partition 1 is deleted

Command (m for help): d
Partition number (2,3, default 3): 2
Partition 2 is deleted

Command (m for help): d
Selected partition 3
Partition 3 is deleted
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 (63-41943039, default 63):
Using default value 63
Last sector, +sectors or +size{K,M,G} (63-41943039, default 41943039): +2G
Partition 1 of type Linux and of size 2 GiB is set

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# fdisk -l
Disk /dev/sda: 128.8 GB, 128849018880 bytes, 251658240 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: 0x000c9352

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   251658239   124779520   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
Disk label type: dos
Disk identifier: 0xf49418ad

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1              63     4194367     2097152+  83  Linux

Disk /dev/mapper/centos-root: 53.7 GB, 53687091200 bytes, 104857600 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 /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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 /dev/mapper/centos-home: 71.9 GB, 71932313600 bytes, 140492800 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
# mke2fs -t ext4 -L TEST -m 1 -b 2048  /dev/sdb1  #創建文件系統
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=TEST
OS type: Linux
Block size=2048 (log=1)
Fragment size=2048 (log=1)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 1048576 blocks
10485 blocks (1.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=269484032
64 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

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
# mount -o acl /dev/sdb1 /test   #手動掛載文件系統
# dumpe2fs /dev/sdb1             #查看文件系統信息
dumpe2fs 1.42.9 (28-Dec-2013)
Filesystem volume name:   TEST
Last mounted on:          /test
Filesystem UUID:          22f10ffd-935e-421f-bbf6-1ee912c81b62
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery extent 64bit flex_bg sparse_super huge_file uninit_bg dir_nlink extra_isize
Filesystem flags:         signed_directory_hash
Default mount options:    user_xattr acl
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              131072
Block count:              1048576
Reserved block count:     10485
Free blocks:              994651
Free inodes:              131061
First block:              0
Block size:               2048
Fragment size:            2048
Group descriptor size:    64

# blkid /dev/sdb1
/dev/sdb1: LABEL="TEST" UUID="22f10ffd-935e-421f-bbf6-1ee912c81b62" TYPE="ext4"

# vim /etc/fstab
# /etc/fstab    
# Created by anaconda on Tue Jan  5 01:15:38 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=ff1c2699-9446-44cc-84fd-76cd0ec08f11 /boot                   xfs     defaults        0 0
/dev/mapper/centos-home /home                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
UUID=22f10ffd-935e-421f-bbf6-1ee912c81b62 /test      ext4    acl             0 0

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

在/dev/sdb磁盤上增加兩個分區

# fdisk -l /dev/sdb   #查看磁盤當前狀態

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: 0xf49418ad

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1              63     4194367     2097152+  83  Linux

# 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 (4194368-41943039, default 4196352):
Using default value 4196352
Last sector, +sectors or +size{K,M,G} (4196352-41943039, default 41943039): +10G
Partition 2 of type Linux and of size 10 GiB is set

Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3):
First sector (4194368-41943039, default 25167872):
Using default value 25167872
Last sector, +sectors or +size{K,M,G} (25167872-41943039, default 41943039):
Using default value 41943039
Partition 3 of type Linux and of size 8 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.
# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.

創建pv

]# pvcreate /dev/sdb2
  Physical volume "/dev/sdb2" successfully created.
[root@localhost test]# pvcreate /dev/sdb3
  Physical volume "/dev/sdb3" successfully created.
]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sdb2
  VG Name               testvg
  PV Size               10.00 GiB / not usable 16.00 MiB
  Allocatable           yes
  PE Size               16.00 MiB
  Total PE              639
  Free PE               639
  Allocated PE          0
  PV UUID               u4Kbz2-BSaN-nv2h-PWxq-hXqL-78XB-JdIta2

  --- Physical volume ---
  PV Name               /dev/sdb3
  VG Name               testvg
  PV Size               <8.00 GiB / not usable 15.00 MiB
  Allocatable           yes
  PE Size               16.00 MiB
  Total PE              511
  Free PE               511
  Allocated PE          0
  PV UUID               Rw2kLK-nVTu-oSUp-b3Oa-HlTM-fisb-m9pe5t

  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <119.00 GiB / not usable 3.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              30463
  Free PE               1
  Allocated PE          30462
  PV UUID               efozR1-vEot-NYF6-oBy4-HC6c-mR01-2nyuif

創建vg

]# vgcreate -s 16M testvg /dev/sdb2 /dev/sdb3
# 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               <17.97 GiB
  PE Size               16.00 MiB
  Total PE              1150
  Alloc PE / Size       0 / 0
  Free  PE / Size       1150 / <17.97 GiB
  VG UUID               iZCsJG-6NOm-3Hvi-lv4i-rIds-3U9I-3m38jS

創建lv

# lvcreate -L 5G -n testlv testvg
  Logical volume "testlv" created.
[root@localhost test]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/testvg/testlv
  LV Name                testlv
  VG Name                testvg
  LV UUID                TpjH6V-zh7z-06TT-VjtN-q04v-UYcz-f8mLzB
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2021-01-05 07:30:46 -0500
  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:3

創建文件系統

# mke2fs -t ext4 /dev/testvg/testlv
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 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

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

掛載至/users

# mkdir /users
# mount /dev/testvg/testlv /users
# df -Th
Filesystem                Type      Size  Used Avail Use% Mounted on
devtmpfs                  devtmpfs  439M     0  439M   0% /dev
tmpfs                     tmpfs     451M     0  451M   0% /dev/shm
tmpfs                     tmpfs     451M  7.3M  443M   2% /run
tmpfs                     tmpfs     451M     0  451M   0% /sys/fs/cgroup
/dev/mapper/centos-root   xfs        50G  1.3G   49G   3% /
/dev/sdb1                 ext4      2.0G  9.1M  1.9G   1% /test
/dev/sda1                 xfs      1014M  137M  878M  14% /boot
/dev/mapper/centos-home   xfs        67G   33M   67G   1% /home
tmpfs                     tmpfs      91M     0   91M   0% /run/user/0
/dev/mapper/testvg-testlv ext4      4.8G   20M  4.6G   1% /users

設置成自動掛載

# vi /etc/fstab
# /etc/fstab
# Created by anaconda on Tue Jan  5 01:15:38 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=ff1c2699-9446-44cc-84fd-76cd0ec08f11 /boot                   xfs     defaults        0 0
/dev/mapper/centos-home /home                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
UUID=22f10ffd-935e-421f-bbf6-1ee912c81b62 /test     ext4    acl             0 0
/dev/testvg/testlv  /users  ext4  defaults  0 0

# mount -a
# df -h
Filesystem                 Size  Used Avail Use% Mounted on
devtmpfs                   439M     0  439M   0% /dev
tmpfs                      451M     0  451M   0% /dev/shm
tmpfs                      451M  7.3M  443M   2% /run
tmpfs                      451M     0  451M   0% /sys/fs/cgroup
/dev/mapper/centos-root     50G  1.3G   49G   3% /
/dev/sda1                 1014M  137M  878M  14% /boot
/dev/sdb1                  2.0G  9.1M  1.9G   1% /test
/dev/mapper/centos-home     67G   33M   67G   1% /home
tmpfs                       91M     0   91M   0% /run/user/0
/dev/mapper/testvg-testlv  4.8G   20M  4.6G   1% /users        #不能使用UUID
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章