linux中磁盘的管理

来源:互联网 发布:软件锁许可管理器 cad 编辑:程序博客网 时间:2024/05/16 17:12

1.名词解释

mbr分区具有局限性。存在于驱动器开始部分的一个特殊扇区,MBR支持最大2TB磁盘,它无法处理大于2TB容量的磁盘。MBR还只支持最多4个主分区——如果你想要更多分区,你需要创建所谓“扩展分区”,并在其中创建逻辑分区。

2.磁盘查看命令

fdisk -l ##系统中的所有磁盘设备
df -TH ##系统正在挂载的磁盘设备
blkid ##系统可以挂载的磁盘设备id

3.分区划分

fidsk /dev/vdb
这里写图片描述

图中的命令解释如下:[root@localhost ~]# 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.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): pPartition number (2-4, default 2): 2        **主分区id**First sector (206848-20971519, default 206848): **此分区的起始位置**Using default value 206848Last sector, +sectors or +size{K,M,G} (206848-20971519, default 20971519): +200M **分区大小**Partition 2 of type Linux and of size 200 MiB is setCommand (m for help): wq   **保存并退出,只输入q则表示放弃更改退出**The partition table has been altered!Calling ioctl() to re-read partition table.Syncing disks.
[root@localhost ~]# partprobe       **同步分区表**mkfs.xfs /dev/vdb1  **格式化**mount /dev/vdb1  /mnt   **临时挂载**vim /etc/fstab      **永久挂载**

这里写图片描述
图为临时挂载/dev/vdb2到/mnt/上,df表示查看是否挂载成功。

编写格式:

/dev/vdb1 /mnt xfs defaults 0 0
mount -a 使/etc/fastab中记录的挂载生效
这里写图片描述
图为在/etc/fstab/中永久挂载/dev/vdb2到 /mnt/上

4.删除分区

(1)先使用umount命令解除挂载
注:当挂载点被使用时,无法解除挂载,需要用fuser -kvm结束使用的进程,然后进行解除挂载
(2) 解除挂载之后,使用fdisk /dev/vdb命令 删除分区
这里写图片描述
图为解除临时挂载,解除永久挂载需要打开/etc/fstab/删除挂载项。

5.设定分区方式,将mkdocs 改为 gpt

gtp相比于mbr的优势

GPT意为GUID分区表。(GUID意为全局唯一标识符)。这是一个正逐渐取代MBR的新标准。它和UEFI相辅相成——UEFI用于取代老旧的BIOS,而GPT则取代老旧的MBR。之所以叫作“GUID分区表”,是因为你的驱动器上的每个分区都有一个全局唯一的标识符(globally unique identifier,GUID)——这是一个随机生成的字符串,可以保证为地球上的每一个GPT分区都分配完全唯一的标识符。
这个标准没有MBR的那些限制。磁盘驱动器容量可以大得多,大到操作系统和文件系统都没法支持。它同时还支持几乎无限个分区数量,限制只在于操作系统——Windows支持最多128个GPT分区,而且你还不需要创建扩展分区。
在MBR磁盘上,分区和启动信息是保存在一起的。如果这部分数据被覆盖或破坏,事情就麻烦了。相对的,GPT在整个磁盘上保存多个这部分信息的副本,因此它更为健壮,并可以恢复被破坏的这部分信息。GPT还为这些信息保存了循环冗余校验码(CRC)以保证其完整和正确——如果数据被破坏,GPT会发觉这些破坏,并从磁盘上的其他地方进行恢复。而MBR则对这些问题无能为力——只有在问题出现后,你才会发现计算机无法启动,或者磁盘分区都不翼而飞了。
兼容性
使用GPT的驱动器会包含一个“保护性MBR”。这种MBR会认为GPT驱动器有一个占据了整个磁盘的分区。如果你使用老实的MBR磁盘工具对GPT磁盘进行管理,你只会看见一个占据整个磁盘的分区。这种保护性MBR保证老式磁盘工具不会把GPT磁盘当作没有分区的空磁盘处理而用MBR覆盖掉本来存在的GPT信息。
在基于UEFI的计算机系统上,所有64位版本的Windows 8.1、8、7和Vista,以及其对应的服务器版本,都只能从GPT分区启动。所有版本的Windows 8.1、8、7和Vista都可以读取和使用GPT分区。
其他现代操作系统也同样支持GPT。Linux内建了GPT支持。苹果公司基于Intel芯片的MAC电脑也不再使用自家的APT(Apple Partition Table),转而使用GPT。

更改之前需要把正在使用的分区关闭,才可以进行更改
partoff /dev/vdb2
parted /dev/vdb

6.添加swap分区

fdisk /dev/vdb ##添加一个分区
这里写图片描述

[root@localhost ~]# fdisk /dev/vdbWARNING: 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): **+1G****给定一个大小**Created partition 2Command (m for help): **t****更改分区方式**Partition number (1,2, default 2): 2Partition type (type L to list all types): **l****查看分区的信息以便于选择自己想要的分区格式**

这里写图片描述
图为“l“查看的分区类型信息,选择swap对应的数字或者字母,在这里为82。
这里写图片描述
图为“p“命令查看是否成功转为swap类型。

[root@localhost ~]# partprobe      **同步分区表**[root@localhost ~]# mkswap /dev/vdb1   **格式化成swap类型**[root@localhost ~]# swapon -a /dev/vdb1 **加入swap**[root@localhost ~]# swapon -sFilename                Type        Size    Used    Priority/dev/vdb1                               partition   1048572 0   -1

这里写图片描述

7.用文件来添加swap

目的:当系统中的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优先级
这里写图片描述

9.删除swap

vim /etc/fstab  **删除此文件中添加的swap行**swapoff /swapfile **断开swap文件链接**swapoff /dev/vdb1 **断开swap磁盘链接**rm -rf /swapfile  **删除文件**fdisk /dev/vdb    **删除磁盘分区**partprobe     **同步分区表**

这里写图片描述
这里写图片描述
图为清除swap,以及swap文件分区。

原创粉丝点击