美文网首页
Linux使用fdisk创建分区详解

Linux使用fdisk创建分区详解

作者: moxiaoxiao | 来源:发表于2018-09-02 15:48 被阅读47次

    一、fdisk命令语法

    • fdisk [-l] [设备名称]

    命令描述

    • fdisk用来对小于2T的磁盘进行分区,如果大于2T需用parted工具

    命令选项

    • -l 后面不加设备名称,会列出系统中所有的磁盘设备以及分区表;加上设备名称,会列出设备的分区表。

    二、fdisk实操
    使用

    [root@localhost]~# fdisk -l |grep dev
    Disk /dev/sda: 10.7 GB, 10737418240 bytes
    /dev/sda1   *           1          64      512000   83  Linux
    /dev/sda2              64        1306     9972736   8e  Linux LVM
    Disk /dev/sdb: 1073 MB, 1073741824 bytes
    Disk /dev/sdc: 1073 MB, 1073741824 bytes
    Disk /dev/sdd: 1073 MB, 1073741824 bytes
    Disk /dev/sde: 1073 MB, 1073741824 bytes
    Disk /dev/mapper/VolGroup-lv_swap: 1044 MB, 1044381696 bytes
    Disk /dev/mapper/VolGroup-lv_root: 9164 MB, 9164554240 bytes
    [root@localhost]~#
    

    这里我将使用/dev/sdb 这块1G大小的磁盘进行分区,分为/dev/sdb1

    [root@localhost]~# 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 0x06194404.
    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): n
    
    
    输入n 创建新分区
    
    Command (m for help): n
    Command action
       e   extended
       p   primary partition (1-4)      #这里可以选择是作为扩展分区还是主分区。这里作为主分区,则选择p
    p
    Partition number (1-4): 1         #做第一块主分区
    First cylinder (1-130, default 1):
    Using default value 1
    Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130): +500M      #分区大小为K,M,G。制作分区的大小,这里选择第一块分区大小为500M
    
    Command (m for help): p      #输入p可以查看刚才分区的情况
    
    Disk /dev/sdb: 1073 MB, 1073741824 bytes
    255 heads, 63 sectors/track, 130 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: 0x06194404
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1               1          65      522081   83  Linux
    
    Command (m for help):
    

    最好输入w 保存我们刚才从sdb分区出来的sdb1

    Command (m for help): w
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    Syncing disks.
    [root@localhost]~#
    

    使用命令:fdisk -l |grep dev 可以查看分区成功

    [root@localhost]~# fdisk -l |grep dev
    Disk /dev/sda: 10.7 GB, 10737418240 bytes
    /dev/sda1   *           1          64      512000   83  Linux
    /dev/sda2              64        1306     9972736   8e  Linux LVM
    Disk /dev/sdb: 1073 MB, 1073741824 bytes
    /dev/sdb1               1          65      522081   83  Linux
    Disk /dev/sdc: 1073 MB, 1073741824 bytes
    Disk /dev/sdd: 1073 MB, 1073741824 bytes
    Disk /dev/sde: 1073 MB, 1073741824 bytes
    Disk /dev/mapper/VolGroup-lv_swap: 1044 MB, 1044381696 bytes
    Disk /dev/mapper/VolGroup-lv_root: 9164 MB, 9164554240 bytes
    [root@localhost]~#
    

    或者使用lsblk命令也可以查看。sdb磁盘下已经分一个区出来,名为sdb1

    [root@localhost]/#
    [root@localhost]/# lsblk
    NAME                        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
    sr0                          11:0    1 420.8M  0 rom
    sda                           8:0    0    10G  0 disk
    ├─sda1                        8:1    0   500M  0 part /boot
    └─sda2                        8:2    0   9.5G  0 part
      ├─VolGroup-lv_swap (dm-0) 253:0    0   996M  0 lvm  [SWAP]
      └─VolGroup-lv_root (dm-1) 253:1    0   8.5G  0 lvm  /
    sdb                           8:16   0     1G  0 disk
    └─sdb1                        8:17   0 509.9M  0 part
    sdc                           8:32   0     1G  0 disk
    sdd                           8:48   0     1G  0 disk
    sde                           8:64   0     1G  0 disk
    [root@localhost]/#
    [root@localhost]/#
    

    至此,我们已经成功从sdb重分出sdb1分区出来。想创建更多分区,同上步骤操作即可。如果想挂载我们刚才的sdb1分区,需要先进行格式化,才能挂载,否则会报错

    [root@localhost]/# mount /dev/sdb1 /sdb1
    mount: you must specify the filesystem type
    [root@localhost]/#
    

    格式化sdb1分区

    [root@localhost]/# mkfs.ext4 /dev/sdb1
    mke2fs 1.41.12 (17-May-2010)
    文件系统标签=
    操作系统:Linux
    块大小=1024 (log=0)
    分块大小=1024 (log=0)
    Stride=0 blocks, Stripe width=0 blocks
    130560 inodes, 522080 blocks
    26104 blocks (5.00%) reserved for the super user
    第一个数据块=1
    Maximum filesystem blocks=67633152
    64 block groups
    8192 blocks per group, 8192 fragments per group
    2040 inodes per group
    Superblock backups stored on blocks:
        8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409
    
    正在写入inode表: 完成
    Creating journal (8192 blocks): 完成
    Writing superblocks and filesystem accounting information: 完成
    
    This filesystem will be automatically checked every 22 mounts or
    180 days, whichever comes first.  Use tune2fs -c or -i to override.
    [root@localhost]/#
    

    至此 可以正常挂载sdb1 分区了

    相关文章

      网友评论

          本文标题:Linux使用fdisk创建分区详解

          本文链接:https://www.haomeiwen.com/subject/kvemwftx.html