第四章 磁盘分区知识与Linux系统分区实践
1、fdisk命令分区
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: 1073 MB, 1073741824 bytes, 2097152 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: 0x000b907d
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-2097151, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): +150M
Partition 1 of type Linux and of size 150 MiB is set
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p):
Using default response p
Partition number (2-4, default 2):
First sector (309248-2097151, default 309248):
Using default value 309248
Last sector, +sectors or +size{K,M,G} (309248-2097151, default 2097151): +150M
Partition 2 of type Linux and of size 150 MiB is set
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p):
Using default response p
Partition number (3,4, default 3):
First sector (616448-2097151, default 616448):
Using default value 616448
Last sector, +sectors or +size{K,M,G} (616448-2097151, default 2097151): +150M
Partition 3 of type Linux and of size 150 MiB is set
Command (m for help): N
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e):
Using default response e
Selected partition 4
First sector (923648-2097151, default 923648):
Using default value 923648
Last sector, +sectors or +size{K,M,G} (923648-2097151, default 2097151):
Using default value 2097151
Partition 4 of type Extended and of size 573 MiB is set
Command (m for help): P
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x000b907d
Device Boot Start End Blocks Id System
/dev/sdb1 2048 309247 153600 83 Linux
/dev/sdb2 309248 616447 153600 83 Linux
/dev/sdb3 616448 923647 153600 83 Linux
/dev/sdb4 923648 2097151 586752 5 Extended
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@yuzhiwei/]# cat /proc/partitions
major minor #blocks name
11 0 4481024 sr0
8 0 20971520 sda
8 1 204800 sda1
8 2 2048000 sda2
8 3 18717696 sda3
8 16 1048576 sdb
8 17 153600 sdb1
8 18 153600 sdb2
8 19 153600 sdb3
8 20 1 sdb4
8 21 153600 sdb5
分区完成后使用partprobe /dev/sdb 命令将分区信息通知内核
2、parted命令分区
分区前先看一下分区格式,fdisk命令分区用dos分区格式,parted命令分区使用gpt分区格式。
[root@yuzhiwei/]# parted /dev/sdb
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
(parted) mklabel 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) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
(parted) mkpart primary 0 480
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? i
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 17.4kB 480MB 480MB primary
(parted) mkpart primary 481 580
(parted) mkpart logic 581 600
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 17.4kB 480MB 480MB primary
2 481MB 580MB 98.6MB primary
3 581MB 600MB 18.9MB logic
parted命令分区时没有扩展分区,只有主分区和逻辑分区。
3、文件系统
3.1、什么是文件系统?
计算机存储和组织数据的方法或者机制,落地是一个软件。
3.2、为什么需要文件系统?
磁盘、物理介质、磁粒子物理元素。硬件需要软件驱动使用,磁盘需要文件系统驱动。
文件系统实现通过磁盘管理规划、存取数据。
3.3、文件系统有哪些类型?
Windows:NTFS、fat32、msdos
Linux:ext2、ext3(C5)、ext4(C6)、xfs(C7)、btrfs
3.4、创建文件系统
首先用上面的命令创建分区
首先对分区进行格式化,其实关键就是生成一定数量的Inode和Block。
[root@yuzhiwei/]# mkfs.ext4 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
38456 inodes, 153600 blocks
7680 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33816576
19 block groups
8192 blocks per group, 8192 fragments per group
2024 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
也可以指定指定块大小和inode大小进行格式化
[root@oldboyedu ~]# mkfs -t ext4 -b 4096 -I 512 /dev/sdb3
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
38400 inodes, 38400 blocks
1920 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=39845888
2 block groups
32768 blocks per group, 32768 fragments per group
19200 inodes per group
Superblock backups stored on blocks:
32768
Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
然后进行挂载并查看挂载结果
[root@yuzhiwei/]# mount /dev/sdb1 /mnt
[root@yuzhiwei/]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 1.7G 17G 10% /
devtmpfs 900M 5.0M 895M 1% /dev
tmpfs 910M 0 910M 0% /dev/shm
tmpfs 910M 9.6M 901M 2% /run
tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/sda1 197M 116M 82M 59% /boot
tmpfs 182M 0 182M 0% /run/user/0
/dev/sdb1 142M 1.6M 130M 2% /mnt
[root@yuzhiwei/]# cat /proc/mounts
rootfs / rootfs rw 0 0
/dev/sdb1 /mnt ext4 rw,relatime,data=ordered 0 0
以上挂载命令只是临时挂载到/mnt
如何开机自动挂载?
/etc/fstab 开机自动挂载
几列? 6列
[root@oldboyedu ~]# cat /etc/fstab
UUID=3a3a295f-88f8-456d-94dc-1a3eeb517c02 / xfs defaults 0 0
UUID=fd2e0ca7-32be-425f-86a2-85c02b9ec5ea /boot xfs defaults 0 0
UUID=79a3924b-739e-48dc-ab0c-0444b9ac6591 swap swap defaults 0 0
设备 挂载点 文件系统类型 默认挂载选项 是否备份 是否开机磁盘检查
/dev/sdb2 /opt xfs defaults 0 0
umount -lf 命令可以强制卸载挂载