硬盘的0柱面、0磁头、1扇区称为主引导扇区(也要主引导记录MBR)。它由三个部分组成:主引导程序、硬盘分区表DPT和分区有效标志。
MBR:全程Master Boot Record,即硬盘的主引导记录。
分区编号:主分区编号1到4号。逻辑分区编号:5号以后。
Linux规定:逻辑分区必须建立在扩展分区之上,而不是建立在主分区上。
分区作用:
主分区:主要是用来启动操作系统的,它主要放的是操作系统的启动或引导程序。如:/boot分区最好放在主分区上。
扩展分区:扩展分区不能使用,它只是作为逻辑分区的容器存在的。我们真正存放数据的是主分区和逻辑分区。大量数据都放在逻辑分区中。
磁盘命名方式:
/dev/sd[a-z]n
a-z表示设备的序列号,n表示每块磁盘上划分的磁盘分区编号。
fdisk磁盘分区,是Linux发现版本中最常用的分区工具。
语法:fdisk [参数] 磁盘
参数:
-l:查看硬盘分区表
虚机中添加一块硬盘。不再演示。
[root@localhost ~]# fdisk /dev/sdc
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 0x5b34be3f.
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/sdc: 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: 0x5b34be3f
Device Boot Start End Blocks Id System
Command (m for help): n #新建一个分区
Partition type:
p primary (0 primary, 0 extended, 4 free) #创建主分区选p
e extended #创建逻辑分区选e
Select (default p): e
Partition number (1-4, default 1): #直接默认(选择分区遍号,默认选择1编号)
First sector (2048-41943039, default 2048): #直接默认(选择分区的起始扇区号)
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +1G #输入分区大小
Partition 1 of type Extended and of size 1 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 /dev/sdc
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): p #打印分区表
Disk /dev/sdc: 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: 0x5b34be3f
Device Boot Start End Blocks Id System
/dev/sdc1 2048 2099199 1048576 5 Extended
Command (m for help): n #新建分区
Partition type:
p primary (0 primary, 1 extended, 3 free) #创建主分区选p
l logical (numbered from 5) #
Select (default p): p #选择主分区创建
Partition number (2-4, default 2): #默认分区号为2
First sector (2099200-41943039, default 2099200): #默认扇区号从2099200开始
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-41943039, default 41943039): +100M #设置分区大小
Partition 2 of type Linux and of size 100 MiB is set
Command (m for help): w #保存并退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
reboot服务器是最好方法。如不想重启服务器,也可以使用:
[root@localhost ~]# partx -a /dev/sdc使分区生效。
[root@localhost ~]# mkfs.xfs /dev/sdc2
meta-data=/dev/sdc2 isize=512 agcount=4, agsize=6400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=25600, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=855, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@localhost ~]# mkdir xfs
[root@localhost ~]# mount /dev/sdc2 xfs/
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 55G 4.0G 51G 8% /
devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 3.9G 13M 3.8G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sda1 497M 166M 332M 34% /boot
tmpfs 781M 12K 781M 1% /run/user/42
tmpfs 781M 0 781M 0% /run/user/0
/dev/sdc2 97M 5.3M 92M 6% /root/xfs
[root@localhost ~]# vim /etc/fstab
将下面一句,写入/etc/fstab文件的最后
#ci_pan_fen_qu_zi_dong_gua_zai
/dev/sdc2 /root/xfs xfs defaults 0 0
[root@localhost ~]# mount –a #自动挂载/etc/fstab文件中没有挂载的文件
解释:
/dev/sdc2 |
/root/xfs |
xfs |
defaults |
0 |
0 |
要挂载的分区设备 |
挂载点 |
文件系统类型 |
挂载选项 |
是否备份 |
是否检测 |
查看分区的UUID
[root@localhost ~]# blkid
/dev/sdc2: UUID="4d0f543c-0b56-48e9-a252-05f4031a8887" TYPE="xfs"
/dev/sda1: UUID="855a2439-afad-4943-aa2f-fc1c8e1caba5" TYPE="xfs"
/dev/sda2: UUID="76aa7e90-e32e-4d3a-8ead-20da21664365" TYPE="swap"
/dev/sda3: UUID="dec77a47-26dd-4686-a209-66c35dbc9395" TYPE="xfs"
/dev/sdb1: UUID="2961bf24-6f30-4f55-947f-faa56491c09e" TYPE="xfs"
/dev/sr0: UUID="2018-10-10-18-34-13-00" LABEL="RHEL-7.6 Server.x86_64" TYPE="iso9660" PTTYPE="dos"
将UUID追加到配置文件中
[root@localhost ~]# echo "UUID=4d0f543c-0b56-48e9-a252-05f4031a8887 /root/xfs defaults 0 0" >> /etc/fstab
/dev/sdc2 |
/root/xfs |
xfs |
defaults |
0 |
0 |
要挂载的分区设备 |
挂载点 |
文件系统类型 |
挂载选项 |
是否备份 |
是否检测 |
Async/sync |
设置是否为同步方式运行,默认为async |
auto/noauto |
当执行mount -a 的命令时,此文件系统是否被主动挂载。默认为auto |
rw/ro |
是否以以只读或者读写模式挂载 |
exec/noexe |
限制此文件系统内是否能够进行"执行"的操作 |
user/nouser |
是否允许用户使用mount命令挂载 |
suid/nosuid |
是否允许SUID的存在 |
Usrquota |
启动文件系统支持磁盘配额模式 |
Grpquota |
启动文件系统对群组磁盘配额模式的支持 |
Defaults |
同时具有rw,suid,dev,exec,auto,nouser,async等默认参数的设置 samba nfs |
0 |
代表不要做备份 |
1 |
代表要每天进行操作 |
2 |
代表不定日期的进行操作 |
0 |
不要检验 |
1 |
最早检验(一般根目录会选择) |
2 |
1级别检验完成之后进行检验 |
注意:使用gdisk后将格式化磁盘。所以要先备份数据!!!
gdisk主要是用来划分容量大于4T的硬盘。
分区表有两种类型,分别是GPT和MBR,其中MBR不支持4T以上的分区。
GPT分区:GPT,全局唯一标识分区表(GUID Partition Table),它使用128位GUID来唯一标识每个磁盘和分区,与MBR存在单一故障点不同,GPT提供分区表信息的冗余,一个在磁盘头部一个在磁盘尾部;它通过CRC校验和来检测GPT头和分区表中的错误与损坏;默认一个硬盘支持128个分区
[root@bogon ~]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10
Partition table scan:
MBR: not present
BSD: not present
APM: not present
GPT: not present
Creating new GPT entries.
Command (? for help): ?
b back up GPT data to a file
c change a partition‘s name
d delete a partition #删除分区
i show detailed information on a partition
l list known partition types
n add a new partition #新建分区
o create a new empty GUID partition table (GPT)
p print the partition table #打印分区列表
q quit without saving changes #退出不保存
r recovery and transformation options (experts only)
s sort partitions
t change a partition‘s type code
v verify disk
w write table to disk and exit #写入分区表并保存
x extra functionality (experts only)
? print this menu #查看帮助信息
Command (? for help): n #新建分区表
Partition number (1-128, default 1): #默认回车,选择分区号1
First sector (34-41943006, default = 2048) or {+-}size{KMGTP}: #默认回车,选择起始扇区
Last sector (2048-41943006, default = 41943006) or {+-}size{KMGTP}: +5G #设置分区大小
Current type is ‘Linux filesystem‘
Hex code or GUID (L to show codes, Enter = 8300): #选择分区类型,L显示所有类型
Changed type of partition to ‘Linux filesystem‘
Command (? for help): p #查看分区列表
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): ED5BF6EB-C37C-40A3-8926-175AF7137CBE
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 31457213 sectors (15.0 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 10487807 5.0 GiB 8300 Linux filesystem
Command (? for help): w #保存并退出
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y #确认写入
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.
reboot服务器是最好方法。如不想重启服务器,也可以使用:
[root@localhost ~]# partx -a /dev/sdb使分区生效。
[root@bogon ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=327680 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=1310720, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@bogon ~]# mkdir test
[root@bogon ~]# mount /dev/sdb1 test/
[root@bogon ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 55G 3.9G 51G 8% /
devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 3.9G 13M 3.8G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sda1 497M 166M 332M 34% /boot
tmpfs 781M 12K 781M 1% /run/user/42
tmpfs 781M 0 781M 0% /run/user/0
/dev/sdb1 5.0G 33M 5.0G 1% /root/test
参考fdisk的设置文件开机自动挂载。
原文:https://www.cnblogs.com/ai-fei-ai/p/14216008.html