linux服務器硬盤格式化掛載:
請根據以下步驟對數據盤進行分區以及格式化,並掛載分區使數據盤可用。
1.1 查看數據盤信息
登錄Linux雲服務器後,可以使用“fdisk -l”命令查看數據盤相關信息。
使用“df -h”命令,無法看到未分區和格式化的數據盤。
1.2 數據盤分區
執行以下命令,對數據盤進行分區。
fdisk /dev/xvdb
按照界面的提示,依次輸入“n”(新建分區)、“p”(新建擴展分區)、“1”(使用第1個主分區),兩次回車(使用默認配置),輸入“wq”(保存分區表),開始分區。
這裏是以創建1個分區爲例,開發者也可以根據自己的需求創建多個分區。
1.3 查看新分區
使用“fdisk -l”命令,即可查看到,新的分區xvdb1已經創建完成。
1.4 格式化新分區
在進行分區格式化時,開發者可以自行決定文件系統的格式,如ext2、ext3等。
這裏以“ext3”爲例:
使用下面的命令對新分區進行格式化。
mkfs.ext3 /dev/xvdb1
1.5 掛載新分區
使用命令“mkdir /mydata”創建mydata目錄,再通過“mount /dev/xvdb1 /mydata”命令手動掛載新分區後,用“df -h”命令查看,出現以下信息說明掛載成功,即可以查看到數據盤了。
1.6 添加分區信息
如果希望雲服務器在重啓或開機時能自動掛載數據盤,必須將分區信息添加到/etc/fstab中。如果沒有添加,則雲服務器重啓或開機後,都不能自動掛載數據盤。
使用“echo '/dev/xvdb1 /mydata ext3 defaults 0 0' >> /etc/fstab”命令添加分區信息後,使用“cat /etc/fstab”命令查看,出現以下信息表示添加分區信息成功。
2.騰訊雲服務器硬盤擴充,新空間增加到已有分區空間中:
線上業務服務器1T硬盤,日誌量太大,需要擴充到2T硬盤,關機購買硬盤以後啓動開始在原有盤上面開始擴充:
2.1 [root@VM_0_17_centos ~]# fdisk -l
Disk /dev/vda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x000c7a75
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 104857599 52427776 83 Linux
Disk /dev/vdb: 2147.5 GB, 2147483648000 bytes, 4194304000 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: 0x947d4eff
Device Boot Start End Blocks Id System
/dev/vdb1 2048 2097151999 1048574976 83 Linux
2.2查看原來1T磁盤:
df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 50G 4.1G 43G 9% /
devtmpfs 16G 0 16G 0% /dev
tmpfs 16G 24K 16G 1% /dev/shm
tmpfs 16G 300K 16G 1% /run
tmpfs 16G 0 16G 0% /sys/fs/cgroup
/dev/vdb1 985G 150G 785G 17% /data
tmpfs 3.2G 0 3.2G 0% /run/user/0
2.3卸載原硬盤:
umount /data/
[root@VM_0_17_centos ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 50G 4.1G 43G 9% /
devtmpfs 16G 0 16G 0% /dev
tmpfs 16G 24K 16G 1% /dev/shm
tmpfs 16G 300K 16G 1% /run
tmpfs 16G 0 16G 0% /sys/fs/cgroup
tmpfs 3.2G 0 3.2G 0% /run/user/0
2.4確認卸載:
[root@VM_0_17_centos ~]# fdisk -l
Disk /dev/vda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x000c7a75
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 104857599 52427776 83 Linux
Disk /dev/vdb: 2147.5 GB, 2147483648000 bytes, 4194304000 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: 0x947d4eff
Device Boot Start End Blocks Id System
/dev/vdb1 2048 2097151999 1048574976 83 Linux
2.5 開始格盤:
按照界面的提示,一次輸入”p”(查看已有分區信息), ”d”(刪除需要擴容的分區)、“n”(新建分區)、“p”(新建主分區)、“1”(使用第1個主分區,必須保持與需要擴容的分區一樣),兩次回車(使用默認配置),輸入“w”(保存分區表),開始分區。
這裏是以創建1個分區爲例,用戶也可以根據自己的需求擴容多個分區。
[root@VM_0_17_centos ~]# 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.
Command (m for help): p
Disk /dev/vdb: 2147.5 GB, 2147483648000 bytes, 4194304000 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: 0x947d4eff
Device Boot Start End Blocks Id System
/dev/vdb1 2048 2097151999 1048574976 83 Linux
Command (m for help): d
Selected partition 1
Partition 1 is deleted
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-4194303999, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-4194303999, default 4194303999):
Using default value 4194303999
Partition 1 of type Linux and of size 2 TiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@VM_0_17_centos ~]# fdisk -l
Disk /dev/vda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x000c7a75
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 104857599 52427776 83 Linux
Disk /dev/vdb: 2147.5 GB, 2147483648000 bytes, 4194304000 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: 0x947d4eff
Device Boot Start End Blocks Id System
/dev/vdb1 2048 4194303999 2097150976 83 Linux
2.6確認磁盤,使用“fdisk –l /dev/vdb1”命令,即可查看到,新的分區vdb1已經創建並擴容完成:
[root@VM_0_17_centos ~]# fdisk -l /dev/vdb1
Disk /dev/vdb1: 2147.5 GB, 2147482599424 bytes, 4194301952 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
2.7 再使用e2fsck -f /dev/xvdc1檢查擴容的分區是否ok
[root@VM_0_17_centos /]# e2fsck -f /dev/vd
vda vda1 vdb vdb1
[root@VM_0_17_centos /]# e2fsck -f /dev/vdb1
e2fsck 1.42.9 (28-Dec-2013)
/dev/vdb1 is mounted.
e2fsck: Cannot continue, aborting.
似乎看到一個報錯,不過後面查看擴充成功了,是不是成功了半信半疑的。。
2.8 使用resize2fs /dev/xvdc1命令擴容分區:
resize2fs /dev/vdb
vdb vdb1
[root@VM_0_17_centos /]# resize2fs /dev/vdb1
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vdb1 is mounted on /data; on-line resizing required
old_desc_blocks = 125, new_desc_blocks = 250
The filesystem on /dev/vdb1 is now 524287744 blocks long.
df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 50G 4.1G 43G 9% /
devtmpfs 16G 0 16G 0% /dev
tmpfs 16G 24K 16G 1% /dev/shm
tmpfs 16G 300K 16G 1% /run
tmpfs 16G 0 16G 0% /sys/fs/cgroup
/dev/vdb1 2.0T 150G 1.7T 8% /data
tmpfs 3.2G 0 3.2G 0% /run/user/0
2.9 掛載/data盤,寫入/etc/fstab 不過做擴充時候是自動寫進去的,重啓過正常,數據沒丟。。
cat /etc/fstab
/dev/vda1 / ext3 noatime,acl,user_xattr 1 1
proc /proc proc defaults 0 0
sysfs /sys sysfs noauto 0 0
debugfs /sys/kernel/debug debugfs noauto 0 0
devpts /dev/pts devpts mode=0620,gid=5 0 0
/dev/disk/bd/virtio-diskt1 /data ext4 defaults,nofail 0 1