Linux:磁盘分区和挂载(parted和fdisk两种方式详细说明)
parted和fdisk
parted命令可以划分单个分区大于2T的GPT格式的分区,也可以划分普通的MBR分区,fdisk命令对于大于2T的分区无法划分(大于2.2TB的存储空间用fdisk不支持,需要采用parted来分区),所以用fdisk无法看到parted划分的GPT格式的分区。
##parted和fdisk在操作上的区别
下面通过两个例子来说明。
环境:
-
Centos6.5 64位操作系统(阿里云虚拟机)
-
双硬盘,一块以挂载,一块为空。所有的操作都在第二块硬盘上。
parted分区操作步骤
先查看信息
[root@z3 ~]# parted -lModel: Xen Virtual Block Device (xvd)Disk /dev/xvda: 21.5GBSector size (logical/physical): 512B/512BPartition Table: msdosNumber Start End Size Type File system Flags 1 1049kB 21.5GB 21.5GB primary ext4 bootError: /dev/xvdb: unrecognised disk label开始分区
[root@z3 ~]# parted /dev/xvdbGNU Parted 2.1Using /dev/xvdbWelcome to GNU Parted! Type 'help' to view a list ofcommands. (parted) help //使用帮助查看 align-check TYPE N check partition N for TYPE(min|opt) alignment check NUMBER do a simple check on the file system cp [FROM-DEVICE] FROM-NUMBER TO-NUMBER copy file system to another partition help [COMMAND] print general help, or help on COMMAND mklabel,mktable LABEL-TYPE create a new disklabel (partition table) mkfs NUMBER FS-TYPE make a FS-TYPE file system on partition NUMBER mkpart PART-TYPE [FS-TYPE] START END make a partition mkpartfs PART-TYPE FS-TYPE START END make a partition with a file system move NUMBER START END move partition NUMBER 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 resize NUMBER START END resize partition NUMBER and its file system rm NUMBER delete partition NUMBER select DEVICE choose the device to edit 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 //选择格式 New disk label type? gpt (parted) mkpart //选择分区的名称 Partition name? []? part1 File system type? [ext2]? //直接回车Start? 1 //从1开始 End? 20G //选择20G大小(parted) print //查看结果Model: Xen Virtual Block Device (xvd)Disk /dev/xvdb: 21.5GBSector size (logical/physical): 512B/512BPartition Table: gptNumber Start End Size File system Name Flags 1 1049kB 20.0GB 20.0GB part1 (parted) mkpart //由于还有剩余空间,建立第二个分区 Partition name? []? part2 File system type? [ext2]?Start? 20.0GB End? 21.5GB (parted) print Model: Xen Virtual Block Device (xvd)Disk /dev/xvdb: 21.5GBSector size (logical/physical): 512B/512BPartition Table: gptNumber Start End Size File system Name Flags 1 1049kB 20.0GB 20.0GB part1 2 20.0GB 21.5GB 1474MB part2(parted) quit Information: You may need to update /etc/fstab.//查看结果[root@z3 ~]# parted -lModel: Xen Virtual Block Device (xvd)Disk /dev/xvda: 21.5GBSector size (logical/physical): 512B/512BPartition Table: msdosNumber Start End Size Type File system Flags 1 1049kB 21.5GB 21.5GB primary ext4 bootModel: Xen Virtual Block Device (xvd)Disk /dev/xvdb: 21.5GBSector size (logical/physical): 512B/512BPartition Table: gptNumber Start End Size File system Name Flags 1 1049kB 20.0GB 20.0GB part1 2 20.0GB 21.5GB 1474MB part2然后进行格式化
[root@z3 ~]# mkfs.ext4 /dev/xvdb1mke2fs 1.41.12 (17-May-2010)Filesystem label=OS type: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks1220608 inodes, 4882432 blocks244121 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=4294967296149 block groups32768 blocks per group, 32768 fragments per group8192 inodes per groupSuperblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000Writing inode tables: done Creating journal (32768 blocks): doneWriting superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 39 mounts or180 days, whichever comes first. Use tune2fs -c or -i to override.[root@z3 ~]# df -hFilesystem Size Used Avail Use% Mounted on/dev/xvda1 20G 2.1G 17G 12% /tmpfs 938M 0 938M 0% /dev/shm[root@z3 ~]# mkfs.ext4 /dev/xvdb2mke2fs 1.41.12 (17-May-2010)Filesystem label=OS type: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks90112 inodes, 359936 blocks17996 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=36909875211 block groups32768 blocks per group, 32768 fragments per group8192 inodes per groupSuperblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912Writing inode tables: done Creating journal (8192 blocks): doneWriting superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 33 mounts or180 days, whichever comes first. Use tune2fs -c or -i to override.[root@z3 ~]# parted -lModel: Xen Virtual Block Device (xvd)Disk /dev/xvda: 21.5GBSector size (logical/physical): 512B/512BPartition Table: msdosNumber Start End Size Type File system Flags 1 1049kB 21.5GB 21.5GB primary ext4 bootModel: Xen Virtual Block Device (xvd)Disk /dev/xvdb: 21.5GBSector size (logical/physical): 512B/512BPartition Table: gptNumber Start End Size File system Name Flags 1 1049kB 20.0GB 20.0GB ext4 part1 2 20.0GB 21.5GB 1474MB ext4 part2[root@z3 /]# mount /dev/xvdb1 /mnt[root@z3 /]# mkdir /part2//输入错误了,但是通过这次操作可以发现,同一个分区是可以挂载在两个目录下的。[root@z3 /]# mount /dev/xvdb1 /part2[root@z3 /]# df -hFilesystem Size Used Avail Use% Mounted on/dev/xvda1 20G 2.1G 17G 12% /tmpfs 938M 0 938M 0% /dev/shm/dev/xvdb1 19G 172M 18G 1% /mnt/dev/xvdb1 19G 172M 18G 1% /part2[root@z3 /]# umount /part2[root@z3 /]# df -hFilesystem Size Used Avail Use% Mounted on/dev/xvda1 20G 2.1G 17G 12% /tmpfs 938M 0 938M 0% /dev/shm/dev/xvdb1 19G 172M 18G 1% /mnt[root@z3 /]# mount /dev/xvdb2 /part2[root@z3 /]# df -hFilesystem Size Used Avail Use% Mounted on/dev/xvda1 20G 2.1G 17G 12% /tmpfs 938M 0 938M 0% /dev/shm/dev/xvdb1 19G 172M 18G 1% /mnt/dev/xvdb2 1.4G 35M 1.3G 3% /part2fdisk分区操作步骤
//由于之前已经尝试分区过了,先删除,顺便记录一下删除惭怍。[root@z2 ~]# fdisk -lDisk /dev/xvda: 21.5 GB, 21474836480 bytes255 heads, 63 sectors/track, 2610 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00078f9c Device Boot Start End Blocks Id System/dev/xvda1 * 1 2611 20970496 83 LinuxDisk /dev/xvdb: 21.5 GB, 21474836480 bytes255 heads, 63 sectors/track, 2610 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0xbc79a9bb Device Boot Start End Blocks Id System/dev/xvdb1 1 2610 20964793+ 5 Extended/dev/xvdb5 1 2610 20964762 83 Linux[root@z2 ~]# fdisk /dev/xvdbWARNING: 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): d //删除Partition number (1-5): 1 //选择分区Command (m for help): p Disk /dev/xvdb: 21.5 GB, 21474836480 bytes255 heads, 63 sectors/track, 2610 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0xbc79a9bb Device Boot Start End Blocks Id SystemCommand (m for help): n //新建Command action e extended p primary partition (1-4)p //新建主分区Partition number (1-4): 1 First cylinder (1-2610, default 1):Using default value 1Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +19GCommand (m for help): n Command action e extended p primary partition (1-4)e //再新建一个扩展分区Partition number (1-4): 2First cylinder (2482-2610, default 2482): //直接默认起始位置Using default value 2482Last cylinder, +cylinders or +size{K,M,G} (2482-2610, default 2610):Using default value 2610Command (m for help): n Command action l logical (5 or over) p primary partition (1-4)l //在扩展分区上新建逻辑分区,直接使用所有的空间First cylinder (2482-2610, default 2482):Using default value 2482Last cylinder, +cylinders or +size{K,M,G} (2482-2610, default 2610):Using default value 2610Command (m for help): pDisk /dev/xvdb: 21.5 GB, 21474836480 bytes255 heads, 63 sectors/track, 2610 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0xbc79a9bb Device Boot Start End Blocks Id System/dev/xvdb1 1 2481 19928601 83 Linux/dev/xvdb2 2482 2610 1036192+ 5 Extended/dev/xvdb5 2482 2610 1036161 83 LinuxCommand (m for help): wThe partition table has been altered!Calling ioctl() to re-read partition table.Syncing disks.[root@z2 ~]# fdisk -lDisk /dev/xvda: 21.5 GB, 21474836480 bytes255 heads, 63 sectors/track, 2610 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00078f9c Device Boot Start End Blocks Id System/dev/xvda1 * 1 2611 20970496 83 LinuxDisk /dev/xvdb: 21.5 GB, 21474836480 bytes255 heads, 63 sectors/track, 2610 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0xbc79a9bb Device Boot Start End Blocks Id System/dev/xvdb1 1 2481 19928601 83 Linux/dev/xvdb2 2482 2610 1036192+ 5 Extended/dev/xvdb5 2482 2610 1036161 83 Linux格式化的时候需要注意,逻辑分区可能不能直接分区,网上的方法是使用partprobe,但是我一直不能成功,最后还是通过reboot解决。
[root@z2 ~]# mkfs.ext4 /dev/xvdb1mke2fs 1.41.12 (17-May-2010)Filesystem label=OS type: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks1246032 inodes, 4982150 blocks249107 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=4294967296153 block groups32768 blocks per group, 32768 fragments per group8144 inodes per groupSuperblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000Writing inode tables: done Creating journal (32768 blocks): doneWriting superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 28 mounts or180 days, whichever comes first. Use tune2fs -c or -i to override.[root@z2 ~]# mkfs.ext4 /dev/xvdb5mke2fs 1.41.12 (17-May-2010)Filesystem label=OS type: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks64768 inodes, 259040 blocks12952 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=2684354568 block groups32768 blocks per group, 32768 fragments per group8096 inodes per groupSuperblock backups stored on blocks: 32768, 98304, 163840, 229376Writing inode tables: done Creating journal (4096 blocks): doneWriting superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 36 mounts or180 days, whichever comes first. Use tune2fs -c or -i to override.[root@z2 ~]# fdisk -lDisk /dev/xvda: 21.5 GB, 21474836480 bytes255 heads, 63 sectors/track, 2610 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00078f9c Device Boot Start End Blocks Id System/dev/xvda1 * 1 2611 20970496 83 LinuxDisk /dev/xvdb: 21.5 GB, 21474836480 bytes255 heads, 63 sectors/track, 2610 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0xbc79a9bb Device Boot Start End Blocks Id System/dev/xvdb1 1 2481 19928601 83 Linux/dev/xvdb2 2482 2610 1036192+ 5 Extended/dev/xvdb5 2482 2610 1036161 83 Linux[root@z2 ~]# mount /dev/xvdb1 /mnt[root@z2 ~]# mkdir /part2[root@z2 ~]# mkfs.ext4 /dev/xvdb5mke2fs 1.41.12 (17-May-2010)Filesystem label=OS type: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks64768 inodes, 259040 blocks12952 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=2684354568 block groups32768 blocks per group, 32768 fragments per group8096 inodes per groupSuperblock backups stored on blocks: 32768, 98304, 163840, 229376Writing inode tables: done Creating journal (4096 blocks): doneWriting superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 38 mounts or180 days, whichever comes first. Use tune2fs -c or -i to override.[root@z2 ~]# mount /dev/xvdb5 /part22015-08-14 22:14:54
作者:dantezhao | 简书 | CSDN | GITHUB 文章推荐:http://dantezhao.com/readme 个人主页:http://dantezhao.com 文章可以转载, 但必须以超链接形式标明文章原始出处和作者信息