fdisk分区工具介绍
fdisk - Partition table manipulator for Linux
Linux下最常用的分区工具,一般是装完系统后进行分区。装系统之前的分区
多数都是系统ISO里面的分区工具,或者raid里的小磁盘。
fdisk只能root权限用户使用。
fdisk适合于对装系统后的剩余空间进行分区,例如:安装系统时没有全部分区,或者安装系统后添加新磁盘。
fdisk支持mbr分区表(2TB以内)
fdisk分区的实质
用fdisk分区的实质,就是修改0磁头0磁道1扇区的前446字节之后的64字节的分区表信息。
可以使用fdisk分区的磁盘大小必须小于2T,如果大于2T,分区就用parted(gpt)。
parted:
parted是一个磁盘分区管理工具,它比fdisk更加灵活,功能也更丰富,同时还支持GUID分区表,这在IA64平台上管理磁盘时非常有用,他同时还支持交互模式和非交互模式,它除了能够进行分区的添加,删除等常见的操作外,还可以移动分区,制作文件系统,调整文件系统大小,复制文件系统。
实践:
创建分区:
[root@oldboyedu ~]# 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): n #<===新建一个分区
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): 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): +100M
Partition 1 of type Linux and of size 100 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: 0x000006b6
Device Boot Start End Blocks Id System
/dev/sdb1 2048 206847 102400 83 Linux
Command (m for help): n#<===新建分区
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (2-4, default 2):
First sector (206848-2097151, default 206848):
Using default value 206848
Last sector, +sectors or +size{K,M,G} (206848-2097151, default 2097151): +100M
Partition 2 of type Linux and of size 100 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: 0x000006b6
Device Boot Start End Blocks Id System
/dev/sdb1 2048 206847 102400 83 Linux
/dev/sdb2 206848 411647 102400 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@oldboyedu ~]# ls -l /dev/sdb*
brw-rw---- 1 root disk 8, 16 Mar 29 11:31 /dev/sdb
brw-rw---- 1 root disk 8, 17 Mar 29 11:31 /dev/sdb1
brw-rw---- 1 root disk 8, 18 Mar 29 11:31 /dev/sdb2
[root@oldboyedu ~]# 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): l #<==显示磁盘信息
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT
1e Hidden W95 FAT1 80 Old Minix
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: 0xbc9b0906
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
/dev/sdb5 925696 1232895 153600 83 Linux
/dev/sdb6 1234944 1542143 153600 83 Linux
/dev/sdb7 1544192 1851391 153600 83 Linux
Command (m for help): t #<==更改分区系统id
Partition number (1-7, default 7): 7
Hex code (type L to list all codes): L
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT
1e Hidden W95 FAT1 80 Old Minix
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
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: 0xbc9b0906
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
/dev/sdb5 925696 1232895 153600 83 Linux
/dev/sdb6 1234944 1542143 153600 83 Linux
/dev/sdb7 1544192 1851391 153600 8e Linux LVM
Command (m for help):
dd if=/dev/sda of=/dev/sdc bs=512 count=10240
来源 生成文件系统 block大小 块个数
[root@oldboyedu ~]# dd if=/dev/sda of=/dev/sdc bs=512 count=10240 #<==生成文件系统
10240+0 records in
10240+0 records out
5242880 bytes (5.2 MB) copied, 0.0810606 s, 64.7 MB/s
[root@oldboyedu ~]# ls -l /dev/sdc #<==查看分区前的设备的状态
-rw-r--r-- 1 root root 5242880 Mar 29 09:55 /dev/sdc
[root@oldboyedu ~]# file /dev/sdc
/dev/sdc: x86 boot sector; partition 1: ID=0x83, active, starthead 32, startsector 2048, 524288 sectors; partition 2: ID=0x82, starthead 194, startsector 526336, 1572864 sectors; partition 3: ID=0x83, starthead 170, startsector 2099200, 39843840 sectors, code offset 0x63
[root@oldboyedu ~]# cat /proc/partitions
major minor #blocks name
8 16 1048576 sdb
8 17 153600 sdb1
8 18 153600 sdb2
8 19 153600 sdb3
8 20 1 sdb4
8 21 153600 sdb5
8 22 153600 sdb6
8 23 153600 sdb7
8 0 20971520 sda
8 1 262144 sda1
8 2 786432 sda2
8 3 19921920 sda3
11 0 1048575 sr0
[root@oldboyedu ~]# partprobe /dev/sdb #<==更新内核的硬盘分区表信息
用parted分区;
[root@oldboyedu ~]# parted /dev/sdb #<==对/dev下的sdb分区
GNU Parted 3.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
help [COMMAND] print general help, or help on COMMAND
mklabel,mktable LABEL-TYPE create a new disklabel (partition table)
mkpart PART-TYPE [FS-TYPE] START END make a partition
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
resizepart NUMBER END resize partition NUMBER
rm NUMBER delete partition NUMBER
select DEVICE choose the device to edit
disk_set FLAG STATE change the FLAG on selected device
disk_toggle [FLAG] toggle the state of FLAG on selected device
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) mklabel gpt #<==创建分区表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? Yes #<==Yes
(parted) p #<==显示分区表信息
Model: VMware, VMware Virtual S (scsi) #<==磁盘型号,这里采用VMware虚拟化演示
Disk /dev/sdb: 1074MB #<==磁盘大小
Sector size (logical/physical): 512B/512B #<==扇区大小
Partition Table: gpt #<==分区表类型
Disk Flags:
Number Start End Size File system Name Flags
(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)
(parted) mkpart primary 481 580 #<==创建主分区,大小为100M
Warning: You requested a partition from 481MB to 580MB (sectors 939453..1132812).
The closest location we can manage is 481MB to 481MB (sectors 940031..940031).
Is this still acceptable to you?
Yes/No? yes
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? I #<==输入Ignore忽略警告
(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
3 481MB 481MB 512B primary
2 481MB 580MB 98.6MB primary
(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
3 481MB 481MB 512B primary
2 481MB 580MB 98.6MB primary
4 581MB 600MB 18.9MB logic
(parted) help
align-check TYPE N check partition N for TYPE(min|opt) alignment
help [COMMAND] print general help, or help on COMMAND
mklabel,mktable LABEL-TYPE create a new disklabel (partition table)
mkpart PART-TYPE [FS-TYPE] START END make a partition
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
resizepart NUMBER END resize partition NUMBER
rm NUMBER delete partition NUMBER
select DEVICE choose the device to edit
disk_set FLAG STATE change the FLAG on selected device
disk_toggle [FLAG] toggle the state of FLAG on selected device
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) rm 4 #<==删除
(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
3 481MB 481MB 512B primary
2 481MB 580MB 98.6MB primary
交互式分区:
[root@oldboyedu ~]# parted /dev/sdb #<==交互式开始分区
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) help
(parted) mklabel gpt #<==磁盘创建GPT分区表,大于2G的磁盘必须执行这一步
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? Yes
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt #<==修改结果
(parted) mkpart primary 0 480 #<==创建第一个主分区480M。
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? Ignore #忽略提示。
(parted) mkpart primary 481 580 #<==创建第二个主分区100M。
Warning: You requested a partition from 481MB to 580MB (sectors 939453..1132812).
The closest location we can manage is 481MB to 481MB (sectors 940031..940031).
Is this still acceptable to you?
Yes/No? Y
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? I #<==忽略提示。
(parted) mkpart logic 581 600 #<==创建一个20M逻辑分区。
(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
3 481MB 481MB 512B primary
2 481MB 580MB 98.6MB primary
4 581MB 600MB 18.9MB logic
(parted) rm 4 #删除
(parted) p
修改分区格式:
(parted) mklabel msdos #<==修改分区表格式
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: msdos #<==修改结果
Disk Flags:
fsck:检查并修复Linux文件系统
[root@oldboyedu ~]# mkfs -t ext4 /dev/sdb1 #格式化ext4文件系统 mkfs.ext4
其实关键就是生成一定数量的Inode和Block。
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
25688 inodes, 102400 blocks
5120 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33685504
13 block groups
8192 blocks per group, 8192 fragments per group
1976 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
挂载:
[root@oldboyedu ~]# mount /dev/sdb1 /mnt #<==临时挂载到/mnt
[root@oldboyedu ~]# df -h #<==查看挂载结果
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 1.9G 18G 10% /
devtmpfs 980M 5.0M 975M 1% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 18M 973M 2% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sda1 253M 136M 118M 54% /boot
tmpfs 199M 0 199M 0% /run/user/0
tmpfs 199M 0 199M 0% /run/user/1000
/dev/sdb1 93M 1.6M 85M 2% /mnt #<====已挂载。
[root@oldboyedu ~]# cat /proc/mounts #<==查看挂载结果
rootfs / rootfs rw 0 0
sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0
proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
configfs /sys/kernel/config configfs rw,relatime 0 0
/dev/sda3 / xfs rw,relatime,attr2,inode64,noquota 0 0
systemd-1 /proc/sys/fs/binfmt_misc autofs rw,relatime,fd=32,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=19277 0 0
hugetlbfs /dev/hugepages hugetlbfs rw,relatime 0 0
debugfs /sys/kernel/debug debugfs rw,relatime 0 0
mqueue /dev/mqueue mqueue rw,relatime 0 0
/dev/sda1 /boot xfs rw,relatime,attr2,inode64,noquota 0 0
tmpfs /run/user/0 tmpfs rw,nosuid,nodev,relatime,size=202812k,mode=700 0 0
tmpfs /run/user/1000 tmpfs rw,nosuid,nodev,relatime,size=202812k,mode=700,uid=1000,gid=1000 0 0
/dev/sdb1 /mnt ext4 rw,relatime,data=ordered 0 0 #<==已挂载。
[root@oldboyedu ~]# touch /mnt/oldboy #<==测试。
[root@oldboyedu ~]# ls /mnt
lost+found oldboy
卸载文件系统:
[root@oldboyedu /mnt]# umount /mnt
umount: /mnt: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
[root@oldboyedu /mnt]# pwd
/mnt
You have new mail in /var/spool/mail/root
[root@oldboyedu /mnt]# umount -lf /mnt #<==强制卸载
[root@oldboyedu /mnt]# df -h #<==查看
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 1.9G 18G 10% /
devtmpfs 980M 5.0M 975M 1% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 18M 973M 2% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sda1 253M 136M 118M 54% /boot
tmpfs 199M 0 199M 0% /run/user/0
tmpfs 199M 0 199M 0% /run/user/1000
[root@oldboyedu ~]# mount /dev/sdb2 /opt #<==没有格式化没法挂载
mount: /dev/sdb2 is write-protected, mounting read-only
mount: unknown filesystem type '(null)'
[root@oldboyedu ~]# mkfs.xfs /dev/sdb2 #<==格式化xfs文件系统
meta-data=/dev/sdb2 isize=512 agcount=4, agsize=6400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=25600, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=855, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@oldboyedu ~]# mount -t xfs /dev/sdb2 /opt
You have new mail in /var/spool/mail/root
[root@oldboyedu ~]# df -h|grep opt
/dev/sdb2 97M 5.3M 92M 6% /opt
[root@oldboyedu ~]# touch /opt/oldgirl
[root@oldboyedu ~]# ls /opt
oldgirl
网友评论