[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"blog-ubuntu在vm中擴展ubuntu的磁碟空間lvm":3},{"id":4,"slug":5,"title":6,"description":7,"content":8,"date":9,"image":10,"imageAlt":6,"author":11,"categories":14,"path":18,"minRead":19},85,"ubuntu在vm中擴展ubuntu的磁碟空間lvm","[Ubuntu]在VM中擴展Ubuntu的磁碟空間(LVM)","在VM中擴展Ubuntu的硬碟空間(LVM) 手上管理的專案中有幾台服務是建置在VMware上，最近處理一些事 […]","\u003Ch1>在VM中擴展Ubuntu的硬碟空間(LVM)\u003C\u002Fh1>\n\u003Cp>手上管理的專案中有幾台服務是建置在VMware上，最近處理一些事情發現空間不夠用了，於是參考了\u003Ca href=\"https:\u002F\u002Fblog.ndk.name\u002Fextend-vmware-virtual-machine-lvm-disk\u002F\" title=\"這篇\">這篇\u003C\u002Fa>進行擴展磁碟空間。\u003Cbr \u002F>\nUbuntu版本為16.04.5\u003C\u002Fp>\n\u003Ch2>什麼是LVM\u003C\u002Fh2>\n\u003Cp>LVM (Logical Volume Manager)，可以翻譯成邏輯捲軸管理員吧。LVM的機制目的為『可以彈性調整檔案空間的容量』達到幾個特性：容易利用、管理映碟。\u003Cbr \u002F>\n當空間不足時，則需要使用vg和lv來調整空間，調整時不會影響到原先存放的資料&lt;---重要。\u003Cbr \u002F>\n老實說我也是這次要擴展才去看了一下LVM的機制說明XD\u003C\u002Fp>\n\u003Ch2>作法\u003C\u002Fh2>\n\u003Cp>所以預期我需要做幾件事：\u003C\u002Fp>\n\u003Col>\n\u003Cli>先在VM中配置一組新硬碟\u003C\u002Fli>\n\u003Cli>在Ubuntu中設定LVM擴展空間\u003C\u002Fli>\n\u003C\u002Fol>\n\u003Ch2>VM中配置一組新硬碟\u003C\u002Fh2>\n\u003Cp>在VM介面中新增一顆硬碟，設定為100G，原本只有40G\u003Cbr \u002F>\n\u003Cimg decoding=\"async\" src=\"\u002Fapi\u002Fproxy-image?url=https%3A%2F%2Fpost.billxu.net%2Fwp-content%2Fuploads%2F2022%2F06%2F%E6%88%AA%E5%9C%96-2022-06-29-%E4%B8%8B%E5%8D%881.03.53.png\" alt=\"\" \u002F>\u003C\u002Fp>\n\u003Ch2>Ubuntu中設定LVM擴展空間\u003C\u002Fh2>\n\u003Cp>這邊設定就較為繁瑣，當上一個動作做完後，開啟Ubuntu後應該能看到有顆新硬碟\u002Fdev\u002Fsdb，驗證為100G\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-shell\">root@ubuntu:\u002F# ls \u002Fdev\u002Fsd*\n\u002Fdev\u002Fsda  \u002Fdev\u002Fsda1  \u002Fdev\u002Fsda2  \u002Fdev\u002Fsda5  \u002Fdev\u002Fsdb\n\nroot@ubuntu:\u002F# sudo fdisk -l | grep &quot;Disk \u002Fdev&quot;\nDisk \u002Fdev\u002Fsda: 40 GiB, 42949672960 bytes, 83886080 sectors\nDisk \u002Fdev\u002Fsdb: 100 GiB, 107374182400 bytes, 209715200 sectors\nDisk \u002Fdev\u002Fmapper\u002Fubuntu--vg-root: 38.3 GiB, 41150316544 bytes, 80371712 sectors\nDisk \u002Fdev\u002Fmapper\u002Fubuntu--vg-swap_1: 980 MiB, 1027604480 bytes, 2007040 sectors\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>使用fdisk \u002Fdev\u002Fsdb 作partition，注意Command下的指令\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-shell\">root@ubuntu:\u002F# fdisk \u002Fdev\u002Fsdb\n\nWelcome to fdisk (util-linux 2.27.1).\nChanges will remain in memory only, until you decide to write them.\nBe careful before using the write command.\n\nDevice does not contain a recognized partition table.\nCreated a new DOS disklabel with disk identifier 0x96b1b396.\n\nCommand (m for help): p\nDisk \u002Fdev\u002Fsdb: 100 GiB, 107374182400 bytes, 209715200 sectors\nUnits: sectors of 1 * 512 = 512 bytes\nSector size (logical\u002Fphysical): 512 bytes \u002F 512 bytes\nI\u002FO size (minimum\u002Foptimal): 512 bytes \u002F 512 bytes\nDisklabel type: dos\nDisk identifier: 0x96b1b396\n\nCommand (m for help): n\nPartition type\n   p   primary (0 primary, 0 extended, 4 free)\n   e   extended (container for logical partitions)\nSelect (default p): p\nPartition number (1-4, default 1): 1\nFirst sector (2048-209715199, default 2048):\nLast sector, +sectors or +size{K,M,G,T,P} (2048-209715199, default 209715199):\n\nCreated a new partition 1 of type &#039;Linux&#039; and of size 100 GiB.\n\nCommand (m for help): w\nThe partition table has been altered.\nCalling ioctl() to re-read partition table.\nSyncing disks.\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>將這個partition指定為Linux LVM\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-shell\">root@ubuntu:\u002F# fdisk \u002Fdev\u002Fsdb\n\nWelcome to fdisk (util-linux 2.27.1).\nChanges will remain in memory only, until you decide to write them.\nBe careful before using the write command.\n\nCommand (m for help): t\nSelected partition 1\nPartition type (type L to list all types): 8e\nChanged type of partition &#039;Linux&#039; to &#039;Linux LVM&#039;.\n\nCommand (m for help): w\nThe partition table has been altered.\nCalling ioctl() to re-read partition table.\nSyncing disks.\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>成功的話會看到新增一個\u002Fdev\u002Fsdb1\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-shell\">root@ubuntu:\u002F# ls \u002Fdev\u002Fsd*\n\u002Fdev\u002Fsda  \u002Fdev\u002Fsda1  \u002Fdev\u002Fsda2  \u002Fdev\u002Fsda5  \u002Fdev\u002Fsdb  \u002Fdev\u002Fsdb1\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>在剛剛切割好的磁碟\u002Fdev\u002Fsdb1建立physical volume，並且擴展(extend)到原本的volume group到這個volume。\u003Cbr \u002F>\nvolume group的名稱，可通過vgdisplay的命令查到。\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-shell\">root@ubuntu:\u002F# sudo pvcreate \u002Fdev\u002Fsdb1\n  Physical volume &quot;\u002Fdev\u002Fsdb1&quot; successfully created\nroot@ubuntu:\u002F# sudo vgextend  ubuntu-vg \u002Fdev\u002Fsdb1\n  Volume group &quot;ubuntu-vg&quot; successfully extended\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>使用vgdisplay指令可以看volume group資訊，也確認一下剛剛的操作有沒有成功\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-shell\">  Volume group &quot;ubuntu-vg&quot; successfully extended\nroot@ubuntu:\u002F# sudo vgdisplay\n  --- Volume group ---\n  VG Name               ubuntu-vg\n  System ID\n  Format                lvm2\n  Metadata Areas        2\n  Metadata Sequence No  4\n  VG Access             read\u002Fwrite\n  VG Status             resizable\n  MAX LV                0\n  Cur LV                2\n  Open LV               2\n  Max PV                0\n  Cur PV                2\n  Act PV                2\n  VG Size               139.28 GiB\n  PE Size               4.00 MiB\n  Total PE              35655\n  Alloc PE \u002F Size       10056 \u002F 39.28 GiB\n  Free  PE \u002F Size       25599 \u002F 100.00 GiB\n  VG UUID               Lz4ixT-KeGT-kcDb-lf9N-Tykq-LChm-SGwOlG\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>發現原本40G空間變成了139G！\u003C\u002Fp>\n\u003Cp>接著確認目前的磁區與檔案系統，要將新擴展的空間掛載到根目錄\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-shell\">root@ubuntu:\u002F# df -m\nFilesystem                  1M-blocks  Used Available Use% Mounted on\nudev                              980     0       980   0% \u002Fdev\ntmpfs                             201     6       195   3% \u002Frun\n\u002Fdev\u002Fmapper\u002Fubuntu--vg-root     38500 36046       476  99% \u002F\ntmpfs                            1001     0      1001   0% \u002Fdev\u002Fshm\ntmpfs                               5     0         5   0% \u002Frun\u002Flock\ntmpfs                            1001     0      1001   0% \u002Fsys\u002Ffs\u002Fcgroup\n\u002Fdev\u002Fsda1                         720    58       626   9% \u002Fboot\ntmpfs                             201     0       201   0% \u002Frun\u002Fuser\u002F112\ntmpfs                             201     0       201   0% \u002Frun\u002Fuser\u002F0\nroot@ubuntu:\u002F# df\nFilesystem                  1K-blocks     Used Available Use% Mounted on\nudev                          1003008        0   1003008   0% \u002Fdev\ntmpfs                          204816     6016    198800   3% \u002Frun\n\u002Fdev\u002Fmapper\u002Fubuntu--vg-root  39423400 36910900    486824  99% \u002F\ntmpfs                         1024068        0   1024068   0% \u002Fdev\u002Fshm\ntmpfs                            5120        0      5120   0% \u002Frun\u002Flock\ntmpfs                         1024068        0   1024068   0% \u002Fsys\u002Ffs\u002Fcgroup\n\u002Fdev\u002Fsda1                      736752    59236    640092   9% \u002Fboot\ntmpfs                          204816        0    204816   0% \u002Frun\u002Fuser\u002F112\ntmpfs                          204816        0    204816   0% \u002Frun\u002Fuser\u002F0\nroot@ubuntu:\u002F# sudo lvextend \u002Fdev\u002Fmapper\u002Fubuntu--vg-root dev\u002Fsdb1\n  Size of logical volume ubuntu-vg\u002Froot changed from 38.32 GiB (9811 extents) to 138.32 GiB (35410 extents).\n  Logical volume root successfully resized.\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>最後resize 這個空間為ext4，成功的話就能看到根目錄增加了100G\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-shell\">root@ubuntu:\u002F# sudo resize2fs \u002Fdev\u002F\nDisplay all 199 possibilities? (y or n)\nroot@ubuntu:\u002F# sudo resize2fs \u002Fdev\u002Fubuntu-vg\u002F\nroot    swap_1\nroot@ubuntu:\u002F# sudo resize2fs \u002Fdev\u002Fubuntu-vg\u002Froot\nresize2fs 1.42.13 (17-May-2015)\nFilesystem at \u002Fdev\u002Fubuntu-vg\u002Froot is mounted on \u002F; on-line resizing required\nold_desc_blocks = 3, new_desc_blocks = 9\nThe filesystem on \u002Fdev\u002Fubuntu-vg\u002Froot is now 36259840 (4k) blocks long.\n\nroot@ubuntu:\u002F# df -m\nFilesystem                  1M-blocks  Used Available Use% Mounted on\nudev                              980     0       980   0% \u002Fdev\ntmpfs                             201     6       195   3% \u002Frun\n\u002Fdev\u002Fmapper\u002Fubuntu--vg-root    139289 36058     97160  28% \u002F\ntmpfs                            1001     0      1001   0% \u002Fdev\u002Fshm\ntmpfs                               5     0         5   0% \u002Frun\u002Flock\ntmpfs                            1001     0      1001   0% \u002Fsys\u002Ffs\u002Fcgroup\n\u002Fdev\u002Fsda1                         720    58       626   9% \u002Fboot\ntmpfs                             201     0       201   0% \u002Frun\u002Fuser\u002F112\ntmpfs                             201     0       201   0% \u002Frun\u002Fuser\u002F0\u003C\u002Fcode>\u003C\u002Fpre>\n","2022-07-25T10:20:06","",{"name":12,"link":13},"Chih Fan","http:\u002F\u002Fpost.billxu.net",[15],{"name":16,"slug":17},"Devops","devops","\u002Fblog\u002Fubuntu在vm中擴展ubuntu的磁碟空間lvm",4]