{"id":325,"date":"2017-12-06T11:16:00","date_gmt":"2017-12-06T10:16:00","guid":{"rendered":"http:\/\/owncloud.gonscak.sk\/?p=325"},"modified":"2017-12-06T11:16:00","modified_gmt":"2017-12-06T10:16:00","slug":"how-to-resize-physical-volume-and-shrink-disk-partition","status":"publish","type":"post","link":"https:\/\/www.gonscak.sk\/?p=325","title":{"rendered":"How to resize Physical volume and shrink disk partition"},"content":{"rendered":"<p>I Installed proxmox environment on Intel 240GB SSD. Installation take the whole disk for lvm. So I need to reduce the used space and create a new partition for drbd.<br \/>\nThis is my disk. You can see, that the disk is full allocated with 171G free.<\/p>\n<pre>root@pve1:\/# gdisk -l \/dev\/sda\nDisk \/dev\/sda: 468862128 sectors, 223.6 GiB\nNumber Start (sector) End (sector) Size Code Name\n 1      34          2047        1007.0  KiB EF02\n 2      2048        262143      127.0   MiB EF00\n 3      262144      111411199   53.0    GiB 8E00 Linux LVM<\/pre>\n<pre>root@pve1:\/# pvs\n PV VG Fmt Attr PSize PFree\n \/dev\/sda3 pve lvm2 a-- 223.44g 171.44g<\/pre>\n<pre>root@pve1:\/# vgs\n VG #PV #LV #SN Attr VSize VFree\n pve 1 3 0 wz--n- 223.44g 171.44g<\/pre>\n<pre>root@pve1:\/# lvs\n LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert\n data pve -wi-ao--- 40.00g\n root pve -wi-ao--- 10.00g\n swap pve -wi-ao--- 2.00g<\/pre>\n<p>So, we list our logical volumes with segments on physical volume \/dev\/sda3:<\/p>\n<pre>root@pve1:\/# pvs -v --segments \/dev\/sda3\n Using physical volume(s) on command line\n PV VG Fmt Attr PSize PFree Start SSize LV Start Type PE Ranges\n \/dev\/sda3 pve lvm2 a-- 223.44g 171.44g 0 512 swap 0 linear \/dev\/sda3:0-511\n \/dev\/sda3 pve lvm2 a-- 223.44g 171.44g 512 2560 root 0 linear \/dev\/sda3:512-3071\n \/dev\/sda3 pve lvm2 a-- 223.44g 171.44g 3072 10240 data 0 linear \/dev\/sda3:3072-13311\n \/dev\/sda3 pve lvm2 a-- 223.44g 171.44g 13312 43889 0 free<\/pre>\n<p>We can see, the size of PV is 223,44G and we have free 171,44G. So, we must shrink this physical volume about 171,44G. So compute the space for size of this physical volume: 223,44 &#8211; 171,44 = 52G. So, our PV must have at least 52G. Next, we resize this pv:<\/p>\n<pre>root@pve1:\/# pvresize --setphysicalvolumesize 52G \/dev\/sda3\n \/dev\/sda3: cannot resize to 13311 extents as 13312 are allocated.\n 0 physical volume(s) resized \/ 1 physical volume(s) not resized\nroot@pve1:\/# pvresize --setphysicalvolumesize 52.1G \/dev\/sda3\n Physical volume \"\/dev\/sda3\" changed\n 1 physical volume(s) resized \/ 0 physical volume(s) not resized<\/pre>\n<p>As we can see, we cannost shrink exact to this space. So we add 100M and use the 52,1G size. Now we can see:<\/p>\n<pre>root@pve1:\/# pvs -v --segments \/dev\/sda3\n Using physical volume(s) on command line\n PV VG Fmt Attr PSize PFree Start SSize LV Start Type PE Ranges\n \/dev\/sda3 pve lvm2 a-- 52.10g 100.00m 0 512 swap 0 linear \/dev\/sda3:0-511\n \/dev\/sda3 pve lvm2 a-- 52.10g 100.00m 512 2560 root 0 linear \/dev\/sda3:512-3071\n \/dev\/sda3 pve lvm2 a-- 52.10g 100.00m 3072 10240 data 0 linear \/dev\/sda3:3072-13311\n \/dev\/sda3 pve lvm2 a-- 52.10g 100.00m 13312 25 0 free<\/pre>\n<p>At this point, we must work on the lowest layer of disk, so we must delete this partition and create a new one. The new partition must start on the same sector as previous and the last sector must be after last segment of physical volume. I use gdisk, because my disk have GPT partition table:<\/p>\n<pre>root@pve1:\/# gdisk \/dev\/sda\nCommand (? for help): <span style=\"color: #ff0000;\"><strong>p<\/strong><\/span>\nDisk \/dev\/sda: 468862128 sectors, 223.6 GiB\nLogical sector size: 512 bytes\nFirst usable sector is 34, last usable sector is 468862094\nNumber Start (sector) End (sector) Size Code Name\n 1 34 2047 1007.0 KiB EF02\n 2 2048 262143 127.0 MiB EF00\n 3 262144 468862094 223.4 GiB 8E00\nCommand (? for help): <span style=\"color: #ff0000;\"><strong>d<\/strong><\/span>\nPartition number (1-3): <span style=\"color: #ff0000;\"><strong>3<\/strong><\/span>\nCommand (? for help): <span style=\"color: #ff0000;\"><strong>n<\/strong><\/span>\nPartition number (3-128, default 3):\nFirst sector (262144-468862094, default = 262144) or {+-}size{KMGTP}:\nLast sector (262144-468862094, default = 468862094) or {+-}size{KMGTP}: <span style=\"color: #ff0000;\"><strong>+53G<\/strong><\/span>\nCurrent type is 'Linux filesystem'\nHex code or GUID (L to show codes, Enter = 8300): <span style=\"color: #ff0000;\"><strong>8E00<\/strong><\/span>\nChanged type of partition to 'Linux LVM'\nCommand (? for help): <span style=\"color: #ff0000;\"><strong>p<\/strong><\/span>\nDisk \/dev\/sda: 468862128 sectors, 223.6 GiB\nTotal free space is 357450895 sectors (170.4 GiB)\nNumber Start (sector) End (sector) Size         Code Name\n 1     34             2047         1007.0 KiB   EF02\n 2     2048           262143       127.0 MiB    EF00\n 3     262144         111411199    53.0 GiB     8E00 Linux LVM\nCommand (? for help): <span style=\"color: #ff0000;\"><strong>w<\/strong><\/span>\nFinal checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING\nPARTITIONS!!\nDo you want to proceed? (Y\/N): <span style=\"color: #ff0000;\"><strong>y<\/strong><\/span>\nOK; writing new GUID partition table (GPT) to \/dev\/sda.\nWarning: The kernel is still using the old partition table.\nThe new table will be used at the next reboot.\nThe operation has completed successfully.<\/pre>\n<p>Now, we must reboot our computer to use new partition table. And after reboot, use this command to resize physical volume on partition \/dev\/sda3<\/p>\n<pre>root@pve1:\/# pvresize \/dev\/sda3\n Physical volume \"\/dev\/sda3\" changed\n 1 physical volume(s) resized \/ 0 physical volume(s) not resized\nroot@pve1:\/# pvs\n PV VG Fmt Attr PSize PFree\n \/dev\/sda3 pve lvm2 a-- 53.00g 1020.00m<\/pre>\n<p>Now, if we can use all of free space for the logical volume &#8220;data&#8221;, we can resize it to whole free space, like this:<\/p>\n<pre>root@pve1:\/# lvresize \/dev\/pve\/data -l +100%FREE\n Extending logical volume data to 41.00 GiB\n Logical volume data successfully resized<\/pre>\n<pre> root@pve1:\/# lvs\n LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert\n data pve -wi-ao--- 41.00g\n root pve -wi-ao--- 10.00g\n swap pve -wi-ao--- 2.00g<\/pre>\n<pre>root@pve1:\/# pvs\n PV VG Fmt Attr PSize PFree\n \/dev\/sda3 pve lvm2 a-- 53.00g 0<\/pre>\n<p>Now, we can create a new partition at the end of disk:<\/p>\n<pre>gdisk \/dev\/sda\nCommand (? for help): <span style=\"color: #ff0000;\"><strong>p<\/strong><\/span>\nDisk \/dev\/sda: 468862128 sectors, 223.6 GiB\nLogical sector size: 512 bytes\nNumber Start (sector) End (sector) Size Code Name\n 1 34 2047 1007.0 KiB EF02\n 2 2048 262143 127.0 MiB EF00\n 3 262144 111411199 53.0 GiB 8E00 Linux LVM\nCommand (? for help): <span style=\"color: #ff0000;\"><strong>n<\/strong><\/span>\nPartition number (4-128, default 4):\nFirst sector (111411200-468862094, default = 111411200) or {+-}size{KMGTP}:\nLast sector (111411200-468862094, default = 468862094) or {+-}size{KMGTP}:\nCurrent type is 'Linux filesystem'\nHex code or GUID (L to show codes, Enter = 8300):\nChanged type of partition to 'Linux filesystem'\nCommand (? for help): <strong><span style=\"color: #ff0000;\">w<\/span><\/strong>\nFinal checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING\nPARTITIONS!!\nDo you want to proceed? (Y\/N): <span style=\"color: #ff0000;\"><strong>y<\/strong><\/span>\nOK; writing new GUID partition table (GPT) to \/dev\/sda.\nWarning: The kernel is still using the old partition table.\nThe new table will be used at the next reboot.\nThe operation has completed successfully.<\/pre>\n<pre>root@pve1:~# gdisk -l \/dev\/sda\nNumber Start (sector) End (sector) Size         Code Name\n 1     34             2047         1007.0 KiB   EF02\n 2     2048           262143       127.0 MiB    EF00\n 3     262144         111411199    53.0 GiB     8E00 Linux LVM\n 4     111411200      468862094    170.4 GiB    8300 Linux filesystem<\/pre>\n<p>And if we list details about physical volume, we can see, that there is no free space:<\/p>\n<pre>root@pve1:~# pvs -v --segments \/dev\/sda3\n Using physical volume(s) on command line\n PV VG Fmt Attr PSize PFree Start SSize LV Start Type PE Ranges\n \/dev\/sda3 pve lvm2 a-- 53.00g 0 0 512 swap 0 linear \/dev\/sda3:0-511\n \/dev\/sda3 pve lvm2 a-- 53.00g 0 512 2560 root 0 linear \/dev\/sda3:512-3071\n \/dev\/sda3 pve lvm2 a-- 53.00g 0 3072 10495 data 0 linear \/dev\/sda3:3072-13566<\/pre>\n<p>And what is drbd, you can see in another post on my page. Have a fun.<\/p>\n ","protected":false},"excerpt":{"rendered":"<p>I Installed proxmox environment on Intel 240GB SSD. Installation take the whole disk for lvm. So I need to reduce the used space and create a new partition for drbd. This is my disk. You can see, that the disk is full allocated with 171G free. root@pve1:\/# gdisk -l \/dev\/sda Disk \/dev\/sda: 468862128 sectors, 223.6 &hellip; <a href=\"https:\/\/www.gonscak.sk\/?p=325\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">How to resize Physical volume and shrink disk partition<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[16,80,81,51,82,83,84,85],"class_list":["post-325","post","type-post","status-publish","format-standard","hentry","category-centos","tag-disk","tag-fdisk","tag-gdisk","tag-lvm","tag-partition","tag-physical-volume","tag-pvdisplay","tag-pvm"],"_links":{"self":[{"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=\/wp\/v2\/posts\/325","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=325"}],"version-history":[{"count":0,"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=\/wp\/v2\/posts\/325\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}