The question of how to resize a disk partition/file system has come up a number of times on IRC and elsewhere.
A user writing a raspberry pi image to an SD card that is larger than the image and wanting to grow the file system to use the rest of the SSD Creating a live USB system A user who rented a VPS and then paid to grow the disk from 20GB to 80GB but the default install had the swap partition at the end. So, I wrote this section for the handbook that describes how to deal with those issues. -- Allan Jude
Index: disks/chapter.xml =================================================================== --- disks/chapter.xml (revision 43726) +++ disks/chapter.xml (working copy) @@ -252,6 +252,143 @@ <screen>&prompt.root; <userinput>mount /newdisk</userinput></screen> </sect1> + <sect1 xml:id="disks-growing"> + <info> + <title>Resizing and Growing Disks</title> + + <authorgroup> + <author> + <personname> + <firstname>Allan</firstname> + <surname>Jude</surname> + </personname> + <contrib>Originally contributed by </contrib> + </author> + </authorgroup> + </info> + + <indexterm> + <primary>disks</primary> + <secondary>resizing</secondary> + </indexterm> + + <para>This section describes how to resize (grow) an existing disk + in &os;. This procedure is most common with virtual machines, + but can also apply to images written to USB devices or other + cases where the partitions written to a disk do not match the + actual size of the disk.</para> + + <para>Determine the device name of the disk that needs to be + resized by inspecting <filename>/var/run/dmesg.boot</filename>. + In this example, there is only one <acronym>SATA</acronym> disk + in the system, so the drive will appear as + <filename>ada0</filename>.</para> + + <indexterm><primary>partitions</primary></indexterm> + <indexterm> + <primary><command>gpart</command></primary> + </indexterm> + + <para>List the partitions on the disk to determine what needs to + be done to resize the desired partition:</para> + + <screen>&prompt.root; <userinput>gpart show ada0</userinput> +=> 34 83886013 ada0 GPT (48G) [CORRUPT] + 34 128 1 freebsd-boot (64k) + 162 79691648 2 freebsd-ufs (38G) + 79691810 4194236 3 freebsd-swap (2G) + 83886046 1 - free - (512B)</screen> + + <note> + <para>If the disk was formatted with the <link + xlink:href="http://en.wikipedia.org/wiki/GUID_Partition_Table"> + <acronym>GPT</acronym></link> partitioning scheme, it may show + as corrupted because <acronym>GPT</acronym> stores a backup + of the partition table at the end of the drive, which has now + moved.</para> + + <screen>&prompt.root; <userinput>gpart recover ada0</userinput> +ada0 recovered</screen> + </note> + + <para>Now the additional space on the disk is available:</para> + + <screen>&prompt.root; <userinput>gpart show ada0</userinput> +=> 34 102399933 ada0 GPT (48G) + 34 128 1 freebsd-boot (64k) + 162 79691648 2 freebsd-ufs (38G) + 79691810 4194236 3 freebsd-swap (2G) + 83886046 18513921 - free - (8.8G)</screen> + + <para>It is only possible to resize a partition into contiguous + free space, however the last partition on the disk is the swap + partition, rather than the partition to be resized. Since swap + does not contain any non-volatile data, it can safely be deleted + and recreated after.</para> + + <screen>&prompt.root; <userinput>swapoff /dev/ada0p3</userinput> +&prompt.root; <userinput>gpart delete -i 3 ada0</userinput> +ada0p3 deleted +&prompt.root; <userinput>gpart show ada0</userinput> +=> 34 102399933 ada0 GPT (48G) + 34 128 1 freebsd-boot (64k) + 162 79691648 2 freebsd-ufs (38G) + 79691810 22708157 - free - (10G)</screen> + + <para>Next resize the partition, leaving room to recreate a swap + partition of the desired size.</para> + + <note> + <para>There is risk involved with modifying a live file system, + it is best to perform the following steps while running off of + a live CD or USB device. However it can be done on a live + system with the following work around: + + <screen>&prompt.root; <userinput>sysctl kern.geom.debugflags=16</userinput></screen> + </para> + </note> + + <screen>&prompt.root; <userinput>gpart resize -i 2 -s 47G ada0</userinput> +ada0p2 resized +&prompt.root; <userinput>gpart show ada0</userinput> +=> 34 102399933 ada0 GPT (48G) + 34 128 1 freebsd-boot (64k) + 162 98566144 2 freebsd-ufs (47G) + 98566306 3833661 - free - (1.8G)</screen> + + <para>Next, recreate the swap partition:</para> + + <screen>&prompt.root; <userinput>gpart add -t freebsd-swap ada0</userinput> +ada0p3 added +&prompt.root; <userinput>gpart show ada0</userinput> +=> 34 102399933 ada0 GPT (48G) + 34 128 1 freebsd-boot (64k) + 162 98566144 2 freebsd-ufs (47G) + 98566306 3833661 3 freebsd-swap (1.8G) +&prompt.root; <userinput>swapon /dev/ada0p3</userinput></screen> + + <para>Finally, grow the UFS file system on the resized + partition:</para> + + <note> + <para>growing a live UFS file system is only possible in &os; + 10.0-RELEASE and later, for earlier versions, the file system + must not be mounted.</para> + </note> + + <screen>&prompt.root; <userinput>growfs /dev/ada0p2</userinput> +Device is mounted read-write; resizing will result in temporary write suspension for /. +It's strongly recommended to make a backup before growing the file system. +OK to grow filesystem on /dev/ada0p2, mounted on /, from 38GB to 47GB? [Yes/No] <userinput>Yes</userinput> +super-block backups (for fsck -b #) at: + 80781312, 82063552, 83345792, 84628032, 85910272, 87192512, 88474752, + 89756992, 91039232, 92321472, 93603712, 94885952, 96168192, 97450432</screen> + + <para>The partition has now been resized to make use of all + available space on the disk.</para> + + </sect1> + <sect1 xml:id="usb-disks"> <info> <title>USB Storage Devices</title>
signature.asc
Description: OpenPGP digital signature