磁盘命令规则:
硬件设备: /dev/
/dev/sd* (scsi sata sas usb)
sda1 第一块磁盘的第一个分区
sda5 第一块磁盘的第一个逻辑分区
/dev/sr* (sata ide)
cdrom
#hd (IDE)
分区步骤:
-
为机器添加新的磁盘
编辑虚拟机的硬件设备(硬盘、光驱、网卡)
-
a. 先对MBR分区表的磁盘进行分区
fdisk /dev/sdb
主:n--p--1 --起始点--结束点
扩:n--e--2 --起始点--结束点
逻辑:n--l--起始点--结束点
w 保存分区操作
d 删除分区
MBR分区
[root@chenyun ~]# fdisk /dev/sdb
Command (m for help): m
Command action
a toggle a bootable flag --- 切换引导方式
b edit bsd disklabel --- 切换bsd磁盘标签
c toggle the dos compatibility flag --- 切换可兼容性DOS标签
d delete a partition --- 删除分区
g create a new empty GPT partition table --- 创建GPT分区表
G create an IRIX (SGI) partition table ---创建一个IRIX (SGI)分区表
l list known partition types --- 列出系统可用的分区类型
m print this menu --- 获取菜单信息(帮助信息)
n add a new partition --- 新建分区
o create a new empty DOS partition table --- 创建一个新的DOS表
p print the partition table --- 显示当前分区信息
q quit without saving changes --- 不保存退出
s create a new empty Sun disklabel --- 创建一个新的Sun标签
t change a partition's system id ---改变分区的system 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): 1 默认为主分区
First sector (2048-41943039, default 2048): --- 分区起始点
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +10G ---分区结束点
Partition 1 of type Linux and of size 10 GiB is set
扩展分区
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): e
Partition number (2-4, default 2): 2
First sector (20973568-41943039, default 20973568):
Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-41943039, default 41943039): +5G
Partition 2 of type Extended and of size 5 GiB is set
创建swap分区(主分区才能创建swap)
Command (m for help): t --- 改变磁盘system id
Partition number (1,2, default 2): 1
Hex code (type L to list all codes): b8 --- swap分区system id
Changed type of partition 'Linux' to 'BSDI swap'
Command (m for help): w ---保存退出
Command (m for help): p --- 查看当前分区信息
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x67a3630d
Device Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 b8 BSDI swap
/dev/sdb2 20973568 31459327 5242880 5 Extended
GPT分区
[root@chenyun ~]# parted /dev/sdb
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) m
align-check TYPE N check partition N for TYPE(min|opt) alignment
help [COMMAND] print general help, or help on COMMAND
mklabel,mktable LABEL-TYPE create a new disklabel (partition table)
mkpart PART-TYPE [FS-TYPE] START END make a partition
name NUMBER NAME name partition NUMBER as NAME
print [devices|free|list,all|NUMBER] display the partition table, available devices, free space, all found
partitions, or a particular partition
quit exit program
rescue START END rescue a lost partition near START and END
resizepart NUMBER END resize partition NUMBER
rm NUMBER delete partition NUMBER
select DEVICE choose the device to edit
disk_set FLAG STATE change the FLAG on selected device
disk_toggle [FLAG] toggle the state of FLAG on selected device
set NUMBER FLAG STATE change the FLAG on partition NUMBER
toggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBER
unit UNIT set the default unit to UNIT
version display the version number and copyright information of GNU Parted
磁盘GPT分区
partetd /dev/sdb
mklabel 修改分区表类型
print 打印已存在分区信息
mkpart 创建新的分区
name 给分区重命名
rm 按照分区序号进行删
quit 退出交互模式
#所有操作立即生效
(parted) mklabel --- 修改分区类型
New disk label type? GPT --- 分区为GPT分区
Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to
continue?
Yes/No? Y
(parted) mkpart
Partition name? []? /dev/sdb1 --- 给分区命名
File system type? [ext2]? xfs --- 修改文本系统类型
Start? 0 --- 分区起始点
End? 10G --- 分区结束点
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? i
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 17.4kB 10.0GB 10000MB /dev/sdb1
[root@chenyun ~]# fdisk -l
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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
Disk identifier: 8190656E-C04F-4367-BD92-25A9EA463C74
# Start End Size Type Name
1 34 19531250 9.3G Microsoft basic /dev/sdb1
格式化(为分区创建指定的文件系统)
mkfs -t ext* /dev/sdb1 (mbr)
mkfs.ext* /dev/sdb1 (gpt)
mke2fs -t ext*/xfs -b大小 -i大小 dev/sdb1
每间隔多少分一个inode的大小:确定该分区有多少个可用inode而已
inode大小:记录文件信息时所需要的空间大小128byte
mkswap /dev/sdb1 将分区格式化为swap
[root@chenyun ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=610351 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=2441402, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
创建挂载点,并挂载(开机自动挂载)
mkdir -p /disk/sdb1
mount /dev/sdb1 /disk/sdb1
vim /etc/fstab
/dev/sdb1 /disk/sdb1 ext4 defaults 0 0
查看磁盘UUID
[root@chenyun ~]# blkid
/dev/sda1: UUID="134a011f-da62-4de9-95cc-0886930f51de" TYPE="xfs"
/dev/sda2: UUID="zrqfq8-H9s1-VtMW-jFCU-UP8k-2Ubh-iyrgqh" TYPE="LVM2_member"
/dev/sdb1: UUID="309c07e9-4604-4cd8-8cc1-e28305a90642" TYPE="xfs" PARTLABEL="/dev/sdb1" PARTUUID="849b35f2-3c8e-43b2-800f-4d8f924f1265"
/dev/sr0: UUID="2019-09-11-18-50-31-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/mapper/centos-root: UUID="8f4dd943-1e82-4beb-befe-28dd692c859b" TYPE="xfs"
/dev/mapper/centos-swap: UUID="f6505240-3b2d-4859-8d95-c1d541ff1f38" TYPE="swap"
永久挂载
[root@chenyun ~]# vim /etc/fstab
UUID=309c07e9-4604-4cd8-8cc1-e28305a90642 /mnt xfs defaults 0 0
查看/dev/sdb1是否挂载成功