zoukankan      html  css  js  c++  java
  • 磁盘管理之管理分区

    管理分区

     

    列出块设备
    lsblk


    创建分区使用:
    fdisk 创建MBR分区
    gdisk 创建GPT分区
    parted 高级分区操作
    partprobe-重新设置内存中的内核分区表版本

     一查看分区

       1查看内存上的分区

    看到数字出现了就说明是硬盘已经分区并且生效了,生效就是保存在内存里了。

    分区表在两个地方有,磁盘和内存。下面的命令查看到的内容是来自内存。

    只有内存里面保存了内存才可以对硬盘的数据处理,而如果只是放到硬盘上就无法处理了。

    磁盘包括硬盘,是树林和树的关系。

    法1

    [root@centos65 ~]# ls  /dev/sd*
    /dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sda4  /dev/sda5  /dev/sdb  /dev/sdc  /dev/sdd
    [root@centos65 ~]# ll  /dev/sd*
    brw-rw----. 1 root disk 8,  0 Jan 23 22:50 /dev/sda
    brw-rw----. 1 root disk 8,  1 Jan 23 22:51 /dev/sda1
    brw-rw----. 1 root disk 8,  2 Jan 23 22:51 /dev/sda2
    brw-rw----. 1 root disk 8,  3 Jan 23 22:51 /dev/sda3
    brw-rw----. 1 root disk 8,  4 Jan 23 22:50 /dev/sda4
    brw-rw----. 1 root disk 8,  5 Jan 23 22:50 /dev/sda5
    brw-rw----. 1 root disk 8, 16 Jan 23 23:27 /dev/sdb
    brw-rw----. 1 root disk 8, 32 Jan 23 23:00 /dev/sdc
    brw-rw----. 1 root disk 8, 48 Jan 23 23:00 /dev/sdd

    法2:

    [root@centos65 ~]# cat  /proc/partitions 
    major minor  #blocks  name
    
       8        0  209715200 sda
       8        1    1048576 sda1
       8        2   51200000 sda2
       8        3   20480000 sda3
       8        4          1 sda4
       8        5    2097152 sda5
       8       16  104857600 sdb
       8       32   83886080 sdc
       8       48   62914560 sdd

    法3:

    [root@centos65 ~]# lsblk 
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sr0     11:0    1  3.7G  0 rom  /mnt
    sda      8:0    0  200G  0 disk 
    ├─sda1   8:1    0    1G  0 part /boot
    ├─sda2   8:2    0 48.8G  0 part /
    ├─sda3   8:3    0 19.5G  0 part /app
    ├─sda4   8:4    0    1K  0 part 
    └─sda5   8:5    0    2G  0 part [SWAP]
    sdb      8:16   0  100G  0 disk 
    sdc      8:32   0   80G  0 disk 
    sdd      8:48   0   60G  0 disk 

    注意

    上面的3种方法显示的内容都是一样,因为都是来自内存的分区表。

    对磁盘上的文件进行修改都是先读取到内存中,在内存里面修改

    当使用vim编辑器时修改文件就要w才可以保存在磁盘上。

    dd命令是直接在磁盘上把数据修改了,所以内存分区表和磁盘分区表有可能就不同步了。

       2查看磁盘上的分区

    以第1块硬盘为例:

    255 heads, 63 sectors/track, 26108 cylinders从这里就可以看出是CSH结构,也就是使用了MBR分区类型,这是默认的。

    表示255个虚拟磁头,63个扇区/磁道,26108个柱面。

    Units = cylinders of 16065 * 512 = 8225280 bytes  一个柱面的大小

    Sector size (logical/physical): 512 bytes / 512 bytes  一个扇区的字节数

    I/O size (minimum/optimal): 512 bytes / 512 bytes  每次读写的字节数

    Device表示硬盘上面的分区,可以看到有5个分区。

    *表示此分区为启动分区,Start 表示开始的柱面, End表示结束的柱面

     Blocks表示块数量, Id表示分区类型ID,System表示分区类型

    [root@centos65 ~]# fdisk  -l
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes
    255 heads, 63 sectors/track, 26108 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: 0x0000c75a
    
       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        6505    51200000   83  Linux
    /dev/sda3            6505        9055    20480000   83  Linux
    /dev/sda4            9055       26109   136985600    5  Extended
    /dev/sda5            9055        9316     2097152   82  Linux swap / Solaris
    
    Disk /dev/sdb: 107.4 GB, 107374182400 bytes
    255 heads, 63 sectors/track, 13054 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
    
    
    Disk /dev/sdc: 85.9 GB, 85899345920 bytes
    255 heads, 63 sectors/track, 10443 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
    
    
    Disk /dev/sdd: 64.4 GB, 64424509440 bytes
    255 heads, 63 sectors/track, 7832 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

    这是克隆了sda的分区表,512字节无法克隆数据。

    硬盘没有分区就没有分区类型

     

    二分区工具parted的使用

    parted是一个磁盘分区和分区大小调整程序。

    它允许你创建,销毁,调整大小,移动和复制ext2、linux-swap、FAT、FAT32和reiserfs分区。

    它可以创建,调整大小,并移动Macintosh HFS parti-以及检测jfs、ntfs、ufs和xfs分区。它有助于为新操作创建空间系统,重新组织磁盘使用,并将数据复制到新的硬盘。

    注意parted的操作都是实时生效的,小心使用


    用法:parted [选项]... [设备 [命令 [参数]...]...]


    parted /dev/sdb mklabel gpt|msdos

    parted /dev/sdb  print

    parted /dev/sdb mkpart primary 1 200 (默认M)
    parted /dev/sdb rm 1
    parted -l

       1显示帮助

    注意是交互式的,输入help显示了帮助内容

    [root@centos65 ~]# parted   /dev/sdb
    GNU Parted 2.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
      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

       2创建分区表,定义分区类型

    mklabel,mktable LABEL-TYPE create a new disklabel (partition table)

    parted      /dev/sdb   mklabel   gpt|msdos

    msdos就是MBR分区。

    [root@centos65 ~]# parted  /dev/sdc    mklabel    msdos
    Information: You may need to update /etc/fstab.                           
    
    [root@centos65 ~]# parted  /dev/sdc   print
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdc: 85.9GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    
    Number  Start  End  Size  Type  File system  Flags
    
    

    注意

    [root@centos65 ~]# fdisk  /dev/sdc
    
    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').

    警告:不推荐dos兼容模式。强烈推荐

    关闭模式(命令“c”)并将显示单元更改为扇区(命令“u”)。

    Command (m for help): p
    
    Disk /dev/sdc: 85.9 GB, 85899345920 bytes
    255 heads, 63 sectors/track, 10443 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: 0x0009eebb
    
       Device Boot      Start         End      Blocks   Id  System

    在sdb硬盘上定义GPT分区类型

    注意GPT分区和MBR是不能兼容的,也不能转换。

    [root@centos65 ~]# parted    mklabel    /dev/sdb   gpt
    Error: Could not stat device mklabel - No such file or directory.         
    Retry/Cancel? c                                                           
    [root@centos65 ~]# parted     /dev/sdb    mklabel     gpt
    Information: You may need to update /etc/fstab.                           
    
    [root@centos65 ~]# parted  /dev/sdb   print
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 107GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    
    Number  Start  End  Size  File system  Name  Flags
    [root@centos65 ~]# lsblk 
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sr0     11:0    1  3.7G  0 rom  
    sda      8:0    0  200G  0 disk 
    ├─sda1   8:1    0    1G  0 part /boot
    ├─sda2   8:2    0 48.8G  0 part /
    ├─sda3   8:3    0 19.5G  0 part /app
    ├─sda4   8:4    0    1K  0 part 
    └─sda5   8:5    0    2G  0 part [SWAP]
    sdb      8:16   0  100G  0 disk 
    sdc      8:32   0   80G  0 disk 
    sdd      8:48   0   60G  0 disk 

     出现警告,因为fdisk只支持对小于2TB的磁盘进行分区

    [root@centos65 ~]# fdisk  /dev/sdb
    
    WARNING: GPT (GUID Partition Table) detected on '/dev/sdb'! The util fdisk doesn't support GPT. Use GNU Parted.
    警告:在'/dev/sdb'上检测到GPT (GUID分区表)!util fdisk不支持GPT。使用GNU Parted。
    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): p Disk /dev/sdb: 107.4 GB, 107374182400 bytes 255 heads, 63 sectors/track, 13054 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 13055 104857599+ ee GPT

    在sdc上就没有警报,因为使用MBR分区

    [root@centos65 ~]# fdisk   /dev/sdc  -l
    
    Disk /dev/sdc: 85.9 GB, 85899345920 bytes
    255 heads, 63 sectors/track, 10443 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: 0x00088833
    
       Device Boot      Start         End      Blocks   Id  System

    在sdb上创建分区,分区大小为1000M

    在前面已经指定为GPT分区类型了

                        
    [root@centos65 ~]# parted    /dev/sdb      mkpart     primary    1  1000
    Warning: You requested a partition from 1000kB to 1000MB.                 
    The closest location we can manage is 1000MB to 1000MB.
    我们能管理的最近的位置是1000MB到1000MB。 Is this still acceptable to you? Yes/No? yes Warning: The resulting partition is not properly aligned for best performance. Ignore/Cancel? ^C Information: You may need to update /etc/fstab.

    创建了分区之后就同步到内存的分区表里面了

    [root@centos65 ~]# parted    /dev/sdb      mkpart     primary    1  1000
    Information: You may need to update /etc/fstab.                           
    
    [root@centos65 ~]# parted    /dev/sdb    print
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 107GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    
    Number  Start   End     Size   File system  Name     Flags
     1      1049kB  1000MB  999MB               primary
    
    [root@centos65 ~]# lsblk 
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sr0     11:0    1  3.7G  0 rom  /mnt
    sda      8:0    0  200G  0 disk 
    ├─sda1   8:1    0    1G  0 part /boot
    ├─sda2   8:2    0 48.8G  0 part /
    ├─sda3   8:3    0 19.5G  0 part /app
    ├─sda4   8:4    0    1K  0 part 
    └─sda5   8:5    0    2G  0 part [SWAP]
    sdb      8:16   0  100G  0 disk 
    └─sdb1   8:17   0  953M  0 part 
    sdc      8:32   0   80G  0 disk 
    sdd      8:48   0   60G  0 disk 

    创建的不合适,可以删除

    [root@centos65 ~]#  parted    /dev/sdb      print
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 107GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    
    Number  Start   End     Size   File system  Name     Flags
     1      1049kB  1000MB  999MB               primary

    rm   partition——Delete partition.
    parted   /dev/sdb rm 1 其中1代表分区的序号

    [root@centos65 ~]# parted    /dev/sdb    rm  1
    Information: You may need to update /etc/fstab.                           
                         
    [root@centos65 ~]# parted    /dev/sdb    print
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 107GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    
    Number  Start  End  Size  File system  Name  Flags

     注意如果要使用fdisk查看或者创建MBR分区,在使用parted删除了分区之后要重新指定类型

    [root@centos65 ~]# parted /dev/sdb    rm  1
    Information: You may need to update /etc/fstab.                           
    
    [root@centos65 ~]# parted /dev/sdb   print
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 107GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    
    Number  Start  End  Size  File system  Name  Flags
    
    [root@centos65 ~]# fdisk  /dev/sdb
    
    WARNING: GPT (GUID Partition Table) detected on '/dev/sdb'! The util fdisk doesn't support GPT. Use GNU Parted.
    
    
    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): ^C
    [root@centos65 ~]# parted /dev/sdb 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                                                                 
    Information: You may need to update /etc/fstab.                           
    
    [root@centos65 ~]# fdisk  /dev/sdb
    
    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): p
    
    Disk /dev/sdb: 107.4 GB, 107374182400 bytes
    255 heads, 63 sectors/track, 13054 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: 0x000c381a
    
       Device Boot      Start         End      Blocks   Id  System

     内存也把分区删除了

    [root@centos65 ~]# lsblk 
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sr0     11:0    1  3.7G  0 rom  
    sda      8:0    0  200G  0 disk 
    ├─sda1   8:1    0    1G  0 part /boot
    ├─sda2   8:2    0 48.8G  0 part /
    ├─sda3   8:3    0 19.5G  0 part /app
    ├─sda4   8:4    0    1K  0 part 
    └─sda5   8:5    0    2G  0 part [SWAP]
    sdb      8:16   0  100G  0 disk 
    sdc      8:32   0   80G  0 disk 
    sdd      8:48   0   60G  0 disk 

    三分区工具gdisk的使用

    类fdisk 的GPT分区工具

    使用gdisk对分区进行管理

       1显示帮助

    和parted一样是交互式的

     报警,是因为刚才设置成了MBR分区类型

    [root@centos65 ~]# gdisk  /dev/sdb
    GPT fdisk (gdisk) version 0.8.10
    
    Partition table scan:
      MBR: MBR only
      BSD: not present
      APM: not present
      GPT: not present
    
    
    ***************************************************************
    Found invalid GPT and valid MBR; converting MBR to GPT format
    in memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by
    typing 'q' if you don't want to convert your MBR partitions
    to GPT format!
    ***************************************************************
    

    发现无效的GPT和有效的MBR;将MBR转换为GPT格式

    在内存中。这个操作具有潜在的破坏性!

    如果不希望转换MBR分区,请键入“q”GPT格式!



    Command (? for help): ? b back up GPT data to a file c change a partition's name d delete a partition i show detailed information on a partition l list known partition types n add a new partition o create a new empty GUID partition table (GPT) p print the partition table q quit without saving changes r recovery and transformation options (experts only) s sort partitions t change a partition's type code v verify disk w write table to disk and exit x extra functionality (experts only) ? print this menu

       2打印分区表

    p print the partition table

    现在没有分区

    Command (? for help): p
    Disk /dev/sdb: 209715200 sectors, 100.0 GiB
    Logical sector size: 512 bytes
    Disk identifier (GUID): CCCA39F5-8CD6-4F25-B42E-277BF4956DF3
    Partition table holds up to 128 entries
    First usable sector is 34, last usable sector is 209715166
    Partitions will be aligned on 2048-sector boundaries
    Total free space is 209715133 sectors (100.0 GiB)
    
    Number  Start (sector)    End (sector)  Size       Code  Name

    使用parted创建分区,分区类型为GPT

    [root@centos65 ~]# parted /dev/sdb mklabel   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? y                                                                 
    Information: You may need to update /etc/fstab.                           
    
    [root@centos65 ~]# gdisk  /dev/sdb
    GPT fdisk (gdisk) version 0.8.10
    
    Partition table scan:
      MBR: protective
      BSD: not present
      APM: not present
      GPT: present
    
    Found valid GPT with protective MBR; using GPT.
    [root@centos65 ~]# parted   /dev/sdb    mkpart    primary     1  1000
    Information: You may need to update /etc/fstab.                           
    
    [root@centos65 ~]# parted   /dev/sdb    print
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 107GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    
    Number  Start   End     Size   File system  Name     Flags
     1      1049kB  1000MB  999MB               primary
                            
    
    [root@centos65 ~]# gdisk  /dev/sdb
    GPT fdisk (gdisk) version 0.8.10
    
    Partition table scan:
      MBR: protective
      BSD: not present
      APM: not present
      GPT: present
    
    Found valid GPT with protective MBR; using GPT.
    
    Command (? for help): p
    Disk /dev/sdb: 209715200 sectors, 100.0 GiB
    Logical sector size: 512 bytes
    Disk identifier (GUID): 957106F8-950F-49F1-BE76-4E637A2A3567
    Partition table holds up to 128 entries
    First usable sector is 34, last usable sector is 209715166
    Partitions will be aligned on 2048-sector boundaries
    Total free space is 207763389 sectors (99.1 GiB)
    
    Number  Start (sector)    End (sector)  Size       Code  Name
       1            2048         1953791   953.0 MiB   0700  primary

    sda是MBR类型的也会显示,但是会警告

    [root@centos65 ~]# gdisk  /dev/sda
    GPT fdisk (gdisk) version 0.8.10
    
    Partition table scan:
      MBR: MBR only
      BSD: not present
      APM: not present
      GPT: not present
    
    
    ***************************************************************
    Found invalid GPT and valid MBR; converting MBR to GPT format
    in memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by
    typing 'q' if you don't want to convert your MBR partitions
    to GPT format!
    ***************************************************************
    
    Command (? for help): p
    Disk /dev/sda: 419430400 sectors, 200.0 GiB
    Logical sector size: 512 bytes
    Disk identifier (GUID): E742544D-FD2C-467B-808B-1F34B879B5BC
    Partition table holds up to 128 entries
    First usable sector is 34, last usable sector is 419430366
    Partitions will be aligned on 2048-sector boundaries
    Total free space is 269778877 sectors (128.6 GiB)
    
    Number  Start (sector)    End (sector)  Size       Code  Name
       1            2048         2099199   1024.0 MiB  8300  Linux filesystem
       2         2099200       104499199   48.8 GiB    8300  Linux filesystem
       3       104499200       145459199   19.5 GiB    8300  Linux filesystem
       5       145463296       149657599   2.0 GiB     8200  Linux swap
    [root@centos65 ~]# fdisk  /dev/sda
    
    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): p
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes
    255 heads, 63 sectors/track, 26108 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: 0x0000c75a
    
       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        6505    51200000   83  Linux
    /dev/sda3            6505        9055    20480000   83  Linux
    /dev/sda4            9055       26109   136985600    5  Extended
    /dev/sda5            9055        9316     2097152   82  Linux swap / Solaris

       3使用gdisk创建分区

    在sdb上创建新分区

    n   add a new partition

    Command (? for help): n
    Partition number (2-128, default 2): 3
    First sector (34-209715166, default = 1953792) or {+-}size{KMGTP}: 

    会显示默认要创建的分区,default 2

    比如已经创建了第1个分区,那么就会显示默认创建第2个分区,也可以自定义。

    First sector (34-209715166, default = 1953792) or {+-}size{KMGTP}:  这个表示开始的扇区

    显示扇区的位置,和前面的分区有关,默认是从前1个分区的下1个扇划分 default = 1953792。

    创建分区的开始扇区也可以使用括号里面的扇区,从34开始,但是最多只能到2047,因为第1个分区的扇区是从2048开始的。

    Command (? for help): p
    Disk /dev/sdb: 209715200 sectors, 100.0 GiB
    Logical sector size: 512 bytes
    Disk identifier (GUID): B9D45D7D-405D-4EC1-BE1D-DC9CA5406FD0
    Partition table holds up to 128 entries
    First usable sector is 34, last usable sector is 209715166
    Partitions will be aligned on 2048-sector boundaries
    Total free space is 207763389 sectors (99.1 GiB)
    
    Number  Start (sector)    End (sector)  Size       Code  Name
       1            2048         1953791   953.0 MiB   0700  primary

    使用默认的直接回车即可,接着就显示结束扇区

    Command (? for help): n
    Partition number (2-128, default 2): 3
    First sector (34-209715166, default = 1953792) or {+-}size{KMGTP}: 
    Last sector (1953792-209715166, default = 209715166) or {+-}size{KMGTP}: 

    209715166表示结束扇区的最大范围,也就把所有的空间都分光了。

    Last sector (1953792-209715166, default = 209715166) or {+-}size{KMGTP}:

    这个最好不要选择默认,因为默认是以扇区为单位的,所以直接指定分区的空间大小会更方便,不过要写上数据存储单位。

    +1G表示的是从前一个分区的最后一个扇区开始增加1G大小的分区。

    -表示往前面的扇区划分,因为被其他分区占用分不了,+表示往后面的扇区划分。

    下面显示的是标签,提醒一下是做什么的。

    回车就是选择默认

    Command (? for help): n
    Partition number (2-128, default 2): 3
    First sector (34-209715166, default = 1953792) or {+-}size{KMGTP}: 
    Last sector (1953792-209715166, default = 209715166) or {+-}size{KMGTP}: +G
    Last sector (1953792-209715166, default = 209715166) or {+-}size{KMGTP}: +1G        
    Current type is 'Linux filesystem'
    Hex code or GUID (L to show codes, Enter = 8300): L
    0700 Microsoft basic data  0c01 Microsoft reserved    2700 Windows RE          
    3000 ONIE boot             3001 ONIE config           4100 PowerPC PReP boot   
    4200 Windows LDM data      4201 Windows LDM metadata  7501 IBM GPFS            
    7f00 ChromeOS kernel       7f01 ChromeOS root         7f02 ChromeOS reserved   
    8200 Linux swap            8300 Linux filesystem      8301 Linux reserved      
    8302 Linux /home           8400 Intel Rapid Start     8e00 Linux LVM           
    a500 FreeBSD disklabel     a501 FreeBSD boot          a502 FreeBSD swap        
    a503 FreeBSD UFS           a504 FreeBSD ZFS           a505 FreeBSD Vinum/RAID  
    a580 Midnight BSD data     a581 Midnight BSD boot     a582 Midnight BSD swap   
    a583 Midnight BSD UFS      a584 Midnight BSD ZFS      a585 Midnight BSD Vinum  
    a800 Apple UFS             a901 NetBSD swap           a902 NetBSD FFS          
    a903 NetBSD LFS            a904 NetBSD concatenated   a905 NetBSD encrypted    
    a906 NetBSD RAID           ab00 Apple boot            af00 Apple HFS/HFS+      
    af01 Apple RAID            af02 Apple RAID offline    af03 Apple label         
    af04 AppleTV recovery      af05 Apple Core Storage    be00 Solaris boot        
    bf00 Solaris root          bf01 Solaris /usr & Mac Z  bf02 Solaris swap        
    bf03 Solaris backup        bf04 Solaris /var          bf05 Solaris /home       
    bf06 Solaris alternate se  bf07 Solaris Reserved 1    bf08 Solaris Reserved 2  
    bf09 Solaris Reserved 3    bf0a Solaris Reserved 4    bf0b Solaris Reserved 5  
    c001 HP-UX data            c002 HP-UX service         ea00 Freedesktop $BOOT   
    eb00 Haiku BFS             ed00 Sony system partitio  ed01 Lenovo system partit
    Press the <Enter> key to see more codes: 
    ef00 EFI System            ef01 MBR partition scheme  ef02 BIOS boot partition 
    fb00 VMWare VMFS           fb01 VMWare reserved       fc00 VMWare kcore crash p
    fd00 Linux RAID            
    Hex code or GUID (L to show codes, Enter = 8300): 
    Changed type of partition to 'Linux filesystem'
    
    Command (? for help): p
    Disk /dev/sdb: 209715200 sectors, 100.0 GiB
    Logical sector size: 512 bytes
    Disk identifier (GUID): B9D45D7D-405D-4EC1-BE1D-DC9CA5406FD0
    Partition table holds up to 128 entries
    First usable sector is 34, last usable sector is 209715166
    Partitions will be aligned on 2048-sector boundaries
    Total free space is 205666237 sectors (98.1 GiB)
    
    Number  Start (sector)    End (sector)  Size       Code  Name
       1            2048         1953791   953.0 MiB   0700  primary
       3         1953792         4050943   1024.0 MiB  8300  Linux filesystem

       4存盘退出

    注意不论是删除和增加都要存盘才会使结果生效

    Command (? for help): w
    
    Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
    PARTITIONS!!
    
    Do you want to proceed? (Y/N): y
    OK; writing new GUID partition table (GPT) to /dev/sdb.
    The operation has completed successfully.
    [root@centos65 ~]# lsblk 
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sr0     11:0    1  3.7G  0 rom  
    sda      8:0    0  200G  0 disk 
    ├─sda1   8:1    0    1G  0 part /boot
    ├─sda2   8:2    0 48.8G  0 part /
    ├─sda3   8:3    0 19.5G  0 part /app
    ├─sda4   8:4    0    1K  0 part 
    └─sda5   8:5    0    2G  0 part [SWAP]
    sdb      8:16   0  100G  0 disk 
    ├─sdb1   8:17   0  953M  0 part 
    └─sdb3   8:19   0    1G  0 part 
    sdc      8:32   0   80G  0 disk 
    sdd      8:48   0   60G  0 disk 

       5删除分区

    Command (? for help): d
    Partition number (1-3): 3
    
    Command (? for help): p             
    Disk /dev/sdb: 209715200 sectors, 100.0 GiB
    Logical sector size: 512 bytes
    Disk identifier (GUID): B9D45D7D-405D-4EC1-BE1D-DC9CA5406FD0
    Partition table holds up to 128 entries
    First usable sector is 34, last usable sector is 209715166
    Partitions will be aligned on 2048-sector boundaries
    Total free space is 207763389 sectors (99.1 GiB)
    
    Number  Start (sector)    End (sector)  Size       Code  Name
       1            2048         1953791   953.0 MiB   0700  primary

       6查看高级选项

    高级选项

    Command (? for help): x
    
    Expert command (? for help): ?
    a    set attributes
    c    change partition GUID
    d    display the sector alignment value
    e    relocate backup data structures to the end of the disk
    g    change disk GUID
    h    recompute CHS values in protective/hybrid MBR
    i    show detailed information on a partition
    l    set the sector alignment value
    m    return to main menu
    n    create a new protective MBR
    o    print protective MBR data
    p    print the partition table
    q    quit without saving changes
    r    recovery and transformation options (experts only)
    s    resize partition table
    t    transpose two partition table entries
    u    replicate partition table on new device
    v    verify disk
    w    write table to disk and exit
    z    zap (destroy) GPT data structures and exit
    ?    print this menu

    选择存盘退出

    Command (? for help): p
    Disk /dev/sdb: 209715200 sectors, 100.0 GiB
    Logical sector size: 512 bytes
    Disk identifier (GUID): 957106F8-950F-49F1-BE76-4E637A2A3567
    Partition table holds up to 128 entries
    First usable sector is 34, last usable sector is 209715166
    Partitions will be aligned on 2048-sector boundaries
    Total free space is 205666237 sectors (98.1 GiB)
    
    Number  Start (sector)    End (sector)  Size       Code  Name
       1            2048         1953791   953.0 MiB   0700  primary
       3         1953792         4050943   1024.0 MiB  8300  Linux filesystem
    
    Command (? for help): d
    Partition number (1-3): 3
    
    Command (? for help): w
    
    Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
    PARTITIONS!!
    
    Do you want to proceed? (Y/N): y
    OK; writing new GUID partition table (GPT) to /dev/sdb.
    The operation has completed successfully.
    [root@centos65 ~]# lsblk 
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sr0     11:0    1  3.7G  0 rom  
    sda      8:0    0  200G  0 disk 
    ├─sda1   8:1    0    1G  0 part /boot
    ├─sda2   8:2    0 48.8G  0 part /
    ├─sda3   8:3    0 19.5G  0 part /app
    ├─sda4   8:4    0    1K  0 part 
    └─sda5   8:5    0    2G  0 part [SWAP]
    sdb      8:16   0  100G  0 disk 
    └─sdb1   8:17   0  953M  0 part 
    sdc      8:32   0   80G  0 disk 
    sdd      8:48   0   60G  0 disk 
    [root@centos65 ~]# parted    /dev/sdb
    GNU Parted 2.1
    Using /dev/sdb
    Welcome to GNU Parted! Type 'help' to view a list of commands.
    (parted) p                                                                
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 107GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    
    Number  Start   End     Size   File system  Name     Flags
     1      1049kB  1000MB  999MB               primary

    四分区工具fdisk的使用

    fdisk的子命令,和sed一样,也是有子命令的

       1查看帮助

    显示报警是因为之前使用的是GPT分区类型

    [root@centos65 ~]# fdisk   /dev/sdb
    
    WARNING: GPT (GUID Partition Table) detected on '/dev/sdb'! The util fdisk doesn't support GPT. Use GNU Parted.
    
    
    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)

    修改分区表类型,修改成fdisk支持的MBR

    即使把分区删除了,分区结构或者分区表类型还是之前的GPT或者MBR。

    要想修改原来的分区表类型可以使用mklabel,这样之前的分区类型就会被替换了。

    [root@centos65 ~]#  parted    /dev/sdb   print
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 107GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    
    Number  Start   End     Size   File system  Name     Flags
     1      1049kB  1000MB  999MB               primary
    
    [root@centos65 ~]# parted   /dev/sdb   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                                                                 
    Information: You may need to update /etc/fstab.                           
    
    [root@centos65 ~]#  parted    /dev/sdb   print
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 107GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    
    Number  Start  End  Size  Type  File system  Flags

    现在转换成了MBR,原来的GPT类型也没有了

    [root@centos65 ~]# fdisk  /dev/sdb
    
    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): p
    
    Disk /dev/sdb: 107.4 GB, 107374182400 bytes
    255 heads, 63 sectors/track, 13054 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: 0x0006ff87
    
       Device Boot      Start         End      Blocks   Id  System
    [root@centos65 ~]# fdisk  /dev/sdb  -l
    
    Disk /dev/sdb: 107.4 GB, 107374182400 bytes
    255 heads, 63 sectors/track, 13054 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: 0x000d8fb4
    
       Device Boot      Start         End      Blocks   Id  System

       2查看分区

    [root@centos65 ~]# fdisk   /dev/sdb
    
    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): p
    
    Disk /dev/sdb: 107.4 GB, 107374182400 bytes
    255 heads, 63 sectors/track, 13054 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: 0x000d8fb4
    
       Device Boot      Start         End      Blocks   Id  System

    DOS-compatible mode表示的是以柱面为单位划分分区,可以修改成以扇区为单位

    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').

       3关闭dos兼容模式

    下面显示的是以柱面为单位的。

    不在柱面的边界会出现报警Partition 1 does not end on cylinder boundary.

    [root@centos65 ~]# fdisk   /dev/sda
    
    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): p
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes
    255 heads, 63 sectors/track, 26108 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: 0x0000c75a
    
       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        6505    51200000   83  Linux
    /dev/sda3            6505        9055    20480000   83  Linux
    /dev/sda4            9055       26109   136985600    5  Extended
    /dev/sda5            9055        9316     2097152   82  Linux swap / Solaris

    如果不想看到报警信息加上c,也就关闭了dos兼容模式

    [root@centos65 ~]# fdisk   /dev/sda  -c
    
    WARNING: cylinders as display units are deprecated. Use command 'u' to
             change units to sectors.
    
    Command (m for help): p
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes
    255 heads, 63 sectors/track, 26108 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: 0x0000c75a
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *           1         131     1048576   83  Linux
    /dev/sda2             131        6505    51200000   83  Linux
    /dev/sda3            6505        9055    20480000   83  Linux
    /dev/sda4            9055       26109   136985600    5  Extended
    /dev/sda5            9055        9316     2097152   82  Linux swap / Solaris
    [root@centos65 ~]# fdisk  /dev/sdb  -c
    
    WARNING: cylinders as display units are deprecated. Use command 'u' to
             change units to sectors.
    
    Command (m for help): p
    
    Disk /dev/sdb: 107.4 GB, 107374182400 bytes
    255 heads, 63 sectors/track, 13054 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: 0x0006ff87
    
       Device Boot      Start         End      Blocks   Id  System

       4在6上修改分区单位

    修改成了扇区单位

    不想看到报警信息加上c,关闭了dos兼容模式,而且以扇区为单位划分的就加上u,两个选项可以合并写

    Blocks是以k为单位

    [root@centos65 ~]# fdisk   /dev/sda  -cu
    
    Command (m for help): p
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes
    255 heads, 63 sectors/track, 26108 cylinders, total 419430400 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 identifier: 0x0000c75a
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *        2048     2099199     1048576   83  Linux
    /dev/sda2         2099200   104499199    51200000   83  Linux
    /dev/sda3       104499200   145459199    20480000   83  Linux
    /dev/sda4       145459200   419430399   136985600    5  Extended
    /dev/sda5       145463296   149657599     2097152   82  Linux swap / Solaris

    通过柱面的数量计算出分区的大小,比如sda1的大小131柱面x8M就是1G

    [root@centos65 ~]# fdisk   /dev/sda  
    
    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): p
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes
    255 heads, 63 sectors/track, 26108 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: 0x0000c75a
    
       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        6505    51200000   83  Linux
    /dev/sda3            6505        9055    20480000   83  Linux
    /dev/sda4            9055       26109   136985600    5  Extended
    /dev/sda5            9055        9316     2097152   82  Linux swap / Solaris
    [root@centos65 ~]# lsblk 
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sr0     11:0    1  3.7G  0 rom  /mnt
    sda      8:0    0  200G  0 disk 
    ├─sda1   8:1    0    1G  0 part /boot
    ├─sda2   8:2    0 48.8G  0 part /
    ├─sda3   8:3    0 19.5G  0 part /app
    ├─sda4   8:4    0    1K  0 part 
    └─sda5   8:5    0    2G  0 part [SWAP]
    sdb      8:16   0  100G  0 disk 
    sdc      8:32   0   80G  0 disk 
    sdd      8:48   0   60G  0 disk 

       5在7版本修改分区单位

    划分分区是以柱面还是扇区看个人习惯,扇区是最小单位,感觉细了些。

    在7里面fdisk默认就是以扇区的方式

    在虚拟机72上添加硬盘

    [root@centos72 ~]# lsblk 
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda      8:0    0  200G  0 disk 
    ├─sda1   8:1    0    1G  0 part /boot
    ├─sda2   8:2    0   50G  0 part /
    ├─sda3   8:3    0   20G  0 part /app
    ├─sda4   8:4    0    1K  0 part 
    └─sda5   8:5    0    2G  0 part [SWAP]
    sr0     11:0    1  4.2G  0 rom  /mnt

     

     如果硬盘没有显示就只能重启服务了

    [root@centos72 ~]# echo   '- - -'   >   /sys/class/scsi_host/host2/scan 
    [root@centos72 ~]# lsblk 
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda      8:0    0  200G  0 disk 
    ├─sda1   8:1    0    1G  0 part /boot
    ├─sda2   8:2    0   50G  0 part /
    ├─sda3   8:3    0   20G  0 part /app
    ├─sda4   8:4    0    1K  0 part 
    └─sda5   8:5    0    2G  0 part [SWAP]
    sr0     11:0    1  4.2G  0 rom  /mnt
    [root@centos72 ~]# 
    Connection closed by foreign host.
    
    Disconnected from remote host(192.168.137.72:22) at 17:24:51.
    
    Type `help' to learn how to use Xshell prompt.
    [c:~]$ ssh   192.168.137.72
    
    
    Connecting to 192.168.137.72:22...
    Connection established.
    To escape to local shell, press Ctrl+Alt+].
    
    WARNING! The remote SSH server rejected X11 forwarding request.
    Last login: Thu Feb 21 17:22:43 2019 from 192.168.137.34
    [root@centos72 ~]# lsblk 
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda      8:0    0  200G  0 disk 
    ├─sda1   8:1    0    1G  0 part /boot
    ├─sda2   8:2    0   50G  0 part /
    ├─sda3   8:3    0   20G  0 part /app
    ├─sda4   8:4    0    1K  0 part 
    └─sda5   8:5    0    2G  0 part [SWAP]
    sdb      8:16   0  100G  0 disk 
    sr0     11:0    1  4.2G  0 rom  /mnt

    在7上只显示扇区,相当于默认就加上了cu选项

    [root@centos72 ~]# 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.
    
    Device does not contain a recognized partition table
    Building a new DOS disklabel with disk identifier 0xc4a044a6.
    
    Command (m for help): p
    
    Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0xc4a044a6
    
       Device Boot      Start         End      Blocks   Id  System

    在6上不仅显示扇区还显示柱面,磁头

    [root@centos65 ~]# fdisk   /dev/sda  
    
    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): p
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes
    255 heads, 63 sectors/track, 26108 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: 0x0000c75a
    
       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        6505    51200000   83  Linux
    /dev/sda3            6505        9055    20480000   83  Linux
    /dev/sda4            9055       26109   136985600    5  Extended
    /dev/sda5            9055        9316     2097152   82  Linux swap / Solaris

    可以修改成以柱面为单位

    [root@centos72 ~]# fdisk   --help
    fdisk: invalid option -- '-'
    Usage:
     fdisk [options] <disk>    change partition table
     fdisk [options] -l <disk> list partition table(s)
     fdisk -s <partition>      give partition size(s) in blocks
    
    Options:
     -b <size>             sector size (512, 1024, 2048 or 4096)
     -c[=<mode>]           compatible mode: 'dos' or 'nondos' (default)
     -h                    print this help text
     -u[=<unit>]           display units: 'cylinders' or 'sectors' (default)
     -v                    print program version
     -C <number>           specify the number of cylinders
     -H <number>           specify the number of heads
     -S <number>           specify the number of sectors per track

    现在转换成了以柱面为单位了

    [root@centos72 ~]# fdisk   /dev/sda    -u=cylinders
    
    WARNING: cylinders as display units are deprecated. Use command 'u' to
             change units to sectors.
    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): p
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors
    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 label type: dos
    Disk identifier: 0x000c7076
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *           1         131     1048576   83  Linux
    /dev/sda2             131        6658    52428800   83  Linux
    /dev/sda3            6658        9269    20971520   83  Linux
    /dev/sda4            9269       26109   135265280    5  Extended
    /dev/sda5            9269        9530     2097152   82  Linux swap / S

    注意选项只要不是加在命令前面就有效

    [root@centos72 ~]# fdisk       -u=cylinders   /dev/sda
    
    WARNING: cylinders as display units are deprecated. Use command 'u' to
             change units to sectors.
    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): p
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors
    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 label type: dos
    Disk identifier: 0x000c7076
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *           1         131     1048576   83  Linux
    /dev/sda2             131        6658    52428800   83  Linux
    /dev/sda3            6658        9269    20971520   83  Linux
    /dev/sda4            9269       26109   135265280    5  Extended
    /dev/sda5            9269        9530     2097152   82  Linux swap / Solaris

    和6是一样的

    [root@centos65 ~]# fdisk   /dev/sda  -c
    
    WARNING: cylinders as display units are deprecated. Use command 'u' to
             change units to sectors.
    
    Command (m for help): p
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes
    255 heads, 63 sectors/track, 26108 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: 0x0000c75a
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *           1         131     1048576   83  Linux
    /dev/sda2             131        6505    51200000   83  Linux
    /dev/sda3            6505        9055    20480000   83  Linux
    /dev/sda4            9055       26109   136985600    5  Extended
    /dev/sda5            9055        9316     2097152   82  Linux swap / Solaris

       6在6版本的硬盘上创建分区

    在6版本的sda硬盘上创建分区

    [root@centos65 ~]# fdisk   /dev/sda  
    
    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): p
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes
    255 heads, 63 sectors/track, 26108 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: 0x0000c75a
    
       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        6505    51200000   83  Linux
    /dev/sda3            6505        9055    20480000   83  Linux
    /dev/sda4            9055       26109   136985600    5  Extended
    /dev/sda5            9055        9316     2097152   82  Linux swap / Solaris
    
    Command (m for help): n
    First cylinder (9316-26109, default 9316): 

    9316属于扩展分区的一部分,扩展分区的范围是9055-26109,所以9316之后的内容是在扩展分区里面。

    注意4个分区已经分出来了,最后一个是扩展分区。要创建新分区只能在这4个分区里面分。

    可以跳过9316,从1000开始,那么9316-10000之间的柱面是闲置的。

    接着是柱面的结束位置,默认到最后,如果觉得空间大了,可以指定大小,可以以柱面为单位,直接写上20000,就表示分区是从10000到20000.

    因为一个柱面是8M,10000个柱面就是80000M,也就是80G。

    更方便的写法就是数字+存储单位。+size{K,M,G}

    设置分区是1G,那么直接写上+1G即可

    [root@centos65 ~]# fdisk   /dev/sda  
    
    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): p
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes
    255 heads, 63 sectors/track, 26108 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: 0x0000c75a
    
       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        6505    51200000   83  Linux
    /dev/sda3            6505        9055    20480000   83  Linux
    /dev/sda4            9055       26109   136985600    5  Extended
    /dev/sda5            9055        9316     2097152   82  Linux swap / Solaris
    
    Command (m for help): n
    First cylinder (9316-26109, default 9316): 10000
    Last cylinder, +cylinders or +size{K,M,G} (10000-26109, default 26109): +1G
    
    Command (m for help): p       
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes
    255 heads, 63 sectors/track, 26108 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: 0x0000c75a
    
       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        6505    51200000   83  Linux
    /dev/sda3            6505        9055    20480000   83  Linux
    /dev/sda4            9055       26109   136985600    5  Extended
    /dev/sda5            9055        9316     2097152   82  Linux swap / Solaris
    /dev/sda6           10000       10131     1060290   83  Linux
    划分了131个柱面

    为什么在上面划分分区时不会询问选择什么样的分区类型,是主分区还是扩展分区逻辑分区呢?

    因为4个分区已经分完了,所以逼上梁山没有选择只能分逻辑分区。

    继续在硬盘上划分分区

    选择默认的9316

    (9316-9999, default 9999)表示结束的柱面是9999,分区大小就是683个柱面的大小,也就是5464M,差不多5G

    Command (m for help): p       
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes
    255 heads, 63 sectors/track, 26108 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: 0x0000c75a
    
       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        6505    51200000   83  Linux
    /dev/sda3            6505        9055    20480000   83  Linux
    /dev/sda4            9055       26109   136985600    5  Extended
    /dev/sda5            9055        9316     2097152   82  Linux swap / Solaris
    /dev/sda6           10000       10131     1060290   83  Linux
    
    Command (m for help): n
    First cylinder (9316-26109, default 9316): 
    Using default value 9316
    Last cylinder, +cylinders or +size{K,M,G} (9316-9999, default 9999): +10G
    Value out of range.因为分区最大空间是5G左右。
    Last cylinder, +cylinders or +size{K,M,G} (9316-9999, default 9999): 

    接受默认的,那么就只能划分出5G左右的分区了。对此不满就把此分区删除重新创建

    注意夹缝问题,也就是上面出现空间不足的问题。起始值和结束值要好好观察,三思而后行。

    分区一旦分好了就不能改变大小了,除非删除重新创建。

    Command (m for help): p
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes
    255 heads, 63 sectors/track, 26108 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: 0x0000c75a
    
       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        6505    51200000   83  Linux
    /dev/sda3            6505        9055    20480000   83  Linux
    /dev/sda4            9055       26109   136985600    5  Extended
    /dev/sda5            9055        9316     2097152   82  Linux swap / Solaris
    /dev/sda6           10000       10131     1060290   83  Linux
    /dev/sda7            9316        9999     5488104+  83  Linux
    
    Partition table entries are not in disk order
    
    Command (m for help): d
    Partition number (1-7): 7
    
    Command (m for help): n
    First cylinder (9316-26109, default 9316): 10132
    Last cylinder, +cylinders or +size{K,M,G} (10132-26109, default 26109): +10G
    
    Command (m for help): p             
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes
    255 heads, 63 sectors/track, 26108 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: 0x0000c75a
    
       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        6505    51200000   83  Linux
    /dev/sda3            6505        9055    20480000   83  Linux
    /dev/sda4            9055       26109   136985600    5  Extended
    /dev/sda5            9055        9316     2097152   82  Linux swap / Solaris
    /dev/sda6           10000       10131     1060290   83  Linux
    /dev/sda7           10132       11437    10490413+  83  Linux

    把第6分区删除了,那么第7分区就变成6了

    说明了分区的设备标识是不稳定的

    这样就会在磁盘上涉及到分区配置文件的修改产生问题。

    删除了分区设备名就变了。

    创建完文件系统之后有生成UUID,是唯一稳定不变的。

    Command (m for help): p             
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes
    255 heads, 63 sectors/track, 26108 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: 0x0000c75a
    
       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        6505    51200000   83  Linux
    /dev/sda3            6505        9055    20480000   83  Linux
    /dev/sda4            9055       26109   136985600    5  Extended
    /dev/sda5            9055        9316     2097152   82  Linux swap / Solaris
    /dev/sda6           10000       10131     1060290   83  Linux
    /dev/sda7           10132       11437    10490413+  83  Linux
    
    Command (m for help): d
    Partition number (1-7): 6
    
    Command (m for help): p
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes
    255 heads, 63 sectors/track, 26108 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: 0x0000c75a
    
       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        6505    51200000   83  Linux
    /dev/sda3            6505        9055    20480000   83  Linux
    /dev/sda4            9055       26109   136985600    5  Extended
    /dev/sda5            9055        9316     2097152   82  Linux swap / Solaris
    /dev/sda6           10132       11437    10490413+  83  Linux

    把第6分区删除了,继续创建分区

    Command (m for help): p
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes
    255 heads, 63 sectors/track, 26108 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: 0x0000c75a
    
       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        6505    51200000   83  Linux
    /dev/sda3            6505        9055    20480000   83  Linux
    /dev/sda4            9055       26109   136985600    5  Extended
    /dev/sda5            9055        9316     2097152   82  Linux swap / Solaris
    /dev/sda6           10132       11437    10490413+  83  Linux
    
    Command (m for help): d
    Partition number (1-6): 6
    
    Command (m for help): n
    First cylinder (9316-26109, default 9316):    
    Using default value 9316
    Last cylinder, +cylinders or +size{K,M,G} (9316-26109, default 26109): +10G
    
    Command (m for help): p
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes
    255 heads, 63 sectors/track, 26108 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: 0x0000c75a
    
       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        6505    51200000   83  Linux
    /dev/sda3            6505        9055    20480000   83  Linux
    /dev/sda4            9055       26109   136985600    5  Extended
    /dev/sda5            9055        9316     2097152   82  Linux swap / Solaris
    /dev/sda6            9316       10621    10484351   83  Linux

    再创建分区

    Command (m for help): p
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes
    255 heads, 63 sectors/track, 26108 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: 0x0000c75a
    
       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        6505    51200000   83  Linux
    /dev/sda3            6505        9055    20480000   83  Linux
    /dev/sda4            9055       26109   136985600    5  Extended
    /dev/sda5            9055        9316     2097152   82  Linux swap / Solaris
    /dev/sda6            9316       10621    10484351   83  Linux
    
    Command (m for help): n
    First cylinder (10622-26109, default 10622): 
    Using default value 10622
    Last cylinder, +cylinders or +size{K,M,G} (10622-26109, default 26109): +6G
    
    Command (m for help): p
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes
    255 heads, 63 sectors/track, 26108 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: 0x0000c75a
    
       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        6505    51200000   83  Linux
    /dev/sda3            6505        9055    20480000   83  Linux
    /dev/sda4            9055       26109   136985600    5  Extended
    /dev/sda5            9055        9316     2097152   82  Linux swap / Solaris
    /dev/sda6            9316       10621    10484351   83  Linux
    /dev/sda7           10622       11405     6297448+  83  Linux

       7在6版本上存盘

    存盘。

    出现了报错,重读分区表失败了,也就是从磁盘同步到内存里时失败了。

    Command (m for help): p
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes
    255 heads, 63 sectors/track, 26108 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: 0x0000c75a
    
       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        6505    51200000   83  Linux
    /dev/sda3            6505        9055    20480000   83  Linux
    /dev/sda4            9055       26109   136985600    5  Extended
    /dev/sda5            9055        9316     2097152   82  Linux swap / Solaris
    /dev/sda6            9316       10621    10484351   83  Linux
    /dev/sda7           10622       11405     6297448+  83  Linux
    
    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 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 run partprobe(8) or kpartx(8)
    Syncing disks.

    查看磁盘和内存的分区

    查看磁盘的分区表只有1种方法:

    [root@centos65 ~]# fdisk  /dev/sda  -l
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes
    255 heads, 63 sectors/track, 26108 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: 0x0000c75a
    
       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        6505    51200000   83  Linux
    /dev/sda3            6505        9055    20480000   83  Linux
    /dev/sda4            9055       26109   136985600    5  Extended
    /dev/sda5            9055        9316     2097152   82  Linux swap / Solaris
    /dev/sda6            9316       10621    10484351   83  Linux
    /dev/sda7           10622       11405     6297448+  83  Linux
    

    下面的查看内存分区表的3种方法:

    [root@centos65 ~]# lsblk 
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sr0     11:0    1  3.7G  0 rom  /mnt
    sda      8:0    0  200G  0 disk 
    ├─sda1   8:1    0    1G  0 part /boot
    ├─sda2   8:2    0 48.8G  0 part /
    ├─sda3   8:3    0 19.5G  0 part /app
    ├─sda4   8:4    0    1K  0 part 
    └─sda5   8:5    0    2G  0 part [SWAP]
    sdb      8:16   0  100G  0 disk 
    sdc      8:32   0   80G  0 disk 
    sdd      8:48   0   60G  0 disk 
    [root@centos65 ~]# cat  /proc/partitions 
    major minor  #blocks  name
    
       8        0  209715200 sda
       8        1    1048576 sda1
       8        2   51200000 sda2
       8        3   20480000 sda3
       8        4          1 sda4
       8        5    2097152 sda5
       8       16  104857600 sdb
       8       32   83886080 sdc
       8       48   62914560 sdd


    查看硬盘设备名称: [root@centos65 ~]# ls /dev/sda* /dev/sda /dev/sda1 /dev/sda2 /dev/sda3 /dev/sda4 /dev/sda5

    五在不同版本上同步分区表

    注意在不同版本上是不同的。


    查看内核是否已经识别新的分区:
    cat /proc/partations


    centos6通知内核重新读取硬盘分区表
    新增分区用
    partx -a /dev/DEVICE
    kpartx -a /dev/DEVICE -f: force


    删除分区用
    partx -d --nr M-N /dev/DEVICE
     CentOS 5,7: 使用partprobe
    partprobe [/dev/DEVICE]

       1在6版本上同步增加的分区表

    通过上面查看磁盘和内存的分区不同步,这样就无法管理新创建的分区了。

    注意在生产中虽然重启服务可以实现同步,但是机器启动是很慢的,这样就会给企业或公司带来很大的损失。

    要实现同步可以使用partprobe,但是在6上不生效,适合5和7版本。

    [root@centos65 ~]# partprobe 
    Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy).  As a result, it may not reflect all of your changes until after reboot.
    Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
    Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
    Error: Invalid partition table - recursive partition on /dev/sr0.
    [root@centos65 ~]# ls /dev/sda*
    /dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sda4  /dev/sda5
    
    [root@centos65 ~]# cat  /proc/partitions 
    major minor  #blocks  name
    
       8        0  209715200 sda
       8        1    1048576 sda1
       8        2   51200000 sda2
       8        3   20480000 sda3
       8        4          1 sda4
       8        5    2097152 sda5
       8       16  104857600 sdb
       8       32   83886080 sdc
       8       48   62914560 sdd

    在6上同步就比较麻烦,要先考虑是增加分区还是删除分区造成不同步的

    如果是增加就使用partx -a

    [root@centos65 ~]# 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
    [root@centos65 ~]# lsblk 
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sr0     11:0    1  3.7G  0 rom  /mnt
    sda      8:0    0  200G  0 disk 
    ├─sda1   8:1    0    1G  0 part /boot
    ├─sda2   8:2    0 48.8G  0 part /
    ├─sda3   8:3    0 19.5G  0 part /app
    ├─sda4   8:4    0    1K  0 part 
    ├─sda5   8:5    0    2G  0 part [SWAP]
    ├─sda6   8:6    0   10G  0 part 
    └─sda7   8:7    0    6G  0 part 
    sdb      8:16   0  100G  0 disk 
    sdc      8:32   0   80G  0 disk 
    sdd      8:48   0   60G  0 disk 

       2在6版本上同步删除的分区表

    删除刚才创建的分区。

    在6上如果是因为删除分区造成不同步就使用partx    -d   --nr    M-N     /dev/DEVICE

    [root@centos65 ~]# fdisk   /dev/sda
    
    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): p
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes
    255 heads, 63 sectors/track, 26108 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: 0x0000c75a
    
       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        6505    51200000   83  Linux
    /dev/sda3            6505        9055    20480000   83  Linux
    /dev/sda4            9055       26109   136985600    5  Extended
    /dev/sda5            9055        9316     2097152   82  Linux swap / Solaris
    /dev/sda6            9316       10621    10484351   83  Linux
    /dev/sda7           10622       11405     6297448+  83  Linux
    
    Command (m for help): d
    Partition number (1-7): 7  
    
    Command (m for help): d
    Partition number (1-6): 6
    
    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 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 run partprobe(8) or kpartx(8)
    Syncing disks.
    [root@centos65 ~]# lsblk 
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sr0     11:0    1  3.7G  0 rom  /mnt
    sda      8:0    0  200G  0 disk 
    ├─sda1   8:1    0    1G  0 part /boot
    ├─sda2   8:2    0 48.8G  0 part /
    ├─sda3   8:3    0 19.5G  0 part /app
    ├─sda4   8:4    0    1K  0 part 
    ├─sda5   8:5    0    2G  0 part [SWAP]
    ├─sda6   8:6    0   10G  0 part 
    └─sda7   8:7    0    6G  0 part 
    sdb      8:16   0  100G  0 disk 
    sdc      8:32   0   80G  0 disk 
    sdd      8:48   0   60G  0 disk 
    [root@centos65 ~]# fdisk   /dev/sda  -l
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes
    255 heads, 63 sectors/track, 26108 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: 0x0000c75a
    
       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        6505    51200000   83  Linux
    /dev/sda3            6505        9055    20480000   83  Linux
    /dev/sda4            9055       26109   136985600    5  Extended
    /dev/sda5            9055        9316     2097152   82  Linux swap / Solaris

    删除分区用partx -d --nr   M-N    /dev/DEVICE
    也可以一个个同步partx -d    --nr   M    /dev/DEVICE

    [root@centos65 ~]# partx  -d  --nr  6-7  /dev/sda
    [root@centos65 ~]# lsblk 
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sr0     11:0    1  3.7G  0 rom  /mnt
    sda      8:0    0  200G  0 disk 
    ├─sda1   8:1    0    1G  0 part /boot
    ├─sda2   8:2    0 48.8G  0 part /
    ├─sda3   8:3    0 19.5G  0 part /app
    ├─sda4   8:4    0    1K  0 part 
    └─sda5   8:5    0    2G  0 part [SWAP]
    sdb      8:16   0  100G  0 disk 
    sdc      8:32   0   80G  0 disk 
    sdd      8:48   0   60G  0 disk 
    [root@centos65 ~]# fdisk   /dev/sda  -l
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes
    255 heads, 63 sectors/track, 26108 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: 0x0000c75a
    
       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        6505    51200000   83  Linux
    /dev/sda3            6505        9055    20480000   83  Linux
    /dev/sda4            9055       26109   136985600    5  Extended
    /dev/sda5            9055        9316     2097152   82  Linux swap / Solaris

       3在7版本上同步分区表

    在7上创建新的分区并且实现同步

    First sector (153098240-419430399, default 153098240):

    为什么默认从153098240 开始进行分区,而不是153096191?

    因为之间的空间存储了分区的一些信息,比如MBR,EBR

    [root@centos72 ~]# fdisk         /dev/sda
    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): p
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x000c7076
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *        2048     2099199     1048576   83  Linux
    /dev/sda2         2099200   106956799    52428800   83  Linux
    /dev/sda3       106956800   148899839    20971520   83  Linux
    /dev/sda4       148899840   419430399   135265280    5  Extended
    /dev/sda5       148901888   153096191     2097152   82  Linux swap / Solaris
    
    Command (m for help): n
    All primary partitions are in use
    Adding logical partition 6
    First sector (153098240-419430399, default 153098240): 
    Command (m for help): p
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x000c7076
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *        2048     2099199     1048576   83  Linux
    /dev/sda2         2099200   106956799    52428800   83  Linux
    /dev/sda3       106956800   148899839    20971520   83  Linux
    /dev/sda4       148899840   419430399   135265280    5  Extended
    /dev/sda5       148901888   153096191     2097152   82  Linux swap / Solaris
    
    Command (m for help): n
    All primary partitions are in use
    Adding logical partition 6
    First sector (153098240-419430399, default 153098240): 
    Using default value 153098240
    Last sector, +sectors or +size{K,M,G} (153098240-419430399, default 419430399): +10G
    Partition 6 of type Linux and of size 10 GiB is set
    
    Command (m for help): p
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x000c7076
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *        2048     2099199     1048576   83  Linux
    /dev/sda2         2099200   106956799    52428800   83  Linux
    /dev/sda3       106956800   148899839    20971520   83  Linux
    /dev/sda4       148899840   419430399   135265280    5  Extended
    /dev/sda5       148901888   153096191     2097152   82  Linux swap / Solaris
    /dev/sda6       153098240   174069759    10485760   83  Linux
    
    Command (m for help): n
    All primary partitions are in use
    Adding logical partition 7
    First sector (174071808-419430399, default 174071808): 
    Using default value 174071808
    Last sector, +sectors or +size{K,M,G} (174071808-419430399, default 419430399): +6G
    Partition 7 of type Linux and of size 6 GiB is set
    
    Command (m for help): p
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x000c7076
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *        2048     2099199     1048576   83  Linux
    /dev/sda2         2099200   106956799    52428800   83  Linux
    /dev/sda3       106956800   148899839    20971520   83  Linux
    /dev/sda4       148899840   419430399   135265280    5  Extended
    /dev/sda5       148901888   153096191     2097152   82  Linux swap / Solaris
    /dev/sda6       153098240   174069759    10485760   83  Linux
    /dev/sda7       174071808   186654719     6291456   83  Linux
    
    Command (m for help): w注意使用w并不能够实现磁盘和内存分区的同步
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    
    WARNING: Re-reading the partition table 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 run partprobe(8) or kpartx(8)
    Syncing disks.
    [root@centos72 ~]# lsblk 
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda      8:0    0  200G  0 disk 
    ├─sda1   8:1    0    1G  0 part /boot
    ├─sda2   8:2    0   50G  0 part /
    ├─sda3   8:3    0   20G  0 part /app
    ├─sda4   8:4    0    1K  0 part 
    └─sda5   8:5    0    2G  0 part [SWAP]
    sdb      8:16   0  100G  0 disk 
    sr0     11:0    1  4.2G  0 rom  /mnt

     partprobe 实现同步了

    [root@centos72 ~]# lsblk 
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda      8:0    0  200G  0 disk 
    ├─sda1   8:1    0    1G  0 part /boot
    ├─sda2   8:2    0   50G  0 part /
    ├─sda3   8:3    0   20G  0 part /app
    ├─sda4   8:4    0    1K  0 part 
    └─sda5   8:5    0    2G  0 part [SWAP]
    sdb      8:16   0  100G  0 disk 
    sr0     11:0    1  4.2G  0 rom  /mnt
    [root@centos72 ~]# partprobe 
    Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
    [root@centos72 ~]# lsblk 
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda      8:0    0  200G  0 disk 
    ├─sda1   8:1    0    1G  0 part /boot
    ├─sda2   8:2    0   50G  0 part /
    ├─sda3   8:3    0   20G  0 part /app
    ├─sda4   8:4    0  512B  0 part 
    ├─sda5   8:5    0    2G  0 part [SWAP]
    ├─sda6   8:6    0   10G  0 part 
    └─sda7   8:7    0    6G  0 part 
    sdb      8:16   0  100G  0 disk 
    sr0     11:0    1  4.2G  0 rom  /mnt

    把分区删除了

    [root@centos72 ~]# lsblk 
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda      8:0    0  200G  0 disk 
    ├─sda1   8:1    0    1G  0 part /boot
    ├─sda2   8:2    0   50G  0 part /
    ├─sda3   8:3    0   20G  0 part /app
    ├─sda4   8:4    0  512B  0 part 
    ├─sda5   8:5    0    2G  0 part [SWAP]
    ├─sda6   8:6    0   10G  0 part 
    └─sda7   8:7    0    6G  0 part 
    sdb      8:16   0  100G  0 disk 
    sr0     11:0    1  4.2G  0 rom  /mnt
    [root@centos72 ~]# fdisk         /dev/sda
    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): p
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x000c7076
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *        2048     2099199     1048576   83  Linux
    /dev/sda2         2099200   106956799    52428800   83  Linux
    /dev/sda3       106956800   148899839    20971520   83  Linux
    /dev/sda4       148899840   419430399   135265280    5  Extended
    /dev/sda5       148901888   153096191     2097152   82  Linux swap / Solaris
    /dev/sda6       153098240   174069759    10485760   83  Linux
    /dev/sda7       174071808   186654719     6291456   83  Linux
    
    Command (m for help): d  
    Partition number (1-7, default 7): 7
    Partition 7 is deleted
    
    Command (m for help): d
    Partition number (1-6, default 6): 6
    Partition 6 is deleted
    
    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 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 run partprobe(8) or kpartx(8)
    Syncing disks.
    [root@centos72 ~]# partprobe 
    Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
    [root@centos72 ~]# lsblk 
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda      8:0    0  200G  0 disk 
    ├─sda1   8:1    0    1G  0 part /boot
    ├─sda2   8:2    0   50G  0 part /
    ├─sda3   8:3    0   20G  0 part /app
    ├─sda4   8:4    0  512B  0 part 
    └─sda5   8:5    0    2G  0 part [SWAP]
    sdb      8:16   0  100G  0 disk 
    sr0     11:0    1  4.2G  0 rom  /mnt

    六直接存盘立即生效的情况

    如果是新的硬盘就不存在分区问题

    注意如果是一台主机的第1块硬盘必须要有一个主分区,但是对于第2块硬盘就不需要了,主分区可有可无。

    [root@centos65 ~]# fdisk   /dev/sdb 
    
    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): p
    
    Disk /dev/sdb: 107.4 GB, 107374182400 bytes
    255 heads, 63 sectors/track, 13054 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: 0x000d8fb4
    
       Device Boot      Start         End      Blocks   Id  System
    
    Command (m for help): n
    Command action
       e   extended
       p   primary partition (1-4)

    创建扩展分区,并且编号是1,因为扩展分区里面还要分很多的小的逻辑分区,所以扩展分区的空间是比较大的,

    [root@centos65 ~]# fdisk   /dev/sdb 
    
    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): p
    
    Disk /dev/sdb: 107.4 GB, 107374182400 bytes
    255 heads, 63 sectors/track, 13054 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: 0x000d8fb4
    
       Device Boot      Start         End      Blocks   Id  System
    
    Command (m for help): n
    Command action
       e   extended
       p   primary partition (1-4)
    e
    Partition number (1-4): 1
    First cylinder (1-13054, default 1): 
    Using default value 1
    Last cylinder, +cylinders or +size{K,M,G} (1-13054, default 13054): +50G
    
    Command (m for help): p
    
    Disk /dev/sdb: 107.4 GB, 107374182400 bytes
    255 heads, 63 sectors/track, 13054 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: 0x000d8fb4
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1               1        6528    52436128+   5  Extended
    
    Command (m for help): 

    现在出现了逻辑分区,因为之前创建了扩展分区。出现逻辑分区的必要条件是已经创建了扩展分区。

    创建逻辑分区,只能是在扩展分区的范围里面分。

    Command (m for help): n
    Command action
       e   extended
       p   primary partition (1-4)
    e
    Partition number (1-4): 1
    First cylinder (1-13054, default 1): 
    Using default value 1
    Last cylinder, +cylinders or +size{K,M,G} (1-13054, default 13054): +50G
    
    Command (m for help): p
    
    Disk /dev/sdb: 107.4 GB, 107374182400 bytes
    255 heads, 63 sectors/track, 13054 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: 0x000d8fb4
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1               1        6528    52436128+   5  Extended
    
    Command (m for help): n
    Command action
       l   logical (5 or over)
       p   primary partition (1-4)
    l
    First cylinder (1-6528, default 1): 
    Using default value 1
    Last cylinder, +cylinders or +size{K,M,G} (1-6528, default 6528): +10G
    
    Command (m for help): p
    
    Disk /dev/sdb: 107.4 GB, 107374182400 bytes
    255 heads, 63 sectors/track, 13054 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: 0x000d8fb4
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1               1        6528    52436128+   5  Extended
    /dev/sdb5               1        1306    10490382   83  Linux

    创建主分区

    Command (m for help): p
    
    Disk /dev/sdb: 107.4 GB, 107374182400 bytes
    255 heads, 63 sectors/track, 13054 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: 0x000d8fb4
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1               1        6528    52436128+   5  Extended
    /dev/sdb5               1        1306    10490382   83  Linux
    
    Command (m for help): n
    Command action
       l   logical (5 or over)
       p   primary partition (1-4)
    p
    Partition number (1-4): 2
    First cylinder (6529-13054, default 6529): 
    Using default value 6529
    Last cylinder, +cylinders or +size{K,M,G} (6529-13054, default 13054): +5G
    
    Command (m for help): p
    
    Disk /dev/sdb: 107.4 GB, 107374182400 bytes
    255 heads, 63 sectors/track, 13054 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: 0x000d8fb4
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1               1        6528    52436128+   5  Extended
    /dev/sdb2            6529        7182     5253255   83  Linux
    /dev/sdb5               1        1306    10490382   83  Linux
    
    Command (m for help): w   #注意此时是在干净的硬盘上创建分区,所以w可以同步成功,旧的硬盘因为已经有分区就会同步失败。
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    Syncing disks.
    [root@centos65 ~]# lsblk 
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sr0     11:0    1  3.7G  0 rom  /mnt
    sda      8:0    0  200G  0 disk 
    ├─sda1   8:1    0    1G  0 part /boot
    ├─sda2   8:2    0 48.8G  0 part /
    ├─sda3   8:3    0 19.5G  0 part /app
    ├─sda4   8:4    0    1K  0 part 
    └─sda5   8:5    0    2G  0 part [SWAP]
    sdb      8:16   0  100G  0 disk 
    ├─sdb1   8:17   0    1K  0 part 
    ├─sdb2   8:18   0    5G  0 part 
    └─sdb5   8:21   0   10G  0 part 
    sdc      8:32   0   80G  0 disk 
    sdd      8:48   0   60G  0 disk 

    七使用shell脚本批量创建分区

    通过写脚本批量创建分区,就要分析前面创建分区的步骤。注意硬盘要求是还未分区的

    使用脚本创建分区的情况在生产里面用的不多。

    第1步:输入n并且要回车,写到命令行n

    第2步:输入p并且要回车,写到命令行p

    第2步:输入1并且要回车,写到命令行1

    第3步:直接回车接收默认值,写到命令行1

    第4步:+10G并且回车,写到命令行+10G

    第5步:输入w并且回车,写到命令行w

    [root@centos65 ~]# lsblk 
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sr0     11:0    1  3.7G  0 rom  /mnt
    sda      8:0    0  200G  0 disk 
    ├─sda1   8:1    0    1G  0 part /boot
    ├─sda2   8:2    0 48.8G  0 part /
    ├─sda3   8:3    0 19.5G  0 part /app
    ├─sda4   8:4    0    1K  0 part 
    └─sda5   8:5    0    2G  0 part [SWAP]
    sdb      8:16   0  100G  0 disk 
    ├─sdb1   8:17   0    1K  0 part 
    ├─sdb2   8:18   0    5G  0 part 
    └─sdb5   8:21   0   10G  0 part 
    sdc      8:32   0   80G  0 disk 
    sdd      8:48   0   60G  0 disk 
    [root@centos65 ~]# echo   -e     'n
    p
    1
    
    +10G
    w
    '    |  fdisk   /dev/sdc
    
    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): Command action
       e   extended
       p   primary partition (1-4)
    Partition number (1-4): First cylinder (1-10443, default 1): Using default value 1
    Last cylinder, +cylinders or +size{K,M,G} (1-10443, default 10443): 
    Command (m for help): The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    Syncing disks.
    [root@centos65 ~]# lsblk 
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sr0     11:0    1  3.7G  0 rom  /mnt
    sda      8:0    0  200G  0 disk 
    ├─sda1   8:1    0    1G  0 part /boot
    ├─sda2   8:2    0 48.8G  0 part /
    ├─sda3   8:3    0 19.5G  0 part /app
    ├─sda4   8:4    0    1K  0 part 
    └─sda5   8:5    0    2G  0 part [SWAP]
    sdb      8:16   0  100G  0 disk 
    ├─sdb1   8:17   0    1K  0 part 
    ├─sdb2   8:18   0    5G  0 part 
    └─sdb5   8:21   0   10G  0 part 
    sdc      8:32   0   80G  0 disk 
    └─sdc1   8:33   0   10G  0 part 
    sdd      8:48   0   60G  0 disk 

    上面使用了标准输入重定向,fdisk接收键盘输入

    还可以多行重定向

    [root@centos65 ~]# fdisk   /dev/sdc  <<EOF
    > n
    > p
    > 2
    > 
    > +6G
    > w
    > EOF
    
    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): Command action
       e   extended
       p   primary partition (1-4)
    Partition number (1-4): First cylinder (1307-10443, default 1307): Using default value 1307
    Last cylinder, +cylinders or +size{K,M,G} (1307-10443, default 10443): 
    Command (m for help): The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    Syncing disks.
    [root@centos65 ~]# lsblk 
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sr0     11:0    1  3.7G  0 rom  /mnt
    sda      8:0    0  200G  0 disk 
    ├─sda1   8:1    0    1G  0 part /boot
    ├─sda2   8:2    0 48.8G  0 part /
    ├─sda3   8:3    0 19.5G  0 part /app
    ├─sda4   8:4    0    1K  0 part 
    └─sda5   8:5    0    2G  0 part [SWAP]
    sdb      8:16   0  100G  0 disk 
    ├─sdb1   8:17   0    1K  0 part 
    ├─sdb2   8:18   0    5G  0 part 
    └─sdb5   8:21   0   10G  0 part 
    sdc      8:32   0   80G  0 disk 
    ├─sdc1   8:33   0   10G  0 part 
    └─sdc2   8:34   0    6G  0 part 
    sdd      8:48   0   60G  0 disk 

    对于旧硬盘就要在脚本后面加上partx -a   /dev/sdc或者partprobe   /dev/sdc

    注意要学会总结,遇到交互式命令比如fdisk是可以接收键盘输入的的,那么就可以使用到重定向甚至多行重定向,并且写入到脚本里面。

    因为不同步,返回值是1

    [root@centos65 ~]# lsblk 
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sr0     11:0    1  3.7G  0 rom  /mnt
    sda      8:0    0  200G  0 disk 
    ├─sda1   8:1    0    1G  0 part /boot
    ├─sda2   8:2    0 48.8G  0 part /
    ├─sda3   8:3    0 19.5G  0 part /app
    ├─sda4   8:4    0    1K  0 part 
    └─sda5   8:5    0    2G  0 part [SWAP]
    sdb      8:16   0  100G  0 disk 
    ├─sdb1   8:17   0    1K  0 part 
    ├─sdb2   8:18   0    5G  0 part 
    └─sdb5   8:21   0   10G  0 part 
    sdc      8:32   0   80G  0 disk 
    ├─sdc1   8:33   0   10G  0 part 
    └─sdc2   8:34   0    6G  0 part 
    sdd      8:48   0   60G  0 disk 
    [root@centos65 ~]# fdisk   /dev/sda
    
    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): p
    
    Disk /dev/sda: 214.7 GB, 214748364800 bytes
    255 heads, 63 sectors/track, 26108 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: 0x0000c75a
    
       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        6505    51200000   83  Linux
    /dev/sda3            6505        9055    20480000   83  Linux
    /dev/sda4            9055       26109   136985600    5  Extended
    /dev/sda5            9055        9316     2097152   82  Linux swap / Solaris
    
    Command (m for help): n
    First cylinder (9316-26109, default 9316): 
    Using default value 9316
    Last cylinder, +cylinders or +size{K,M,G} (9316-26109, default 26109): +1G
    
    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 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 run partprobe(8) or kpartx(8)
    Syncing disks.
    [root@centos65 ~]# echo   $?
    1
    [root@centos65 ~]# lsblk 
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sr0     11:0    1  3.7G  0 rom  /mnt
    sda      8:0    0  200G  0 disk 
    ├─sda1   8:1    0    1G  0 part /boot
    ├─sda2   8:2    0 48.8G  0 part /
    ├─sda3   8:3    0 19.5G  0 part /app
    ├─sda4   8:4    0    1K  0 part 
    └─sda5   8:5    0    2G  0 part [SWAP]
    sdb      8:16   0  100G  0 disk 
    ├─sdb1   8:17   0    1K  0 part 
    ├─sdb2   8:18   0    5G  0 part 
    └─sdb5   8:21   0   10G  0 part 
    sdc      8:32   0   80G  0 disk 
    ├─sdc1   8:33   0   10G  0 part 
    └─sdc2   8:34   0    6G  0 part 
    sdd      8:48   0   60G  0 disk 

    如果写脚本就可以判断是否同步

    如果返回值不是0就要在后面加上同步的操作。

    [root@centos65 ~]# lsblk 
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sr0     11:0    1  3.7G  0 rom  /mnt
    sda      8:0    0  200G  0 disk 
    ├─sda1   8:1    0    1G  0 part /boot
    ├─sda2   8:2    0 48.8G  0 part /
    ├─sda3   8:3    0 19.5G  0 part /app
    ├─sda4   8:4    0    1K  0 part 
    └─sda5   8:5    0    2G  0 part [SWAP]
    sdb      8:16   0  100G  0 disk 
    ├─sdb1   8:17   0    1K  0 part 
    ├─sdb2   8:18   0    5G  0 part 
    └─sdb5   8:21   0   10G  0 part 
    sdc      8:32   0   80G  0 disk 
    ├─sdc1   8:33   0   10G  0 part 
    └─sdc2   8:34   0    6G  0 part 
    sdd      8:48   0   60G  0 disk 
    [root@centos65 ~]# fdisk   /dev/sdb
    
    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): p
    
    Disk /dev/sdb: 107.4 GB, 107374182400 bytes
    255 heads, 63 sectors/track, 13054 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: 0x000d8fb4
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1               1        6528    52436128+   5  Extended
    /dev/sdb2            6529        7182     5253255   83  Linux
    /dev/sdb5               1        1306    10490382   83  Linux
    
    Command (m for help): n
    Command action
       l   logical (5 or over)
       p   primary partition (1-4)
    p
    Partition number (1-4): 3
    First cylinder (7183-13054, default 7183): 
    Using default value 7183
    Last cylinder, +cylinders or +size{K,M,G} (7183-13054, default 13054): +2G
    
    Command (m for help): w
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    Syncing disks.
    [root@centos65 ~]# echo   $?
    0
    [root@centos65 ~]# lsblk 
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sr0     11:0    1  3.7G  0 rom  /mnt
    sda      8:0    0  200G  0 disk 
    ├─sda1   8:1    0    1G  0 part /boot
    ├─sda2   8:2    0 48.8G  0 part /
    ├─sda3   8:3    0 19.5G  0 part /app
    ├─sda4   8:4    0    1K  0 part 
    └─sda5   8:5    0    2G  0 part [SWAP]
    sdb      8:16   0  100G  0 disk 
    ├─sdb1   8:17   0    1K  0 part 
    ├─sdb2   8:18   0    5G  0 part 
    ├─sdb3   8:19   0    2G  0 part 
    └─sdb5   8:21   0   10G  0 part 
    sdc      8:32   0   80G  0 disk 
    ├─sdc1   8:33   0   10G  0 part 
    └─sdc2   8:34   0    6G  0 part 
    sdd      8:48   0   60G  0 disk 


    作者:wang618
    出处:https://www.cnblogs.com/wang618/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。

  • 相关阅读:
    HTML5入门
    vue进阶:vuex(数据池)
    vue进阶:vue-router之导航守卫、路由元信息、路由懒加载
    vue进阶:vue-router(vue路由)的安装与基本使用
    vue进阶:vs code添加vue代码片段
    vue进阶:基于vue-cli创建项目(搭建手脚架)
    vue入门:(底层渲染实现render函数、实例生命周期)
    vue入门:(组件)
    webpack配置不同打包配置
    webpack开启本地服务器与热更新
  • 原文地址:https://www.cnblogs.com/wang618/p/11132938.html
Copyright © 2011-2022 走看看