{"id":162,"date":"2008-09-03T12:14:19","date_gmt":"2008-09-03T20:14:19","guid":{"rendered":"http:\/\/www.outflux.net\/blog\/?p=162"},"modified":"2008-09-16T12:43:53","modified_gmt":"2008-09-16T20:43:53","slug":"kvm-disk-image-filesystem-growth-notes","status":"publish","type":"post","link":"https:\/\/outflux.net\/blog\/archives\/2008\/09\/03\/kvm-disk-image-filesystem-growth-notes\/","title":{"rendered":"kvm disk image filesystem growth notes"},"content":{"rendered":"<p>Here are my notes on growing a KVM disk image&#8217;s root filesystem.  I had a few 4G partitions that really needed to be bigger.  This shows how to get a report on the sizes of the disk images, convert them to raw, work on the partition tables, grow the root filesystem, and rebuild the swap partition with the original UUID.  With some work, it could probably become fully scripted, but since the partition layout may not always be the same from VM to VM, the &#8220;fdisk&#8221; step needs human interaction to delete and rebuild the partition table.  Note that the method below also maintains the sparseness of the images.<\/p>\n<blockquote>\n<pre><code># Look for files to change\r\nfor i in \/vmware\/*\/*{vmdk,qcow2}; do qemu-img info $i; done\r\n...\r\n\r\n# Pick one...\r\ncd dir...\r\nORIG=64bit-Ubuntu-7.10-desktop.vmdk\r\nSIZE=8G\r\n\r\n\r\nORIG_TYPE=$(echo $ORIG | awk -F. '{print $NF}')\r\nTARGET_TYPE=&quot;qcow2&quot;\r\nTARGET_BASE=$(basename &quot;$ORIG&quot; .&quot;$ORIG_TYPE&quot;)\r\nTARGET_RAW=&quot;$TARGET_BASE&quot;.raw\r\nTARGET=&quot;$TARGET_BASE&quot;.&quot;$TARGET_TYPE&quot;\r\n\r\nqemu-img convert -f &quot;$ORIG_TYPE&quot; &quot;$ORIG&quot; -O raw &quot;$TARGET_RAW&quot;\r\n\r\ntrunc &quot;$TARGET_RAW&quot; &quot;$SIZE&quot;\r\n\r\nsudo kpartx -a &quot;$TARGET_RAW&quot;\r\nSWAP_PART=$(for i in \/dev\/mapper\/loop0p*; do sudo vol_id $i | \\\r\n    grep -q ^ID_FS_TYPE=swap &amp;&amp; echo $i; done | head -n 1)\r\nUUID=$(sudo vol_id &quot;$SWAP_PART&quot; | grep ^ID_FS_UUID= | cut -d= -f2)\r\nsudo kpartx -d &quot;$TARGET_RAW&quot;\r\n\r\n# use losetup otherwise fdisk doesn't know cylinder count\r\nsudo losetup \/dev\/loop0 &quot;$TARGET_RAW&quot;\r\n# FIXME: Need to automate fdisk (detect swap partition size, etc)\r\n# I'm deleting the swap and growing the root partition, then re-adding swap\r\nsudo fdisk \/dev\/loop0\r\nsudo losetup -d \/dev\/loop0\r\n\r\nsudo kpartx -a &quot;$TARGET_RAW&quot;\r\nsudo e2fsck -f \/dev\/mapper\/loop0p1\r\nsudo resize2fs \/dev\/mapper\/loop0p1\r\nsudo mkswap -U &quot;$UUID&quot; &quot;$SWAP_PART&quot;\r\nsudo kpartx -d &quot;$TARGET_RAW&quot;\r\n\r\nqemu-img convert -f raw &quot;$TARGET_RAW&quot; -O &quot;$TARGET_TYPE&quot; &quot;$TARGET&quot;\r\nrm &quot;$TARGET_RAW&quot;\r\n# FIXME: change disk image path\r\nsudo vi \/etc\/libvirt\/qemu\/THING\r\n# FIXME: have the daemon notice the file change\r\nsudo \/etc\/init.d\/libvirt-bin restart\r\nif [ &quot;$ORIG&quot; != &quot;$TARGET&quot; ]; then rm &quot;$ORIG&quot;; fi\r\n<\/code><\/pre>\n<\/blockquote>\n<p>The &#8220;trunc&#8221; command above is based on my <a href=\"\/blog\/archives\/2006\/07\/29\/encrypted-network-filesystems\/\">network backups<\/a> post, but is now a general script I use:<\/p>\n<blockquote>\n<pre><code>#!\/usr\/bin\/perl\r\n# Copyright (C) 2006-2008 Kees Cook &lt;kees@outflux.net&gt;, License: GPLv3\r\nuse strict;\r\nuse warnings;\r\n\r\nmy $filename = $ARGV[0];\r\ndie &quot;Need valid size also\\n&quot; unless ($ARGV[1] =~ \/^(\\d+)([KMG])$\/);\r\nmy $size       = $1 + 0;\r\nmy $multiplier = $2;\r\n\r\n$size *= 1024 if $multiplier =~ \/^[KMG]$\/;\r\n$size *= 1024 if $multiplier =~ \/^[MG]$\/;\r\n$size *= 1024 if $multiplier =~ \/^[G]$\/;\r\n\r\n#die &quot;Not trunc'ing existing file\\n&quot; if (-e $filename);\r\ndie &quot;$filename: $!\\n&quot; if (!open(FILE,&quot;&gt;&gt;$filename&quot;));\r\ndie &quot;seek: $!\\n&quot; if (!(seek(FILE,$size,0)));\r\ndie &quot;truncate: $!\\n&quot; if (!(truncate(FILE,$size)));\r\ndie &quot;close: $!\\n&quot; if (!(close(FILE)));\r\n<\/code><\/pre>\n<\/blockquote>\n<p style='text-align:left'>&copy; 2008, <a href=\"https:\/\/outflux.net\/blog\/\">Kees Cook<\/a>. This work is licensed under a <a rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/4.0\/\">Creative Commons Attribution-ShareAlike 4.0 License<\/a>.<br \/><a rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/4.0\/\"><img decoding=\"async\" alt=\"CC BY-SA 4.0\" style=\"border-width:0\" src=\"https:\/\/i.creativecommons.org\/l\/by-sa\/4.0\/88x31.png\" \/><\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here are my notes on growing a KVM disk image&#8217;s root filesystem. I had a few 4G partitions that really needed to be bigger. This shows how to get a report on the sizes of the disk images, convert them to raw, work on the partition tables, grow the root filesystem, and rebuild the swap [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,18,14,19],"tags":[],"_links":{"self":[{"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/posts\/162"}],"collection":[{"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/comments?post=162"}],"version-history":[{"count":0,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/posts\/162\/revisions"}],"wp:attachment":[{"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/media?parent=162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/categories?post=162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/tags?post=162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}