侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

VMware下ubuntu扩展磁盘空间的方法

linux  /  管理员 发布于 7年前   223

近日由于虚拟机下安装软件过多,时不时弹出磁盘空间不足的问题。查找了很多资料,都没有很好的解决办法。

朋友发来一个链接,还是老外有良心。翻译出来放在这里,根据我的实际需求进行了操作,达到了预期目的。

以防万一,在操作之前将虚拟机进行了备份。

         由于安装时没有自己进行分区,磁盘大小也使用了默认的20G。后来发现果然悲剧。

使用df -h命令查看具体使用情况

@ubuntu:~$ df -h Filesystem   Size Used Avail Use% Mounted on /dev/sda1    19G  17G 1020M 95% / none      4.0K   0 4.0K  0% /sys/fs/cgroup udev      745M 4.0K 745M  1% /dev tmpfs      151M 1.3M 150M  1% /run none      5.0M   0 5.0M  0% /run/lock none      755M 220K 755M  1% /run/shm none      100M  48K 100M  1% /run/user 

使用root权限 fdisk -l查看

在虚拟机的设置――>硬盘――>扩展 选项,选择需要扩展到的容量,这里我是从20G扩展到40G。

设置完成后发现依然无法使用,而是在扩展分区里。

Disk /dev/sda: 42.9 GB, 42949672960 bytes 255 heads, 63 sectors/track, 5221 cylinders, total 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 identifier: 0x000ea50f   Device Boot   Start     End   Blocks  Id System /dev/sda1  *    2048  39845887  19921920  83 Linux /dev/sda2    39847934  41940991   1046529  5 Extended /dev/sda5    39847936  41940991   1046528  82 Linux swap / Solaris 

具体思路是将分区先删除,然后再重新进行分区。注意,这里使用fdisk -l查看的信息建议记录下来,后面进行重新分区时会使用到。

使用free -m命令查看

bay@ubuntu:~$ free -m        total    used    free   shared  buffers   cached Mem:     1508    1065    443     5     74    419 -/+ buffers/cache:    572    936 Swap:     1021     0    1021 

我们可以看到交换分区的大小,先将交换分区关掉使用命令

root@ubuntu:# swapoff -a root@ubuntu:# free -m        total    used    free   shared  buffers   cached Mem:     1508    1075    433     5     73    419 -/+ buffers/cache:    581    926 Swap:   0     0     0 

这时候可以看到swap的分区大小已经为0了。

接下来的步骤就是删除掉  /dev/sda1  和 /dev/sda2。使用fdisk /dev/sda删除分区

root@ubuntu:# fdisk /dev/sda Command (m for help): p Disk /dev/sda: 42.9 GB, 42949672960 bytes 255 heads, 63 sectors/track, 5221 cylinders, total 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 identifier: 0x000ea50f   Device Boot   Start     End   Blocks  Id System /dev/sda1  *    2048  39845887  19921920  83 Linux /dev/sda2    39847934  41940991   1046529  5 Extended /dev/sda5    39847936  41940991   1046528  82 Linux swap / Solaris Command (m for help): d Partition number (1-5): 1 Command (m for help): d Partition number (1-5): 2 Command (m for help): p Disk /dev/sda: 42.9 GB, 42949672960 bytes 255 heads, 63 sectors/track, 5221 cylinders, total 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 identifier: 0x000ea50f   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): 82869552 

注意这是时候可能会有人疑惑,这个大小该如何设置。

这个大小是总的大小减去Swap的大小:83886080 - 1046528 = 82839552。这里的1046528 就是Swap的大小。

接着将分区的事情做完:

Command (m for help): n Partition type:   p  primary (1 primary, 0 extended, 3 free)   e  extended Select (default p): p Partition number (1-4, default 2): 2 First sector (82839553-83886079, default 82839553):  Using default value 82839553 Last sector, +sectors or +size{K,M,G} (82839553-83886079, default 83886079):  Using default value 83886079 Command (m for help): p Disk /dev/sda: 42.9 GB, 42949672960 bytes 255 heads, 63 sectors/track, 5221 cylinders, total 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 identifier: 0x000ea50f   Device Boot   Start     End   Blocks  Id System /dev/sda1      2048  82839552  41418752+ 83 Linux /dev/sda2    82839553  83886079   523263+ 83 Linux 

这个时候分区大小是正确的了,但是ID还是同一个值。在这里我们将交换分区的ID修改为82.

Command (m for help): t Partition number (1-4): 2 Hex code (type L to list codes): 82 Changed system type of partition 2 to 82 (Linux swap / Solaris) 

使用w进行保存

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. 

将虚拟机进行重启。

交换分区使用UUID进行挂载。在创建新的分区之后,UUID将会不匹配,在reboot之后将没有交换分区可以使用。这里有两种方式进行处理

1)在 /etc/fstab 中使用新的UUID

2)在新的分区上使用旧的UUID。这里使用第二种方法。

查看UUID:

root@ubuntu:/home/bay# awk '/swap/ { print $1 }' /etc/fstab # UUID=990ae69c-2bbe-425f-bb8f-91200f70c9b9 [html] view plain copyroot@ubuntu:# swapoff -a root@ubuntu:# free -m        total    used    free   shared  buffers   cached Mem:     1508    1171    337     6    191    424 -/+ buffers/cache:    555    953 Swap:      0     0     0 root@ubuntu:# dd if=/dev/zero of=/dev/sda2 dd: writing to ‘/dev/sda2': No space left on device 1046528+0 records in 1046527+0 records out 535821824 bytes (536 MB) copied, 1.89975 s, 282 MB/s root@ubuntu:# mkswap -U 990ae69c-2bbe-425f-bb8f-91200f70c9b9 /dev/sda2 Setting up swapspace version 1, size = 523256 KiB no label, UUID=990ae69c-2bbe-425f-bb8f-91200f70c9b9 root@ubuntu:# swapon -a root@ubuntu:# free -m        total    used    free   shared  buffers   cached Mem:     1508    717    790     6     6    164 -/+ buffers/cache:    546    961 Swap:     510     0    510 root@ubuntu:# df -h Filesystem   Size Used Avail Use% Mounted on /dev/sda1    19G  17G 1023M 95% / none      4.0K   0 4.0K  0% /sys/fs/cgroup udev      745M  12K 745M  1% /dev tmpfs      151M 1.3M 150M  1% /run none      5.0M   0 5.0M  0% /run/lock none      755M 152K 755M  1% /run/shm none      100M  48K 100M  1% /run/user root@ubuntu:# resize2fs /dev/sda1 resize2fs 1.42.9 (4-Feb-2014) Filesystem at /dev/sda1 is mounted on /; on-line resizing required old_desc_blocks = 2, new_desc_blocks = 3 The filesystem on /dev/sda1 is now 10354688 blocks long. root@ubuntu:# df -h Filesystem   Size Used Avail Use% Mounted on /dev/sda1    39G  17G  21G 45% / none      4.0K   0 4.0K  0% /sys/fs/cgroup udev      745M  12K 745M  1% /dev tmpfs      151M 1.3M 150M  1% /run none      5.0M   0 5.0M  0% /run/lock none      755M 152K 755M  1% /run/shm none      100M  48K 100M  1% /run/user 

dd命令仅仅是保证在分区开始的地方没有数据。

以上所述是小编给大家介绍的VMware下ubuntu扩展磁盘空间的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对站的支持!


  • 上一条:
    在ubuntu下为nginx配置支持cgi脚本的方案
    下一条:
    解决Ubuntu 16.04下提示boot分区空间不足的办法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在Linux系统中使用Iptables实现流量转发功能流程步骤(0个评论)
    • vim学习笔记-入门级需要了解的一些快捷键(0个评论)
    • 在centos7系统中实现分区并格式化挂载一块硬盘到/data目录流程步骤(0个评论)
    • 在Linux系统种查看某一个进程所占用的内存命令(0个评论)
    • Linux中grep命令中的10种高级用法浅析(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-11
    • 2017-07
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-03
    • 2022-04
    • 2022-08
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-06
    • 2023-07
    • 2023-10
    • 2023-12
    • 2024-01
    • 2024-04
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客