磁盘分区原理与规则

磁盘分区类型:主分区,扩展分区,逻辑分区。 分区规则: 1. 主分区+扩展分区的数量不能超过4个,且扩展分区只能有1个。逻辑分区要在扩展分区之上进行划分,逻辑分区没有数量限制,可以任意个。 2. 扩展分区是不能直接用的,他是以逻辑分区的方式来使用的,所以说扩展分区可分成若干逻辑分区。他们的关系是包含的关系,所有的逻辑分区都是扩展分区的一部分。 3. 硬盘的容量=主分区的容量+扩展分区的容量;扩展分区的容量=各个逻辑分区的容量之和

主分区就是普通磁盘分盘,但是由于磁盘设备由大量的扇区组成,一个扇区的容量为512字节。
磁盘的第一个扇区最为重要,记录了主引导记录与分区表信息。
就第一个扇区而言,主引导信息记录需要占用466个字节,分区表64个字节,结束符占用2个字节;
其中分区表中每记录一个分区信息就需要16个字节,所以最多只有4个分区信息可以记录在第一个扇区中,所以主分区+扩展分区的数量不能超过4个。
但是为了创建更多的分区,就使用扩展分区做份下若干个分区的指针,划分若干个逻辑分区,来满足分区数大于4个的需求。 扩展分区不需要挂载,但是可以格式化。

  1. 查看 # fdisk -l Disk /dev/vda: 85.9 GB, 85899345920 bytes, 167772160 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: 0x000b2d99 Device Boot Start End Blocks Id System /dev/vda1 * 2048 167766794 83882373+ 83 Linux Disk /dev/vdb: 128.8 GB, 128849018880 bytes, 251658240 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: 0x000b2d99

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048   167766794    83882373+  83  Linux

上面的信息显示/dev/vda已挂载,/dev/vdb未挂载.

  1. 创建分区

    # fdisk /dev/vdb
    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 0x9b667550.
    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                      删除分区
    g   create a new empty GPT partition table
    G   create an IRIX (SGI) partition table
    l   list known partition types
    m   print this menu
    n   add a new partition 创建一个新分区
    o   create a new empty DOS partition table
    p   print the partition table
    q   quit without saving changes              退出时,保存变更
    s   create a new empty Sun disklabel
    t   change a partition's system id
    u   change display/entry units
    v   verify the partition table
    w   write table to disk and exit             写入分区表
    x   extra functionality (experts only)
    Command (m for help): n
    Partition type: #分区类型
    p   primary (0 primary, 0 extended, 4 free)  主分区
    e   extended                                 扩展分区
    Select (default p):
    Using default response p                        
    Partition number (1-4, default 1):              分区标识号
    First sector (2048-251658239, default 2048):
    Using default value 2048                        分区起点
    Last sector, +sectors or +size{K,M,G} (2048-251658239, default 251658239):
    Using default value 251658239                   分区终点
    Partition 1 of type Linux and of size 120 GiB is set
    Command (m for help): wq                           
    The partition table has been altered!
    Calling ioctl() to re-read partition table.
    Syncing disks.
    
  2. 再次查看

    # fdisk -l
    Disk /dev/vda: 85.9 GB, 85899345920 bytes, 167772160 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: 0x000b2d99
    Device Boot      Start         End      Blocks   Id  System
    /dev/vda1   *        2048   167766794    83882373+  83  Linux
    Disk /dev/vdb: 128.8 GB, 128849018880 bytes, 251658240 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: 0x9b667550
    Device Boot      Start         End      Blocks   Id  System
    /dev/vdb1            2048   251658239   125828096   83  Linux
    
  3. 格式化分区

    # mkfs -t ext4 /dev/vdb1
    mke2fs 1.42.9 (28-Dec-2013)
    Filesystem label=
    OS type: Linux
    Block size=4096 (log=2)
    Fragment size=4096 (log=2)
    Stride=0 blocks, Stripe width=0 blocks
    7864320 inodes, 31457024 blocks
    1572851 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=2178940928
    960 block groups
    32768 blocks per group, 32768 fragments per group
    8192 inodes per group
    Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872
    Allocating group tables: done
    Writing inode tables: done
    Creating journal (32768 blocks): done
    Writing superblocks and filesystem accounting information: done
    
  4. 挂载分区

    mount /dev/vdb1 /data
    
  5. 卸载分区

    umount /dev/vdb1
    
  6. 永久挂载

    vi /etc/fstab
    # `/dev/sdb1` 分区名 `/data` 挂载路径 `ext4` 格式,其他默认)
    /dev/sdb1   /data/primary  ext4  defaults   0 0
    
  7. 查看分区信息

    # df -h
    Filesystem      Size  Used Avail Use% Mounted on
    devtmpfs        7.8G     0  7.8G   0% /dev
    tmpfs           7.8G     0  7.8G   0% /dev/shm
    tmpfs           7.8G  452K  7.8G   1% /run
    tmpfs           7.8G     0  7.8G   0% /sys/fs/cgroup
    /dev/vda1        79G  3.0G   73G   4% /
    tmpfs           1.6G     0  1.6G   0% /run/user/0
    /dev/vdb1       118G   61M  112G   1% /data