linux硬盘分区、格式化、挂载的方法是什么

发布时间:2022-01-20 15:25:03 作者:iii
来源:亿速云 阅读:129

这篇文章主要介绍“linux硬盘分区、格式化、挂载的方法是什么”,在日常操作中,相信很多人在linux硬盘分区、格式化、挂载的方法是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”linux硬盘分区、格式化、挂载的方法是什么”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

一、分区分类

MBR:全称为Master Boot Record,即硬盘的主引导记录。

GPT:分区模式使用GUID分区表,是源自EFI标准的一种较新的磁盘分区表结构的标准。与普遍使用的主引导记录(MBR)分区方案相比,GPT提供了更加灵活的磁盘分区机制。

1. MBR概述

分区编号:主分区1-4 ,逻辑分区5……  。主分区最多 4个

LINUX规定:逻辑分区必须建立在扩展分区之上,而不是建立在主分区上

分区作用:

主分区:主要是用来启动操作系统的,它主要放的是操作系统的启动或引导程序,/boot分区最好放在主分区上

扩展分区不能使用的,它只是做为逻辑分区的容器存在的;我们真正存放数据的是主分区和逻辑分区,大量数据都放在逻辑分区中

这是采用MBR分区方案的硬盘分区示意图(一块硬盘 ),关于主分区、扩展分区与逻辑分区的各自含义,后面会有说明。

MBR是传统的分区表,如果你使用的电脑比较老,该电脑硬盘使用MBR分区方案的可能性比较大。下面,我们列出MBR分区表的一些重要特点:

如果你一个普通用户,对技术细节不是那么感兴趣,那其实关于MBR分区方案你需要记住的只有两点:

2.查看硬盘设备以及分区情况
[root@centos7 ~]# ls /dev/sda*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sda4  /dev/sda5

命名方式:/dev/sd[a-z]n

其中:a-z 表示硬盘的序号,如sda表示第一块scsi硬盘,sdb就是第二块......

          n  表示每块磁盘上划分的磁盘分区编号

3.使用分区工具fdisk对磁盘进行操作,分区,格式化(重点)

fdisk:磁盘分区,是Linux发行版本中最常用的分区工具

用法:fdisk [选项] device    

常用的选项 : -l 查看硬盘分区表  

案例:在sdb盘上建一个分区,大小为1G

brw-rw----. 1 root disk 8,  0 Feb 23 01:02 /dev/sda
brw-rw----. 1 root disk 8,  1 Feb 23 01:02 /dev/sda1
brw-rw----. 1 root disk 8,  2 Feb 23 01:02 /dev/sda2
brw-rw----. 1 root disk 8,  3 Feb 23 01:02 /dev/sda3
brw-rw----. 1 root disk 8,  4 Feb 23 01:02 /dev/sda4
brw-rw----. 1 root disk 8,  5 Feb 23 01:02 /dev/sda5
brw-rw----. 1 root disk 8, 16 Feb 23 01:02 /dev/sdb

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 0xeb311dde.

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)

Disk /dev/sdb: 8526 MB, 8526585856 bytes, 16653488 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: 0xeb311dde
   Device Boot      Start         End      Blocks   Id  System

Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended

Partition number (1-4, default 1): 1     
First sector (2048-16653487, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-16653487, default 16653487): 1G     
Value out of range.

Partition 1 of type Linux and of size 1 GiB is set

The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

/dev/sdb  /dev/sdb1
[root@centos7 ~]#

让新的分区生效

[root@centos7 ~]# partx -a /dev/sdb
partx: /dev/sdb: error adding partition 1

格式化分区并挂载使用

Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        733M     0  733M   0% /dev
tmpfs           748M     0  748M   0% /dev/shm
tmpfs           748M  9.5M  739M   2% /run
tmpfs           748M     0  748M   0% /sys/fs/cgroup
/dev/sda2        10G  4.5G  5.6G  45% /
/dev/sda5        52G  111M   52G   1% /home
/dev/sda1       197M  146M   52M  75% /boot
tmpfs           150M   20K  150M   1% /run/user/0
/dev/sr0         57M   57M     0 100% /run/media/root/VBox_GAs_6.1.2
/dev/sdb1       976M  2.6M  907M   1% /root/data
4.写入配置文件,让它开机自动挂载

方法1:

写入配置文件,让它开机自动挂载:/etc/fstab

#
# /etc/fstab
# Created by anaconda on Sat Feb 22 09:25:14 2020
#
# 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=b2af0e57-3f99-40b9-a621-6697301e4b88 /                       xfs     defaults        0 0
UUID=dd56dea7-8797-4fef-aa8e-ef2a8adf2f29 /boot                   xfs     defaults        0 0
UUID=c05c7778-3202-44c8-810e-23f9e711cf8f /home                   xfs     defaults        0 0
UUID=c26caf1e-444a-4445-8f91-49af5b828113 swap                    swap    defaults        0 0
/dev/sdb1 /root/data ext4 defaults 0 0

Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        733M     0  733M   0% /dev
tmpfs           748M     0  748M   0% /dev/shm
tmpfs           748M  9.5M  739M   2% /run
tmpfs           748M     0  748M   0% /sys/fs/cgroup
/dev/sda2        10G  4.5G  5.6G  45% /
/dev/sda5        52G  111M   52G   1% /home
/dev/sda1       197M  146M   52M  75% /boot
tmpfs           150M   20K  150M   1% /run/user/0
/dev/sr0         57M   57M     0 100% /run/media/root/VBox_GAs_6.1.2
/dev/sdb1       976M  2.6M  907M   1% /root/data

配置文件各列解释

/dev/sdb1

/sdb1

 ext4

defaults

0

0

要挂载的分区设备

挂载点

文件系统类型

挂载选项

是否备份

是否检测

方法2:

使用设备的uuid挂载,避免硬盘拔插,分区号改变挂错硬盘:

/dev/sr0: UUID="2020-01-13-11-26-32-82" LABEL="VBox_GAs_6.1.2" TYPE="iso9660" 
/dev/sda1: UUID="dd56dea7-8797-4fef-aa8e-ef2a8adf2f29" TYPE="xfs" 
/dev/sda2: UUID="b2af0e57-3f99-40b9-a621-6697301e4b88" TYPE="xfs" 
/dev/sda3: UUID="c26caf1e-444a-4445-8f91-49af5b828113" TYPE="swap" 
/dev/sda5: UUID="c05c7778-3202-44c8-810e-23f9e711cf8f" TYPE="xfs" 
/dev/sdb1: UUID="911fc7a2-bc67-407d-9517-ed2992e54b3a" TYPE="ext4"

GPT分区方案

相比较于MBR,GPT是新一代的分区方案,如果你使用的电脑很新,有可能该电脑上的硬盘就是使用了GPT分区方案。

GPT比MBR要复杂,下面我们简单说明一下GPT的技术细节,如果你不是技术控,对这些技术细节不感兴趣,可以跳过这部分,直接去阅读GPT的重要特点。

首先,你会注意到,这张用LBA 0、LBA 1这样的方法来表明硬盘上的地址,这是因为以前一般都是用chs方式对硬盘寻址的,现在一般都用LBA方式对硬盘寻址,关于chs与LBA,不明白且有兴趣专研的读者,可以自行查阅相关资料,对上面这张图来说,LBA 0指的是物理序号为0的第一个扇区,LBA 1指的是物理序号为1的第二个扇区,依次类推。

下面比照着上面这张图,解释下GPT分区方案。

以上就是GPT分区方案的大致内容,其实也不算复杂了。对普通用户而言,其实需要了解的GPT最重要的特点是:使用GPT分区方案,没有硬盘容量不能超过2TB大小的限制,这是GPT区别于MBR的特点与优点!

现在,可能你会觉得如何使用MBR与GPT了:硬盘容量小于或等于2TB时,用MBR或GPT都行;硬盘容量大于2TB时,用GPT。这种说法没错,但实际情况比这个要复杂些。因为,正确的使用MBR或GPT,你还需要了解另外一些知识:比如BIOS与EFI。

 1.gdisk 磁盘分区工具 与fdisk工具类似

gdisk主要是用来划分容量大于2T的硬盘,大于2T fdisk搞不定

两种类型的分区表:GPT和MBR ; MBR不支持4T以上

GPT分区:GPT,全局唯一标识分区表(GUID Partition Table),它使用128位GUID来唯一标识每个磁盘和分区,与MBR存在单一故障点不同,GPT提供分区表信息的冗余,一个在磁盘头部一个在磁盘尾部;它通过CRC校验和来检测GPT头和分区表中的错误与损坏;默认一个硬盘支持128个分区

例:对sdb做gpt分区,创建一个sdb1

brw-rw----. 1 root disk 8, 16 Feb 23 01:37 /dev/sdb
[root@centos7 ~]# 
[root@centos7 ~]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10
Partition table scan:
  MBR: MBR only
  BSD: not present
  APM: not present
  GPT: not present

***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by
typing 'q' if you don't want to convert your MBR partitions
to GPT format!
***************************************************************


bback up GPT data to a file
cchange a partition's name
ddelete a partition   #删除分区
ishow detailed information on a partition
llist known partition types
nadd a new partition   # 添加一个分区
ocreate a new empty GUID partition table (GPT)
pprint the partition table  # 打印分区表
qquit without saving changes   # 退出不保存
rrecovery and transformation options (experts only)
ssort partitions
tchange a partition's type code
vverify disk
wwrite table to disk and exit   # 写入分区表并退出
xextra functionality (experts only)
?print this menu



Current type is 'Linux filesystem'

Changed type of partition to 'Linux filesystem'

Disk /dev/sdb: 16653488 sectors, 7.9 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): E08228E1-79AB-4AC8-8A82-782A795B9AFF
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 16653454
Partitions will be aligned on 2048-sector boundaries
Total free space is 14556269 sectors (6.9 GiB)
Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         2099199   1024.0 MiB  8300  Linux filesystem

  
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.

mkfs.xfs: /dev/sdb1 appears to contain an existing filesystem (ext4).
mkfs.xfs: Use the -f option to force overwrite.
[root@centos7 ~]# mkfs.xfs -f  /dev/sdb1   #格式化
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=65536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=262144, 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

其他挂载查看等和MBR分区一致,这里不再赘述,  查看磁盘分区挂载 还可以用 fdisk -l

Disk /dev/sda: 68.7 GB, 68719476736 bytes, 134217728 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: 0x00060ad4
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648    21383167    10485760   83  Linux
/dev/sda3        21383168    25577471     2097152   82  Linux swap / Solaris
/dev/sda4        25577472   134217727    54320128    5  Extended
/dev/sda5        25579520   134217727    54319104   83  Linux
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Disk /dev/sdb: 8526 MB, 8526585856 bytes, 16653488 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: gpt
Disk identifier: E08228E1-79AB-4AC8-8A82-782A795B9AFF

#         Start          End    Size  Type            Name
 1         2048      2099199      1G  Linux filesyste Linux filesystem
 2      2099200     16653454      7G  Linux filesyste Linux filesystem

到此,关于“linux硬盘分区、格式化、挂载的方法是什么”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. linux中的挂载是什么?怎么用?
  2. linux进行硬盘分区挂载的实现

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

linux

上一篇:Linux Shell编程的使用方法有哪些

下一篇:如何在Ubuntu 18.04/Linux Mint 19中安装Wine 4

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》