Linux/Ubuntu服务器数据盘挂载及扩容

本文详细介绍了如何在阿里云服务器上进行数据盘的扩容操作,包括分区、格式化、挂载及文件系统调整等步骤,适用于Linux环境下的ext4和xfs文件系统。
服务器部署:
运行 fdisk -l 命令查看实例上的数据盘:    fdisk -l
查看磁盘使用情况:    df    -lh
 
① 分区:
    依次执行以下命令以创建一个单分区数据盘:
    1. 运行 fdisk -u /dev/vdb:分区数据盘。
    2. 输入 p:查看数据盘的分区情况。本示例中,数据盘没有分区。
    3. 输入 n:创建一个新分区。
    4. 输入 p:选择分区类型为主分区。
        说明 本示例中创建一个单分区数据盘,所以只需要创建主分区。如果要创建 4 个以上分区,您应该创建至少一个扩展分区,即选择 e(extended)。
    5. 输入分区编号并按回车键。本示例中,仅创建一个分区,输入 1。
    6. 输入第一个可用的扇区编号:按回车键采用默认值 2048。
    7. 输入最后一个扇区编号:本示例仅创建一个分区,按回车键采用默认值。
    8. 输入 p:查看该数据盘的规划分区情况。
    9. 输入 w:开始分区,并在分区后退出。
② 运行命令 fdisk -lu /dev/vdb 查看新分区。
③ 写系统文件:
    mkfs.ext4 /dev/vdb1
④ 挂载:
    1.(建议)运行命令 cp /etc/fstab /etc/fstab.bak 备份 etc/fstab。
    2. 运行命令 echo /dev/vdb1 /mnt ext4 defaults 0 0 >> /etc/fstab 向 /etc/fstab 写入新分区信息。
⑤ 运行命令 cat /etc/fstab 查看 /etc/fstab 中的新分区信息。
⑥ 运行命令 mount /dev/vdb1 /mnt 挂载文件系统。
⑦ 运行命令 df -h 查看目前磁盘空间和使用情况。
在 mnt 下可新建目录进行使用。
 
对已有的进行扩展容量 参考:
 
​扩展已有MBR分区:
首先在阿里服务器添加数据盘
① 修改分区表:
    1. 运行以下命令查看分区信息,并记录旧分区的起始和结束的扇区位置。    
fdisk -lu /dev/vdb
    本示例中,分区 /dev/vdb1 的起始扇区是2048,结束扇区是41943039。
[root@ecshost ~]# fdisk -lu /dev/vdb
Disk /dev/vdb: 42.9 GB, 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
Disk label type: dos
Disk identifier: 0x9277b47b
 
Device Boot Start End Blocks Id System
/dev/vdb1 2048 41943039 20970496 83 Linux
    2. 查看数据盘的挂载路径,根据返回的文件路径卸载分区,直至完全卸载已挂载的分区。
[root@ecshost ~]# mount | grep "/dev/vdb"
/dev/vdb1 on /mnt type ext4 (rw,relatime,data=ordered)
[root@ecshost ~]# umount /dev/vdb1
[root@ecshost ~]# mount | grep "/dev/vdb"
    3.  使用 fdisk工具删除旧分区。*   删除旧分区会一同删除分区内的数据。如有重要数据请备份,避免因删除旧分区而造成数据丢失。
        1. 运行 fdisk -u /dev/vdb:分区数据盘。
        2. 输入 p:打印分区表。
        3. 输入 d:删除分区。
        4. 输入 p:确认分区已删除。
        5. 输入 w:保存修改并退出。
[root@ecshost ~]# fdisk -u /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: 42.9 GB, 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
Disk label type: dos
Disk identifier: 0x9277b47b
Device Boot Start End Blocks Id System
/dev/vdb1 2048 41943039 20970496 83 Linux
Command (m for help): d
Selected partition 1Partition 1 is deleted
Command (m for help): p
Disk /dev/vdb: 42.9 GB, 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
Disk label type: dos
Disk identifier: 0x9277b47b
Device Boot Start End Blocks Id System
Command (m for help): w
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.
    4.  使用fdisk命令新建分区。
        1. 运行 fdisk -u /dev/vdb:分区数据盘。
        2. 输入 p:打印分区表。
        3. 输入 n:新建分区。
        4. 输入 p:选择分区类型为主分区。
        5. 输入<分区号>:选择分区号。本示例选取了 1
        * 警告 新分区的起始位置必须和旧分区的起始位置相同,结束位置必须大于旧分区的结束位置,否则会导致扩容失败。
        6. 输入 w:保存修改并退出。
       本示例中,将 /dev/vdb1 由20GiB扩容到40GiB。
[root@ecshost ~]# fdisk -u /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: 42.9 GB, 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
Disk label type: dos
Disk identifier: 0x9277b47b
Device Boot Start End Blocks Id System
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-83886079, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-83886079, default 83886079):
Partition 1 of type Linux and of size 30 GiB is set
Command (m for help): p
Disk /dev/vdb: 42.9 GB, 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
Disk label type: dos
Disk identifier: 0x9277b47b
Device Boot Start End Blocks Id System
/dev/vdb1 2048 62916607 31457280 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
        7 . 运行 lsblk /dev/vdb 确保分区表已经增加。
        8. 运行 e2fsck -n /dev/vdb1 再次检查文件系统,确认扩容分区后的文件系统状态为clean。
② 扩容文件系统。
    1. ext*文件系统(例如ext3和ext4):依次运行以下命令调整ext*文件系统大小并重新挂载分区。
root@iZj6c3drthhdo9qs7guv0xZ:~# e2fsck -f /dev/vdb1
e2fsck 1.42.9 (4-Feb-2014)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vdb1: 5901/19660800 files (84.0% non-contiguous), 74693043/78642944 blocks​
[root@ecshost ~]# resize2fs /dev/vdb1
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vdb1 to 7864320 (4k) blocks.
The filesystem on /dev/vdb1 is now 7864320 blocks long.
[root@ecshost ~]# mount /dev/vdb1 /mnt
     
    2.  xfs文件系统:依次运行以下命令先重新挂载分区,再调整xfs文件系统大小。
[root@ecshost ~]# mount /dev/vdb1 /mnt/
[root@ecshost ~]# xfs_growfs /dev/vdb1meta-data=/dev/vdb1 isize=512 agcount=4, agsize=1310720 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=5242880, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 5242880 to 7864320
<think>嗯,用户需要Ubuntu系统盘扩容的方法。参考了三个引用资料,其中引用1提到扩容前的准备、添加空间、分区格式化、挂载等步骤;引用2详细描述了VMware虚拟机环境下的操作,包括使用gparted工具调整分区;引用3则显示VirtualBox扩容后系统识别但未应用的问题。 用户明确询问的是"系统盘"扩容而非添加新硬盘,这很关键。结合引用内容,需要区分两种情况:虚拟机环境(如VMware/VirtualBox)和物理机环境。虚拟机通常需要先扩展虚拟磁盘容量再处理系统分区,物理机则可能涉及硬件更换或外接存储。 注意到引用3中的典型问题:虚拟磁盘扩容后操作系统未识别新空间。解决方案应包含分区工具(如fdisk/gparted)的使用和文件系统扩展步骤。尤其要强调LVM分区和非LVM分区的不同处理方式,因为引用1提到了LVM相关内容。 用户可能是系统管理员或开发者,操作涉及高风险命令。回复需包含明确警告(备份数据)和可选方案(使用gparted图形工具更安全)。步骤上要突出: - 虚拟机扩容前的快照创建 - lsblk/fdisk确认磁盘状态 - 分区表调整的关键命令(如parted的resizepart) - 文件系统扩展命令(resize2fs/xfs_growfs) - 最后用df验证 考虑到用户可能遇到引用3的情况,专门列出"常见问题"章节说明分区号错误识别问题。相关拓展问题设计也应围绕操作风险(分区恢复)、特定环境(LVM)和自动化方案(cloud-init),形成知识闭环。 </think>以下是Ubuntu系统盘扩容的详细步骤(以虚拟机环境为例),涵盖物理磁盘扩展文件系统扩容全流程: ```mermaid graph TD A[开始扩容] --> B[虚拟机磁盘扩展] B --> C[启动Ubuntu系统] C --> D{分区类型检查} D -->|LVM| E[扩容物理卷] D -->|非LVM| F[扩容基础分区] E --> G[扩容逻辑卷] G --> H[扩展文件系统] F --> H H --> I[验证扩容结果] ``` ### 一、虚拟机磁盘扩展(以VMware为例) 1. **关闭Ubuntu虚拟机** 2. 在VMware设置中扩展磁盘容量(如从25GB→80GB) 3. 启动虚拟机进入系统 ### 二、系统内扩容操作 #### ▶ 步骤1:确认磁盘状态 ```bash $ lsblk # 查看未分配空间 sda 8:0 0 80G 0 disk ├─sda1 8:1 0 1M 0 part └─sda2 8:2 0 25G 0 part / # 注意此处容量未变 $ sudo fdisk -l /dev/sda Disk /dev/sda: 80 GiB, 85899345920 bytes # 确认磁盘总容量已扩大 ``` [^3] #### ▶ 步骤2:扩展分区(非LVM方案) 1. 安装分区工具: ```bash $ sudo apt install cloud-guest-utils -y # 包含growpart工具 ``` 2. 扩展分区号(此处sda2为例): ```bash $ sudo growpart /dev/sda 2 # 扩展第2个分区 ``` 3. 刷新文件系统: ```bash $ sudo resize2fs /dev/sda2 # ext4文件系统 # 若为xfs: sudo xfs_growfs / ``` #### ▶ LVM方案扩容流程 ```bash $ sudo pvresize /dev/sda3 # 扩展物理卷 $ sudo lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv # 扩展逻辑卷 $ sudo resize2fs /dev/ubuntu-vg/ubuntu-lv # 调整文件系统 ``` ### 三、验证结果 ```bash $ df -h | grep /dev/sda2 /dev/sda2 79G 12G 64G 16% / # 确认容量已更新 ``` ### 📌 关键注意事项 1. **操作前必须备份数据**(可使用`sudo dd if=/dev/sda of=/backup/disk.img bs=4M`) 2. 确保磁盘为MBR或GPT分区表(使用`sudo parted /dev/sda print`查看) 3. 若使用图形工具: ```bash $ sudo apt install gparted $ sudo gparted # 可视化调整分区 ``` ### 常见问题解决 > **现象**:`growpart`报错"partition not found" > **原因**:分区号识别错误(如实际为sda3但误输sda2) > **方案**:用`lsblk -f`确认正确分区号 ### 相关扩展问题 1. 如何给Ubuntu添加新硬盘并自动挂载? 2. LVM管理相比传统分区有哪些优势? 3. 物理服务器如何无损扩容系统盘? 4. 扩容过程中断电如何处理? 5. XFS文件系统扩容有何特殊要求? > 注:云服务器扩容通常需先在云平台扩展磁盘,再执行上述系统内操作。AWS/Azure等平台还需注意[虚拟化驱动兼容性](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/recognize-expanded-volume-linux.html)[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Darryl_Tang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值