如何在Centos 7上硬盘分区

avatar 2019年9月27日22:01:01 评论 1,254 次浏览

如果自己的电脑空间不够了怎么办,添加硬盘呀。不过我的是linux系统,添加硬盘不会怎么办,来呀,浪呀,哥哥教你加硬盘呀。。。。。。。

本次测试在虚拟机上测试的,生产环境一样,把硬盘插进去,后面的事就是这样操作的。先看一下我添加的硬盘:

[root@wulaoer ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: dos
Disk identifier: 0x000adbbf

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM

Disk /dev/sdb: 68.7 GB, 68719476736 bytes, 134217728 sectors       #这是新添加的硬盘
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes

先做一个分区的操作,看一下例子:

[root@wulaoeer ~]# 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 table
Building a new DOS disklabel with disk identifier 0xa912cd7b.

The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance may be impacted.

Command (m for help): n    #创建新分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p         #创建主分区,e扩展分区,只能有一个扩展分区
Partition number (1-4, default 1):          #分区编号
First sector (2048-134217727, default 2048):        #硬盘初始块
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-134217727, default 134217727):        #结束块
Using default value 134217727
Partition 1 of type Linux and of size 64 GiB is set

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@wulaoer ~]#

硬盘创建好分区之后需要格式化一下才可以使用,所以先看一下创建的硬盘:

[root@wulaoer ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: dos
Disk identifier: 0x000adbbf

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM

Disk /dev/sdb: 68.7 GB, 68719476736 bytes, 134217728 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: dos
Disk identifier: 0xa912cd7b

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   134217727    67107840   83  Linux        #新创建的硬盘和上面的初始块和结束块就是定义的空间大小

Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes

格式化一下:

[root@wulaoer ~]# mkfs.ext4  /dev/sdb1     
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
4194304 inodes, 16776960 blocks
838848 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2164260864
512 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

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

硬盘已经格式化了,但是系统找不到,还是不能使用,所以需要挂载到硬盘上面:

[root@wulaoer ~]# mkdir /mnt/db       #创建一个空目录
[root@wulaoer ~]# mount /dev/sdb1 /mnt/db        #把硬盘挂载到空目录上
[root@wulaoer ~]# echo  "/dev/sdb1  /mnt/db   ext4  defaults  0  0"  >>  /etc/fstab     #给系统文件也需要修改,重启自动生效
[root@wulaoer ~]# ll /mnt/db/
total 16
drwx------. 2 root root 16384 Sep 27 15:48 lost+found        #查看一下挂在的文件
[root@wulaoer ~]#

硬盘的添加就这样,是不是特别简单。

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: