磁盘分区

磁盘分区
1、fdisk命令的使用(即分区方式)
[root@localhost ~]# fdisk -l

Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 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: 0x00013f3e

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20970332 10484142+ 83 Linux

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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
[root@localhost ~]# fdisk /dev/vdb ##划分/dev/vdb,未保存时信息保存在内存中
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 0xc7f34c27.

Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel ##设定设备标签
c toggle the dos compatibility flag
d delete a partition ##删除设备
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types ##列出设备类型
m print this menu
n add a new partition ##新建分区
o create a new empty DOS partition table
p print the partition table ##显示分区表
q quit without saving changes ##退出分区工具
s create a new empty Sun disklabel
t change a partition’s sykstem id ##改变分区功能标签
u change display/entry units
v verify the partition table
w write table to disk and exit ##保存更改的分区表,并将信息写入硬盘
x extra functionality (experts only)

Command (m for help): n
Partition type: ##指定划分分区设备的类型
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): ##指定使用的分区表号
First sector (2048-20971519, default 2048): ##分区起始块,选择默认
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +1G ##设备大小指定
Partition 1 of type Linux and of size 1GiB is set
Command (m for help): p ##显示分区表

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0xc7f34c27

Device Boot Start End Blocks Id System
/dev/vdb1 2048 2099199 1048576 83 Linux ##被划分出来的分区

Command (m for help): wq 保存并退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

[root@localhost ~]# partprobe 同步系统分区表

[root@localhost ~]# cat /proc/partitions ##查看系统能识别的分区
major minor #blocks name

253 0 10485760 vda
253 1 10484142 vda1
253 16 10485760 vdb
253 17 1048576 vdb1
11 0 3655680 sr0
2、如何使用新建的分区?

格式化
挂载
vim /etc/fstab 实现永久挂载

**具体操作如下:
[root@localhost ~]# fdisk /dev/vdb ##划分分区
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): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +100M
Partition 1 of type Linux and of size 100 MiB is set

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

Calling ioctl() to re-read partition table.
Syncing disks.

[root@localhost ~]# partprobe ##同步系统分区表
[root@localhost ~]# cat /proc/partitions ##查看系统能识别的分区
major minor #blocks name

253 0 10485760 vda
253 1 10484142 vda1
253 16 10485760 vdb
253 17 102400 vdb1
11 0 3655680 sr0

[root@localhost ~]# mkfs.ext1 /dev/vdb1 ##将设备/dev/vdb1格式化为ext1格式
[root@localhost ~]# mount /dev/vdb1 /mnt/ ##临时挂载设备
[root@localhost ~]# df -h ##查看所有挂载设备
Filesystem Size Used Avail Use% Mounted on
… … … …
/dev/vdb1 93M 1.6M 85M 2% /mnt

[root@localhost ~]# vim /etc/fstab ##开机自动挂载设备
10 /dev/vdb1 /mnt ext4 defaults 0 0

此处检测mount -a 的功能,即重新读取/etc/fstab文件的内容
[root@localhost ~]# umount /dev/vdb1
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
tmpfs 921M 0 921M 0% /sys/fs/cgroup
/dev/sr0 3.5G 3.5G 0 100% /run/media/root/RHEL-7.0 Server.x86_64
[root@localhost ~]# mount -a
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
… … … …
/dev/sr0 3.5G 3.5G 0 100% /run/media/root/RHEL-7.0 Server.x86_64
/dev/vdb1 93M 1.6M 85M 2% /mnt

3、删除设备
卸载
删除/etc/fstab重设备的自动挂载条目
用fdisk删除分区
[root@localhost ~]# umount /dev/vdb1
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
… … … …
/dev/sr0 3.5G 3.5G 0 100% /run/media/root/RHEL-7.0 Server.x86_64
[root@localhost ~]# vim /etc/fstab ##删除文件中该设备自动激活条目
[root@localhost ~]# fdisk /dev/vdb
… … … …
Command (m for help): d ##删除设备
Selected partition 1
Partition 1 is deleted

Command (m for help): wq
The partition table has been altered!
[root@localhost ~]# partprobe
[root@localhost ~]# cat /proc/partitions
major minor #blocks name

253 0 10485760 vda
253 1 10484142 vda1
253 16 10485760 vdb
11 0 3655680 sr0

4、swap
[root@localhost ~]# swapon -s ##查看存在的swap分区

**(1)在不重装系统的情况下扩大swap分区,即建立swap分区
建立swap分区
格式化为swap格式
[root@localhost ~]# fdisk /dev/vdb 划分设备并将其分区标签改为swap
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): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +4G
Partition 1 of type Linux and of size 4 GiB is set

Command (m for help): t 改变设备分区的标签
Selected partition 1
Hex code (type L to list all codes): L 查看分区标签

0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
… …. …. …. ##此处省略部分内容
Hex code (type L to list all codes): 82 ##swap标签的代码为82
Changed type of partition ‘Linux’ to ‘Linux swap / Solaris’

Command (m for help): p 显示分区

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0xc7f34c27

Device Boot Start End Blocks Id System
/dev/vdb1 2048 8390655 4194304 82 Linux swap / Solaris

Command (m for help): wq
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 ~]# mkswap /dev/vdb1 格式化分区
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=b2d80d42-8a8b-41bb-902d-5ab17a1807ac
[root@localhost ~]# swapon -a /dev/vdb1 临时激活swap

[root@localhost ~]# vim /etc/fstab 开机自动激活swap
10 /dev/vdb2 swap swap defaults 0 0
激活swap 文件格式为swap
[root@localhost ~]# swapon -s ##查看系统中的swap分区
Filename Type Size Used Priority
/dev/vdb2 partition 4194300 0 -1
swapon -a的功能,即重新读取配置文件
[root@localhost ~]# swapoff /dev/vdb1 ##关闭激活的swap分区
[root@localhost ~]# swapon -s ##在此处查看没有swap分区
[root@localhost ~]# swapon -a ##让系统重新读取/etc/fstab文件的内容,swap分区又生效
[root@localhost ~]# swapon -s ##在此处查看有swap分区
Filename Type Size Used Priority
/dev/vdb2

**(2)删除扩大的swap分区

[root@localhost ~]# vim /etc/fstab ##删除文件中swap自动激活条目
[root@localhost ~]# swapoff /dev/vdb1 ##关闭激活的swap分区
[root@localhost ~]# swapon -s ##查看当前存在的swap分区

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值