Guest User

Untitled

a guest
Feb 19th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. Occasionally we will deploy a virtual instance into our KVM
  2. infrastructure and realize after the fact that we need more local disk
  3. space available. This is the process we use to expand the disk image.
  4. This process assumes the following:
  5.  
  6. - You're using legacy disk partitions. The process for LVM is similar
  7. and I will describe that in another post.
  8. - The partition you need to resize is the last partition on the disk.
  9.  
  10. This process will work with either a `qcow2` or `raw` disk image. For
  11. `raw` images you can also run `fdisk` on the host, potentially saving
  12. yourself a reboot, but that's less convenient for `qcow2` format
  13. images.
  14.  
  15. ---
  16.  
  17. We start with a 5.5G root filesystem with 4.4G free:
  18.  
  19. [root@localhost ~]# df -h /
  20. Filesystem Size Used Avail Use% Mounted on
  21. /dev/vda2 5.5G 864M 4.4G 17% /
  22.  
  23. We need to shut down the system to grow the underlying image:
  24.  
  25. [root@localhost ~]# poweroff
  26.  
  27. On the host, we use the `qemu-img resize` command to grow the image.
  28. First we need the path to the underlying disk image:
  29.  
  30. [lars@madhatter blog]$ virsh -c qemu:///system dumpxml lars-test-0 | grep file
  31. <disk type='file' device='disk'>
  32. <source file='/var/lib/libvirt/images/lars-test-0-1.img'/>
  33.  
  34. And now we increase the image size by 10G:
  35.  
  36. [lars@madhatter blog]$ sudo qemu-img resize /var/lib/libvirt/images/lars-test-0.img +10G
  37. Image resized.
  38.  
  39. Now reboot the instance:
  40.  
  41. [lars@madhatter blog]$ virsh -c qemu:///system start lars-test-0
  42.  
  43. And login in on the console:
  44.  
  45. [lars@madhatter blog]$ virsh -c qemu:///system console lars-test-0
  46. Connected to domain lars-test-0
  47. Escape character is ^]
  48.  
  49. Fedora release 17 (Beefy Miracle)
  50. Kernel 3.6.2-4.fc17.x86_64 on an x86_64 (ttyS0)
  51.  
  52. localhost login: root
  53. Password:
  54.  
  55. We're going to use `fdisk` to modify the partition layout. Run
  56. `fdisk` on the system disk:
  57.  
  58. [root@localhost ~]# fdisk /dev/vda
  59. Welcome to fdisk (util-linux 2.21.2).
  60.  
  61. Changes will remain in memory only, until you decide to write them.
  62. Be careful before using the write command.
  63.  
  64. Print out the existing partition table and verify that you really are
  65. going to be modifying the final partition:
  66.  
  67. Command (m for help): p
  68.  
  69. Disk /dev/vda: 19.3 GB, 19327352832 bytes
  70. 16 heads, 63 sectors/track, 37449 cylinders, total 37748736 sectors
  71. Units = sectors of 1 * 512 = 512 bytes
  72. Sector size (logical/physical): 512 bytes / 512 bytes
  73. I/O size (minimum/optimal): 512 bytes / 512 bytes
  74. Disk identifier: 0x00007d9f
  75.  
  76. Device Boot Start End Blocks Id System
  77. /dev/vda1 * 2048 1026047 512000 83 Linux
  78. /dev/vda2 1026048 5154815 2064384 82 Linux swap / Solaris
  79. /dev/vda3 5154816 16777215 5811200 83 Linux
  80.  
  81. Delete and recreate the final partition, in this case `/dev/vda3`...
  82.  
  83. Command (m for help): d
  84. Partition number (1-4): 3
  85. Partition 3 is deleted
  86.  
  87. ...and create a new partition, accepting all the defaults. This will
  88. create a new partition starting int he same place and extending to the
  89. end of the disk:
  90.  
  91. Command (m for help): n
  92. Partition type:
  93. p primary (2 primary, 0 extended, 2 free)
  94. e extended
  95. Select (default p): p
  96. Partition number (1-4, default 3): 3
  97. First sector (5154816-37748735, default 5154816):
  98. Using default value 5154816
  99. Last sector, +sectors or +size{K,M,G} (5154816-37748735, default 37748735):
  100. Using default value 37748735
  101. Partition 3 of type Linux and of size 15.6 GiB is set
  102.  
  103. You can print out the new partition table to see that indeed
  104. `/dev/vda3` is now larger:
  105.  
  106. Command (m for help): p
  107.  
  108. Disk /dev/vda: 19.3 GB, 19327352832 bytes
  109. 16 heads, 63 sectors/track, 37449 cylinders, total 37748736 sectors
  110. Units = sectors of 1 * 512 = 512 bytes
  111. Sector size (logical/physical): 512 bytes / 512 bytes
  112. I/O size (minimum/optimal): 512 bytes / 512 bytes
  113. Disk identifier: 0x00007d9f
  114.  
  115. Device Boot Start End Blocks Id System
  116. /dev/vda1 * 2048 1026047 512000 83 Linux
  117. /dev/vda2 1026048 5154815 2064384 82 Linux swap / Solaris
  118. /dev/vda3 5154816 37748735 16296960 83 Linux
  119.  
  120. Write the changes to disk:
  121.  
  122. Command (m for help): w
  123. The partition table has been altered!
  124.  
  125. Calling ioctl() to re-read partition table.
  126.  
  127. WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
  128. The kernel still uses the old table. The new table will be used at
  129. the next reboot or after you run partprobe(8) or kpartx(8)
  130. Syncing disks.
  131.  
  132. **Note the warning!** The kernel has cached a copy of the old
  133. partition table. We need to reboot the system before our changes are
  134. visible! So we reboot the system:
  135.  
  136. [root@localhost ~]# reboot
  137.  
  138. And log back in. Run `df` to see the current size of the root
  139. filesystem:
  140.  
  141. [root@localhost ~]# df -h /
  142. Filesystem Size Used Avail Use% Mounted on
  143. /dev/vda3 5.5G 864M 4.4G 17% /
  144.  
  145. Now run `resize2fs` to resize the root filesystem so that it expands
  146. to fill our extended `/dev/vda3`:
  147.  
  148. [root@localhost ~]# resize2fs /dev/vda3
  149. resize2fs 1.42.3 (14-May-2012)
  150. Filesystem at /dev/vda3 is mounted on /; on-line resizing required
  151. old_desc_blocks = 1, new_desc_blocks = 1
  152. The filesystem on /dev/vda3 is now 4074240 blocks long.
  153.  
  154. Run `df` again to see that we now have additional space available:
  155.  
  156. [root@localhost ~]# df -h /
  157. Filesystem Size Used Avail Use% Mounted on
  158. /dev/vda3 16G 867M 14G 6% /
  159. [root@localhost ~]#
Add Comment
Please, Sign In to add comment