✔ linux磁盘是可以分区
✔ linux磁盘需要分区后进行挂载
✔ linux磁盘最多可以建立四个主分区,其余为逻辑分区
一、摸清情况
1、查看服务器磁盘情况
fdisk -l结果:
Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x000b2d99
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 83875364 41936658+ 83 Linux
Disk /dev/vdb: 322.1 GB, 322122547200 bytes, 629145600 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大致意思如下表:
序号 | 名称 | 大小 |
1 | /dev/vda | 40G |
2 | /dev/vdb | 300G |
从上面我们还能得到一个信息:/dev/vda磁盘,进行了分区,且只有一个分区。
2、查看磁盘挂载情况
df -h结果:
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 1.8G 36G 5% /
devtmpfs 7.7G 0 7.7G 0% /dev
tmpfs 7.7G 0 7.7G 0% /dev/shm
tmpfs 7.7G 436K 7.7G 1% /run
tmpfs 7.7G 0 7.7G 0% /sys/fs/cgroup
tmpfs 1.6G 0 1.6G 0% /run/user/0很明显结果上面没有/dev/vdb,说明这个磁盘没有进行挂载,暂时还不能使用。
二、磁盘分区
1、进入磁盘
fdisk /dev/vdbWelcome 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 0xd29696ca.2、输入n,进行分区
Command (m for help): nPartition type:
p primary (0 primary, 0 extended, 4 free)
e extended3、输入p,选择进行主分区
Select (default p): p4、输入1,划分1个主分区
输入2,则可以划分两个主分区;
输入3,即为3个分区;
输入4,为4个分区;
一个磁盘,最多有4个主分区,其余的为逻辑分区。这里的目的是将磁盘分为一个区,并全部挂载。
Partition number (1-4, default 1): 15、直接回车,选择默认的起始
这里可以自己设置分区的大小,输入“+5G”,则表示划分5个G的空间出来作为分区。
我这里需要全盘做一个分区,所以就直接回车,选择默认的起始
First sector (2048-629145599, default 2048):
Using default value 20486、直接回车,选择默认的终点
这里也是直接回车,选择默认的终点
Last sector, +sectors or +size{K,M,G} (2048-629145599, default 629145599):
Using default value 629145599
Partition 1 of type Linux and of size 300 GiB is set7、检查分区情况
输入p,检查分区的情况
Command (m for help): pDisk /dev/vdb: 322.1 GB, 322122547200 bytes, 629145600 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: 0xd29696ca
Device Boot Start End Blocks Id System
/dev/vdb1 2048 629145599 314571776 83 Linux看到最后两行,就说明已经分好区了。
8、保存
输入w,即为保存;
输入q,即为退出。
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.9、检查分区情况
fdisk -lDisk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x000b2d99
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 83875364 41936658+ 83 Linux
Disk /dev/vdb: 322.1 GB, 322122547200 bytes, 629145600 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: 0xd29696ca
Device Boot Start End Blocks Id System
/dev/vdb1 2048 629145599 314571776 83 Linux现在已经分区完成了,接下来进行挂载。
三、挂载磁盘
1、格式化分区
使用mkfs命令,将磁盘格式化为ext4。
mkfs.ext4 /dev/vdb1这两个命令等价
mkfs -t ext4 /dev/vdb12、挂载
先创建一个文件夹
mkdir /test把这个文件夹进行挂载
mount /dev/vdb /test注意:
挂载之后,文件夹内的原有文件看不见了,但是没有消失,unmount之后才能看见。
3、检查挂载情况
df -hFilesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 1.8G 36G 5% /
devtmpfs 7.7G 0 7.7G 0% /dev
tmpfs 7.7G 0 7.7G 0% /dev/shm
tmpfs 7.7G 444K 7.7G 1% /run
tmpfs 7.7G 0 7.7G 0% /sys/fs/cgroup
/dev/vdb1 296G 65M 281G 1% /test
tmpfs 1.6G 0 1.6G 0% /run/user/0挂载成功!
4、设置开机自动挂载
修改配置文件/etc/fstab,增加一句话
vi /etc/fstab/dev/vdb1 /data ext4 defaults 0 0或者用一个命令:
echo "/dev/vdb1 /data ext4 defaults 0 0" >> /etc/fstab四、取消挂载
unmount 就是取消挂载的命令。
unmount /dev/vdb1 /test这个命令就能取消原先设置的挂载;
如果要恢复原样,记得把配置文件里增加的一句话删除哦!
五、小结
唯有大胆尝试,方能突破囹圄!
















