一、简介
通常我们用的比较多的一般都是fdisk工具来进行分区,但是现在由于磁盘越来越廉价,而且磁盘空间越来越大;而fdisk工具他对分区是有大小限制的,它只能划分小于2T的磁盘。如果使用fdisk对大于2T的磁盘分区,虽然可以分区,但仅识别2T的空间,所以磁盘容量超过2T的话,就使用Parted工具来实现对GPT磁盘进行分区操作。
GPT格式的磁盘相当于原来MBR磁盘中原来保留4个partition table的4*16个字节,只留第一个16个字节,类似于扩展分区,真正的partitiontable在512字节之后,GPT磁盘没有四个主分区的限制。
下面是实际演练,分别使用fdisk与parted对不同容量硬盘进行分区。
二、fdisk分区(2T容量以下硬盘使用)
1、查看当前磁盘情况
df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 133G 31G 96G 25% / tmpfs 16G 9.9G 5.9G 63% /dev/shm /dev/sda1 504M 43M 436M 9% /boot /dev/sda5 2.7T 2.4T 204G 93% /data /dev/sdb1 2.7T 2.4T 204G 93% /data [[email protected] tmp]# fdisk -l Disk /dev/sda: 299.4 GB, 299439751168 bytes 255 heads, 63 sectors/track, 36404 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: 0x000ccf41 Device Boot Start End Blocks Id System /dev/sda1 * 1 66 524288 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 66 1371 10485760 82 Linux swap / Solaris /dev/sda3 1371 18888 140704768 83 Linux /dev/sda4 18888 36405 140705792 5 Extended /dev/sda5 18888 36405 140704768 83 Linux WARNING: GPT (GUID Partition Table) detected on ‘/dev/sdb‘! The util fdisk doesn‘t support GPT. Use GNU Parted. Disk /dev/sdb: 2999.0 GB, 2998960914432 bytes 255 heads, 63 sectors/track, 364602 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 267350 2147483647+ ee GPT Disk /dev/sdc: 1999.8 GB, 1999844147200 bytes 81 heads, 63 sectors/track, 765421 cylinders Units = cylinders of 5103 * 512 = 2612736 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x42d69282
可以看到/dev/sdc是需要分区的
2、使用fdisk进行分区
[[email protected] tmp]# fdisk /dev/sdc Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0xc1b09294. 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): 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): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-243133, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-243133, default 243133): Using default value 243133 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 22: Invalid argument. 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、格式化为ext4系统
[[email protected] tmp]# mkfs.ext4 /dev/sdc1 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 122060800 inodes, 488242944 blocks 24412147 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=4294967296 14900 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 37 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
然后查看磁盘情况
[[email protected] tmp]# fdisk -l Disk /dev/sda: 299.4 GB, 299439751168 bytes 255 heads, 63 sectors/track, 36404 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: 0x000ccf41 Device Boot Start End Blocks Id System /dev/sda1 * 1 66 524288 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 66 1371 10485760 82 Linux swap / Solaris /dev/sda3 1371 18888 140704768 83 Linux /dev/sda4 18888 36405 140705792 5 Extended /dev/sda5 18888 36405 140704768 83 Linux WARNING: GPT (GUID Partition Table) detected on ‘/dev/sdb‘! The util fdisk doesn‘t support GPT. Use GNU Parted. Disk /dev/sdb: 2999.0 GB, 2998960914432 bytes 255 heads, 63 sectors/track, 364602 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 267350 2147483647+ ee GPT Disk /dev/sdc: 1999.8 GB, 1999844147200 bytes 81 heads, 63 sectors/track, 765421 cylinders Units = cylinders of 5103 * 512 = 2612736 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x42d69282 Device Boot Start End Blocks Id System /dev/sdc1 1 765634 1953513560 83 Linux
可以看到/dev/sdc1已经格式化完成
4、挂载
[[email protected] tmp]# mount /dev/sdc1 /u01 [[email protected] tmp]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 133G 32G 95G 25% / tmpfs 16G 9.9G 5.9G 63% /dev/shm /dev/sda1 504M 43M 436M 9% /boot /dev/sda5 2.7T 2.4T 204G 93% /data /dev/sdb1 2.7T 2.4T 204G 93% /data /dev/sdc1 1.8T 196M 1.7T 1% /u01
可以看到/dev/sdc1已经挂载到/u01目录
5、把挂载信息放入fstab里
[[email protected] tmp]# cat /etc/fstab "/etc/fstab" 19L, 979C# # /etc/fstab # Created by anaconda on Sat Dec 29 12:06:41 2012 # # Accessible filesystems, by reference, are maintained under ‘/dev/disk‘ # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=bcc682b0-5a55-4df8-8f7f-2ebfca7e0bf9 / ext4 defaults 1 1 UUID=f75b95fb-78cf-4070-b629-a5b1983edc83 /boot ext4 defaults 1 2 UUID=5743454b-f226-4c7c-8ad4-bf8db690572b /data xfs defaults 1 2 UUID=e9ea4e7a-d959-4c63-a562-dc059d58ac08 swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults,size=16G 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 /dev/sdb1 /data ext3 defaults 0 0
然后对最新的配置生效一下
[[email protected] tmp]# mount -a [[email protected] tmp]# mount /dev/sda3 on / type ext4 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) tmpfs on /dev/shm type tmpfs (rw,size=16G) /dev/sda1 on /boot type ext4 (rw) /dev/sda5 on /data type xfs (rw) /dev/sdb1 on /data type ext3 (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) none on /sys/kernel/config type configfs (rw) sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) /dev/sdc1 on /u01 type ext4 (rw)
6、测试一下新分区对象
[[email protected] tmp]# cd /u01 [[email protected] u01]# ll total 16 drwx------ 2 root root 16384 Jul 21 09:49 lost+found [[email protected] u01]# touch test [[email protected] u01]# ll total 16 drwx------ 2 root root 16384 Jul 21 09:49 lost+found -rw-r--r-- 1 root root 0 Jul 21 10:03 test [[email protected] u01]# rm test rm: remove regular empty file `test‘? y
三、parted分区(2T容量以上硬盘使用)
1、查看分区情况
[email protected]:/home 02:36:30 # fdisk -l Disk /dev/sda: 999.7 GB, 999653638144 bytes 255 heads, 63 sectors/track, 121534 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: 0x00045789 Device Boot Start End Blocks Id System /dev/sda1 * 1 131 1048576 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 131 4309 33554432 82 Linux swap / Solaris /dev/sda3 4309 121535 941620224 83 Linux Disk /dev/sdb: 2999.0 GB, 2998960914432 bytes 255 heads, 63 sectors/track, 364602 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
可以看到/dev/sdb是需要分区的,磁盘容量为3t,fdisk没办法分区,只能使用parted进行gpt分区了
下面是当前已经在用的磁盘情况
[email protected]:/home 02:38:04 # df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 884G 2.0G 838G 1% / tmpfs 32G 0 32G 0% /dev/shm /dev/sda1 1008M 56M 902M 6% /boot
2、使用parted进行分区
[email protected]:/home 02:38:06 # parted /dev/sdb GNU Parted 2.1 Using /dev/sdb Welcome to GNU Parted! Type ‘help‘ to view a list of commands. (parted) p Error: /dev/sdb: unrecognised disk label (parted) mklabel gpt (parted) mkpart primary 0% 100% (parted) print Model: DELL PERC H710P (scsi) Disk /dev/sdb: 2999GB Sector size (logical/physical): 512B/512B Partition Table: gpt Number Start End Size File system Name Flags 1 1049kB 2999GB 2999GB primary (parted) quit Information: You may need to update /etc/fstab.
介绍一下各个命令的意思 p 是打印磁盘信息,查看分区的情况,找到起始和结束位置。 mklabel gpt 是进行gpt类型分区 mkpart primary 0% 100% 是primary指分区类型为主分区,0是分区开始位置,100%是分区结束位置。相同的命令为:mkpart primary 0-1 或者是:mkpart primary 0 XXXXXX结束的空间 print 打印当前分区,查看分区设置是否正确 quit 退出
在看看当前已用硬盘情况,可用看到/dev/sdb还是没有挂载
[email protected]:/home 02:44:05 # df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 884G 2.0G 838G 1% / tmpfs 32G 0 32G 0% /dev/shm /dev/sda1 1008M 56M 902M 6% /boot [email protected]:/home
看看磁盘分区情况
02:44:07 # fdisk -l Disk /dev/sda: 999.7 GB, 999653638144 bytes 255 heads, 63 sectors/track, 121534 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: 0x00045789 Device Boot Start End Blocks Id System /dev/sda1 * 1 131 1048576 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 131 4309 33554432 82 Linux swap / Solaris /dev/sda3 4309 121535 941620224 83 Linux WARNING: GPT (GUID Partition Table) detected on ‘/dev/sdb‘! The util fdisk doesn‘t support GPT. Use GNU Parted. Disk /dev/sdb: 2999.0 GB, 2998960914432 bytes 255 heads, 63 sectors/track, 364602 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 267350 2147483647+ ee GPT
可用看到/dev/sdb1已经进行了gpt类型分区
3、格式化为ext4系统
02:44:12 # 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 183042048 inodes, 732167680 blocks 36608384 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=4294967296 22344 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, 512000000, 550731776, 644972544 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 30 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [email protected]:/home 02:47:23 # df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 884G 2.0G 838G 1% / tmpfs 32G 0 32G 0% /dev/shm /dev/sda1 1008M 56M 902M 6% /boot
4、挂载
创建挂载目录data
[email protected]:/home 02:49:38 # mkdir /data 然后挂载/dev/sdb1到/data目录 [email protected]:/home 02:50:36 # mount /dev/sdb1 /data [email protected]:/home 02:50:41 # df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 884G 2.0G 838G 1% / tmpfs 32G 0 32G 0% /dev/shm /dev/sda1 1008M 56M 902M 6% /boot /dev/sdb1 2.7T 201M 2.6T 1% /data
5、加入fstab
[email protected]:/home 02:51:39 # cat /etc/fstab # # /etc/fstab # Created by anaconda on Wed Aug 27 13:39:16 2014 # # Accessible filesystems, by reference, are maintained under ‘/dev/disk‘ # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=1bc74b58-1fbe-4f65-9790-d02abc23d461 / ext4 defaults 1 1 UUID=ebbbaa89-e509-4fef-8763-be6eed9408c7 /boot ext4 defaults 1 2 UUID=98417724-a1c5-4f9a-9750-85beda11e4e1 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 /dev/sdb1 /data ext4 defaults 0 0
生效一下
02:51:43 # mount -a [email protected]:/home 02:51:45 # df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 884G 2.0G 838G 1% / tmpfs 32G 0 32G 0% /dev/shm /dev/sda1 1008M 56M 902M 6% /boot /dev/sdb1 2.7T 201M 2.6T 1% /data
6、测试
11:04:13 # cd /data [email protected]:/data 11:06:44 # echo "this is test">test [email protected]:/data 11:06:52 # cat test this is test [email protected]:/data 11:06:54 # rm test rm: remove regular file `test‘? y [email protected]:/data
其实本文主要介绍的就是如何对不同容量的硬盘使用不同方法进行分区,并对每一步骤都有解释,希望对各位有帮助。