什麼是分區?
分區是將一個硬盤驅動器分成若干個邏輯驅動器,分區是把硬盤連續的區塊當做一個獨立的磁硬使用。分區表是一個硬盤分區的索引,分區的信息都會寫進分區表。
爲什麼要有多個分區?
防止數據丟失:如果系統只有一個分區,那麼這個分區損壞,用戶將會丟失所的有數據。
增加磁盤空間使用效率:可以用不同的區塊大小來格式化分區,如果有很多1K的文件,而硬盤分區區塊大小爲4K,那麼每存儲一個文件將會浪費3K空間。這時我們需要取這些文件大小的平均值進行區塊大小的劃分。
數據激增到極限不會引起系統掛起:將用戶數據和系統數據分開,可以避免用戶數據填滿整個硬盤,引起的系掛起。
分區工具fdisk用法介紹
- fdisk命令參數介紹
- p、打印分區表。
- n、新建一個新分區。
- d、刪除一個分區。
- q、退出不保存。
- w、把分區寫進分區表,保存並退出。
開始分區
給虛擬機掛載了一塊硬盤
用命令lsblk
查看磁盤是否掛載
lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 253:0 0 40G 0 disk
├─vda1 253:1 0 4G 0 part [SWAP]
└─vda2 253:2 0 36G 0 part /
vdb 253:16 0 100G 0 disk
可以看到 vdb 沒有進行分區
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 0xd698e7e1.
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): p
Disk /dev/vdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0xd698e7e1
Device Boot Start End Blocks Id System
Command (m for help): n # 新建分區
Partition type:
p primary (0 primary, 0 extended, 4 free)# 只能分4個主分區
e extended
Select (default p): p # 主分區
Partition number (1-4, default 1): 1
First sector (44-209715199, default 44): #起始扇區
Using default value 44
Last sector, +sectors or +size{K,M,G} (44-209715199, default 209715199):
Using default value 209715199# 結束山區
Partition 1 of type Linux and of size 100 GiB is set
Command (m for help): p
Disk /dev/vdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0xd698e7e1
Device Boot Start End Blocks Id System
/dev/vdb1 44 209715199 104857578 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
可以看到分區已存在
lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 253:0 0 40G 0 disk
├─vda1 253:1 0 4G 0 part [SWAP]
└─vda2 253:2 0 36G 0 part /
vdb 253:16 0 100G 0 disk
└─vdb1 253:17 0 512B 0 part
通過df -T
查看文件系統格式
df -T
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/vda2 xfs 37729264 3132628 34596636 9% /
devtmpfs devtmpfs 3993564 0 3993564 0% /dev
tmpfs tmpfs 4004244 0 4004244 0% /dev/shm
tmpfs tmpfs 4004244 8820 3995424 1% /run
tmpfs tmpfs 4004244 0 4004244 0% /sys/fs/cgroup
tmpfs tmpfs 800852 0 800852 0% /run/user/0
tmpfs tmpfs 800852 0 800852 0% /run/user/1001
可以看到/dev/vda2
爲 xfs 文件系統格式
mkfs -t xfs /dev/vdb1
meta-data=/dev/vdb1 isize=512 agcount=4, agsize=6553599 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=26214394, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=12799, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
將磁盤掛載到/home
先備份/home 下的文件目錄
mkdir -p /tmp/bak
cp -pr * /tmp/bak/
mount /dev/vdb1 /home
lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 253:0 0 40G 0 disk
├─vda1 253:1 0 4G 0 part [SWAP]
└─vda2 253:2 0 36G 0 part /
vdb 253:16 0 100G 0 disk
└─vdb1 253:17 0 100G 0 part /home
發現 磁盤已掛載上
開機自動掛載硬盤
通過blkid
命令查看
blkid /dev/vdb1
/dev/vdb1: UUID="0d9e0faf-e273-482e-9784-451b5239d5c1" TYPE="xfs"
cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu May 9 01:56:05 2019
#
# 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
#
UUID=a83f4fdc-2ea1-4fec-a1e2-a42016ce0afe / xfs defaults 0 0
UUID=9e33bd6f-c68c-41c7-95c8-703f4fe8c3d4 swap swap defaults 0 0
在/etc/fstab
最低下插入
echo 'UUID=0d9e0faf-e273-482e-9784-451b5239d5c1 /home xfs defaults 0 0 ' >>/etc/fstab
Ref
linux磁盤分區fdisk命令詳解
linux下查看磁盤分區的文件系統格式
Linux查看分區文件系統類型總結
CentOS7設置開機自動掛載硬盤