今天心情不高興,做IT不容易被公司重視,一定要速度學會運營,成為一個高逼格的技術男。
今天我要熟練掌握linux系統分區的能力。大家都知道,linux系統分區有兩種分區格式:GTP和MBR。
MBR作為傳統legacy的bios啟動方式被我們經常使用,新老主板bios都支持而且分區簡單,操作方便。
1、 MBR特點
① 最多支持4個主分區
② 最大支持2.1tb硬盤
③ 擴展分區一個硬盤只能有一個
2、 mbr結構:
引導占用硬盤開頭的512字節,前446字節為引導代碼,中間64個字節為4個主分區表信息,最后兩個字節為啟動標識。
3、 mbr分區實戰:
fdisk命令
語法:fdisk [選項] [參數]
選項:-b 指定每個分區大小
-l 列出分區表信息
-v 顯示fdisk版本
添加硬盤后,首先使用ll /dev/sd*查看設備是否識別,使用fdisk -l命令列出磁盤信息。
對/dev/sdb進行mbr分區
[root@linux-node2 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
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/sdb1
First sector (2048-6291455, default 2048): #開始扇區,回車默認從2048
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-6291455, default 6291455): +100M
Partition 1 of type Linux and of size 100 MiB is set
Command (m for help): n #新建分區
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): e #選擇擴展分區
Partition number (2-4, default 2): 3 #擴展分區編號/dev/sdb3
First sector (206848-6291455, default 206848): #默認回車,從當前扇區開始
Using default value 206848
Last sector, +sectors or +size{K,M,G} (206848-6291455, default 6291455): #默認回車分配所有剩余空間
Using default value 6291455
Partition 3 of type Extended and of size 2.9 GiB is set
Command (m for help): n
Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): l #新建邏輯分區
Adding logical partition 5 #默認邏輯分區編號為5
First sector (208896-6291455, default 208896): #邏輯分區起始位置
Using default value 208896
Last sector, +sectors or +size{K,M,G} (208896-6291455, default 6291455): +500M
Partition 5 of type Linux and of size 500 MiB is set
Command (m for help): n #新建第二個邏輯分區,分配剩余空間
Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): l
Adding logical partition 6
First sector (1234944-6291455, default 1234944):
Using default value 1234944
Last sector, +sectors or +size{K,M,G} (1234944-6291455, default 6291455):
Using default value 6291455
Partition 6 of type Linux and of size 2.4 GiB is set
Command (m for help): w 保存退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
查看分區:
[root@linux-node2 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 500M 0 part /boot
└─sda2 8:2 0 19.5G 0 part
├─rhel-root 253:0 0 17.5G 0 lvm /
└─rhel-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 3G 0 disk
├─sdb1 8:17 0 100M 0 part
├─sdb3 8:19 0 1K 0 part
├─sdb5 8:21 0 500M 0 part
└─sdb6 8:22 0 2.4G 0 part
格式化分區:
[root@linux-node2 ~]# mkfs.ext4 /dev/sdb1
手動掛載:
[root@linux-node2 ~]# mkdir /datasdb1
[root@linux-node2 ~]# mount /dev/sdb1 /datasdb1/
開機自動掛載:
[root@linux-node2 ~]# echo "/dev/sdb1 /datasdb1 ext4 defaults 0 0 " >> /etc/fstab
檢查掛載情況:
[root@linux-node2 ~]# df -h
如果磁盤分區為gpt需要轉換為mbr,請使用parted命令,再使用fdisk分區
[root@linux-node2 ~]# parted /dev/sdb GNU Parted 3.1 Using /dev/sdb Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) mklabel msdos Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to continue? Yes/No? y
