我拿到一臺新機器,df -Th 查看掛載情況,如下:
fdisk -l 查看磁盤情況,如下:
上圖結合 df -Th 可知,
/dev/vda 盤已經分區過了,只有 /dev/vda2分區掛載了,dev/vda2 分區大小 36G,掛載目錄 /
/dev/vda1 分區大小爲4G:(8390655 - 2048)*512/1024/1024/1024=4G。 可知 /dev/vda盤還有60G爲分區
/dev/vdb 盤有250G 容量,還未分區(更別提掛載了)
使用 parted /dev/vda 可以查看已經分好的區的情況(顯示的分區大小不準):
[root@ecs-a3b8-0206426 ~]# parted /dev/vda
GNU Parted 3.1
Using /dev/vda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p // 輸入p 打印查看 已經分好的區的情況
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:Number Start End Size Type File system Flags
1 1049kB 4296MB 4295MB primary linux-swap(v1)
2 4296MB 42.9GB 38.7GB primary ext3 boot(parted) q // 輸入 q 退出
[root@ecs-a3b8-0206426 ~]#
------------------------------------------------------------------
二 對 /dev/vdb 盤分區
1. 通過命令fdisk-l查看硬盤信息:
物理卷 pv 和邏輯卷 lv 可不創建,省略這一步。
2. 對 /dev/vdb 盤分區:
執行命令 fdisk /dev/vdb, 進入fdisk模式,開始對新增數據盤執行分區操作。--- f說明:fdisk /dev/vdb 操作,即是創建物理分區
[root@ecs-a3b8-0206426 ~]# fdisk /dev/vdb
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 0x5e1d7d4e.
Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
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): //保持默認,也可輸入1,新建 /dev/vdb 的第一個主分區
First sector (2048-524287999, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-524287999, default 524287999): 209713152 // 算好了 100G
Partition 1 of type Linux and of size 100 GiB is set
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 (209713153-524287999, default 209715200):
Using default value 209715200
Last sector, +sectors or +size{K,M,G} (209715200-524287999, default 524287999): // 保持默認直接回車,即將剩下的150G全部分到第2個區裏
Using default value 524287999
Partition 2 of type Linux and of size 150 GiB is set
接下來輸入p,回車,查看新建分區的詳細信息。
Command (m for help): p
Disk /dev/vdb: 268.4 GB, 268435456000 bytes, 524288000 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: 0x5e1d7d4e
Device Boot Start End Blocks Id System
/dev/vdb1 2048 209713152 104855552+ 83 Linux
/dev/vdb2 209715200 524287999 157286400 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@ecs-a3b8-0206426 ~]#
如上圖,接着輸入w保存,將分區結果寫入分區表中。
若回顯信息如下,則說明分區成功:
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
若萬一回顯信息 顯示的是 The new table will be used at the next reboot , 意思就是這個分區在下次啓動的時候才能使用,我們可以執行一個命令, 讓這個分區馬上生效,命令爲 partprobe ,將新的分區表變更同步至操作系統。 注意:也有 partprobe /dev/vdb 這樣執行的
fdisk -l 查看磁盤情況,發現 /dev/vdb 盤已經分好了2個區
3 格式化分區 /dev/vdb1
執行以下命令,將新建分區文件系統設爲系統所需格式。
執行的命令爲:
mkfs.ext3 /dev/vdb1
或
mkfs -t ext3 /dev/vdb1
4 格式化分區 /dev/vdb2
執行以下命令,將新建的分區文件系統設爲所需格式
mkfs.ext3 /dev/vdb2
或
mkfs -t ext3 /dev/vdb2
5. 將格式化後的分區 /dev/vdb2 掛載到 /data 目錄下
創建 /data 目錄
mkdir /data
編輯 vim /etc/fstab 文件,掛載
mount -a
(mount -a 的意思是將/etc/fstab的所有內容重新加載)
擴展:
執行 lsblk 可查看磁盤分區情況和各分區掛載情況(只要分過區,不管有沒有格式化分區都會精確到顯示出磁盤的分區信息)
擴展:
我們不採用 /etc/fstab 直接指定 /dev/vdb2 的方式設置開機自動掛載,而推薦使用 UUID 來配置自動掛載數據盤。
先執行命令 blkid /dev/vdb2 ,查詢磁盤分區的UUID:
再查看 /etc/fstab , 會發現 /dev/vda1 和 /dev/vda2 就是採用 UUID 的方式設置的開機自動掛載
所以,/etc/fstab 文件中,/dev/vdb2 也可以改成 UUID方式。方式參照 /dev/vda1, /dev/vda2。
擴展:
如果對數據盤未進行過卷組劃分等相關操作
使用 vgdisplay查看卷組情況,lvdisolay 查看邏輯卷組情況 ,是查不出數據的:
如果進行了卷組劃分等相關操作
使用 vgdisplay查看卷組情況,lvdisolay 查看邏輯卷組情況 (一個卷組vg可以劃分出多個邏輯卷 lv):
-----------------------------------
對沒有物理卷,卷組信息,邏輯分區的系統盤擴容
以前面格式化好分區的系統爲例:
注意點:前面的操作,其實已經有了物理卷,但沒有創建物理卷,卷組,邏輯卷,直接mount的。
1. 查看磁盤信息:分別執行 df -Th, lsblk 命令查看
2. 創建物理卷
pvcreate /dev/vdb1
[root@ecs-a3b8-0206426 ~]# pvcreate /dev/vdb1
WARNING: ext3 signature detected on /dev/vdb1 at offset 1080. Wipe it? [y/n]: y //輸入y,即同意
Wiping ext3 signature on /dev/vdb1.
Physical volume "/dev/vdb1" successfully created.
[root@ecs-a3b8-0206426 ~]#
3. 查看系統裏所有的物理卷 pvdisplay
[root@ecs-a3b8-0206426 ~]# pvdisplay
"/dev/vdb1" is a new physical volume of "<100.00 GiB"
--- NEW Physical volume ---
PV Name /dev/vdb1
VG Name
PV Size <100.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID IQhByQ-5zev-82Te-5Re5-ZARN-WaDu-giBCTu
[root@ecs-a3b8-0206426 ~]#
也可使用 pvs 命令:查看系統裏的物理卷,更直觀簡潔。
4 對系統分區創建物理卷, 如果拿到的系統一開始就沒有創建系統分區物理卷,則對系統分區創建物理卷時會失敗
所以,沒有系統分區物理卷的系統,想擴容系統分區掛載根目錄是不可能的。