磁盘管理+fdisk+gdisk+mkfs.xfs+mkswap+blkid
对sdb这块盘划分一个100M的分区出来,并格式化为xfs的文件系统
[[email protected] ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
Device does not contain a recognized partition table
使用磁盘标识符 0x401b175c 创建新的 DOS 磁盘标签。
命令(输入 m 获取帮助):n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
分区号 (1-4,默认 1):
起始 扇区 (2048-41943039,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039):+100M
分区 1 已设置为 Linux 类型,大小设为 100 MiB
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
正在同步磁盘。
[[email protected] ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=6400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=25600, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=855, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[[email protected] ~]# mkdir /sdb1
[[email protected] ~]# mount /dev/sdb1 /sdb1
[[email protected] ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 10G 4.7G 5.4G 47% /
devtmpfs 975M 0 975M 0% /dev
tmpfs 992M 0 992M 0% /dev/shm
tmpfs 992M 11M 982M 2% /run
tmpfs 992M 0 992M 0% /sys/fs/cgroup
/dev/sr0 4.2G 4.2G 0 100% /mnt
/dev/sda1 197M 135M 62M 69% /boot
tmpfs 199M 8.0K 199M 1% /run/user/42
tmpfs 199M 0 199M 0% /run/user/0
/dev/sdb1 97M 5.2M 92M 6% /sdb1
解决分区无法卸载的问题
[[email protected] ~]# cd /sdb1
[[email protected] sdb1]# umount /sdb1
umount: /sdb1:目标忙。
(有些情况下通过 lsof(8) 或 fuser(1) 可以
找到有关使用该设备的进程的有用信息)
方法一:cd出去,然后在执行卸载命令。
方法二:一个终端上执行lsof查看进程,另一个终端上杀死这个bash,然后卸载。
开机自动挂载的2种方式
**方式一:**写入配置文件,让它开机自动挂载。vim /etc/fstab
**方式二:**使用UUID来进行开机自动挂载.
blkid查看UUID情况,然后使用以下命令重定向到文件中。
/etc/fstab写入内容的说明
MBR与GPT+gdisk与fdisk类似
gdisk主要是用来划分容量大于2T的硬盘,大于2T fdisk搞不定
GPT分区:GPT,全局唯一标识分区表(GUID Partition Table),
它使用128位GUID来唯一标识每个磁盘和分区,与MBR存在单一故障点不同,GPT提供分区表信息的冗余
一个在磁盘头部一个在磁盘尾部;它通过CRC校验和来检测GPT头和分区表中的错误与损坏;
默认一个硬盘支持128个分区。它没有限制主分区个数。
MBR概述:全称为Master Boot Record,即硬盘的主引导记录。
硬盘的0柱面、0磁头、1扇区称为主引导扇区(也叫主引导记录MBR)。
它由三个部分组成,主引导程序、硬盘分区表DPT(Disk Partition table)和分区有效标志(55AA)。
在总共512字节的主引导扇区里主引导程序(boot loader)占446个字节,
第二部分是Partition table区(分区表),即DPT,占64个字节,硬盘中分区有多少以及每一分区的大小都记在其中。第三部分是magic number,占2个字节,固定为55AA。
分区编号:主分区1-4 ,逻辑分区5……
LINUX规定:逻辑分区必须建立在扩展分区之上,而不是建立在主分区上
分区作用:
主分区:主要是用来启动操作系统的,它主要放的是操作系统的启动或引导程序,/boot分区最好放在主分区上
扩展分区不能使用的,它只是做为逻辑分区的容器存在的;我们真正存放数据的是主分区和逻辑分区,大量数据都放在逻辑分区中
对sdb做gpt分区,创建一个sdb1。
[[email protected] ~]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.6
Partition table scan:
MBR: MBR only
BSD: not present
APM: not present
GPT: not present
***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format.
THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by typing 'q' if
you don't want to convert your MBR partitions to GPT format!
***************************************************************
Command (? for help): n
Partition number (2-128, default 2):
First sector (34-41943006, default = 206848) or {+-}size{KMGTP}:
Last sector (206848-41943006, default = 41943006) or {+-}size{KMGTP}: +1G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot.
The operation has completed successfully.
扩展swap分区
Swap分区在系统的物理内存不够用的时候,把硬盘空间中的一部分空间释放出来,以供当前运行的程序使用。
mkswap /devices (格式化成swap格式)
swapon /swap (**/swap,加入到swap分区中)
vim /etc/fstab (开机自启动新添加的swap分区) ,在最后追加:
/devices swap swap defaults 0 0
如果不想使用需要删除,只需要执行#swapoff /swap
使用新的分区进行扩展swap
#使用gdisk进行分区
[[email protected] ~]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.6
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): E443781A-4F76-4AC9-B286-AA0429923793
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 39845821 sectors (19.0 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 2099199 1024.0 MiB 8300 Linux filesystem
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot.
The operation has completed successfully.
[[email protected] ~]# reboot
#分区格式化+开启分区+关闭
[[email protected] ~]# mkswap /dev/sdb1
mkswap: /dev/sdb1: warning: wiping old xfs signature.
正在设置交换空间版本 1,大小 = 1048572 KiB
无标签,UUID=15b3b984-f3ea-4032-83b2-1b0af7a13565
[[email protected] ~]# free -m
total used free shared buff/cache available
Mem: 1982 410 1187 10 384 1362
Swap: 2047 0 2047
[[email protected] ~]# swapon /dev/sdb1
[[email protected] ~]# free -m
total used free shared buff/cache available
Mem: 1982 410 1187 10 384 1362
Swap: 3071 0 3071
[[email protected] ~]# swapoff /dev/sdb1
[[email protected] ~]# free -m
total used free shared buff/cache available
Mem: 1982 416 1181 10 384 1356
Swap: 2047 0 2047
使用新的文件进行swap扩展
dd if=/dev/zero of=swap_file bs=1M count=500
mkswap /root/swap_file
mkswap -f /root/swap_file
swapon /root/swap_file **
free -m
swapoff /root/swap_file 关闭
swapon -s 查看
dd if=/dev/zero of=swap_file bs=1M count=500
记录了500+0 的读入
记录了500+0 的写出
524288000字节(524 MB)已复制,2.4751 秒,212 MB/秒
[[email protected] ~]# ll /root/swap_file
-rw-------. 1 root root 524288000 3月 28 23:01 /root/swap_file
[[email protected] ~]# mkswap /root/swap_file
mkswap: /root/swap_file: warning: wiping old swap signature.
正在设置交换空间版本 1,大小 = 511996 KiB
无标签,UUID=bfe10c31-8721-45a4-9a32-6ea37d184325
[[email protected] ~]# swapon /root/swap_file
[[email protected] ~]# free -m
total used free shared buff/cache available
Mem: 1982 423 1177 10 381 1349
Swap: 2547 0 2547
[[email protected] ~]# swapoff /root/swap_file
[[email protected] ~]# free -m
total used free shared buff/cache available
Mem: 1982 423 1177 10 381 1349
Swap: 2047 0 2047