Linux 磁盘分区、挂载、卸载、删除、修改

1. 查看磁盘及分区信息

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 查看磁盘信息
$ sudo fdisk -l

Disk /dev/sda: 2 GiB, 2147483648 bytes, 4194304 sectors # 新加的硬盘
Disk model: VMware Virtual S
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 /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xbeb16a3a

Device Boot Start End Sectors Size Id Type
/dev/sdb1 * 2048 39942143 39940096 19G 83 Linux
/dev/sdb2 39944190 41940991 1996802 975M 5 Extended
/dev/sdb5 39944192 41940991 1996800 975M 82 Linux swap / Solaris

# 查看磁盘分区信息
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 2G 0 disk # 新加的硬盘
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 19G 0 part /
├─sdb2 8:18 0 1K 0 part
└─sdb5 8:21 0 975M 0 part [SWAP]
sr0 11:0 1 1024M 0 rom

# 查看磁盘挂载信息
$ df -lh
Filesystem Size Used Avail Use% Mounted on
udev 1.9G 0 1.9G 0% /dev
tmpfs 389M 592K 388M 1% /run
/dev/sdb1 19G 2.6G 16G 15% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 389M 0 389M 0% /run/user/1000

# fdisk命令帮助
$ sudo fdisk /dev/sda

Command (m for help): m

Help:

DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag

Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition

Misc
m print this menu
u change display/entry units
x extra functionality (experts only)

Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file

Save & Exit
w write table to disk and exit
q quit without saving changes

Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table

2. 创建分区

  • 主分区 + 扩展分区总共不能超过 4 个,扩展分区最多只有 1 个,主分区至少有 1 个。逻辑分区可以有若干个
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
$ sudo fdisk /dev/sda
# 1G主分区sda1
Command (m for help): n # 新建分区
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p # 新建主分区
Partition number (1-4, default 1): 1 # 分区号
First sector (2048-4194303, default 2048): # 柱面起始值
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-4194303, default 4194303): +1G # 柱面结束值

Created a new partition 1 of type 'Linux' and of size 1 GiB.

# 200M主分区sda2
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (2099200-4194303, default 2099200):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2099200-4194303, default 4194303): +200M

Created a new partition 2 of type 'Linux' and of size 200 MiB.

# 200M主分区sda3
Command (m for help): n
Partition type
p primary (2 primary, 0 extended, 2 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (3,4, default 3): 3
First sector (2508800-4194303, default 2508800):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2508800-4194303, default 4194303): +200M

Created a new partition 3 of type 'Linux' and of size 200 MiB.

# 200M扩展分区sda4
Command (m for help): n
Partition type
p primary (3 primary, 0 extended, 1 free)
e extended (container for logical partitions)
Select (default e): e # 新建扩展分区

Selected partition 4
First sector (2918400-4194303, default 2918400):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2918400-4194303, default 4194303): +200M

Created a new partition 4 of type 'Extended' and of size 200 MiB.

# 100M逻辑分区sda5
Command (m for help): n
All primary partitions are in use.
Adding logical partition 5
First sector (2920448-3327999, default 2920448):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2920448-3327999, default 3327999): +100M

Created a new partition 5 of type 'Linux' and of size 100 MiB.

# 98M逻辑分区sda6
Command (m for help): n
All primary partitions are in use.
Adding logical partition 6
First sector (3127296-3327999, default 3127296):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (3127296-3327999, default 3327999):

Created a new partition 6 of type 'Linux' and of size 98 MiB.

Command (m for help): w # 保存分区表
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

# 打印当前硬盘分区信息
$ sudo fdisk /dev/sda -l
Disk /dev/sda: 2 GiB, 2147483648 bytes, 4194304 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x1bd200c3

Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 2099199 2097152 1G 83 Linux # 1G主分区
/dev/sda2 2099200 2508799 409600 200M 83 Linux # 200M主分区
/dev/sda3 2508800 2918399 409600 200M 83 Linux # 200M主分区
/dev/sda4 2918400 3327999 409600 200M 5 Extended # 200M扩展分区
/dev/sda5 2920448 3125247 204800 100M 83 Linux # 100M逻辑分区(sda5是第一个逻辑分区,所以起始柱面和sda4一样)
/dev/sda6 3127296 3327999 200704 98M 83 Linux # 98M逻辑分区

3. 格式化分区

  • 建立好分区后,需要先格式化分区。否则会提示必须知道文件系统类型
1
2
3
4
5
$ sudo mkfs.ext4 /dev/sda1  # sudo mkfs -t ext4 /dev/sda1
$ sudo mkfs.ext4 /dev/sda2
$ sudo mkfs.ext4 /dev/sda3
$ sudo mkfs.ext4 /dev/sda5
$ sudo mkfs.ext4 /dev/sda6

4. 挂载分区

  • 新建 5 个文件夹用来挂载分区。sda4 是扩展分区不能用来挂载,它的逻辑分区 sda5 和 sda6 是可以挂载的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ sudo mkdir -p /data/disk{1..6}
$ sudo mount /dev/sda1 /data/disk1
$ sudo mount /dev/sda2 /data/disk2
$ sudo mount /dev/sda3 /data/disk3
$ sudo mount /dev/sda5 /data/disk5
$ sudo mount /dev/sda6 /data/disk6

$ df -lh
Filesystem Size Used Avail Use% Mounted on
udev 1.9G 0 1.9G 0% /dev
tmpfs 389M 616K 388M 1% /run
/dev/sdb1 19G 3.4G 15G 19% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 389M 0 389M 0% /run/user/1000
/dev/sda1 974M 24K 907M 1% /data/disk1
/dev/sda2 189M 14K 175M 1% /data/disk2
/dev/sda3 189M 14K 175M 1% /data/disk3
/dev/sda5 92M 14K 85M 1% /data/disk5
/dev/sda6 90M 14K 83M 1% /data/disk6

5. 自动挂载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 这种方式有点问题,虚拟机重启后sda变成了sdb
# $ sudo vim /etc/fstab
# /dev/sda1 /data/disk1 ext4 defaults 0 2
# /dev/sda2 /data/disk2 ext4 defaults 0 2
# /dev/sda3 /data/disk3 ext4 defaults 0 2
# /dev/sda5 /data/disk5 ext4 defaults 0 2
# /dev/sda6 /data/disk6 ext4 defaults 0 2

$ sudo blkid /dev/sda1
/dev/sda1: UUID="532f00f4-71ce-40a0-9b6d-bfa53727d16c" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="1bd200c3-01"

$ sudo vim /etc/fstab
UUID=532f00f4-71ce-40a0-9b6d-bfa53727d16c /data/disk1 ext4 defaults 0 2
UUID=d87ac855-1ffc-4295-9595-f33803449893 /data/disk2 ext4 defaults 0 2
UUID=1882b989-e8f9-46ff-b187-df05f64469c5 /data/disk3 ext4 defaults 0 2
UUID=c3ac8358-b7e7-4205-9ad2-0dba3440c2a0 /data/disk5 ext4 defaults 0 2
UUID=c78ea3ad-c0c4-4f66-b224-7c20a0b801ca /data/disk6 ext4 defaults 0 2

# 把配置文件中的所有分区重新挂载一遍
$ sudo mount -a

6. 卸载分区

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ sudo umount /dev/sda6
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 2G 0 disk
├─sda1 8:1 0 1G 0 part /data/disk1
├─sda2 8:2 0 200M 0 part /data/disk2
├─sda3 8:3 0 200M 0 part /data/disk3
├─sda4 8:4 0 1K 0 part
├─sda5 8:5 0 100M 0 part /data/disk5
└─sda6 8:6 0 98M 0 part
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 19G 0 part /
├─sdb2 8:18 0 1K 0 part
└─sdb5 8:21 0 975M 0 part [SWAP]
sr0 11:0 1 1024M 0 rom

7. 删除分区

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
$ sudo fdisk /dev/sda

Welcome to fdisk (util-linux 2.36.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): d # 删除分区
Partition number (1-6, default 6): 6 # 分区编号

Partition 6 has been deleted.

Command (m for help): w # 写入磁盘分区表
The partition table has been altered.
Syncing disks.

$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 2G 0 disk
├─sda1 8:1 0 1G 0 part /data/disk1
├─sda2 8:2 0 200M 0 part /data/disk2
├─sda3 8:3 0 200M 0 part /data/disk3
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 100M 0 part /data/disk5
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 19G 0 part /
├─sdb2 8:18 0 1K 0 part
└─sdb5 8:21 0 975M 0 part [SWAP]
sr0 11:0 1 1024M 0 rom
  • 使用 partprobe 或者 kpartx 使分区表立刻生效,或者重启机器
  • 如果要删除一个磁盘所有分区,更简单的做法是格式化磁盘
1
$ sudo mkfs.ext4 /dev/sda

8. 修改分区系统类型 id

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 把sda2分区系统类型id修改为Linux LVM
$ sudo fdisk /dev/sda
Command (m for help): t # 改变分区类型
Partition number (1-5, default 5): 2 # 分区号
Hex code or alias (type L to list all): L # 查看所有类型

00 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
01 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
02 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
03 XENIX usr 3c PartitionMagic 84 OS/2 hidden or c6 DRDOS/sec (FAT-
04 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
05 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
06 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
07 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
08 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
09 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
0a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
0b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
0c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi ea Linux extended
0e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD eb BeOS fs
0f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ee GPT
10 OPUS 55 EZ-Drive a7 NeXTSTEP ef EFI (FAT-12/16/
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f0 Linux/PA-RISC b
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f1 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f4 SpeedStor
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ f2 DOS secondary
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fb VMware VMFS
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fc VMware VMKCORE
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fd Linux raid auto
1c Hidden W95 FAT3 75 PC/IX bc Acronis FAT32 L fe LANstep
1e Hidden W95 FAT1 80 Old Minix be Solaris boot ff BBT

Aliases:
linux - 83
swap - 82
extended - 05
uefi - EF
raid - FD
lvm - 8E
linuxex - 85
Hex code or alias (type L to list all): 8e # 改为LVM

Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): p
Disk /dev/sda: 2 GiB, 2147483648 bytes, 4194304 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x1520bf3e

Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 2099199 2097152 1G 83 Linux
/dev/sda2 2099200 2508799 409600 200M 8e Linux LVM # 类型已改变
/dev/sda3 2508800 2918399 409600 200M 83 Linux
/dev/sda4 2918400 3327999 409600 200M 5 Extended
/dev/sda5 2920448 3125247 204800 100M 83 Linux
Command (m for help): w # 写入磁盘分区表
The partition table has been altered.
Syncing disks.

9. 添加交换分区

1
2
3
4
5
6
7
8
9
10
# 创建一个主分区或逻辑分区
# 初始化交换分区
$ mkswap /dev/sda7
# 挂载分区
$ vim /etc/fstab
/dev/vda7 swap swap defaults 0 0
# 启动交换分区
$ swapon -a
# 查看交换分区
$ swapon

参考