使用fdisk与parted管理Linux磁盘

目录大纲

  • 1.硬盘类型介绍
  • 2.记录磁盘分区表的方法
    • 2.1.MBR
    • 2.2.GPT
  • 3.使用fdisk添加分区
    • 3.1.查看当前分区情况:
    • 3.2.查看fdisk帮助:
    • 3.3.新建10G分区:
    • 3.4.格式化分区
    • 3.5.手动挂载分区
    • 3.6.设置开机自动挂载
    • 3.7.无法卸载分区的问题
  • 4.使用parted添加分区
    • 4.1.查看parted工具是否安装
    • 4.2.添加一块20G硬盘
    • 4.3.查看parted帮助
    • 4.4.使用parted分区
    • 4.5.删除分区
    • 4.6.格式分区
  • 5.创建交换分区
    • 5.1.格式化分区为swap分区
    • 5.2.启用和关闭swap分区

环境说明:

  • 电脑系统:win7 64位
  • 虚拟机:VMware Wordstation 12
  • 测试系统:centos6.5 x86_64

    1.硬盘类型介绍

    常见的磁盘接口类型有:SAS,SATA,SSD,SCSI,IDE
    服务器上:常用SAS硬盘,15000转/分,随着固态硬盘的普及,也有用SSD固态硬盘的。SATA也有用到服务器上的,比较少见。
    普通家用电脑,笔记本等:常用SATA硬盘,转速一般为5400转/分钟,或者是7200转/分钟。

2.记录磁盘分区表的方法

常见的2种磁盘分区表的记录方式:MBR:master boot record,GPT:GUID Partition Table

2.1.MBR

MBR:master boot record,主引导记录
硬盘的0柱面、0磁头、1扇区称为主引导扇区(也叫主引导记录MBR)。它由三个部分组成,主引导程序、硬盘分区表DPT(Disk Partition table)和分区有效标志(55AA)。
在总共512字节的主引导扇区里主引导程序(boot loader)占446个字节,第二部分是Partition table区(分区表),即DPT,占64个字节,16*4=64,硬盘中分区有多少以及每一分区的大小都记在其中。第三部分是magic number,占2个字节,固定为55AA。

magic number:•结束标志字,偏移地址01FE–01FF的2个字节值为结束标志55AA,称为“魔数”(magic number)。如果该标志错误系统就不能启动。

2.2.GPT

GPT:GUID Partition Table,全局唯一标识分区表
GPT,全局唯一标识分区表(GUID Partition Table),GUID,与MBR最大4个分区表项的限制相比,GPT对分区数量没有限制,但Windows最大仅支持128个GPT分区。GPT可管理硬盘大小达到了18EB(1EB=1024PB=1,048,576TB),不过NTFS格式最大仅支持256TB。

3.使用fdisk添加分区

添加磁盘步骤:添加设备 > 分区 > 格式化(创建文件系统) > [起名] > 修改配置文件 > 创建一个挂载点 > 挂载
一般小于2T的磁盘,使用MBR的方式记录分区信息,这时候,我们可以使用fdisk工具,进行分区。

注意:#号后面是我添加的注释。

下面使用fdisk工具添加一个10G空间的硬盘

3.1.查看当前分区情况:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@chmod ~]# fdisk -l

Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
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 identifier: 0x00073cd9

Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 3889 30720000 83 Linux
/dev/sda3 3889 4399 4096000 82 Linux swap / Solaris

3.2.查看fdisk帮助:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@chmod ~]# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').

Command (m for help): m #输入m查看帮助
Command action
a toggle a bootable flag #指定启动分区
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition #删除分区
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)

3.3.新建10G分区:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[root@chmod ~]# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').


Command (m for help): n #输入n新建分区
Command action
e extended
p primary partition (1-4)
p #输入p选择新建主分区
Selected partition 4
First cylinder (4399-26108, default 4399):
Using default value 4399
Last cylinder, +cylinders or +size{K,M,G} (4399-26108, default 26108): +10G #分配分区空间

Command (m for help): w #保存修改,并退出
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
# 上面3行提示,需要重启系统,或者运行partprobe命令,以便内核读取新的分区表信息。
[root@chmod ~]# partprobe
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy).
As a result, it may not reflect all of your changes until after reboot.
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
Error: Invalid partition table - recursive partition on /dev/sr0.
[root@chmod ~]#
#运行partprobe,读取新分区表失败,只好重启。

3.4.格式化分区

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@chmod ~]# ls /dev/sda*
/dev/sda /dev/sda1 /dev/sda2 /dev/sda3 /dev/sda4
[root@chmod ~]# mkfs.ext4 /dev/sda4
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655776 inodes, 2622089 blocks
131104 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2688548864
81 block groups
32768 blocks per group, 32768 fragments per group
8096 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 23 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@chmod ~]#

mkfs.ext4 /dev/sda4也可以使用:mkfs -t ext4 /dev/sda4

3.5.手动挂载分区

1
2
3
4
5
6
7
8
9
[root@chmod ~]# mkdir /data2        #创建挂载点
[root@chmod ~]# mount /dev/sda4 /data2 #挂载
[root@chmod ~]# df -Th #查看情况
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 ext4 29G 5.0G 23G 18% /
tmpfs tmpfs 427M 0 427M 0% /dev/shm
/dev/sda1 ext4 485M 39M 421M 9% /boot
/dev/sda4 ext4 9.9G 151M 9.2G 2% /data2
[root@chmod ~]#

3.6.设置开机自动挂载

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@chmod ~]# vim /etc/fstab
[root@chmod ~]# tail -1 /etc/fstab
/dev/sda4 /data2 ext4 defaults 0 0

#检查,配置是否生效:
[root@chmod ~]# umount /dev/sda4
[root@chmod ~]# mount -a
[root@chmod ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 ext4 29G 5.0G 23G 18% /
tmpfs tmpfs 427M 0 427M 0% /dev/shm
/dev/sda1 ext4 485M 39M 421M 9% /boot
/dev/sda4 ext4 9.9G 151M 9.2G 2% /data2

解释:/dev/sda4:要挂载的设备,/data2:挂载点,ext4:文件系统,defaults:挂载类型,0:是否备份,0:是否检测
最后两个0:
fs_dump 是否要使用dump命令进行备份. 0为不备份,1为要备份 。
fs_pass 该字段被fsck命令用来决定在启动时是否需要被扫描的文件系统的顺序,根文件系统/对应该字段的值应该为1,其他文件系统应该为2。若该文件系统无需在启动 时扫描则设置该字段为0

  • 使用卷标挂载

    1
    2
    3
    4
    5
    6
    7
    8
    [root@chmod ~]# umount /dev/sda4
    [root@chmod ~]# e2label /dev/sda4 mydata2 #设置卷标
    [root@chmod ~]# e2label /dev/sda4
    mydata2
    [root@chmod ~]# vim /etc/fstab #修改配置
    [root@chmod ~]# tail -1 /etc/fstab
    LABEL=mydata2 /data2 ext4 defaults 0 0
    [root@chmod ~]#
  • 使用UUID挂载
    UUID的作用:UUID是一个标识你系统中的存储设备的字符串,其目的是帮助使用者唯一的确定系统中的所有存储设备,不管它们是什么类型的。它可以标识DVD驱动器,USB存储设备以及你系统中的硬盘设备等。
    特点:
    它是真正的唯一标志符
    Linux中的许多关键功能现在开始依赖于UUID

  • 查看UUID:

    1
    2
    3
    4
    5
    [root@chmod ~]# blkid
    /dev/sda2: UUID="1ccabda8-aaa7-4cf6-99ee-2132bc36a071" TYPE="ext4"
    /dev/sda1: UUID="c0b34e8f-4d55-45ba-bf43-b53f9cb051bc" TYPE="ext4"
    /dev/sda3: UUID="d7ebf909-36f2-45be-b5f2-fc3b88dd7a67" TYPE="swap"
    /dev/sda4: UUID="0ebe63d5-d160-4ab2-a2cd-d6e3b9433002" TYPE="ext4" LABEL="mydata2"

使用UUID 挂载sda4分区:

1
2
3
4
5
6
7
8
9
10
[root@chmod ~]# vim /etc/fstab
[root@chmod ~]# tail -8 /etc/fstab #最后一行修改成如下:
UUID=1ccabda8-aaa7-4cf6-99ee-2132bc36a071 / ext4 defaults 1 1
UUID=c0b34e8f-4d55-45ba-bf43-b53f9cb051bc /boot ext4 defaults 1 2
UUID=d7ebf909-36f2-45be-b5f2-fc3b88dd7a67 swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
UUID=0ebe63d5-d160-4ab2-a2cd-d6e3b9433002 /data2 ext4 defaults 0 0

3.7.无法卸载分区的问题

当遇到umount: /data2: device is busy.,我们怎么解决?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@chmod data2]# df -Th    #sda4已经挂载到data2
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 ext4 29G 5.0G 23G 18% /
tmpfs tmpfs 427M 0 427M 0% /dev/shm
/dev/sda1 ext4 485M 39M 421M 9% /boot
/dev/sda4 ext4 9.9G 151M 9.2G 2% /data2
[root@chmod data2]# vim hello.txt & #后台打开一个文件
[1] 1813
[root@chmod data2]# umount /dev/sda4 #尝试卸载,发现失败
umount: /data2: device is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))

[1]+ Stopped vim hello.txt

[root@chmod data2]# fuser -muv /dev/sda4 #使用fuser,查看设备上都打开了哪些文件
USER PID ACCESS COMMAND
/dev/sda4: root 1671 ..c.. (root)bash
root 1813 ..c.. (root)vim
[root@chmod data2]#

把打开文件的进程杀死,就可以卸载成功啦。当然看好再杀。

fuser命令参数:-m:显示进程ID,-u:显示进程归属的永远会,-v详细信息

4.使用parted添加分区

4.1.查看parted工具是否安装

1
2
3
4
[root@chmod ~]# which parted    #查看是否安装
/sbin/parted
[root@chmod ~]# rpm -qf `which parted` #查看是哪个包安装的
parted-2.1-21.el6.x86_64

4.2.添加一块20G硬盘

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@chmod ~]# fdisk -l

Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
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 identifier: 0x00073cd9

Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 3889 30720000 83 Linux
/dev/sda3 3889 4399 4096000 82 Linux swap / Solaris
/dev/sda4 4399 5704 10488356 83 Linux

Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
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 identifier: 0x00000000

[root@chmod ~]#

4.3.查看parted帮助

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@chmod ~]# ls /dev/sdb
/dev/sdb
[root@chmod ~]# parted /dev/sdb
GNU Parted 2.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) help
align-check TYPE N check partition N for TYPE(min|opt) alignment
check NUMBER do a simple check on the file system
cp [FROM-DEVICE] FROM-NUMBER TO-NUMBER copy file system to another partition
help [COMMAND] print general help, or help on COMMAND
mklabel,mktable LABEL-TYPE create a new disklabel (partition table)
mkfs NUMBER FS-TYPE make a FS-TYPE file system on partition NUMBER
mkpart PART-TYPE [FS-TYPE] START END make a partition
mkpartfs PART-TYPE FS-TYPE START END make a partition with a file system
move NUMBER START END move partition NUMBER
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
resize NUMBER START END resize partition NUMBER and its file system
rm NUMBER delete partition NUMBER
select DEVICE choose the device to edit
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
(parted) quit

4.4.使用parted分区

添加一个10G和一个5G的分区

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
[root@chmod ~]# parted /dev/sdb
GNU Parted 2.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Error: /dev/sdb: unrecognised disk label #发现不可识别,需要先指定mbr或者gpt分区类型
(parted) mklabel #设置分区表存储类型
New disk label type? gpt
(parted) mkpart #新建分区
Partition name? []? mydisk1 #设置新分区名字
File system type? [ext2]? #文件系统类型,后续我们还可以手动格式化成其他文件系统类型
Start?
Start? 1
End? 10G
(parted) P
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name Flags
1 1049kB 10.0GB 9999MB mydisk1

(parted) mkpart
Partition name? []? mydisk2
File system type? [ext2]?
Start? 10G
End? 15G
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name Flags
1 1049kB 10.0GB 9999MB mydisk1
2 10.0GB 15.0GB 5000MB mydisk2

(parted) quit
Information: You may need to update /etc/fstab.

4.5.删除分区

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[root@chmod ~]# parted /dev/sdb
GNU Parted 2.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: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name Flags
1 1049kB 10.0GB 9999MB mydisk1
2 10.0GB 15.0GB 5000MB mydisk2

(parted) rm 2 #直接使用分区号删除
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name Flags
1 1049kB 10.0GB 9999MB mydisk1

(parted) quit
Information: You may need to update /etc/fstab.

[root@chmod ~]#

4.6.格式分区

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[root@chmod ~]# mkfs.ext4 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
610800 inodes, 2441216 blocks
122060 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2499805184
75 block groups
32768 blocks per group, 32768 fragments per group
8144 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 22 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@chmod ~]# mkdir /mydisk1
[root@chmod ~]# mount /dev/sdb1 /mydisk1
[root@chmod ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 ext4 29G 5.0G 23G 18% /
tmpfs tmpfs 427M 0 427M 0% /dev/shm
/dev/sda1 ext4 485M 39M 421M 9% /boot
/dev/sda4 ext4 9.9G 151M 9.2G 2% /data2
/dev/sdb1 ext4 9.2G 149M 8.6G 2% /mydisk1

parted添加分区后,不用用运行partprobe,或者重启才能读取到新的磁盘信息。不知道是不是因为sdb非系统盘,又或者是parted的优点。

挂载分区和自动挂载分区,和上面3.5,3.6一样。不再赘述。

5.创建交换分区

这里直接把上面的sdb1重新格式化成为交换分区,不再进行添加新分区了。

5.1.格式化分区为swap分区

1
2
3
4
5
6
7
[root@chmod ~]# mkswap /dev/sdb1
/dev/sdb1: Device or resource busy #需要先卸载分区,再进行操作
[root@chmod ~]# umount /dev/sdb1
[root@chmod ~]# mkswap /dev/sdb1 #格式化分区为交换分区
Setting up swapspace version 1, size = 9764860 KiB
no label, UUID=65b88eb9-25f9-493b-a6e2-8b280f410b38
[root@chmod ~]#

5.2.启用和关闭swap分区

启用swap分区:

1
2
3
4
5
6
7
[root@chmod ~]# swapon /dev/sdb1    #启用swap分区
[root@chmod ~]# free -m #看到swap加了10G
total used free shared buffers cached
Mem: 852 168 684 0 22 60
-/+ buffers/cache: 84 768
Swap: 13535 0 13535
[root@chmod ~]#

自动挂载swap分区:

1
2
3
[root@chmod ~]# tail -1 /etc/fstab
/dev/sdb1 swap swap defaults 0 0
[root@chmod ~]#

关闭swap分区:

1
2
3
4
5
6
7
[root@chmod ~]# swapoff /dev/sdb1
[root@chmod ~]# free -m
total used free shared buffers cached
Mem: 852 165 686 0 23 64
-/+ buffers/cache: 78 774
Swap: 3999 0 3999
[root@chmod ~]#

文章目录
  1. 目录大纲
  2. 1.硬盘类型介绍
  3. 2.记录磁盘分区表的方法
    1. 2.1.MBR
    2. 2.2.GPT
  4. 3.使用fdisk添加分区
    1. 3.1.查看当前分区情况:
    2. 3.2.查看fdisk帮助:
    3. 3.3.新建10G分区:
    4. 3.4.格式化分区
    5. 3.5.手动挂载分区
    6. 3.6.设置开机自动挂载
    7. 3.7.无法卸载分区的问题
  5. 4.使用parted添加分区
    1. 4.1.查看parted工具是否安装
    2. 4.2.添加一块20G硬盘
    3. 4.3.查看parted帮助
    4. 4.4.使用parted分区
    5. 4.5.删除分区
    6. 4.6.格式分区
  6. 5.创建交换分区
    1. 5.1.格式化分区为swap分区
    2. 5.2.启用和关闭swap分区