一、创建分区
如果使用fdisk命令来创建、删除、修改分区,可以直接使用fdisk [DEVICE]来操作,如:
# fdisk /dev/sda
但需要注意的是,用命令来操作硬件,很容易造成数据丢失,故务必谨慎使用。
fdisk命令是个交互命令,使用fdisk [DEVICE] 时,系统会提示各种可以使用的选项:
[root@localhost ~]# fdisk /dev/sda
# 不要加上 -l选项
——————————————运行结果—————————————————
WARNING:DOS-compatible mode is deprecated. It‘s strongly recommended to switch off the mode (command ‘c‘) andchange display units to sectors (command ‘u‘).
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
# 查看分区类型对应的System ID
m print this menu
n adda 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
# 修改指定分区的System ID。每一个分区都会有一个系统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): l
# 查看分区类型对应的System ID
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris 1 FAT12 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT- 2 XENIXroot 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT- 3 XENIXusr 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT- 4 FAT16<32M 41 PPC PReP Boot 85 Linux extended c7 Syrinx 5 Extended 42 SFS 86 NTFS volume set da Non-FS data 6 FAT16 4d QNX4.x 87 NTFS volume set db CP/M / CTOS /. 7 HPFS/NTFS 4e QNX4.x 2nd part 88 Linux plaintext de DellUtility 8 AIX 4f QNX4.x 3rd part 8e Linux LVM df BootIt 9 AIXbootable 50 OnTrack DM 93 Amoeba e1 DOS access a OS/2Boot Manag 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O b W95FAT32 52 CP/M 9f BSD/OS e4 SpeedStor c W95FAT32 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs e W95FAT16 (LBA) 54 OnTrackDM6 a5 FreeBSD ee GPT f W95Ext‘d (LBA) 55 EZ-Drive a6 OpenBSD ef EFI (FAT-12/16/ 10 OPUS 56 Golden Bow a7 NeXTSTEP f0 Linux/PA-RISC b 11 Hidden FAT12 5c Priam Edisk a8 Darwin UFS f1 SpeedStor 12 Compaq diagnost 61 SpeedStor a9 NetBSD f4 SpeedStor 14 Hidden FAT16 <3 63 GNU HURD or Sys ab Darwin boot f2 DOS secondary 16 Hidden FAT16 64 Novell Netware af HFS / HFS+ fb VMware VMFS 17 Hidden HPFS/NTF 65 Novell Netware b7 BSDIfs fc VMware VMKCORE 18 AST SmartSleep 70 DiskSecure Mult b8 BSDIswap fd Linux raid auto 1b Hidden W95 FAT3 75 PC/IX bb Boot Wizard hid fe LANstep 1c Hidden W95 FAT3 80 Old Minix be Solaris boot ff BBT 1e Hidden W95 FAT1
上述分区类型中需要着重记忆的有:
82: Linux 分区
83: Linux/swap 交换分区
85: Linux extended扩展分区
8e: Linux LVM 逻辑卷
fd: Software Raid 软Raid 软件模拟Raid
前文中介绍过,如果要创建5个以上的分区,那么除了3个分区可以创建为主分区外,其余的分区都必须创建成扩展分区。使用fdisk -n 命令可以创建新的分区
[root@localhost~]# fdisk /dev/sda
Command(m for help): n
Command action
e extended
# 创建为扩展分区
p primary partition (1-4)
# 创建为扩展分区
p
# 这里输入p,表示要创建主分区
Partitionnumber (1-4): 3
# 根据前文的查询结果,1和2已经用过了,所以选择3
First cylinder (7859-15665,default 7859):
#可创建分区的柱面为7859-15665个,默认从第7859个柱面开始
Using default value 64
# 不指定就选择默认值为开始柱面
Last cylinder, +cylinders or+size{K,M,G} (7859-15665, default 15665): +10G
# 可以指定新分区大小,新分区的大小指定方式可以是柱面,也可以是大小,以KB,MB,GB为单位。这里指定10G。
Command (m for help): p
Disk /dev/sda: 128.8 GB,128849018880 bytes 255 heads, 63 sectors/track,15665 cylinders Units = cylinders of 16065 * 512= 8225280 bytes Sector size (logical/physical):512 bytes / 512 bytes I/O size (minimum/optimal): 512bytes / 512 bytes Disk identifier: 0x0008a622 Device Boot Start End Blocks Id System /dev/sda1 * 1 64 512000 83 Linux Partition 1 does not end oncylinder boundary. /dev/sda2 64 7858 62606361 83 Linux
/dev/sda3 7859 9164 10490445 83 Linux # 默认从第7859号柱面开始, # 自动选择9164号柱面为结束,默认的分区类型为 83 Linux
Command(m for help): w
# 如果新创建的分区符合要求,就输入w保存退出
The partition table has beenaltered! Calling ioctl() to re-readpartition table. WARNING: Re-reading the partitiontable failed with error 16: Device or resource busy. The kernel still uses the oldtable. The new table will be used at the next reboot or after you runpartprobe(8) or kpartx(8) Syncing disks.
如果还想再创建2个5G的分区,可以重复上述操作:
[root@localhost~]# fdisk /dev/sda
WARNING: DOS-compatible mode isdeprecated. It‘s strongly recommended to switch off the mode (command ‘c‘) andchange display units to sectors (command ‘u‘).
Command(m for help): p
# 查看当前分区信息
Disk /dev/sda: 128.8 GB,128849018880 bytes 255 heads, 63 sectors/track,15665 cylinders Units = cylinders of 16065 * 512= 8225280 bytes Sector size (logical/physical):512 bytes / 512 bytes I/O size (minimum/optimal): 512bytes / 512 bytes Disk identifier: 0x0008a622 Device Boot Start End Blocks Id System /dev/sda1 * 1 64 512000 83 Linux Partition 1 does not end oncylinder boundary. /dev/sda2 64 7858 62606361 83 Linux
/dev/sda3 7859 9164 10490445 83 Linux
Command (m for help): n
# 创建一个新的分区
Command action
e extended
p primary partition (1-4)
e
# 这里只能选择e(扩展分区),因为1、2、 3号主分区都已存在,而主分区最多只能有4个,如果还想创建多个分区,那么只能创建在扩展分区下
Selectedpartition 4
# 系统自动选择4号分区为新的扩展分区,5、6、 7... 号分区都只能存在于4号分区之下
First cylinder (9165-15665,default 9165): Using default value 9165 Lastcylinder, +cylinders or +size{K,M,G} (9165-15665, default 15665): # 这里选择默认柱面为结束位置,因为新的分区都只能创建在4号扩展分区之下, # 如果结束柱面小于15665,那么剩余的柱面将无法使用了。 Using default value 15665
Command(m for help): p
Disk /dev/sda: 128.8 GB,128849018880 bytes 255 heads, 63 sectors/track,15665 cylinders Units = cylinders of 16065 * 512= 8225280 bytes Sector size (logical/physical):512 bytes / 512 bytes I/O size (minimum/optimal): 512bytes / 512 bytes Disk identifier: 0x0008a622 Device Boot Start End Blocks Id System /dev/sda1 * 1 64 512000 83 Linux Partition 1 does not end oncylinder boundary. /dev/sda2 64 7858 62606361 83 Linux /dev/sda3 7859 9164 10490445 83 Linux
/dev/sda4 9165 15665 52219282+ 5 Extended # 新分区已经创建成功了,但必须注意的是,扩展分区无法直接使用 # 需要在扩展分区上再次使用n来创建新分区。
Command(m for help): n
# 这里可以看到,没有之前出现过的分区类型提示符,因为主分区已经全部被占用
# 新创建的分区只可能是逻辑分区,存在于4号扩展分区上。
First cylinder (9165-15665,default 9165): Using default value 9165 Last cylinder, +cylinders or+size{K,M,G} (9165-15665, default 15665): +5G # 新扩展分区选择5G
Command(m for help): n
# 再创建一个5G的扩展分区
First cylinder (9819-15665,default 9819): Using default value 9819 Last cylinder, +cylinders or+size{K,M,G} (9819-15665, default 15665): +5G
Command(m for help): p
Disk /dev/sda: 128.8 GB,128849018880 bytes 255 heads, 63 sectors/track,15665 cylinders Units = cylinders of 16065 * 512= 8225280 bytes Sector size (logical/physical):512 bytes / 512 bytes I/O size (minimum/optimal): 512bytes / 512 bytes Disk identifier: 0x0008a622 Device Boot Start End Blocks Id System /dev/sda1 * 1 64 512000 83 Linux Partition 1 does not end oncylinder boundary. /dev/sda2 64 7858 62606361 83 Linux /dev/sda3 7859 9164 10490445 83 Linux
/dev/sda4 9165 15665 52219282+ 5 Extended # 新创建了两个5G的 /dev/sda5 9165 9818 5253223+ 83 Linux # 逻辑分区,且逻辑分区 /dev/sda6 9819 10472 5253223+ 83 Linux # 为扩展分区的内部分区
Command(m for help): w
# 保存退出
The partition table has beenaltered! Calling ioctl() to re-readpartition table. WARNING: Re-reading the partitiontable 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 runpartprobe(8) or kpartx(8) Syncing disks.
二、删除分区
删除分区同样也可以使用fdisk [DEVICE]命令,选择d选项:
[root@localhost~]# fdisk /dev/sda
WARNING: DOS-compatible mode isdeprecated. It‘s strongly recommended to switch off the mode (command ‘c‘) andchange display units to sectors (command ‘u‘).
Command(m for help): d
# 输入d命令删除分区
Partitionnumber (1-6): 4
# 选择删除4号分区
Command(m for help): p
Disk /dev/sda: 128.8 GB,128849018880 bytes 255 heads, 63 sectors/track,15665 cylinders Units = cylinders of 16065 * 512= 8225280 bytes Sector size (logical/physical):512 bytes / 512 bytes I/O size (minimum/optimal): 512bytes / 512 bytes Disk identifier: 0x0008a622 Device Boot Start End Blocks Id System /dev/sda1 * 1 64 512000 83 Linux Partition 1 does not end oncylinder boundary. /dev/sda2 64 7858 62606361 83 Linux /dev/sda3 7859 9164 10490445 83 Linux
#使用p命令查看,可以发现4、 5、 6号分区都被删除了,因为4号分区是扩展分区。 #故此操作非常危险。
Command(m for help): q
# 为了防止误操作,使用q命令不保存退出。
[root@localhost~]# fdisk /dev/sda
WARNING: DOS-compatible mode isdeprecated. It‘s strongly recommended to switch off the mode (command ‘c‘) andchange display units to sectors (command ‘u‘).
Command(m for help): p
# 再次进入此命令,可以看到4、 5、 6号分区还没有被删除
Disk /dev/sda: 128.8 GB,128849018880 bytes 255 heads, 63 sectors/track,15665 cylinders Units = cylinders of 16065 * 512= 8225280 bytes Sector size (logical/physical):512 bytes / 512 bytes I/O size (minimum/optimal): 512bytes / 512 bytes Disk identifier: 0x0008a622 Device Boot Start End Blocks Id System /dev/sda1 * 1 64 512000 83 Linux Partition 1 does not end oncylinder boundary. /dev/sda2 64 7858 62606361 83 Linux /dev/sda3 7859 9164 10490445 83 Linux /dev/sda4 9165 15665 52219282+ 5 Extended /dev/sda5 9165 9818 5253223+ 83 Linux /dev/sda6 9819 10472 5253223+ 83 Linux
Command(m for help): d
Partitionnumber (1-6): 6
# 这次选择删除6号分区
Command(m for help): p
Disk /dev/sda: 128.8 GB,128849018880 bytes 255 heads, 63 sectors/track,15665 cylinders Units = cylinders of 16065 * 512= 8225280 bytes Sector size (logical/physical):512 bytes / 512 bytes I/O size (minimum/optimal): 512bytes / 512 bytes Disk identifier: 0x0008a622 Device Boot Start End Blocks Id System /dev/sda1 * 1 64 512000 83 Linux Partition 1 does not end oncylinder boundary. /dev/sda2 64 7858 62606361 83 Linux /dev/sda3 7859 9164 10490445 83 Linux /dev/sda4 9165 15665 52219282+ 5 Extended /dev/sda5 9165 9818 5253223+ 83 Linux
Command(m for help): w
# 确认要删除6号分区,可以保存退出
The partition table has beenaltered! Calling ioctl() to re-readpartition table. WARNING: Re-reading the partitiontable failed with error 16: Device or resource busy. The kernel still uses the oldtable. The new table will be used at the next reboot or after you runpartprobe(8) or kpartx(8) Syncing disks.
分区的创建和删除完成后,可以使用 fdisk -l 来查看具体的分区信息:
[root@localhost~]# fdisk -l /dev/sda
——————————————运行结果—————————————————
Disk /dev/sda: 128.8 GB,128849018880 bytes 255 heads, 63 sectors/track,15665 cylinders Units = cylinders of 16065 * 512= 8225280 bytes Sector size (logical/physical):512 bytes / 512 bytes I/O size (minimum/optimal): 512bytes / 512 bytes Disk identifier: 0x0008a622 Device Boot Start End Blocks Id System /dev/sda1 * 1 64 512000 83 Linux Partition 1 does not end oncylinder boundary. /dev/sda2 64 7858 62606361 83 Linux /dev/sda3 7859 9164 10490445 83 Linux /dev/sda4 9165 15665 52219282+ 5 Extended /dev/sda5 9165 9818 5253223+ 83 Linux
三、内核同步分区信息
需要注意的是,尽管这里可以看到新的磁盘分区3、4、5,并不意味着内核可以识别这三个新分区,如果内核识别不了,说明没有这样的硬件设备。可以通过文件/proc/partitions来查看内核信息:
[root@localhost~]# cat /proc/partitions
——————————————运行结果—————————————————
major minor #blocks name 8 0 125829120 sda 8 1 512000 sda1 8 2 125316096 sda2 253 0 52428800 dm-0 253 1 2064384 dm-1 253 2 70819840 dm-2
这里可以看到,3、4、5号分区并不存在,说明尽管软件可以识别这三个分区,但内核识别不了。这是由于之前的修改并没有同步给内核。
Red Hat5和Red Hat6在同步内核上有所不同,RedHat5可以直接使用partprobe [DEVICE]命令来读取硬件分区表信息,如果指定了具体的设备名称,则只探测这一个设备,如果不指定,则探测全局设备。
[root@localhost~]# partprobe /dev/sda
Warning: WARNING: the kernelfailed to re-read the partition table on /dev/sda (Device or resourcebusy). As a result, it may not reflectall of your changes until after reboot. #Red Hat5无需重启,但Red Hat6则重启之前无法获取最终的分区信息。
在Red Hat6上,要同步分区信息,可以使用的是partx命令,先使用man命令来查看一下该命令的使用方法:
[root@localhost~]# man partx
——————————————运行结果—————————————————
PARTX(8) PARTX(8) NAME partx - telling the kernel about presence and numbering of on-disk partitions. SYNOPSIS partx [-a|-d|-l] [--type TYPE] [--nrM-N] [partition] disk DESCRIPTION Given a block device ( disk ) and apartition table type , try to parsethe partition table, and list the contents. Optionally add or remove partitions. This is not an fdisk - adding andremoving partitions is not a change of the disk, but just telling the kernel about presence and numbering of on disk partitions. OPTIONS
-a add specified partitions or read disk andadd all partitions # 使用-a选项,可以读取磁盘分区信息到内核中。 -d delete specified or all partitions # 使用-d选项,可以将某个分区从分区列表中移除,或移除所有分区信息 -l list partitions. Note that the all numbersare in 512-byte sectors. # 显示所有的分区状况
...
故使用partx -a [PARTITION]DEVICE命令可以读取磁盘设备信息:
[root@localhost~]# partx -a /dev/sda
——————————————运行结果—————————————————
BLKPG: Device or resource busy error adding partition 1 BLKPG: Device or resource busy error adding partition 2 BLKPG: Device or resource busy error adding partition 3 BLKPG: Device or resource busy error adding partition 4 BLKPG: Device or resource busy error adding partition 5
# 这里会发现仍然无法读取磁盘信息,仍然需要重启设备。
如果在使用partx命令的同时,指定具体的磁盘分区,则可以同步分区信息,而无需重启设备。
[root@localhost~]# partx -a /dev/sdb3 /dev/sdb
#新挂载了一块硬盘,故新分区划分在设备/dev/sdb上
[root@localhost~]# partx -a /dev/sdb4 /dev/sdb
# 仍然有可能报设备繁忙的错误
BLKPG: Device or resource busy
error adding partition 1
[root@localhost~]# partx -a /dev/sdb5 /dev/sdb
[root@localhost~]# cat /proc/partitions
major minor #blocks name 8 0 26214400 sda 8 1 512000 sda1 8 2 25701376 sda2 8 16 99614720 sdb 8 19 10490413 sdb3 8 20 1 sdb4 8 21 5253223 sdb5 253 0 23633920 dm-0 253 1 2064384 dm-1
此时可以看到新分区的信息已经成功同步到内核上了。只有在/proc/partitions中能够看到分区信息,才认为分区成功,且该分区能够被格式化。至此,分区才创建完成。
本文出自 “重剑无锋 大巧不工” 博客,请务必保留此出处http://wuyelan.blog.51cto.com/6118147/1437276
3. 文件系统——创建、删除分区和内核同步分区信息,布布扣,bubuko.com
原文地址:http://wuyelan.blog.51cto.com/6118147/1437276