Linux磁盤與文件系統管理(一)

fdisk

  經常使用的磁盤分區工具,受mbr分區表的限制,只能給小於2TB的磁盤劃分分區,若是使用fdisk對大於2TB的磁盤進行分區,雖然能夠分區,但只能識別2T的空間,通常使用parted分區工具前端

  -l  # 顯示全部磁盤分區信息node

[root@puppet105 ~]# fdisk -l Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 sectors    # 磁盤/dev/sda 的大小 Units = sectors of 1 * 512 = 512 bytes                  # 每一個柱面大小512字節 Sector size (logical/physical): 512 bytes / 512 bytes        # 每一個扇區的字節數 I/O size (minimum/optimal): 512 bytes / 512 bytes          # 每次讀寫的字節數 Disk label type: dos Disk identifier: 0x000a9f20 
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 4196351 1048576 82 Linux swap / Solaris /dev/sda3 4196352 125829119 60816384 8e Linux LVM Disk /dev/mapper/centos_centos7-root: 62.3 GB, 62272831488 bytes, 121626624 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

     Device  # 分區名稱linux

  Boot    # 啓動分區,用*表示的是啓動分區centos

  Start    # 表示開始的柱面app

  End    # 表示結束的柱面ide

  Blocks    # block塊數量工具

  Id      # 分區類型IDui

  System    # 分區類型this

  磁盤分區步驟

# 加了一塊20G的磁盤 [root@centos7 ~]# fdisk -l /dev/sdb Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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 # 查看分區前設備狀態 [root@centos7 ~]# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sdb # 開始分區 [root@centos7 ~]# fdisk /dev/sdb        # 後面直接跟設備文件,或者-cu參數
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 0xfe05c2f7. Command (m for help): m             # 輸入m是幫助菜單 Command action a toggle a bootable flag           # 設置引導扇區 b edit bsd disklabel              # 編輯bsd卷標 c toggle the dos compatibility flag      # 設置dos兼容扇區 d delete a partition                # 刪除一個分區 g create a new empty GPT partition table    # 建立空白gpt分區表 G create an IRIX (SGI) partition table      # 建立IRIX分區表 l list known partition types            # 查看分區類型對應列表編號 m print this menu                  # 打印幫助菜單 n add a new partition                # 添加一個新分區 o create a new empty DOS partition table     # 建立新的DOS分區表 p print the partition table            # 打印分區表 q quit without saving changes        # 不保存退出 s create a new empty Sun disklabel      # 建立新的空sun卷標 t change a partition's system id        # 更改分區系統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)  # 主分區,只能建立4個,第五個必須爲擴展分區 e extended                      # 擴展分區 Select (default p): p                  # 建立主分區標識 Partition number (1-4, default 1): 1          #編號(1-4) First sector (2048-41943039, default 2048):     # 起始位置,直接回車,默認是1 Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +5G # 設置主分區大小 Partition 1 of type Linux and of size 5 GiB is set 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        # 編號2 First sector (10487808-41943039, default 10487808): Using default value 10487808 Last sector, +sectors or +size{K,M,G} (10487808-41943039, default 41943039): +5G  # 也是5G的容量 Partition 2 of type Extended and of size 5 GiB is set Command (m for help): n              # 建立一個新分區 Partition type: p primary (1 primary, 1 extended, 2 free)  # 此時由於建立了擴展分區,因此只剩下主分區和邏輯分區了 l logical (numbered from 5) Select (default p): l                # 建立邏輯分區 Adding logical partition 5              # 邏輯分區編號必須是從5開始,默認的就是5 First sector (10489856-20973567, default 10489856): Using default value 10489856 Last sector, +sectors or +size{K,M,G} (10489856-20973567, default 20973567): +2G  # 給2G的容量 Partition 5 of type Linux and of size 2 GiB is set Command (m for help): p                # 打印建立的分區列表 Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0xfe05c2f7 Device Boot Start End Blocks Id System /dev/sdb1 2048 10487807 5242880 83 Linux /dev/sdb2 10487808 20973567 5242880 5 Extended /dev/sdb5 10489856 14684159 2097152 83 Linux Command (m for help): w                # 保存 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@centos7 ~]# ls /dev/sdb*            # 查看設備狀態 /dev/sdb /dev/sdb1 /dev/sdb2 /dev/sdb5 [root@centos7 ~]# partprobe /dev/sdb        # 通知內核分區表已經更改 [root@centos7 ~]# mkfs.ext4 /dev/sdb1        #格式化文件系統 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 327680 inodes, 1310720 blocks 65536 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1342177280 40 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 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done [root@centos7 ~]# tune2fs -c 0 -i 0 /dev/sdb1    # 此命令能夠米麪磁盤過載自動檢查 tune2fs 1.42.9 (28-Dec-2013) Setting maximal mount count to 1 [root@centos7 ~]# df -h            # 格式化後的磁盤須要掛載才能使用,此處未顯示已經建立的磁盤 Filesystem Size Used Avail Use% Mounted on /dev/sda3 98G 1.7G 97G 2% / devtmpfs 901M 0 901M 0% /dev tmpfs 911M 0 911M 0% /dev/shm tmpfs 911M 9.5M 902M 2% /run tmpfs 911M 0 911M 0% /sys/fs/cgroup /dev/sda1 1014M 135M 880M 14% /boot tmpfs 183M 0 183M 0% /run/user/0 [root@centos7 ~]# mount /dev/sdb1 /mnt    # 掛載磁盤分區 [root@centos7 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 98G 1.7G 97G 2% / devtmpfs 901M 0 901M 0% /dev tmpfs 911M 0 911M 0% /dev/shm tmpfs 911M 9.5M 902M 2% /run tmpfs 911M 0 911M 0% /sys/fs/cgroup /dev/sda1 1014M 135M 880M 14% /boot tmpfs 183M 0 183M 0% /run/user/0 /dev/sdb1 4.8G 20M 4.6G 1% /mnt      # 磁盤已經掛載上去了    [root@centos7 ~]# vi /etc/fstab      # 開機啓動有兩種方式,此爲第一種,另一種 echo "mount /dev/sdb1 /mnt" >>/etc/rc.local # # /etc/fstab # Created by anaconda on Fri Oct 26 21:32:51 2018 # # 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 # UUID=fc662da3-4f5b-480b-bae9-cc9c8698742a / xfs defaults 0 0 UUID=e1b67cc9-08ff-47e7-b1e9-3ce2d9ee5eb6 /boot xfs defaults 0 0 UUID=47b26d55-1602-48e1-adba-91fe4f420b03 swap swap defaults 0 0 /dev/sdb1 /mnt ext4 defaults 0 0

  # 關於LVM進行分區,會單獨寫博文centos7

  fdisk非交互式分區(批量建立)

[root@centos7 ~]# fdisk /dev/sdb<<EOF > n > p > 3
> 
> +2G > p > w
> EOF 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): Partition type: p primary (1 primary, 1 extended, 2 free) l logical (numbered from 5) Select (default p): Partition number (3,4, default 3): First sector (20973568-41943039, default 20973568): Using default value 20973568 Last sector, +sectors or +size{K,M,G} (20973568-41943039, default 41943039): Partition 3 of type Linux and of size 2 GiB is set Command (m for help): Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0xfe05c2f7 Device Boot Start End Blocks Id System /dev/sdb1            2048    10487807     5242880   83 Linux /dev/sdb2        10487808    20973567     5242880    5 Extended /dev/sdb3        20973568    25167871     2097152   83 Linux /dev/sdb5        10489856    14684159     2097152   83 Linux Command (m for help): 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.

 

partprobe

  在硬盤分區發生改變時,更新Linux內核中的硬盤分區表數據。不須要重啓系統就能生效

tnue2fs

  調整或者查看ex2/ex3/3x4文件系統的參數,如能夠調整文件系統開機自檢週期,用的較少

  -c  # 設置強制自檢的掛載次數,沒掛載一次計數就會加1,超過次數就會強制自檢,設置0或-1,此功能關閉

  -i  # 強制自檢的時間間隔

  -l  # 查案文件系統信息

 

parted

 大於2TB磁盤的分區命令,須要將磁盤轉換爲GPT格式

   -l  # 小時需哦有磁盤分區的信息

    -h  # 查看幫助

[root@centos7 ~]# parted GNU Parted 3.1    # 版本號 Using /dev/sda Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) h # 查看幫助 
align
-check TYPE N  #檢查磁盤分區,type分爲min/opt,N表示磁盤分區號
mklabel,mktable LABEL
-TYPE # 建立分區表 table)     mkpart PART-TYPE [FS-TYPE] START END # 建立分區 name NUMBER NAME # 爲分區命名 print [devices|free|list,all|NUMBER] # 顯示分區表信息,簡寫p quit # 退出 resizepart NUMBER END # 重設分區大小 rm NUMBER # 刪除編號number的分區 select DEVICE #選擇要編輯的設備 set NUMBER FLAG STATE # 改變分區標誌 toggle [NUMBER [FLAG]] # 設置分區標誌 unit UNIT   # 設置默認單位 copyright information of GNU Parted (parted) quit

 

  分區步驟,本次使用20GB來作演示

[root@centos7 ~]# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sdb [root@centos7 ~]# parted /dev/sdb      # 開始分區,交互式 GNU Parted 3.1 Using /dev/sdb Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) mklabel gpt              # 爲sdb磁盤建立GPT分區表,大於2T必須執行此步驟,fdisk 是mdos分區表 (parted) mkpart primary 0 5G    # 建立主分區 Warning: The resulting partition is not properly aligned for best performance. Ignore/Cancel? Ignore     # 忽略掉 (parted) p #顯示分區表 Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 10.7GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1      17.4kB 5000MB 5000MB primary    #主分區建立完畢,建立完成以後系統會自動識別, (parted) mkpart logical 5G 10G                  # 建立邏輯分區 (parted) p Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 10.7GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1      17.4kB 5000MB 5000MB primary 2      5001MB  10.0GB 5000MB logical (parted) quit Information: You may need to update /etc/fstab. [root@centos7 ~]# ls /dev/sd*        # 查看分區的設備
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sdb  /dev/sdb1  /dev/sdb2

# 後面的格式化分區,掛載,開機自動掛載和fdisk分區步驟同樣,由於parted在建立分區時,會自動讓內核識別,因此不用使用partprobe命令


  # fdisk非交互式分區(批量建立)

  parted /dev/sdb mklabel gpt yes

  parted /dev/sdb mkpart primary 0 5G

  parted /dev/sdb p

 

mkfs

  建立Linux文件系統,只有格式化建立文件系統後,才能存儲數據

  -t  #指定要建立的文件系統類型

  -c  # 建立文件系統時檢查磁盤壞塊

  -v  # 顯示詳細信息

  

[root@centos7 ~]# mkfs    # 此命令是一個前端命令,會經過-t參數調用mkfs.fstype 所以能夠直接使用mkfs.ext4建立 mkfs mkfs.cramfs mkfs.ext3 mkfs.minix mkfs.btrfs mkfs.ext2 mkfs.ext4 mkfs.xfs 

 

dumpe2fs

  用於導出ext2/ext3/ext4文件系統內部的相關信息,如,文件系統的組成bao包含超級塊,塊組,inode,bock等信息

  -b  # 打印文件系統中預留的塊信息

  -h  # 進現實超級塊信息

  -i  # 從指定的文件系統影像文件中讀取文件的系統信息

  -x  # 以16進制格式打印信息塊成員

resize2fs

  用於擴容或收縮未掛載的ext2,3,4文件系統,在2.6內核版本或者更高,還支持在線擴容已掛在的文件系統,經常使用開針對LVM擴容後的分區使用,對於fdisk和parted應事先規劃好

  -p  # 打印完成任務的進度條

  -r  # 強制執行操做

相關文章
相關標籤/搜索