磁盘分区时需要使用以下两命令:
一、查看目前磁盘使用情况
一个已经分好区的磁盘sda和一个未使用的磁盘sdb,
二、创建磁盘分区
以硬盘设备文件名为参数执行fdisk命令,进入到交互式的分区管理界面中,就可以对相应的硬盘进行创建、删除、更改分区等操作
1
2
3
4
5
6
7
8
9
10
|
[root@justin ~]
# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x9adeb999.
Changes will remain
in
memory only,
until
you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
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):
|
在操作界面中的“Command (m for help):”提示符后,用户可以输入特定的分区操作指令,完成各项分区管理任务。例如输入“m”指令后,可以查看各种操作指令的帮助信息。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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
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)
Command (m
for
help):
|
创建一个新的分区
1
2
3
4
5
6
7
8
9
10
|
Command (m
for
help): n ;创建一个新的分区
Command action
e extended ;扩展分区
p primary partition (1-4) ;主分区,主分区最多只能4个
p ;创建主分区
Partition number (1-4): 1 ;指定主分区号
First cylinder (1-1044, default 1): ;指定第一个柱面,直接回车使用默认
Using default value 1 ;
Last cylinder, +cylinders or +size{K,M,G} (1-1044, default 1044): +3G ;指定分区大小
Command (m
for
help):
|
创建好分区后可以通过命令p查看分区情况
1
2
3
4
5
6
7
8
9
10
|
Command (m
for
help): p
Disk
/dev/sdb
: 8589 MB, 8589934592 bytes
255 heads, 63 sectors
/track
, 1044 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: 0x9adeb999
Device Boot Start End Blocks Id System
/dev/sdb1
1 393 3156741 83 Linux
Command (m
for
help):
|
通过以上步骤可以继续创建其他主分区,但主分区最多为4个,
创建逻辑分区
创建扩展分区:
创建逻辑分区前必须创建扩展分区,且此时需要把所有空间分配给扩展分区
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
Command (m
for
help): n
Command action
e extended
p primary partition (1-4)
e
Partition number (1-4): 4
First cylinder (394-1044, default 394):
Using default value 394
Last cylinder, +cylinders or +size{K,M,G} (394-1044, default 1044):
Using default value 1044
Command (m
for
help): p
Disk
/dev/sdb
: 8589 MB, 8589934592 bytes
255 heads, 63 sectors
/track
, 1044 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: 0x9adeb999
Device Boot Start End Blocks Id System
/dev/sdb1
1 393 3156741 83 Linux
/dev/sdb4
394 1044 5229157+ 5 Extended
Command (m
for
help):
|
创建逻辑分区:
扩展分区创建好之后,接着就可以创建逻辑分区。在创建逻辑分区的时候不需要指定分区编号,系统将会自动从5开始顺序编号
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
Command (m
for
help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (394-1044, default 394):
Using default value 394
Last cylinder, +cylinders or +size{K,M,G} (394-1044, default 1044): +3G
Command (m
for
help): p
Disk
/dev/sdb
: 8589 MB, 8589934592 bytes
255 heads, 63 sectors
/track
, 1044 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: 0x9adeb999
Device Boot Start End Blocks Id System
/dev/sdb1
1 393 3156741 83 Linux
/dev/sdb4
394 1044 5229157+ 5 Extended
/dev/sdb5
394 786 3156741 83 Linux
Command (m
for
help):
|
继续创建一个逻辑分区
1
2
3
4
5
6
7
8
9
10
11
12
13
|
Command (m
for
help): p
Disk
/dev/sdb
: 8589 MB, 8589934592 bytes
255 heads, 63 sectors
/track
, 1044 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: 0x9adeb999
Device Boot Start End Blocks Id System
/dev/sdb1
1 393 3156741 83 Linux
/dev/sdb4
394 1044 5229157+ 5 Extended
/dev/sdb5
394 786 3156741 83 Linux
/dev/sdb6
787 1044 2072353+ 83 Linux
Command (m
for
help):
|
分区创建好需要保存退出(w)或者放弃编辑退出(q)
1
2
3
4
5
|
Command (m
for
help): w
The partition table has been altered!
Calling ioctl() to re-
read
partition table.
Syncing disks.
[root@justin ~]
#
|
新分区需要重启启动或者执行partprobe命令使其生效
1
2
|
[root@justin ~]
# partprobe /dev/sdb
[root@justin ~]
#
|
格式化、挂载分区
创建好的分区需要格式化和挂载才能正常使用
格式化分区
1
2
3
|
[root@justin ~]
# mkfs -V -t ext4 -c /dev/sdb1
[root@justin ~]
# mkfs -t ext4 /dev/sdb5
[root@justin ~]
# mkfs -t vfat /dev/sdb6
|
挂载分区
在安装Linux系统的过程中,自动建立或识别的分区通常会由系统自动完成挂载,如“/”分区、“boot”分区等,对于后来新增加的硬盘分区、优盘、光盘等设备,就必须由管理员手动进行挂载。挂载一个分区时,必须为其指定一个目录作为挂载点,用户通过这个目录就可以访问设备中的文件、目录数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@justin ~]
# mkdir /mnt/{sdb1,sdb5,sdb6}
[root@justin ~]
# mount /dev/sdb1 /mnt/sdb1
[root@justin ~]
# mount /dev/sdb5 /mnt/sdb5
[root@justin ~]
# mount /dev/sdb6 /mnt/sdb6
[root@justin ~]
# df -Th
文件系统 类型 容量 已用 可用 已用%% 挂载点
/dev/sda2
ext4 9.9G 2.7G 6.7G 29% /
tmpfs tmpfs 504M 0 504M 0%
/dev/shm
/dev/sda1
ext4 194M 27M 158M 15%
/boot
/dev/sda5
ext4 7.7G 147M 7.2G 2%
/home
/dev/sdb1
ext4 3.0G 69M 2.8G 3%
/mnt/sdb1
/dev/sdb5
ext4 3.0G 69M 2.8G 3%
/mnt/sdb5
/dev/sdb6
vfat 2.0G 4.0K 2.0G 1%
/mnt/sdb6
[root@justin ~]
# vim /etc/fstab
proc
/proc
proc defaults 0 0
/dev/sdb1
/mnt/sdb1
ext4 defaults 0 0
/dev/sdb5
/mnt/sdb5
ext4 defaults 0 0
/dev/sdb6
/mnt/sdb6
vfat defaults 0 0
|
Tips:如果需要删除已创建好的分区,可以在fdisk命令操作界面中使用“d”指令将指定的分区删除,根据提示输入需要删除的分区序号即可。在删除时建议从最后一个分区开始进行删除,以免fdisk识别的分区序号发生紊乱。另外,如果扩展分区被删除,则扩展分区之下的逻辑分区也将同时被删除
Tips:格式化分区时候出现以下报错,是因为扩展分区不能格式化,需要在创建成逻辑分区
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@localhost ~]
# mkfs.ext4 /dev/sdb4
mke2fs 1.41.12 (17-May-2010)
mkfs.ext4: inode_size (128) * inodes_count (0) too big
for
a
filesystem with 0 blocks, specify higher inode_ratio (-i)
or lower inode count (-N).
[root@localhost ~]
# mkfs.ext4 /dev/sdb4
mke2fs 1.41.12 (17-May-2010)
mkfs.ext4: inode_size (128) * inodes_count (0) too big
for
a
filesystem with 0 blocks, specify higher inode_ratio (-i)
or lower inode count (-N).
[root@localhost ~]
#
|
大于2T分区创建
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
[root@localhost ~]
# fdisk -l
Disk
/dev/sda
: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors
/track
, 13054 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: 0x00047e01
Device Boot Start End Blocks Id System
/dev/sda1
* 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2
26 2115 16777216 82 Linux swap / Solaris
/dev/sda3
2115 13055 87874560 83 Linux
Disk
/dev/sdb
: 2089.1 GB, 2089072092160 bytes
255 heads, 63 sectors
/track
, 253981 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@localhost ~]
# parted /dev/sdb #parted>select sdb 假设磁盘为sdb
GNU Parted 2.1
Using
/dev/sdb
Welcome to GNU Parted! Type
'help'
to view a list of commands.
(parted) mklabel gpt
#将MBR磁盘格式化为GPT,mklabel msdos 将GPT磁盘转化为MBR磁盘
(parted) print
Model: VMware Virtual disk (scsi)
Disk
/dev/sdb
: 2089GB
Sector size (logical
/physical
): 512B
/512B
Partition Table: gpt
Number Start End Size File system Name Flags
(parted) mkpart primary 0 2089GB
#划分一个起始位置为0,全部的2089G的主分区,mkpart primary 0 -1 将整块磁盘分成一个分区
Warning: The resulting partition is not properly aligned
for
best performance.
Ignore
/Cancel
? I
(parted) print
Model: VMware Virtual disk (scsi)
Disk
/dev/sdb
: 2089GB
Sector size (logical
/physical
): 512B
/512B
Partition Table: gpt
Number Start End Size File system Name Flags
1 17.4kB 2089GB 2089GB primary
(parted) quit
Information: You may need to update
/etc/fstab
.
[root@localhost ~]
# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda3
ext4 83G 3.4G 75G 5% /
tmpfs tmpfs 3.9G 72K 3.9G 1%
/dev/shm
/dev/sda1
ext4 190M 39M 142M 22%
/boot
[root@localhost ~]
# fdisk -l
Disk
/dev/sda
: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors
/track
, 13054 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: 0x00047e01
Device Boot Start End Blocks Id System
/dev/sda1
* 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2
26 2115 16777216 82 Linux swap / Solaris
/dev/sda3
2115 13055 87874560 83 Linux
WARNING: GPT (GUID Partition Table) detected on
'/dev/sdb'
! The util
fdisk
doesn't support GPT. Use GNU Parted.
Disk
/dev/sdb
: 2089.1 GB, 2089072092160 bytes
255 heads, 63 sectors
/track
, 253981 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
Device Boot Start End Blocks Id System
/dev/sdb1
1 253982 2040109464+ ee GPT
[root@localhost ~]
# mkfs.ext4 -F /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
127508480 inodes, 510027357 blocks
25501367 blocks (5.00%) reserved
for
the super user
First data block=0
Maximum filesystem blocks=4294967296
15565 block
groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
102400000, 214990848
Writing inode tables:
done
Creating journal (32768 blocks):
done
Writing superblocks and filesystem accounting information:
done
This filesystem will be automatically checked every 31 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]
# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda3
ext4 83G 3.4G 75G 5% /
tmpfs tmpfs 3.9G 72K 3.9G 1%
/dev/shm
/dev/sda1
ext4 190M 39M 142M 22%
/boot
[root@localhost opt]
# mkdir /app
[root@localhost opt]
# mount /dev/sdb
sdb sdb1
[root@localhost opt]
# mount /dev/sdb1 /opt/
[root@localhost opt]
# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda3
ext4 83G 3.4G 75G 5% /
tmpfs tmpfs 3.9G 72K 3.9G 1%
/dev/shm
/dev/sda1
ext4 190M 39M 142M 22%
/boot
/dev/sdb1
ext4 1.9T 67M 1.8T 1%
/opt
[root@localhost opt]
# cd /opt/
[root@localhost opt]
# ls
lost+found
[root@localhost opt]
# umount /opt
umount
:
/opt
: device is busy.
(In some cases useful info about processes that use
the device is found by
lsof
(8) or fuser(1))
[root@localhost opt]
# cd ..
[root@localhost /]
# cd ..
[root@localhost /]
# umount /opt
[root@localhost /]
# cd /opt/
[root@localhost opt]
# ls
rh
[root@localhost opt]
# mount -t ext4 /dev/sdb1 /app/
[root@localhost opt]
# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda3
ext4 83G 3.4G 75G 5% /
tmpfs tmpfs 3.9G 72K 3.9G 1%
/dev/shm
/dev/sda1
ext4 190M 39M 142M 22%
/boot
/dev/sdb1
ext4 1.9T 67M 1.8T 1%
/app
[root@localhost opt]
# blki
blkid blkiomon
[root@localhost opt]
# blkid /dev/sdb1
/dev/sdb1
: UUID=
"452d68c5-58e3-41aa-979c-2ce38b31050f"
TYPE=
"ext4"
[root@localhost opt]
# cat /etc/fstab
UUID=
"452d68c5-58e3-41aa-979c-2ce38b31050f"
/app
ext4 defaults 0 0
[root@localhost opt]
# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda3
ext4 83G 3.5G 75G 5% /
tmpfs tmpfs 3.9G 72K 3.9G 1%
/dev/shm
/dev/sda1
ext4 190M 39M 142M 22%
/boot
/dev/sdb1
ext4 1.9T 67M 1.8T 1%
/app
[root@localhost opt]
#
|