1、前言
新買的服務器發現看不到數據盤,查了文檔發現原來是沒有掛載。
2、步驟
檢查磁盤掛載
[root@AlexWong /]# fdisk -l Disk /dev/vda: 40 GiB, 42949672960 bytes, 83886080 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: 0x5d6e4b6b Device Boot Start End Sectors Size Id Type /dev/vda1 * 2048 83886079 83884032 40G 83 Linux Disk /dev/vdb: 100 GiB, 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
[root@AlexWong /]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 1.9G 0 1.9G 0% /dev tmpfs 1.9G 0 1.9G 0% /dev/shm tmpfs 1.9G 8.5M 1.9G 1% /run tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup /dev/vda1 40G 2.3G 35G 7% / tmpfs 374M 0 374M 0% /run/user/0
Disk /dev/vda:磁盤1,已掛載
Disk /dev/vdb:磁盤2,未掛載磁盤分區
100GB數據硬盤待分區
fdisk /dev/vdb 參數說明: n:新建分區 p: 主分區 e: 擴展分區 上面可以默認回車,最后輸入w (write)將操作寫入磁盤 按順序輸入`n、p、1、w`
[root@AlexWong /]# fdisk /dev/vdb Welcome to fdisk (util-linux 2.32.1). 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. Created a new DOS disklabel with disk identifier 0xdb040091. Command (m for help): m Help: DOS (MBR) a toggle a bootable flag b edit nested BSD disklabel c toggle the dos compatibility flag Generic d delete a partition F list free unpartitioned space l list known partition types n add a new partition p print the partition table t change a partition type v verify the partition table i print information about a partition Misc m print this menu u change display/entry units x extra functionality (experts only) Script I load disk layout from sfdisk script file O dump disk layout to sfdisk script file Save & Exit w write table to disk and exit q quit without saving changes Create a new label g create a new empty GPT partition table G create a new empty SGI (IRIX) partition table o create a new empty DOS partition table s create a new empty Sun partition table Command (m for help): n Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): p Partition number (1-4, default 1): 1 First sector (2048-209715199, default 2048): Last sector, +sectors or +size{K,M,G,T,P} (2048-209715199, default 209715199): Created a new partition 1 of type 'Linux' and of size 100 GiB. Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.格式化剛划分的磁盤,格式成ext4格式
[root@AlexWong /]# mkfs.ext4 /dev/vdb掛載文件目錄
# 新建文件夾 [root@AlexWong /]# mkdir data # 把掛載的分區寫入/etc/fstab,這樣在開機的時候能自動掛載 [root@AlexWong /]# echo "/dev/vdb /data ext4 defaults 0 0" >> /etc/fstab
重新加載fstable,並查看磁盤是否加載成功
# mount -a 初次分區有用,在已經掛載分區后,不再重新生效 [root@AlexWong /]# mount -a [root@AlexWong /]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 1.9G 0 1.9G 0% /dev tmpfs 1.9G 0 1.9G 0% /dev/shm tmpfs 1.9G 8.5M 1.9G 1% /run tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup /dev/vda1 40G 2.3G 35G 7% / tmpfs 374M 0 374M 0% /run/user/0 /dev/vdb 98G 61M 93G 1% /data
# 如果二次修改分區,需要先移除掛載目錄,然后再添加 [root@AlexWong /]# umount /dev/vdb /data [root@AlexWong /]# mount /dev/vdb /home [root@AlexWong home]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 1.9G 0 1.9G 0% /dev tmpfs 1.9G 0 1.9G 0% /dev/shm tmpfs 1.9G 8.8M 1.9G 1% /run tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup /dev/vda1 40G 8.2G 29G 22% / overlay 40G 8.2G 29G 22% /dev/vdb 98G 61M 93G 1% /home
掛載成功。
3、分多個主區
100G分三個,一個20G,另外兩個40G
Units = sectors of 1 * 512 = 512 bytes sectors: 20GB = 20 * 1024 * 1024 * 2 = 41943040 40GB = 40 * 1024 * 1024 * 2 = 83886080 40GB = 40 * 1024 * 1024 * 2 = 83886080
[root@AlexWong /]# fdisk -l Disk /dev/vda: 40 GiB, 42949672960 bytes, 83886080 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: 0x5d6e4b6b Device Boot Start End Sectors Size Id Type /dev/vda1 * 2048 83886079 83884032 40G 83 Linux Disk /dev/vdb: 100 GiB, 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 ----------------------------------20GB---------------------------------------- # 分第一個20G,分配給/share #以新掛載的數據盤/dev/vdb為例 [root@AlexWong /]# fdisk /dev/vdb # 輸入“n”,按“Enter”,開始新建分區 Command (m for help): n Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) # 表示磁盤有兩種分區類型: # “p”表示主分區 # “e”表示擴展分區 # 磁盤使用MBR分區形式,最多可以創建4個主分區,或者3個主分區加1個擴展分區,擴展分區不可以直接使用,需要划分成若干個邏輯分區才可以使用 # 磁盤使用GPT分區形式時,沒有主分區、擴展分區以及邏輯分區之分 # 以創建一個主要分區為例,輸入“p”,按“Enter”,開始創建一個主分區 Select (default p): p # “Partition number”表示主分區編號,可以選擇1-4 Partition number (1-4, default 1): 1 # “First sector”表示起始磁柱值,可以選擇2048-209715199,默認為2048 First sector (2048-209715199, default 2048): 2048 # “Last sector”表示截止磁柱值,可以選擇2048-209715199,默認為209715199 Last sector, +sectors or +size{K,M,G,T,P} (41943040-209715199, default 209715199): 41943040 # 表示20GB的分區已經建立成功 Created a new partition 1 of type 'Linux' and of size 20 GiB. # 輸入“p”,按“Enter”,查看新建分區的詳細信息 Command (m for help): p Disk /dev/vdb: 100 GiB, 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 Disklabel type: dos Disk identifier: 0xb2fef9e3 Device Boot Start End Sectors Size Id Type /dev/vdb1 2048 41943040 41940993 20G 83 Linux # 輸入“w”,按“Enter”,將分區結果寫入分區表中 Command (m for help): w The partition table has been altered. Syncing disks. # 分區創建完成 ----------------------------------40GB---------------------------------------- # 開始創建40GB的分區 [root@AlexWong /]# fdisk /dev/vdb Welcome to fdisk (util-linux 2.32.1). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): n Partition type p primary (1 primary, 0 extended, 3 free) e extended (container for logical partitions) Select (default p): p Partition number (2-4, default 2): 2 First sector (41943041-209715199, default 41945088): # Last sector = 41945088 + 83886080 = 125831168 區間值 Last sector, +sectors or +size{K,M,G,T,P} (41945088-209715199, default 209715199): 125831168 Created a new partition 2 of type 'Linux' and of size 40 GiB. Command (m for help): p Disk /dev/vdb: 100 GiB, 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 Disklabel type: dos Disk identifier: 0xb2fef9e3 Device Boot Start End Sectors Size Id Type /dev/vdb1 2048 41943040 41940993 20G 83 Linux /dev/vdb2 41945088 125831168 83886081 40G 83 Linux Command (m for help): w The partition table has been altered. Syncing disks. ----------------------------------40GB---------------------------------------- # 開始創建最后一個40G分區 [root@AlexWong /]# fdisk /dev/vdb Welcome to fdisk (util-linux 2.32.1). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): n Partition type p primary (2 primary, 0 extended, 2 free) e extended (container for logical partitions) Select (default p): p Partition number (3,4, default 3): 3 # 最后一個硬盤,sector默認enter即可 First sector (41943041-209715199, default 125833216): Last sector, +sectors or +size{K,M,G,T,P} (125833216-209715199, default 209715199): Created a new partition 3 of type 'Linux' and of size 40 GiB. Command (m for help): p Disk /dev/vdb: 100 GiB, 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 Disklabel type: dos Disk identifier: 0xb2fef9e3 Device Boot Start End Sectors Size Id Type /dev/vdb1 2048 41943040 41940993 20G 83 Linux /dev/vdb2 41945088 125831168 83886081 40G 83 Linux /dev/vdb3 125833216 209715199 83881984 40G 83 Linux Command (m for help): w The partition table has been altered. Syncing disks.
# 執行以下命令,將新的分區表變更同步至操作系統。 [root@AlexWong /]# partprobe # 執行以下命令,將新建分區文件系統設為系統所需格式。 # mkfs -t 文件系統格式 /dev/vdb1 [root@AlexWong /]# mkfs -t ext4 /dev/vdb1 mke2fs 1.45.6 (20-Mar-2020) /dev/vdb1 is apparently in use by the system; will not make a filesystem here! # 報錯,系統已占用 [root@AlexWong /]# cat /proc/partitions major minor #blocks name 253 0 41943040 vda 253 1 41942016 vda1 253 16 104857600 vdb 253 17 20970496 vdb1 253 18 41943040 vdb2 253 19 41940992 vdb3 # 上面可以看到dm工具確實在用 [root@AlexWong /]# dmsetup remove_all [root@AlexWong /]# dmsetup status No devices found # 此時對vdb1進行格式化操作 [root@AlexWong /]# mkfs.ext4 /dev/vdb1 [root@AlexWong /]# mkfs.ext4 /dev/vdb2 [root@AlexWong /]# mkfs.ext4 /dev/vdb3 ---------------------------------------------------------- 此時發生了重大事故,我重啟了機器,發現無法登陸。原因就是 我之前已經把/dev/vdb格式化並掛載過。此時再分區才導致如此的問題。 我把/dev/vdb /home ext4 defaults 0 0 寫入了/etc/fstab,重啟后無法SSH登陸。 之后把/dev/vdb重新分區,卻我忘記刪除/etc/fstab的自動掛載。 UUID=f1e9feb2-4a28-4c57-a03b-3a732954b724 / ext4 defaults 1 1 #/dev/vdb /home ext4 defaults 0 0 #/dev/vdb1 /home ext4 defaults 0 0 ---------------------------------------------------------- # 掛載硬盤 [root@AlexWong /]# mount /dev/vdb1 /share [root@AlexWong /]# mount /dev/vdb2 /docker [root@AlexWong /]# mount /dev/vdb3 /project [root@AlexWong /]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 1.9G 0 1.9G 0% /dev tmpfs 1.9G 0 1.9G 0% /dev/shm tmpfs 1.9G 9.0M 1.9G 1% /run tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup /dev/vda1 40G 17G 21G 44% / /dev/vdb1 20G 45M 19G 1% /share /dev/vdb2 40G 49M 38G 1% /docker /dev/vdb3 40G 49M 38G 1% /project ---------------------------------------------------------- # 把掛載的分區寫入/etc/fstab,這樣在開機的時候能自動掛載 # 建議使用UUID的方式掛載 [root@AlexWong /]# blkid /dev/vda1: UUID="f1e9feb2-4a28-4c57-a03b-3a732954b724" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="5d6e4b6b-01" /dev/vdb1: UUID="85c07779-b50b-4397-8b4f-5fc608cd027b" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="b2fef9e3-01" /dev/vdb2: UUID="245b254d-00c4-4666-9adb-a0fffca46a7e" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="b2fef9e3-02" /dev/vdb3: UUID="6420d65e-7946-49ea-b87c-90c25dc790a6" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="b2fef9e3-03" # 0 不檢測硬盤, 根分區是1,數據盤是0 [root@AlexWong /]# echo "UUID=85c07779-b50b-4397-8b4f-5fc608cd027b /share ext4 defaults 0 0" >> /etc/fstab [root@AlexWong /]# echo "UUID=245b254d-00c4-4666-9adb-a0fffca46a7e /docker ext4 defaults 0 0" >> /etc/fstab [root@AlexWong /]# echo "UUID=6420d65e-7946-49ea-b87c-90c25dc790a6 /project ext4 defaults 0 0" >> /etc/fstab [root@AlexWong /]# cat /etc/fstab # /etc/fstab # Created by anaconda on Sat Dec 12 11:50:01 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. # # After editing this file, run 'systemctl daemon-reload' to update systemd # units generated from this file. # UUID=f1e9feb2-4a28-4c57-a03b-3a732954b724 / ext4 defaults 1 1 UUID=85c07779-b50b-4397-8b4f-5fc608cd027b /share ext4 defaults 0 0 UUID=245b254d-00c4-4666-9adb-a0fffca46a7e /docker ext4 defaults 0 0 UUID=6420d65e-7946-49ea-b87c-90c25dc790a6 /project ext4 defaults 0 0 ---------------------------------------------------------- # 發現一個問題,掛載硬盤后,原有文件丟失 [root@AlexWong docker]# ll total 20 drwx------ 2 root root 16384 Jan 5 15:59 lost+found drwxr-xr-x 4 root root 4096 Jan 5 17:54 volumes [root@AlexWong docker]# umount /dev/vdb2 /docker umount: /docker: target is busy. umount: /docker: target is busy. # 被占用 [root@AlexWong docker]# umount -l /dev/vdb2 /docker # 移除掛載后,重新退出文件目錄,再次進入,文件還在
