当前位置:首页 » 《关注互联网》 » 正文

Linux 磁盘分区、格式化和挂载

11 人参与  2024年12月20日 14:00  分类 : 《关注互联网》  评论

点击全文阅读


一、磁盘分区表分类

Linux中想使用磁盘的步骤和Windows一样

加硬盘->分区->格式化->挂载

#通过命令行方式对磁盘进行分区(两种方式,第一种就是MBR,第二种就是GPT)

如果想采用MBR(Master Boot Record)的方式进行分区就使用fdisk命令

MBR特征:

较旧的分区表格式,仅支持最多4个主分区(或者3个主分区加一个扩展分区)。支持的最大磁盘容量为2TB。常用于较旧的BIOS启动系统。MBR的结构较简单,但不如GPT可靠。

如果想采用GPT(GUID Partition Table)的方式进行分区就使用gdisk命令

GPT特征:

现代的分区表格式,支持超过2TB的磁盘容量。支持最多128个主分区。具有更强的冗余功能,可以存储多个备份的分区表。必须使用UEFI(Unified Extensible Firmware Interface)启动。

1、手动创建分区(效率太低)

2、自动创建分区(有一定的操作性)

# 查看当前所有磁盘的分区情况

[server root ~] # fdisk -l

# 查看当前模块硬盘的分区情况

[server root ~] # fdisk -l /dev/sdb 

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors

一个sectors等于512B

[server root ~] # fdisk -lDisk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x000e1369   Device Boot      Start         End      Blocks   Id  System/dev/sda1   *        2048     1050623      524288   83  Linux/dev/sda2         1050624     9439231     4194304   82  Linux swap / Solaris/dev/sda3         9439232    83886079    37223424   83  LinuxDisk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytes[server root ~] # fdisk -l /dev/sdb Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytes[server root ~] # 

二、fdisk命令创建分区

对于服务器来说,磁盘从使用目的来说分为三种,第一种是用来存放操作系统的,此类硬盘一个只存放操作系统以及配置文件,剩下的任何数据都不应该主动存放在此硬盘中。第二种磁盘是用来存放本地大文件,一般这种磁盘叫做本地存储盘。第三种磁盘是用来存放本地大文件,而是使用了网络存储(SAN->storage area network),一般这种磁盘叫做网络存储

1、交互式的命令

[server root ~] # fdisk /dev/sdb

[server root ~] # fdisk /dev/sdbWelcome 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 tableBuilding a new DOS disklabel with disk identifier 0x0f1f0bcc.Command (m for help): q(quit 不保存的)[server root ~] # fdisk /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 tableBuilding a new DOS disklabel with disk identifier 0x57ae1208.Command (m for help): w(write 保存)The partition table has been altered!Calling ioctl() to re-read partition table.Syncing disks.[server root ~] # 

2、分区主分区

对/dev/sdb磁盘进行分区,如果分了四个主分区后,还有剩余空间,那就没法进行额外分区了,如下

[server root ~] # fdisk /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 tableBuilding a new DOS disklabel with disk identifier 0x05cc5d51.Command (m for help): mCommand 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): Command (m for help): nPartition type:   p   primary (0 primary, 0 extended, 4 free)   e   extendedSelect (default p): pPartition number (1-4, default 1): First sector (2048-41943039, default 2048): Using default value 2048Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +5GPartition 1 of type Linux and of size 5 GiB is setCommand (m for help): wThe partition table has been altered!Calling ioctl() to re-read partition table.Syncing disks.[server root ~] # fdisk /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.......Command (m for help): pDisk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x05cc5d51   Device Boot      Start         End      Blocks   Id  System/dev/sdb1            2048    10487807     5242880   83  Linux/dev/sdb2        10487808    12584959     1048576   83  Linux/dev/sdb3        12584960    14682111     1048576   83  Linux/dev/sdb4        14682112    16779263     1048576   83  LinuxCommand (m for help): nIf you want to create more than four partitions, you must replace aprimary partition with an extended partition first.# 如果要创建四个以上的分区,则必须替换首先使用扩展分区的主分区。Command (m for help): nIf you want to create more than four partitions, you must replace aprimary partition with an extended partition first.

3、创建扩展分区,然后继续创建主分区

# 需要先删除一个主分区,然后创建扩展分区,默认剩余空间都给扩展分区[server root ~] # fdisk /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.Command (m for help): dPartition number (1-4, default 4): 4Partition 4 is deletedCommand (m for help): nPartition type:   p   primary (3 primary, 0 extended, 1 free)   e   extendedSelect (default e): eSelected partition 4First sector (14682112-41943039, default 14682112): Using default value 14682112Last sector, +sectors or +size{K,M,G} (14682112-41943039, default 41943039): Using default value 41943039Partition 4 of type Extended and of size 13 GiB is setCommand (m for help): pDisk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x05cc5d51   Device Boot      Start         End      Blocks   Id  System/dev/sdb1            2048    10487807     5242880   83  Linux/dev/sdb2        10487808    12584959     1048576   83  Linux/dev/sdb3        12584960    14682111     1048576   83  Linux/dev/sdb4        14682112    41943039    13630464    5  Extended# 创建扩展分区之后,就可以在创建主分区了[server root ~] # fdisk /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.Command (m for help): nAll primary partitions are in useAdding logical partition 5First sector (14684160-41943039, default 14684160): Using default value 14684160Last sector, +sectors or +size{K,M,G} (14684160-41943039, default 41943039): +5GPartition 5 of type Linux and of size 5 GiB is setCommand (m for help): PDisk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x05cc5d51   Device Boot      Start         End      Blocks   Id  System/dev/sdb1            2048    10487807     5242880   83  Linux/dev/sdb2        10487808    12584959     1048576   83  Linux/dev/sdb3        12584960    14682111     1048576   83  Linux/dev/sdb4        14682112    41943039    13630464    5  Extended/dev/sdb5        14684160    25169919     5242880   83  Linux

4、自动创建分区

自动分区采用了重定向的方法

fdisk /dev/sdb < test

或者

fdisk /dev/sdb < test &>> /dev/null

区别:显示创建过程与不显示创建过程

# 先将需要分区大小,分区类型是主分区还是扩展分区,直接写到一个文本中,如下(注意一定要空行)[server root ~] # vim testnp+1Gnewq# 重定向分区                                                    [server root ~] # fdisk /dev/sdb < testWelcome 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): Partition type:   p   primary (0 primary, 0 extended, 4 free)   e   extendedSelect (default p): Partition number (1-4, default 1): First sector (2048-41943039, default 2048): Using default value 2048Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): Partition 1 of type Linux and of size 1 GiB is setCommand (m for help): Command (m for help): Partition type:   p   primary (1 primary, 0 extended, 3 free)   e   extendedSelect (default p): Partition number (2-4, default 2): First sector (2099200-41943039, default 2099200): Using default value 2099200Last sector, +sectors or +size{K,M,G} (2099200-41943039, default 41943039): Using default value 41943039Partition 2 of type Extended and of size 19 GiB is setCommand (m for help): The partition table has been altered!Calling ioctl() to re-read partition table.Syncing disks.[server root ~] # fdisk -l /dev/sdbDisk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x05cc5d51   Device Boot      Start         End      Blocks   Id  System/dev/sdb1            2048     2099199     1048576   83  Linux/dev/sdb2         2099200    41943039    19921920    5  Extended

注意:在 Linux 中,分区大于 2TB 的硬盘时,通常需要使用 GPT(GUID Partition Table) 分区表,因为 MBR(Master Boot Record) 分区表只支持最多 2TB 的磁盘容量。GPT 分区表则没有这个限制,能够支持最大 9.4ZB 的磁盘容量

需要安装gdisk

apt-get install gdisk                 # Debian/Ubuntu 系统

yum install gdisk                      # CentOS/RHEL 系统

gdisk 相关参数

b back up GPT data to a file                       #备份当前的 GPT 数据到一个文件

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)                #创建一个新的空的 GPT 分区表

p print the partition table                               # 打印当前分区表的内容,显示磁盘的分区信息

q quit without saving changes                       #退出 gdisk

r recovery and transformation options (experts only)            #恢复和转换选项。

s sort partitions                                              #对分区进行排序。

t change a partition's type code                     #更改某个分区的类型

v verify disk                                                    #验证磁盘的 GPT 分区表

w write table to disk and exit                          #将更改后的分区表写入磁盘并退出

x extra functionality (experts only)                  #扩展功能,适用于高级用户,提供一些特殊的操作,如转换分区表格式等

三、格式化分区

1、Linux常见文件系统格式有:ext2、ext3、ext4、xfs、btrfs和btrfs

#ext4的文件系统限制是,单个文件大小不能超过1T

#xfs的文件系统每个文件系统量最大支持8eb,单个文件可以支持16tb

2、mkfs命令格式化分区

# mkfs.ext4格式化分区/dev/sdb1[server root ~] # mkfmkfifo       mkfs.btrfs   mkfs.ext2    mkfs.ext4    mkfs.xfs     mkfs         mkfs.cramfs  mkfs.ext3    mkfs.minix   [server root ~] # mkfs.extmkfs.ext2  mkfs.ext3  mkfs.ext4  [server root ~] # mkfs.ext4 /dev/sdb1 mke2fs 1.42.9 (28-Dec-2013)Filesystem label=OS type: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks327680 inodes, 1310720 blocks65536 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=134217728040 block groups32768 blocks per group, 32768 fragments per group8192 inodes per groupSuperblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736Allocating group tables: done                            Writing inode tables: done                            Creating journal (32768 blocks): doneWriting superblocks and filesystem accounting information: done # mkfs.xfs格式化分区/dev/sdb5[server root ~] # mkfs.xfs /dev/sdb5 meta-data=/dev/sdb5              isize=512    agcount=4, agsize=327680 blks         =                       sectsz=512   attr=2, projid32bit=1         =                       crc=1        finobt=0, sparse=0data     =                       bsize=4096   blocks=1310720, imaxpct=25         =                       sunit=0      swidth=0 blksnaming   =version 2              bsize=4096   ascii-ci=0 ftype=1log      =internal log           bsize=4096   blocks=2560, version=2         =                       sectsz=512   sunit=0 blks, lazy-count=1realtime =none                   extsz=4096   blocks=0, rtextents=0[server root ~] # # mkfs.ext2格式化分区/dev/sdb6[server root ~] # mkfs.ext2 /dev/sdb6 mke2fs 1.42.9 (28-Dec-2013)Filesystem label=OS type: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks393216 inodes, 1572864 blocks78643 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=161061273648 block groups32768 blocks per group, 32768 fragments per group8192 inodes per groupSuperblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736Allocating group tables: done                            Writing inode tables: done                            Writing superblocks and filesystem accounting information: done [server root ~] # 

四、挂载Linux分区(文件系统)

1、临时挂载

mount 命令格式

mount -t 文件系统类型 文件系统所在的分区路径        文件系统的挂载点路径

mount -t fstypes        分区路径        挂载点路径

# 还未挂载前[server root ~] # df -ThFilesystem     Type      Size  Used Avail Use% Mounted ondevtmpfs       devtmpfs  900M     0  900M   0% /devtmpfs          tmpfs     910M     0  910M   0% /dev/shmtmpfs          tmpfs     910M  9.5M  901M   2% /runtmpfs          tmpfs     910M     0  910M   0% /sys/fs/cgroup/dev/sda3      xfs        36G  3.7G   32G  11% //dev/sda1      xfs       509M  138M  372M  28% /boottmpfs          tmpfs     182M     0  182M   0% /run/user/0# 创建挂载目录[server root ~] # mkdir /mount-point{1..3}[server root ~] # [server root ~] # ls /mount-point* -d/mount-point1  /mount-point2  /mount-point3[server root ~] # # 将三个分区进行挂载,挂载到/mount-point(1,2,3)目录下[server root ~] # mount /dev/sdb1 /mount-point1[server root ~] # mount /dev/sdb5 /mount-point2[server root ~] # mount /dev/sdb6 /mount-point3[server root ~] # [server root ~] # df -ThFilesystem     Type      Size  Used Avail Use% Mounted ondevtmpfs       devtmpfs  900M     0  900M   0% /devtmpfs          tmpfs     910M     0  910M   0% /dev/shmtmpfs          tmpfs     910M  9.5M  901M   2% /runtmpfs          tmpfs     910M     0  910M   0% /sys/fs/cgroup/dev/sda3      xfs        36G  3.7G   32G  11% //dev/sda1      xfs       509M  138M  372M  28% /boottmpfs          tmpfs     182M     0  182M   0% /run/user/0/dev/sdb1      ext4      4.8G   20M  4.6G   1% /mount-point1/dev/sdb5      xfs       5.0G   33M  5.0G   1% /mount-point2/dev/sdb6      ext2      6.0G   12M  5.6G   1% /mount-point3[server root ~] # 

2、 永久挂载分区

mount命令只是临时挂载,系统重启之后就没了

如果想文件系统永久挂载,就需要把挂载信息写入到/etc/fstab。

/etc/fstab这个文件非常重要,一旦这个文件删除了,或者里边的某些行错误删除了,甚至写法错误都会导致系统无法正常启动。

# 系统重启后,挂载的分区没了[server root ~] # rebootConnection closing...Socket close.Connection closed by foreign host.Disconnected from remote host(centos) at 14:15:58.Type `help' to learn how to use Xshell prompt.[C:\~]$ Reconnecting in 30 seconds. Press any key to exit local shell...............................Connecting to 192.168.18.134:22...Connection established.To escape to local shell, press 'Ctrl+Alt+]'.Last login: Fri Nov 22 09:21:41 2024 from 192.168.18.1[server root ~] # df -ThFilesystem     Type      Size  Used Avail Use% Mounted ondevtmpfs       devtmpfs  900M     0  900M   0% /devtmpfs          tmpfs     910M     0  910M   0% /dev/shmtmpfs          tmpfs     910M  9.5M  901M   2% /runtmpfs          tmpfs     910M     0  910M   0% /sys/fs/cgroup/dev/sda3      xfs        36G  3.7G   32G  11% //dev/sda1      xfs       509M  138M  372M  28% /boottmpfs          tmpfs     182M     0  182M   0% /run/user/0[server root ~] # 

编辑/etc/fstab之前,最好先备份

# 备份文件[server root ~] # cp /etc/fstab /etc/fstab.bak[server root ~] # ls /etc/fstabfstab      fstab.bak  [server root ~] # cat /etc/fstab ## /etc/fstab# Created by anaconda on Tue Jan 16 23:35:15 2024## 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=7f5b46d3-e0da-430a-97f2-2e6a19f9e4ff /                       xfs     defaults        0 0UUID=ebaf11a7-f84f-4429-8df8-0ee0111e76e5 /boot                   xfs     defaults        0 0UUID=48826076-975a-4d59-bf1a-5b1004e96f29 swap                    swap    defaults        0 0# 第一列是文件系统所在的分区路径#第二列是文件系统的挂载点#第三列是文件系统的类型#第四列是挂载参数#第五列和第六列分别写0,只有在一些特殊的文件系统,后两列才需要改成非0/dev/sdb1       /mount-point1   ext4    defaults        0 0/dev/sdb5       /mount-point2   xfs     defaults        0 0/dev/sdb6       /mount-point3   ext2    defaults        0 0

当fstab文件编辑完成后,需使用mount -a命令自动挂载生效。

注意:如果某个文件系统已经挂载了。且还在fstab里边被记录,那么mount -a不会将该文件卸载,再重新挂载,mount -a会自动忽略改文件系统

[server root ~] # df -ThFilesystem     Type      Size  Used Avail Use% Mounted ondevtmpfs       devtmpfs  900M     0  900M   0% /devtmpfs          tmpfs     910M     0  910M   0% /dev/shmtmpfs          tmpfs     910M  9.5M  901M   2% /runtmpfs          tmpfs     910M     0  910M   0% /sys/fs/cgroup/dev/sda3      xfs        36G  3.7G   32G  11% //dev/sda1      xfs       509M  138M  372M  28% /boottmpfs          tmpfs     182M     0  182M   0% /run/user/0# mount -a命令自动挂载生效[server root ~] # mount -a[server root ~] # df -ThFilesystem     Type      Size  Used Avail Use% Mounted ondevtmpfs       devtmpfs  900M     0  900M   0% /devtmpfs          tmpfs     910M     0  910M   0% /dev/shmtmpfs          tmpfs     910M  9.5M  901M   2% /runtmpfs          tmpfs     910M     0  910M   0% /sys/fs/cgroup/dev/sda3      xfs        36G  3.7G   32G  11% //dev/sda1      xfs       509M  138M  372M  28% /boottmpfs          tmpfs     182M     0  182M   0% /run/user/0/dev/sdb1      ext4      4.8G   20M  4.6G   1% /mount-point1/dev/sdb5      xfs       5.0G   33M  5.0G   1% /mount-point2/dev/sdb6      ext2      6.0G   12M  5.6G   1% /mount-point3[server root ~] # 

重启之后挂载正常

[server root ~] # rebootChannel(Socket) closed from remote host(centos) at 14:35:29.Type `help' to learn how to use Xshell prompt.[C:\~]$ WARNING! The remote SSH server rejected X11 forwarding request.WARNING! The remote SSH server rejected X11 forwarding request.Last login: Fri Nov 22 14:36:10 2024 from 192.168.18.1[server root ~] # df -ThFilesystem     Type      Size  Used Avail Use% Mounted ondevtmpfs       devtmpfs  900M     0  900M   0% /devtmpfs          tmpfs     910M     0  910M   0% /dev/shmtmpfs          tmpfs     910M  9.6M  901M   2% /runtmpfs          tmpfs     910M     0  910M   0% /sys/fs/cgroup/dev/sda3      xfs        36G  3.7G   32G  11% //dev/sdb5      xfs       5.0G   33M  5.0G   1% /mount-point2/dev/sda1      xfs       509M  138M  372M  28% /boot/dev/sdb6      ext2      6.0G   12M  5.6G   1% /mount-point3/dev/sdb1      ext4      4.8G   20M  4.6G   1% /mount-point1tmpfs          tmpfs     182M     0  182M   0% /run/user/0[server root ~] # 


点击全文阅读


本文链接:http://m.zhangshiyu.com/post/204124.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1