Centos格式化挂载

fdisk命令

在Linux系统中,管理硬盘设备最常用的方法就当属fdisk命令了。fdisk命令用于管理磁盘分区,格式为“fdisk [磁盘名称]”,它提供了集添加、删除、转换分区等功能于一身的“一站式分区服务”。不过与前面讲解的直接写到命令后面的参数不同,这条命令的参数是交互式的,因此在管理硬盘设备时特别方便,可以根据需求动态调整。下面列出fdisk命令中的参数以及作用。


参数     作用
m        查看全部可用的参数
n        添加新的分区
d        删除某个分区信息
l        列出所有可用的分区类型
t        改变某个分区的类型
p        查看分区表信息
w        保存并退出
q        不保存直接退出

首先 fdisk -l 查看现有的硬盘,确定要格式化挂载的硬盘(查看磁盘分区)

[root@localhost ~]# fdisk -l

Disk /dev/sda: 1154.5 GB, 1154486370304 bytes, 2254856192 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: 0x000e06d1

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048   104857599    51915776   8e  Linux LVM

Disk /dev/mapper/centos-root: 49.0 GB, 48951721984 bytes, 95608832 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 /dev/mapper/centos-swap: 4160 MB, 4160749568 bytes, 8126464 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

可以看到磁盘已经划分好sda1和sda2两个分区, 我们需要在/dev/sda上再划分一个分区。

(1)我们首先使用fdisk命令来尝试管理/dev/sda硬盘设备。在看到提示信息后输入参数p来查看硬盘设备内已有的分区信息,其中包括了硬盘的容量大小、扇区个数等信息(输入p来打印出分区表):

[root@localhost ~]# fdisk /dev/sda
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/sda: 1154.5 GB, 1154486370304 bytes, 2254856192 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: 0x000e06d1

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048   104857599    51915776   8e  Linux LVM

Command (m for help): 

(2)输入参数n尝试添加新的分区。系统会要求您是选择继续输入参数p来创建主分区,还是输入参数e来创建扩展分区。这里输入参数p来创建一个主分区:

Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3):

(3)在确认创建一个主分区后,系统要求您先输入主分区的编号。我们在前文得知,主分区的编号范围是1~4,因此这里输入默认的3就可以了。接下来系统会提示定义起始的扇区位置(First sector (104857600-2254856191, default 104857600):),这不需要改动,我们敲击回车键保留默认设置即可,系统会自动计算出最靠前的空闲扇区的位置。最后,系统会要求定义分区的结束扇区位置,这其实就是要去定义整个分区的大小是多少。我们不用去计算扇区的个数,只需要输入+500G即可创建出一个容量为500GB的硬盘分区。

 Partition number (3,4, default 3): 3
First sector (104857600-2254856191, default 104857600): 
Using default value 104857600
Last sector, +sectors or +size{K,M,G} (104857600-2254856191, default 2254856191): +500G
Partition 3 of type Linux and of size 500 GiB is set

Command (m for help): 

(4)再次使用参数p来查看硬盘设备中的分区信息。果然就能看到一个名称为/dev/sda3、起始扇区位置为104857600、结束扇区位置为1153433599的主分区了。这时候千万不要直接关闭窗口,而应该敲击参数w后回车,这样分区信息才是真正的写入成功。

Command (m for help): p

Disk /dev/sda: 1154.5 GB, 1154486370304 bytes, 2254856192 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: 0x000e06d1

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048   104857599    51915776   8e  Linux LVM
/dev/sda3       104857600  1153433599   524288000   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

(5)在上述步骤执行完毕之后,Linux系统会自动把这个硬盘主分区抽象成/dev/sda3设备文件。我们可以使用file命令查看该文件的属性,但是刘遄老师在讲课和工作中发现,有些时候系统并没有自动把分区信息同步给Linux内核,而且这种情况似乎还比较常见(但不能算作是严重的bug)。我们可以输入partprobe命令手动将分区信息同步到内核,而且一般推荐连续两次执行该命令,效果会更好。如果使用这个命令都无法解决问题,那么就重启计算机吧。

[root@localhost ~]# file /dev/sda3
/dev/sda3: cannot open (No such file or directory)
[root@localhost ~]# partprobe 
[root@localhost ~]# partprobe 
[root@localhost ~]# file /dev/sda3
/dev/sda3: block special

(6)如果硬件存储设备没有进行格式化,则Linux系统无法得知怎么在其上写入数据。因此,在对存储设备进行分区后还需要进行格式化操作。在Linux系统中用于格式化操作的命令是mkfs。这条命令很有意思,因为在Shell终端中输入mkfs名后再敲击两下用于补齐命令的Tab键,会有如下所示的效果:

[root@localhost ~]# mkfs
mkfs         mkfs.btrfs   mkfs.cramfs  mkfs.ext2    mkfs.ext3    mkfs.ext4    mkfs.fat     mkfs.minix   mkfs.msdos   mkfs.vfat    mkfs.xfs

使用mkfs.ext4格式化/dev/sda3

[root@localhost ~]# mkfs.ext4 /dev/sda3
mke2fs 1.42.9 (28-Dec-2013)
Discarding device blocks: done                            
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
32768000 inodes, 131072000 blocks
6553600 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2279604224
4000 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
    4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 
    102400000

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

终于完成了存储设备的分区和格式化操作,接下来就是要来挂载并使用存储设备了。与之相关的步骤也非常简单:首先是创建一个用于挂载设备的挂载点目录;然后使用mount命令将存储设备与挂载点进行关联;最后使用df -h命令来查看挂载状态和硬盘使用量信息,这里我们将新格式化的分区挂在到/opt目录下。

[root@localhost ~]# mount /dev/sda3 /opt/
[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   46G  974M   45G   3% /
devtmpfs                 1.9G     0  1.9G   0% /dev
tmpfs                    1.9G     0  1.9G   0% /dev/shm
tmpfs                    1.9G  8.4M  1.9G   1% /run
tmpfs                    1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1                497M  121M  376M  25% /boot
/dev/sda3                493G   73M  467G   1% /opt

猜你喜欢

转载自blog.csdn.net/qq_30982323/article/details/80991008
今日推荐