2018年9月22日 星期六

【Linux】使用LVM合併硬碟空間


LVM簡介

Logical Volume Manager,縮寫為LVM,為邏輯捲軸管理員,主要分四層。
PV、VG、PE、LV。
本文在介紹如何使用VLM來管理PV、VG、PE、LV這幾個物件,讓你的兩顆硬碟變成一顆大硬碟。
下圖為LVM的結構,LV可以搭載著檔案系統,VG是將不同的硬碟變成一個群組(LV依附在VG身上),PV則是LVM的基本單位,可以是整顆硬碟,也可以是分割區(VG依附在PV身上)。

圖片取自維基百科



安裝LVM套件

$apt-get install lvm2 dmsetup mdadm reiserfsprogs xfsprogs


查看磁碟


由下列可知,共有兩顆硬碟,分別為sdasdb
其中sda已經被分割過了,分成sda1

$lsblk
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda           8:0    0 931.5G  0 disk
└─sda1        8:1    0 931.5G  0 part
sdb           8:16   0 931.5G  0 disk


管理硬碟


接下來要刪除sda1分割區,並重新分割成LVM分割區。

$fdisk /dev/sda

Welcome to fdisk (util-linux 2.29.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): m  #指令查詢

Help:

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table


Command (m for help): p  #顯示磁碟分割區
Disk /dev/sda: 931.5 GiB, 1000204886016 bytes, 1953525168 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: gpt
Disk identifier: C25BB6AD-BEB0-43F5-BDA2-34B067A5BEE5

Device     Start        End    Sectors   Size Type
/dev/sda1   2048 1953523711 1953521664 931.5G Microsoft basic data

Command (m for help): d  #刪除sda1分割區
Selected partition 1
Partition 1 has been deleted.

Command (m for help): n  #新增sda1分割區
Partition number (1-128, default 1):  #Enter
First sector (34-1953525134, default 2048): #Enter
Last sector, +sectors or +size{K,M,G,T,P} (2048-1953525134, default 1953525134): [分割區大小]
#如:
#900G
#輸入Enter直接將最大磁碟空間作為分割區
Created a new partition 1 of type 'Linux filesystem' and of size 931.5 GiB.
Partition #1 contains a ntfs signature.

Do you want to remove the signature? [Y]es/[N]o: Y

The signature will be removed by a write command.

Command (m for help): t  #顯示分割區資訊
Selected partition 1
Hex code (type L to list all codes): L 
  1 EFI System                     C12A7328-F81F-11D2-BA4B-00A0C93EC93B
  2 MBR partition scheme           024DEE41-33E7-11D3-9D69-0008C781F39F
  3 Intel Fast Flash               D3BFE2DE-3DAF-11DF-BA40-E3A556D89593
  4 BIOS boot                      21686148-6449-6E6F-744E-656564454649
  5 Sony boot partition            F4019732-066E-4E12-8273-346C5641494F
  6 Lenovo boot partition          BFBFAFE7-A34F-448A-9A5B-6213EB736C22
  7 PowerPC PReP boot              9E1A2D38-C612-4316-AA26-8B49521E5A8B
  8 ONIE boot                      7412F7D5-A156-4B13-81DC-867174929325
  9 ONIE config                    D4E6E2CD-4469-46F3-B5CB-1BFF57AFC149
 10 Microsoft reserved             E3C9E316-0B5C-4DB8-817D-F92DF00215AE
 11 Microsoft basic data           EBD0A0A2-B9E5-4433-87C0-68B6B72699C7
 12 Microsoft LDM metadata         5808C8AA-7E8F-42E0-85D2-E1E90434CFB3
 13 Microsoft LDM data             AF9B60A0-1431-4F62-BC68-3311714A69AD
 14 Windows recovery environment   DE94BBA4-06D1-4D40-A16A-BFD50179D6AC
 15 IBM General Parallel Fs        37AFFC90-EF7D-4E96-91C3-2D7AE055B174
 16 Microsoft Storage Spaces       E75CAF8F-F680-4CEE-AFA3-B001E56EFC2D
 17 HP-UX data                     75894C1E-3AEB-11D3-B7C1-7B03A0000000
 18 HP-UX service                  E2A1E728-32E3-11D6-A682-7B03A0000000
 19 Linux swap                     0657FD6D-A4AB-43C4-84E5-0933C84B4F4F
 20 Linux filesystem               0FC63DAF-8483-4772-8E79-3D69D8477DE4
 21 Linux server data              3B8F8425-20E0-4F3B-907F-1A25A76F98E8
 22 Linux root (x86)               44479540-F297-41B2-9AF7-D131D5F0458A
 23 Linux root (ARM)               69DAD710-2CE4-4E3C-B16C-21A1D49ABED3
 24 Linux root (x86-64)            4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709
 25 Linux root (ARM-64)            B921B045-1DF0-41C3-AF44-4C6F280D3FAE
 26 Linux root  (IA-64)             993D8D3D-F80E-4225-855A-9DAF8ED7EA97
 27 Linux reserved                 8DA63339-0007-60C0-C436-083AC8230908
 28 Linux home                     933AC7E1-2EB4-4F13-B844-0E14E2AEF915
 29 Linux RAID                     A19D880F-05FC-4D3B-A006-743F0F84911E
 30 Linux extended boot            BC13C2FF-59E6-4262-A352-B275FD6F7172
 31 Linux LVM                      E6D6D379-F507-44C2-A23C-238F2A3DF928
 32 FreeBSD data                   516E7CB4-6ECF-11D6-8FF8-00022D09712B
 33 FreeBSD boot                   83BD6B9D-7F41-11DC-BE0B-001560B84F0F
 34 FreeBSD swap                   516E7CB5-6ECF-11D6-8FF8-00022D09712B

Hex code (type L to list all codes): 31  #Linux LVM
Changed type of partition 'Linux filesystem' to 'Linux LVM'.

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


重覆操作sdb


查看分割區
$fdisk -l
Disk /dev/sda: 931.5 GiB, 1000204886016 bytes, 1953525168 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: gpt
Disk identifier: C25BB6AD-BEB0-43F5-BDA2-34B067A5BEE5

Device     Start        End    Sectors   Size Type
/dev/sda1   2048 1953525134 1953523087 931.5G Linux LVM


Disk /dev/sdb: 931.5 GiB, 1000204886016 bytes, 1953525168 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: gpt
Disk identifier: 728CE52E-6C1B-4E7F-A83B-C18F1CD262BB

Device     Start        End    Sectors   Size Type
/dev/sdb1   2048 1953525134 1953523087 931.5G Linux LVM



建立LVM 物理卷


PV:物理卷,是LVM系統的最底層的,可以是整顆硬碟,也可以是分割區。


$pvcreate /dev/sda1 /dev/sdb1
  Physical volume "/dev/sda1" successfully created.
  Physical volume "/dev/sdb1" successfully created.
*附註:
刪除LVM分割區為
pvremove /dev/sda1 /dev/sdb1


查看物理卷
$pvdisplay
  "/dev/sda1" is a new physical volume of "931.51 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda1
  VG Name
  PV Size               931.51 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               NtfgYB-D5IM-SRnU-gyXV-ZGCt-Pedw-6nZ0VT

  "/dev/sdb1" is a new physical volume of "931.51 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name
  PV Size               931.51 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               JebAUY-8Oes-Rh2g-g2vN-m3HP-MzgX-lE4oMn


建立LVM 卷組


VG:卷組,由多個PV(物理卷)組成,像是一個群組。

範例將為此卷組命名為storage

$ vgcreate storage /dev/sda1 /dev/sdb1
  Volume group "storage" successfully created

查看卷組
$vgdisplay
  --- Volume group ---
  VG Name               storage
  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               1.82 TiB
  PE Size               4.00 MiB
  Total PE              476932
  Alloc PE / Size       0 / 0
  Free  PE / Size       476932 / 1.82 TiB
  VG UUID               9nb5e5-cNqZ-OaDm-3b5H-G0WO-2Maq-KNcN8j




重新掃描LVM的VG並刷新快取


$vgscan
  Reading volume groups from cache.
  Found volume group "storage" using metadata type lvm2


建立LVM 邏輯卷


LV:邏輯卷,是從VG分出來的一段小空間,可建立檔案系統(EXT3、FAT、NTFS.....)

範例中直接將VG分給LV 1.8TB,使用的卷組為storage,邏輯卷命名為file

$lvcreate --name file --size 1.8T storage
  Rounding up size to full physical extent 1.80 TiB
  Logical volume "file" created.



查看邏輯卷

$lvdisplay
  --- Logical volume ---
  LV Path                /dev/storage/file
  LV Name                file
  VG Name                storage
  LV UUID                epxcZ2-7XL3-KjB3-9EP1-gQ7B-BWro-XZ33Vr
  LV Write Access        read/write
  LV Creation host, time raspberrypi, 2018-09-19 23:54:37 +0800
  LV Status              available
  # open                 0
  LV Size                1.80 TiB
  Current LE             471860
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:0


重新掃描LVM的LV並刷新快取


$lvscan
  ACTIVE            '/dev/storage/file' [1.80 TiB] inherit


建立檔案系統


範例file的檔案系統使用FAT,可依使用改變。
$mkfs.fat /dev/storage/file
mkfs.fat 4.1 (2017-01-24)


查看磁碟


已經完成LVM的設置,接下來只要將/dev/storage/file載掛到電腦中,就完成了~

$lsblk
NAME             MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda                8:0    0 931.5G  0 disk
└─sda1             8:1    0 931.5G  0 part
  └─storage-file 254:0    0   1.8T  0 lvm  /media/storage
sdb                8:16   0 931.5G  0 disk
└─sdb1             8:17   0 931.5G  0 part
  └─storage-file 254:0    0   1.8T  0 lvm  /media/storage
mmcblk0          179:0    0  29.6G  0 disk
├─mmcblk0p1      179:1    0   1.4G  0 part
├─mmcblk0p2      179:2    0     1K  0 part
├─mmcblk0p5      179:5    0    32M  0 part /media/pi/SETTINGS
├─mmcblk0p6      179:6    0    69M  0 part /boot
└─mmcblk0p7      179:7    0  28.1G  0 part /

沒有留言:

張貼留言