磁盘概述
https://www.cnblogs.com/zh-dream/p/11727902.html
Linux分区表格式和文件系统类型
linux磁盘分区表格式分为两种:
- MBR
最多支持四个主分区、系统只能安装在主分区、扩展分区要占一个主分区、MBR最大支持2TB,但拥有最好的兼容性
- GPT
大于2TB时使用,支持无限多个主分区(说是这么说,但是操作系统可能会限制,比如Windows下最多128个分区)、最大支持18EB的大容量
查看分区表格式 fdisk -lu <设备名>
如果System="Linux"
,说明是MBR分区表格式
如果System="GPT"
,说明是GPT分区表格式
[root@master ~]# fdisk -lu /dev/sdb 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: 0x45ef390c Device Boot Start End Blocks Id System /dev/sdb1 2048 4196351 2097152 83 Linux
Linux常见文件系统类型
(1)ext
(2)xfs
使用blkid命令查看以分区的磁盘文件系统类型
-
blkid <以分区设备的磁盘名称>
[root@master ~]# blkid /dev/sda1 /dev/sda1: UUID="bba2c917-8540-41c8-97e6-f1d73d9143ba" TYPE="xfs"
添加磁盘
查看新添加的磁盘
[root@master ~]# lsblk -f ##查看磁盘分区信息 NAME FSTYPE LABEL UUID MOUNTPOINT sda ├─sda1 xfs bba2c917-8540-41c8-97e6-f1d73d9143ba /boot ├─sda2 swap 1c0f8351-49f0-4dd8-9a8b-1aff1d4a77b0 [SWAP] └─sda3 xfs 0bbd5e50-606c-4c47-8cd7-1ae67f812437 / sdb ##新添加的磁盘 sdc ├─sdc1 xfs da38abcd-a06e-4ef1-93fa-49a5b8773f01 └─sdc2 sr0 iso9660 CentOS 7 x86_64 2018-05-03-21-07-04-00
[root@master ~]# ll /dev/sd
sda sda1 sda2 sda3 sdb sdc sdc1 sdc2
fdisk命令对新磁盘进行分区
用法
fdisk [options] <disk> change partition table ##修改分区表 fdisk [options] -l <disk> list partition table(s) ##查看分区表 fdisk -s <partition> give partition size(s) in blocks ## 指定分区块大小
常用选项
选项 | 含义 |
-b <size> | 扇区大小,默认512bytes,可选值(512, 1024, 2048 or 4096) |
-l | 查看分区信息 |
-u[=<unit>] | 显示每个柱面和扇区的大小 |
-h | 查看帮助 |
查看分区信息
[root@master ~]# fdisk -l Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000bdcb7 Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 4196351 1048576 82 Linux swap / Solaris /dev/sda3 4196352 209715199 102759424 83 Linux Disk /dev/sdc: 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: 0xeed0d630 Device Boot Start End Blocks Id System /dev/sdc1 2048 10487807 5242880 83 Linux /dev/sdc2 10487808 14682111 2097152 83 Linux
查看某块盘分区信息
[root@master ~]# fdisk -l /dev/sda Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000bdcb7 Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 4196351 1048576 82 Linux swap / Solaris /dev/sda3 4196352 209715199 102759424 83 Linux
显示柱面单位大小
[root@master ~]# fdisk -u=cylinders -l /dev/sda Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000bdcb7 Device Boot Start End Blocks Id System /dev/sda1 * 1 131 1048576 83 Linux /dev/sda2 131 262 1048576 82 Linux swap / Solaris /dev/sda3 262 13055 102759424 83 Linux
开始分区
新添加的盘是无法识别到的,需要重启
[root@master ~]# ll /dev/sd sda sda1 sda2 sda3 sdb sdc sdc1 sdc2 [root@master ~]# fdisk /dev/sdb fdisk: cannot open /dev/sdb: No such file or directory
[root@master ~]# fdisk /dev/sdc 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 ## 创建一个空GPT分区表 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 u change display/entry units ## 以柱面为单位显示(已遗弃) v verify the partition table w write table to disk and exit ## 保存分区信息并退出 x extra functionality (experts only)
开始分区
[root@master ~]# fdisk /dev/sdb 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): 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: 0x45ef390c Device Boot Start End Blocks Id System 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-41943039, default 2048): ## 初始扇区必须是2048的倍数,即2048*512/(1024*1024)=1M,因为EFI,这部分空间是留给EFI的引导代码 Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +2G ## 分区是按照扇区分的,也可以指定大小 Partition 1 of type Linux and of size 2 GiB is set 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: 0x45ef390c Device Boot Start End Blocks Id System /dev/sdb1 2048 4196351 2097152 83 Linux Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
通知内核分区表的变化
[root@master ~]# partprobe /dev/sdb
检查分区信息
[root@master ~]# lsblk -f NAME FSTYPE LABEL UUID MOUNTPOINT sda ├─sda1 xfs bba2c917-8540-41c8-97e6-f1d73d9143ba /boot ├─sda2 swap 1c0f8351-49f0-4dd8-9a8b-1aff1d4a77b0 [SWAP] └─sda3 xfs 0bbd5e50-606c-4c47-8cd7-1ae67f812437 / sdb └─sdb1 sdc ├─sdc1 ext4 9399025f-d5ca-4951-b90a-92e5141f7e7d └─sdc2 sr0 iso9660 CentOS 7 x86_64 2018-05-03-21-07-04-00
创建文件系统
[root@master ~]# mkfs.xfs /dev/sdb1 meta-data=/dev/sdb1 isize=512 agcount=4, agsize=131072 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=524288, 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
挂载
[root@master ~]# mount /dev/sdb1 /data [root@master ~]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 901M 0 901M 0% /dev tmpfs 911M 8.0K 911M 1% /dev/shm tmpfs 911M 9.6M 902M 2% /run tmpfs 911M 0 911M 0% /sys/fs/cgroup /dev/sda3 98G 2.5G 96G 3% / /dev/sda1 1014M 135M 880M 14% /boot tmpfs 183M 0 183M 0% /run/user/0 /dev/sdb1 2.0G 33M 2.0G 2% /data
写入到 /etc/fstab
将挂载信息写入到 /etc/fstab中,以便系统启动时自动挂载,不用每次手动挂载
查看UUID
[root@master ~]# blkid /dev/sdb1 /dev/sdb1: UUID="6cfbe843-e280-4d85-9ae3-60b0ebcc5909" TYPE="xfs"
写入 /etc/fstab
[root@master ~]# echo "UUID=6cfbe843-e280-4d85-9ae3-60b0ebcc5909 /data xfs defaults 0 0" >> /etc/fstab
重启后查看
[root@master ~]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 901M 0 901M 0% /dev tmpfs 911M 8.0K 911M 1% /dev/shm tmpfs 911M 9.6M 902M 2% /run tmpfs 911M 0 911M 0% /sys/fs/cgroup /dev/sda3 98G 2.5G 96G 3% / /dev/sdb1 2.0G 33M 2.0G 2% /data /dev/sda1 1014M 124M 891M 13% /boot tmpfs 183M 0 183M 0% /run/user/0 [root@master ~]# ll /data/ total 0
使用设备名和label及uuid作为标识的不同
使用设备名称(/dev/sda)来挂载分区时是被固定死的,一旦磁盘的插槽顺序发生了变化,就会出现名称不对应的问题。因为这个名称是会改变的。
不过使用label挂载就不用担心插槽顺序方面的问题。不过要随时注意你的Label name。至于UUID,每个分区被格式化以后都会有一个UUID作为唯一的标识号。使用uuid挂载的话就不用担心会发生错乱的问题了。