在Linux下,有些时候要给现有的磁盘增加一个分区,当然前提是磁盘还有未分区空间,这里介绍一下基本的分区过程。

操作系统:CentOS 6.5 (64bit)

1、查看磁盘分区情况

先查看一下当前的磁盘空间情况:

[root@localhost ~]$df -lh
Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/vg_localhost-lv_root   50G   42G  5.2G  90% /
tmpfs                             5.9G     0  5.9G   0% /dev/shm
/dev/sda1                         485M   39M  421M   9% /boot
/dev/mapper/vg_localhost-lv_home   42G   26G   14G  65% /home

磁盘加上后,用

1
fdisk -l

查看一下当前磁盘情况:

[root@localhost ~]$fdisk -l

Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000a75cc

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64       13055   104344576   8e  Linux LVM

Disk /dev/mapper/vg_localhost-lv_root: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/vg_localhost-lv_swap: 8405 MB, 8405385216 bytes
255 heads, 63 sectors/track, 1021 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/vg_localhost-lv_home: 44.8 GB, 44753223680 bytes
255 heads, 63 sectors/track, 5440 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

从这里可以看出,当前磁盘空间

1
/dev/sda: 214.7 GB

从当前的使用情况看,明显还存在100G左右的空间没有使用,说明可以增加分区。

2、增加磁盘分区

输入

1
fdisk /dev/sda

,根据提示输入命令新建分区,这里把所有的剩余空间分成了一个分区

1
/dev/sda3

,然后输入w保存分区表。

[root@localhost ~]$fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (13055-26108, default 13055):
Using default value 13055
Last cylinder, +cylinders or +size{K,M,G} (13055-26108, default 26108):
Using default value 26108

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.

再次查看分区表:

[root@localhost ~]$fdisk -l

Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000a75cc

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64       13055   104344576   8e  Linux LVM
/dev/sda3           13055       26108   104854910   83  Linux

Disk /dev/mapper/vg_localhost-lv_root: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/vg_localhost-lv_swap: 8405 MB, 8405385216 bytes
255 heads, 63 sectors/track, 1021 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/vg_localhost-lv_home: 44.8 GB, 44753223680 bytes
255 heads, 63 sectors/track, 5440 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

新的分区

1
/dev/sda3

已经出现。

3、格式化新磁盘分区‘/dev/sda3’

此时,由于系统没有识别到新的分区表,因此无法直接对新分区进行格式化,需要重启系统(也有其他让系统直接识别新分区表的方法,但试了一下没有生效,因此采用重启系统的方法)

[root@localhost ~]$reboot

Broadcast message from root@localhost.zhaowy01
        (/dev/pts/0) at 16:31 ...

The system is going down for reboot NOW!

重启之后,可以对新分区进行格式化:

[root@localhost ~]$mkfs.ext4 /dev/sda3
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
6553600 inodes, 26213727 blocks
1310686 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
800 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

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

This filesystem will be automatically checked every 36 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

4、挂载新磁盘分区‘/dev/sda3’

新分区格式化之后,挂载到指定目录上,即可以正常使用:

[root@localhost ~]$mount /dev/sda3 /home/data

查看当前磁盘空间:

[root@localhost ~]$df -lh
Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/vg_localhost-lv_root   50G   42G  5.2G  90% /
tmpfs                             5.9G     0  5.9G   0% /dev/shm
/dev/sda1                         485M   39M  421M   9% /boot
/dev/mapper/vg_localhost-lv_home   42G   26G   14G  65% /home
/dev/sda3                          99G  188M   94G   1% /home/data

新分区已经加入。

最后更新: 2019年04月19日 21:37

原始链接: http://andersjing.com/2016/09/09/2016-09-09-linux_disk/

× 请打赏~
打赏二维码