Unit13
磁盘管理(上)
1、磁盘的扇区
磁盘的第一个扇区主要记录了两个重要的信息:
主引导分区(MBR):可以安装引导加载程序的地方,有446bytes
分区表:记录整块硬盘分区的状态,有64bytes
硬盘有效性标示
主分区
扩展分区
逻辑分区
MBR是很重要的,因为当系统在开机的时候会主动去读取这个区块的内容
fdisk -l ##系统中的所有磁盘设备
df -TH ##系统正在挂载的磁盘设备
blkid ##系统可以挂载的磁盘设备id
2、分区划分
fidsk /dev/vdb
[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): 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 system id ##修改分区功能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 (1 primary, 0 extended, 3 free) ##分区类型为主分区
e extended ##分区类型为扩展分区
Select (default p): p
Partition number (2-4, default 2): 2 ##主分区id
First sector (206848-20971519, default 206848): ##此分区的起始位置
Using default value 206848
Last sector, +sectors or +size{K,M,G} (206848-20971519, default 20971519): +200M ##分区大小
Partition 2 of type Linux and of size 200 MiB is set
Command (m for help): wq ##**保存并退出,只输入q则表示放弃更改退出**
The partition table has been altered!
partprobe ##同步分区表
mkfs.xfs /dev/vdb1 ##格式化
mount /dev/vdb1 /mnt ##临时挂载
vim /etc/fstab ##永久挂载
编写格式:
device mountpoint ftype defaults(mountpoint) 0 0
/dev/vdb1 /mnt xfs defaults 0 0
mount -a ##使/etc/fastab中记录的挂载生效
分区删除
先使用umount命令解除挂载
PS:当挂载点被使用时,无法解除挂载,需要用fuser -kvm结束使用的进程,然后进行解除挂载
解除挂载之后,使用fdisk /dev/vdb命令 删除分区
5.设定分区方式,将mkdocs 改为 gpt
更改之前需要把正在使用的分区关闭,才可以进行更改
partoff /dev/vdb2
parted /dev/vdb
添加swap分区
使用磁盘分区
[root@localhost ~]# fdisk /dev/vdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
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 number (2-128, default 2):
First sector (34-20971486, default 2099200):
Last sector, +sectors or +size{K,M,G,T,P} (2099200-20971486, default 20971486): +4G
Created partition 2
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: gpt
tart End Size Type Name
1 2048 2099199 1G Linux filesyste
2 2099200 10487807 4G Linux filesyste
Command (m for help): t
Partition number (1,2, default 2): 2
Partition type (type L to list all types): l
**14 Linux swap 0657FD6D-A4AB-43C4-84E5-0933C84B4F4F**
Partition type (type L to list all types): 14
Changed type of partition 'Linux filesystem' to 'Linux swap'
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: gpt
Start End Size Type Name
1 2048 2099199 1G Linux filesyste
2 2099200 10487807 4G Linux swap
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 ~]#
[root@localhost ~]# partprobe ##同步分区表
[root@localhost ~]# mkswap /dev/vdb2 ##格式化成swap类型
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=b933737a-1409-48b5-8f92-d663d388134b
[root@localhost ~]# swapon -a /dev/vdb2 ##加入swap
[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/dev/vdb2 partition 4194300 0 -1
[root@localhost ~]#
使用文件添加swap
dd if=/dev/zero of=/swapfile bs=1M count=1000 ##创建一个1G大小的文件
mkswap /swapfile ##格式化为swap类型
swapon -a /swapfile ##临时添加到swap里
-p + 数字 ##更改优先级
vim /etc/fstab ##永久添加swap分区
类型:
/swapfile swap swap defaults,pri=1 0 0 #pri优先级
删除swap
vim /etc/fstab ##删除此文件中添加的swap行
swapoff /swapfile ##断开swap文件链接
swapoff /dev/vdb1 ##断开swap磁盘链接
rm -rf /swapfile ##删除文件
fdisk /dev/vdb ##删除磁盘分区
partprobe ##同步分区表