Ubuntu下如何扩展LVM根目录

服务器   2024年03月06日 21:40  

这篇文章主要介绍“Ubuntu下如何扩展LVM根目录”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Ubuntu下如何扩展LVM根目录”文章能帮助大家解决问题。

调大物理磁盘

虚拟机关机,然后直接在虚拟机管理里面操作。

现在把磁盘从 100g 调整到了 300g

确定要操作的磁盘

先看下磁盘使用情况,运行命令

root@vm003:~#df-hfilesystemsizeusedavailuse%mountedonudev3.9g03.9g0%/devtmpfs798m8.6m789m2%/run/dev/mapper/-root94g88g1.9g98%/tmpfs3.9g03.9g0%/dev/shmtmpfs5.0m05.0m0%/run/locktmpfs3.9g03.9g0%/sys/fs/cgroup/dev/sda1472m382m66m86%/boottmpfs100k0100k0%/run/lxcfs/controllerstmpfs798m0798m0%/run/user/0

虽然我们已经把物理磁盘调整到了 300g,但是根目录还是100g的样子,已用 98%

运行命令

root@vm003:~#fdisk-ldisk/dev/sda:300gib,322122547200bytes,629145600sectorsunits:sectorsof1*512=512bytessectorsize(logical/physical):512bytes/4096bytesi/osize(minimum/optimal):4096bytes/4096bytesdisklabeltype:dosdiskidentifier:0xa88f1366devicebootstartendsectorssizeidtype/dev/sda1*2048999423997376487m83linux/dev/sda2100147020971315120871168299.5g5extended/dev/sda5100147220971315120871168099.5g8elinuxlvmpartition2doesnotstartonphysicalsectorboundary.disk/dev/mapper/ubuntu-root:95.5gib,102563315712bytes,200318976sectorsunits:sectorsof1*512=512bytessectorsize(logical/physical):512bytes/4096bytesi/osize(minimum/optimal):4096bytes/4096bytesdisk/dev/mapper/ubuntu-swap_1:4gib,4294967296bytes,8388608sectorsunits:sectorsof1*512=512bytessectorsize(logical/physical):512bytes/4096bytesi/osize(minimum/optimal):4096bytes/4096bytes

可以看到 /dev/sda 已经确实被调整到了300gib,只是系统还没用到。

也知道了我们要操作 /dev/sda

扩大 逻辑分区所在的物理分区

运行命令

root@vm003:~#parted/dev/sdagnuparted3.2using/dev/sdawelcometognuparted!type'help'toviewalistofcommands.(parted)print#查看分区model:msftvirtualdisk(scsi)disk/dev/sda:322gbsectorsize(logical/physical):512b/4096bpartitiontable:msdosdiskflags:numberstartendsizetypefilesystemflags11049kb512mb511mbprimaryext2boot2513mb107gb107gbextended5513mb107gb107gblogicallvm(parted)resizepart2#调整sda2分区大小end?[107gb]?-0#直接充满(parted)print#再次查看model:msftvirtualdisk(scsi)disk/dev/sda:322gbsectorsize(logical/physical):512b/4096bpartitiontable:msdosdiskflags:numberstartendsizetypefilesystemflags11049kb512mb511mbprimaryext2boot2513mb322gb322gbextended5513mb107gb107gblogicallvm(parted)q#完成退出information:youmayneedtoupdate/etc/fstab.

现在我们已经把 /dev/sda2 给拓展出来了

新增 lvm 逻辑分区

运行命令

root@vm003:~#fdisk/dev/sdawelcometofdisk(util-linux2.27.1).changeswillremaininmemoryonly,untilyoudecidetowritethem.becarefulbeforeusingthewritecommand.command(mforhelp):p#查看现在的分区情况disk/dev/sda:300gib,322122547200bytes,629145600sectorsunits:sectorsof1*512=512bytessectorsize(logical/physical):512bytes/4096bytesi/osize(minimum/optimal):4096bytes/4096bytesdisklabeltype:dosdiskidentifier:0xa88f1366devicebootstartendsectorssizeidtype/dev/sda1*2048999423997376487m83linux/dev/sda21001470629145599628144130299.5g5extended/dev/sda5100147220971315120871168099.5g8elinuxlvmpartition2doesnotstartonphysicalsectorboundary.command(mforhelp):n#新增分区,选择逻辑分区,起止点看情况输入,默认值是填充满整个磁盘allspaceforprimarypartitionsisinuse.addinglogicalpartition6firstsector(209715200-629145599,default209715200):lastsector,+sectorsor+size{k,m,g,t,p}(209715200-629145599,default629145599):createdanewpartition6oftype'linux'andofsize200gib.command(mforhelp):p#查看新增的分区disk/dev/sda:300gib,322122547200bytes,629145600sectorsunits:sectorsof1*512=512bytessectorsize(logical/physical):512bytes/4096bytesi/osize(minimum/optimal):4096bytes/4096bytesdisklabeltype:dosdiskidentifier:0xa88f1366devicebootstartendsectorssizeidtype/dev/sda1*2048999423997376487m83linux/dev/sda21001470629145599628144130299.5g5extended/dev/sda5100147220971315120871168099.5g8elinuxlvm/dev/sda6209715200629145599419430400200g83linuxpartition2doesnotstartonphysicalsectorboundary.command(mforhelp):t#改变分区类型为linuxlvmpartitionnumber(1,2,5,6,default6):6#sda6partitiontype(typeltolistalltypes):8e#lvm类型的id代码changedtypeofpartition'linux'to'linuxlvm'.command(mforhelp):p#再次查看分区情况disk/dev/sda:300gib,322122547200bytes,629145600sectorsunits:sectorsof1*512=512bytessectorsize(logical/physical):512bytes/4096bytesi/osize(minimum/optimal):4096bytes/4096bytesdisklabeltype:dosdiskidentifier:0xa88f1366devicebootstartendsectorssizeidtype/dev/sda1*2048999423997376487m83linux/dev/sda21001470629145599628144130299.5g5extended/dev/sda5100147220971315120871168099.5g8elinuxlvm/dev/sda6209715200629145599419430400200g8elinuxlvmpartition2doesnotstartonphysicalsectorboundary.command(mforhelp):wq#确认没有问题,保存退出thepartitiontablehasbeenaltered.callingioctl()tore-readpartitiontable.re-readingthepartitiontablefailed.:deviceorresourcebusythekernelstillusestheoldtable.thenewtablewillbeusedatthenextrebootorafteryourunpartprobe(8)orkpartx(8).

新分区合并到相应 volumn group

运行命令

root@vm003:~#vgdisplay---volumegroup---vgnameubuntusystemidformatlvm2

可以看出我们要操作的vg name 为 ubuntu,记录下来。

接着运行命令

root@vm003:~#vgextendubuntu/dev/sda6#/dev/sda6是刚刚增加的lvm分区device/dev/sda6notfound(orignoredbyfiltering).unabletoaddphysicalvolume'/dev/sda6'tovolumegroup'ubuntu'.

呃,,,提示没有找到 /dev/sda6 这个设备,还是重启一下好了。

root@vm003:~#reboot

重启后再次执行

root@vm003:~#vgextendubuntu/dev/sda6#/dev/sda6是刚刚增加的lvm分区physicalvolume"/dev/sda6"successfullycreatedvolumegroup"ubuntu"successfullyextended

再查看一下 volumn group 的状态,运行命令

root@vm003:~#vgsvg#pv#lv#snattrvsizevfreeubuntu220wz--n-299.52g200.00g

确实加进去了。

然后运行

root@vm003:~#lvdisplay---logicalvolume---lvpath/dev/ubuntu/rootlvnamerootvgnameubuntu

我们知道了 ubuntu vg 的 lv path 是 /dev/ubuntu/root,记录下来。

然后运行

root@vm003:~#lvresize-l+100%free/dev/ubuntu/root#/dev/ubuntu/root是lvpathsizeoflogicalvolumeubuntu/rootchangedfrom95.52gib(24453extents)to295.52gib(75652extents).logicalvolumerootsuccessfullyresized.

这就成功啦。

警告: 如果操作时出现下面这样的 warning,就说明现在 logic volumn 的总大小还不对,resize 不但不增加空间,反而在缩小空间,如果继续操作下去,必将丢失数据。应立即停止!按 n 取消。

warning: reducing active and open logical volume to 32.00 mibthis may destroy your data (filesystem etc.)do you really want to reduce root? [y/n]*

更新文件系统

最后一步,运行命令

root@vm003:~#resize2fs-p/dev/mapper/ubuntu-root#/dev/mapper/ubuntu-root是从df命令看到的文件系统信息resize2fs1.42.13(17-may-2015)filesystemat/dev/mapper/ubuntu-rootismountedon/;on-lineresizingrequiredold_desc_blocks=6,new_desc_blocks=19thefilesystemon/dev/mapper/ubuntu-rootisnow77467648(4k)blockslong.

这个过程可能会花几分钟时间,耐心等待就好了。

然后运行 df 命令查看磁盘使用

root@vm003:~#df-hfilesystemsizeusedavailuse%mountedonudev3.9g03.9g0%/devtmpfs798m8.6m789m2%/run/dev/mapper/ubuntu-root291g88g191g32%/tmpfs3.9g03.9g0%/dev/shmtmpfs5.0m05.0m0%/run/locktmpfs3.9g03.9g0%/sys/fs/cgroup/dev/sda1472m382m66m86%/boottmpfs100k0100k0%/run/lxcfs/controllerstmpfs798m0798m0%/run/user/0

关于“Ubuntu下如何扩展LVM根目录”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注行业资讯频道,小编每天都会为大家更新不同的知识点。

域名注册
购买VPS主机

您或许对下面这些文章有兴趣:                    本月吐槽辛苦排行榜

看贴要回贴有N种理由!看帖不回贴的后果你懂得的!


评论内容 (*必填):
(Ctrl + Enter提交)   

部落快速搜索栏

各类专题梳理

网站导航栏

X
返回顶部