Linux下分区的几种方式

本文介绍了在Linux系统中对大容量硬盘进行分区的三种方法:使用mkfs直接创建文件系统,通过fdisk创建GPT分区表并划分分区,以及利用parted命令进行磁盘管理。每种方法都详细阐述了操作步骤和注意事项,特别强调了对于大于2TB硬盘必须使用GPT格式来支持大容量。
摘要由CSDN通过智能技术生成

Linux下分区的几种方式

1:mkfs

直接使用mkfs进行分区

root@unassigned:~# mkfs.ext4 /dev/nvme0n1
mke2fs 1.44.1 (24-Mar-2018)
Discarding device blocks: done
Creating filesystem with 937684566 4k blocks and 234422272 inodes
Filesystem UUID: 2e2a5550-f66a-482c-bfbd-531bd4e2007d
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
        102400000, 214990848, 512000000, 550731776, 644972544

Allocating group tables: done
Writing inode tables: done
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done

root@unassigned:~# mount /dev/nvme0n1 /media/
root@unassigned:~# df
Filesystem      1K-blocks    Used  Available Use% Mounted on
udev              3966572       0    3966572   0% /dev
tmpfs              800112    1000     799112   1% /run
/dev/sda2       114276516 9904312   98521068  10% /
tmpfs             4000552       0    4000552   0% /dev/shm
tmpfs                5120       0       5120   0% /run/lock
tmpfs             4000552       0    4000552   0% /sys/fs/cgroup
/dev/sda1          523248    5372     517876   2% /boot/efi
/dev/nvme0n1   3690725568      28 3503172244   1% /media
root@unassigned:~# lsblk
NAME    MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda       8:0    0 111.8G  0 disk
|-sda1    8:1    0   512M  0 part /boot/efi
`-sda2    8:2    0 111.3G  0 part /
nvme0n1 259:0    0   3.5T  0 disk /media
root@unassigned:~# fdisk -l /dev/nvme0n1
Disk /dev/nvme0n1: 3.5 TiB, 3840755982336 bytes, 7501476528 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

这样的分区方法貌似摒弃了gpt格式设定,直接赋予了文件系统,也可以直接挂载使用
不会有显示 p1等分区号,不过使用起来没有看到有什么异常

2:fdisk

使用fdisk 进行分区

root@unassigned:~# fdisk /dev/nvme0n1

Welcome to fdisk (util-linux 2.31.1).
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.
The size of this disk is 3.5 TiB (3840755982336 bytes). DOS partition table format cannot be used on drives for volumes larger than 2199023255040 bytes for 512-byte sectors. Use GUID partition table format (GPT).

Created a new DOS disklabel with disk identifier 0x58e6f7ab.

直接使用的时候,大容量的盘会遇到这样的报错,是因为默认的DOS格式没办法支持到如此大容量的硬盘分区,他最大只能支持到2TB的硬盘,超过2TB的需要更改格式
你可以使用 m 呼出 帮助菜单,查看命令

Command (m for help): m

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table

如上所示,我们接下来会用到的命令是
g ==>它的意思是创建一个空的GPT格式的partition table,GPT的格式是支持大容量的
n ==>添加一个新的分区
w ==>将table写进盘内,可以认为是使设定生效,然后退出

Command (m for help): g
Created a new GPT disklabel (GUID:xxxx-xxx-xxxx-xxx ).

Command (m for help): p
Disk /dev/nvme0n1: 3.5 TiB, 3840755982336 bytes, 7501476528 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
Disklabel type: gpt
Disk identifier: xxxx-xxx-xxxx-xxx

Command (m for help): n			#输入n新建
Partition number (1-128, default 1):	#partition的编号,默认回车就好,有需要可以按照需求输入规则范围内的数字
First sector (2048-7501476494, default 2048):	#分区起始位置,默认回车就好,有需要可以按照需求输入规则范围内的数字
Last sector, +sectors or +size{K,M,G,T,P} (2048-7501476494, default 7501476494):	#分区结束位置,默认回车就好,有需要可以按照需求输入规则范围内的数字

Created a new partition 1 of type 'Linux filesystem' and of size 3.5 TiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

root@unassigned:~# df
Filesystem     1K-blocks    Used Available Use% Mounted on
udev             3966572       0   3966572   0% /dev
tmpfs             800112    1004    799108   1% /run
/dev/sda2      114276516 9904312  98521068  10% /
tmpfs            4000552       0   4000552   0% /dev/shm
tmpfs               5120       0      5120   0% /run/lock
tmpfs            4000552       0   4000552   0% /sys/fs/cgroup
/dev/sda1         523248    5372    517876   2% /boot/efi
root@unassigned:~# lsblk
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda           8:0    0 111.8G  0 disk
|-sda1        8:1    0   512M  0 part /boot/efi
`-sda2        8:2    0 111.3G  0 part /
nvme0n1     259:0    0   3.5T  0 disk
`-nvme0n1p1 259:1    0   3.5T  0 part
root@unassigned:~# fdisk -l /dev/nvme0n1
Disk /dev/nvme0n1: 3.5 TiB, 3840755982336 bytes, 7501476528 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
Disklabel type: gpt
Disk identifier: 348DF763-44B9-5B41-A4EE-C3044FBA66E7

Device         Start        End    Sectors  Size Type
/dev/nvme0n1p1  2048 7501476494 7501474447  3.5T Linux filesystem

3:parted

使用parted命令进行分区构建
先看看parted 命令的用法吧

root@unassigned:~# parted --help
Usage: parted [OPTION]... [DEVICE [COMMAND [PARAMETERS]...]...]
Apply COMMANDs with PARAMETERS to DEVICE.  If no COMMAND(s) are given, run in
interactive mode.

OPTIONs:
  -h, --help                      displays this help message
  -l, --list                      lists partition layout on all block devices
  -m, --machine                   displays machine parseable output
  -s, --script                    never prompts for user intervention
  -v, --version                   displays the version
  -a, --align=[none|cyl|min|opt]  alignment for new partitions

COMMANDs:
  align-check TYPE N                        check partition N for TYPE(min|opt)
        alignment
  help [COMMAND]                           print general help, or help on
        COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition
        table)
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  name NUMBER NAME                         name partition NUMBER as NAME
  print [devices|free|list,all|NUMBER]     display the partition table,
        available devices, free space, all found partitions, or a particular
        partition
  quit                                     exit program
  rescue START END                         rescue a lost partition near START
        and END
  resizepart NUMBER END                    resize partition NUMBER
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  disk_set FLAG STATE                      change the FLAG on selected device
  disk_toggle [FLAG]                       toggle the state of FLAG on selected
        device
  set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition
        NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  display the version number and
        copyright information of GNU Parted

Report bugs to bug-parted@gnu.org

我们会用到的参数是
-s
mklabel
mkpart

root@unassigned:~# parted -s /dev/nvme0n1 mklabel gpt		#创建gpt格式
root@unassigned:~# parted -s /dev/nvme0n1 mkpart primary 0% 100%	#分区,范围为0%-100%,也可以使用具体的容量,如0% 10G,即起始位置为0%,结束位置为10,分了一个10G分区
root@unassigned:~# df
Filesystem     1K-blocks    Used Available Use% Mounted on
udev             3966572       0   3966572   0% /dev
tmpfs             800112    1004    799108   1% /run
/dev/sda2      114276516 9904312  98521068  10% /
tmpfs            4000552       0   4000552   0% /dev/shm
tmpfs               5120       0      5120   0% /run/lock
tmpfs            4000552       0   4000552   0% /sys/fs/cgroup
/dev/sda1         523248    5372    517876   2% /boot/efi
root@unassigned:~# lsblk
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda           8:0    0 111.8G  0 disk
|-sda1        8:1    0   512M  0 part /boot/efi
`-sda2        8:2    0 111.3G  0 part /
nvme0n1     259:0    0   3.5T  0 disk
`-nvme0n1p1 259:2    0   3.5T  0 part
root@unassigned:~# fdisk -l /dev/nvme0n1
Disk /dev/nvme0n1: 3.5 TiB, 3840755982336 bytes, 7501476528 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
Disklabel type: gpt
Disk identifier: 2B26A756-7750-4B95-9833-52EFC967DB32

Device         Start        End    Sectors  Size Type
/dev/nvme0n1p1  2048 7501475839 7501473792  3.5T Linux filesystem
root@unassigned:~#
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值