1. 程式人生 > 實用技巧 >Linux 磁碟分割槽

Linux 磁碟分割槽

Linux 磁碟分割槽

參考:

https://www.cnblogs.com/itech/archive/2010/12/24/1916255.html

https://www.jianshu.com/p/bf939474d69b

gdisk使用參考

https://www.cnblogs.com/Sunzz/p/6908329.html

概念

優於一般linux分配硬碟空間不會超過2T,所以也就無需使用GPT分割槽表,所以使用MBR分割槽工具fdisk即可(GPT分割槽表使用gpart 或是 gdisk

  1. 在linux下SCSI介面裝置以sd命名,第一個是sda,第二個sdb,依次類推。IDE介面裝置用hd命名,第一個是hda,第二個是hdb,依次類推。
  2. 分割槽是用裝置名稱加數字命名。例如sda1代表sda這個硬碟裝置上的第一個分割槽。
  3. MBR分割槽、表最多有四個主分割槽(windows對應C,D,E,F 盤),一個擴充套件分割槽,擴張分割槽可以在分為多個邏輯分割槽。

我們可以通過 lsblk 命令來檢視硬碟,分割槽以及掛載點

linux 中1-4都是主分割槽,從5開始為邏輯分割槽

[root@chz ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part 
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0    1G  0 disk 
sr0              11:0    1  4.4G  0 rom  /run/media/root/CentOS 7 x86_64
[root@chz ~]# 

fdisk

  • fdisk -l

    檢視所有磁碟和分割槽

    $ sudo fdisk -l
    Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 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
    Disklabel type: dos
    Disk identifier: 0xeab59449
    
    Device     Boot    Start      End  Sectors Size Id Type
    /dev/sda1  *    20973568 62914559 41940992  20G 83 Linux
    
    Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 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 /dev/sdc: 10 GiB, 10737418240 bytes, 20971520 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 /dev/sdd: 10 GiB, 10737418240 bytes, 20971520 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 /dev/sde: 10 GiB, 10737418240 bytes, 20971520 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
    

    這裡可以發現,5塊硬碟,sda有兩個主分割槽;sda1做為主引導分割槽。Unites和Sector分別表示扇區大小

  • fdisk /dev/sda

    操作具體某塊硬碟

新增磁碟

  1. 在虛擬機器中新增一塊硬碟

  2. 分割槽

    可以通過 + size {K,M,G} 來指定結束柱面

    這裡建立了一個100M的主分割槽sdb1

    [root@chz ~]# 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 0xac359141.
    
    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-2097151, default 2048): 
    Using default value 2048
    Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): +100M #結束柱面
    Partition 1 of type Linux and of size 100 MiB is set
    
    Command (m for help): p
    
    Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0xac359141
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048      206847      102400   83  Linux
    
    Command (m for help): n
    Partition type:
       p   primary (1 primary, 0 extended, 3 free) #有1個主分割槽,0個擴充套件分割槽,還可以建立3個主分割槽
       e   extended
    Select (default p):  
    

    建立擴充套件分割槽

    Command (m for help): n
    Partition type:
       p   primary (1 primary, 0 extended, 3 free)
       e   extended
    Select (default p): e #選擇擴充套件分割槽
    Partition number (2-4, default 2): 2
    First sector (206848-2097151, default 206848): 
    Using default value 206848
    Last sector, +sectors or +size{K,M,G} (206848-2097151, default 2097151): +100M
    Partition 2 of type Extended and of size 100 MiB is set
    
    Command (m for help): p
    
    Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0xac359141
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048      206847      102400   83  Linux #主分割槽
    /dev/sdb2          206848      411647      102400    5  Extended #擴充套件分割槽
    
    Command (m for help): 
    

    在擴充套件分割槽上建立邏輯分割槽

    Command (m for help): n
    Partition type:
       p   primary (1 primary, 1 extended, 2 free)
       l   logical (numbered from 5) #邏輯分割槽從5開始計數
    Select (default p): l
    Adding logical partition 5 
    First sector (208896-411647, default 208896): 
    Using default value 208896
    Last sector, +sectors or +size{K,M,G} (208896-411647, default 411647): +20M
    Partition 5 of type Linux and of size 20 MiB is set
    
    Command (m for help): p
    
    Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0xf9fe9356
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048      206847      102400   83  Linux
    /dev/sdb2          206848      411647      102400    5  Extended
    /dev/sdb5          208896      249855       20480   83  Linux
    
    Command (m for help): 
    
    

    儲存分割槽

    Command (m for help): w
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    

    lsblk檢視分割槽

    [root@chz ~]# lsblk
    NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda               8:0    0   20G  0 disk 
    ├─sda1            8:1    0    1G  0 part /boot
    └─sda2            8:2    0   19G  0 part 
      ├─centos-root 253:0    0   17G  0 lvm  /
      └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
    sdb               8:16   0    1G  0 disk 
    ├─sdb1            8:17   0  100M  0 part 
    ├─sdb2            8:18   0    1K  0 part 
    └─sdb5            8:21   0   20M  0 part 
    sr0              11:0    1  4.4G  0 rom  /run/media/root/CentOS 7 x86_64
    
  3. 格式化

    這裡將 /dev/sdb1分割槽格式化為ext4檔案系統,預設使用ext2

    這裡mkfs.ext4是mke2fs的衍生命令, 也可以使用mkfs -t ext4 /dev/sdb1

    將磁碟掛載後通過 df -T 來檢視磁碟的型別

    [root@chz ~]# mkfs.ext4 /dev/sdb1
    mke2fs 1.42.9 (28-Dec-2013)
    Filesystem label=
    OS type: Linux
    Block size=1024 (log=0)
    Fragment size=1024 (log=0)
    Stride=0 blocks, Stripe width=0 blocks
    25688 inodes, 102400 blocks
    5120 blocks (5.00%) reserved for the super user
    First data block=1
    Maximum filesystem blocks=33685504
    13 block groups
    8192 blocks per group, 8192 fragments per group
    1976 inodes per group
    Superblock backups stored on blocks: 
    	8193, 24577, 40961, 57345, 73729
    
    Allocating group tables: done                            
    Writing inode tables: done                            
    Creating journal (4096 blocks): done
    Writing superblocks and filesystem accounting information: done 
    
    
  4. 掛載

    這種方式掛載並不會永久掛載,當重啟後失效

    [root@chz ~]# mount /dev/sdb1 /root/test
    [root@chz ~]# df -T
    Filesystem              Type     1K-blocks    Used Available Use% Mounted on
    devtmpfs                devtmpfs    480800       0    480800   0% /dev
    tmpfs                   tmpfs       497840       0    497840   0% /dev/shm
    tmpfs                   tmpfs       497840    8740    489100   2% /run
    tmpfs                   tmpfs       497840       0    497840   0% /sys/fs/cgroup
    /dev/mapper/centos-root xfs       17811456 7798620  10012836  44% /
    /dev/sda1               xfs        1038336  217148    821188  21% /boot
    tmpfs                   tmpfs        99572      20     99552   1% /run/user/0
    /dev/sr0                iso9660    4554702 4554702         0 100% /run/media/root/CentOS 7 x86_64
    /dev/sdb1               ext4         95054    1550     86336   2% /root/test
    
    
  5. 永久掛載

    從左至右欄位分別為,device or filesystem,mount point,type of filesystem,mount options,which filesystems need to be dumped(0表示不存檔),which filesystems need to be checked(0表示在引導時不需要檢查)。

    [root@chz ~]# cat /etc/fstab
    
    #
    # /etc/fstab
    # Created by anaconda on Mon Aug 24 07:49:09 2020
    #
    # Accessible filesystems, by reference, are maintained under '/dev/disk'
    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
    #
    /dev/mapper/centos-root /                       xfs     defaults        0 0
    UUID=52ff1027-e9d7-427d-9f43-3a98ba708796 /boot                   xfs     defaults        0 0
    /dev/mapper/centos-swap swap                    swap    defaults        0 0
    /dev/sdb1 /root/test ext4 defaults 0 0
    
    

    我們也可以通過UUID來掛載,通過blkid來獲取UUID

    [root@chz ~]# blkid
    /dev/sda1: UUID="52ff1027-e9d7-427d-9f43-3a98ba708796" TYPE="xfs" 
    /dev/sda2: UUID="eA52jE-SFuU-BG5t-Isyw-wWdY-lj4K-xso1bu" TYPE="LVM2_member" 
    /dev/sdb1: UUID="8492fdac-fc1c-40e7-9548-ba9d3944e348" TYPE="ext4" 
    /dev/sr0: UUID="2019-09-11-18-50-31-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" 
    /dev/mapper/centos-root: UUID="c18c2095-72b4-49f4-95be-a58d0a6cc2ad" TYPE="xfs" 
    /dev/mapper/centos-swap: UUID="0b4a0dde-4db6-4494-a067-80f3318849f7" TYPE="swap" 
    

刪除分割槽

注意如果直接刪除分割槽,但是沒有修改 /etc/fstab中掛載的就會出現give root password for maintenance