How do I resize partitions using command line without using a GUI on a server?











up vote
34
down vote

favorite
18












I only have access to the server via terminal and I can't use graphical tools such as gparted!

I want to create a new partition from a part of root (about 768mb) for swap.



# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda 20G 1.3G 18G 7% /
udev 10M 0 10M 0% /dev
tmpfs 199M 4.9M 194M 3% /run
tmpfs 100M 12K 100M 1% /run/user
tmpfs 5.0M 4.0K 5.0M 1% /run/lock









share|improve this question




























    up vote
    34
    down vote

    favorite
    18












    I only have access to the server via terminal and I can't use graphical tools such as gparted!

    I want to create a new partition from a part of root (about 768mb) for swap.



    # df -h
    Filesystem Size Used Avail Use% Mounted on
    /dev/vda 20G 1.3G 18G 7% /
    udev 10M 0 10M 0% /dev
    tmpfs 199M 4.9M 194M 3% /run
    tmpfs 100M 12K 100M 1% /run/user
    tmpfs 5.0M 4.0K 5.0M 1% /run/lock









    share|improve this question


























      up vote
      34
      down vote

      favorite
      18









      up vote
      34
      down vote

      favorite
      18






      18





      I only have access to the server via terminal and I can't use graphical tools such as gparted!

      I want to create a new partition from a part of root (about 768mb) for swap.



      # df -h
      Filesystem Size Used Avail Use% Mounted on
      /dev/vda 20G 1.3G 18G 7% /
      udev 10M 0 10M 0% /dev
      tmpfs 199M 4.9M 194M 3% /run
      tmpfs 100M 12K 100M 1% /run/user
      tmpfs 5.0M 4.0K 5.0M 1% /run/lock









      share|improve this question















      I only have access to the server via terminal and I can't use graphical tools such as gparted!

      I want to create a new partition from a part of root (about 768mb) for swap.



      # df -h
      Filesystem Size Used Avail Use% Mounted on
      /dev/vda 20G 1.3G 18G 7% /
      udev 10M 0 10M 0% /dev
      tmpfs 199M 4.9M 194M 3% /run
      tmpfs 100M 12K 100M 1% /run/user
      tmpfs 5.0M 4.0K 5.0M 1% /run/lock






      command-line partitioning swap






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 at 9:11

























      asked Dec 14 '13 at 17:06









      Hamid FzM

      3022511




      3022511






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          30
          down vote



          accepted
          +100










          You cannot shrink/edit a partition if any of the partition on the storage device is mounted. So in order to unmount and edit the root filesystem, the OS need to be shutdown. Then boot into a live system and edit the partition as described in other answers.



          Alternative solution : swap file



          As an alternative to creating an entire partition, a swap file offers the ability to vary its size on-the-fly, and is more easily removed altogether. Swap file can be hot plugable. i.e can be added and removed without unmounting/turning off the OS.





          1. Create a 512 MB file called /swapfile. This will be our swap file.



            fallocate -l 512M /swapfile  


            OR



            dd if=/dev/zero of=/swapfile bs=1M count=512



          2. Set the right permissions (because a world-readable swap file is a huge local vulnerability):



            chmod 600 /swapfile



          3. After creating the correctly sized file, format it to swap:



            mkswap /swapfile



          4. Activate the swap file:



            swapon /swapfile



          5. Edit /etc/fstab and add an entry for the swap file:



            /swapfile none swap defaults 0 0



          More details at arch linux wiki.






          share|improve this answer























          • I think this is the most easiest and efficient answer =)! Are there implications in using a file instead of a partition though?
            – Alaa Ali
            Jul 28 '14 at 13:18






          • 2




            Slightly Low Performance if file is fragmented. There is no performance advantage to either a contiguous swap file or a partition, both are treated the same way.
            – totti
            Jul 28 '14 at 13:47






          • 1




            I think this is the best solution. As you are not formatting, shrinking or otherwise changing partition(tables), there is no risk of data-loss.
            – R110
            Jul 31 '14 at 13:14


















          up vote
          25
          down vote













          First of all is important to know that you cannot resize to shrink your root partition if you are using it (This is called online shrinking). You can only grow it online. This is supported by the resize2fs command. I will assume the following:




          • You don't want to loose your information on the root partition.

          • You don't have physical access to the hard drive in order to use a LiveCD. This can apply to a virtual environment or a remote one. In the case of a virtual one you can still manage to boot from a LiveCD if you set the VM to boot from a LiveCD. This is assumming the VM supports outputting the Desktop GUI from where you would run the Gparted app to resize easily. But since this is less likely I assume you can not.


          There are 2 type of partitions that you can resize, the LVM partitions or Logical Volume Manager partitions which support Online resizing (Shrinking/Growing) since the creation of the galaxy and the standard partitions most of us use. Right now the only one that has almost 100% support of complete online resizing (Shrink/Grow) is the btrfs filesystem (Which is still in development). I will explain how to do the normal partitions most of us use in the ext4 filesystem.



          Resizing (Growing) the Partition



          To grow your partition you can do it with the root mounted. To do this simply do:



          sudo resize2fs /dev/sda1


          Provided you already have the empty space ready to be merged. Afterwards I recommend rebooting for the changes to take effect correctly. The command above would resize to the maximum permitted. If you wish to resize to a particular size then simply add the size at the end:



          sudo resize2fs /dev/sda1 25G


          Note that if you want to specify 25.4 GB, you can not use the ".". you would need to go down one unit of measure. In this case from GB to MB, so it would look like this:



          sudo resize2fs /dev/sda1 25400M


          This way you will have a partition of 25.4G



          Resizing (Shrinking) the Partition



          Shrinking the partition is a two step process which involves:




          • Reducing the size of the file system by the amount needed.

          • Reducing the size of the underlying block device to match that of the file system.


          Before reducing the capacity of a file system you need to reduce the size of the block device (Which can be a partition or a logical volume). Since this is not available for any of the ext* file systems you won't be able to shrink it from 20 GB to 19.5 GB to create the 500 MB swap one.



          Even Ext4 does not support online shrinking. If you try to do it you will get the following:



          enter image description here



          Your only bet as far as I know is to either:




          • Install another Ubuntu version on the same server (On another partition) that can then be used to shrink the root partition of the original Ubuntu Server.


          • Install Ubuntu server from scratch with the size you actually want



          • Use the Ubuntu Server Live Image to resize the partition. For this case, you will need to get to this screen:



            enter image description here



            And choose the Resize option as shown in the image above. From there you will select what the new size will be since from here you can unmount the unit and shrink it if you want.




          As an additional help here is the gparted filesystem suppor http://gparted.org/features.php which gives a very detailed list of supported ones and includes if they have full online resizing. Btrfs is amongst them.






          share|improve this answer























          • I'm curious, are there any other filesystems that support online shrinking? zfs or btrfs maybe?
            – Seth
            Aug 2 '14 at 0:25










          • I have added a link at the bottom. The kernel also needs to support this if the filesystem is to resize online correctly.
            – Luis Alvarado
            Aug 2 '14 at 15:48










          • I have ubuntu server on vmware machine and its disk size is 120G. But after use first solution, I get this error: The filesystem is already 27262720 blocks long. Nothing to do!
            – Dr.jacky
            Dec 6 '15 at 12:30






          • 1




            Thank you @Mr.Hyde. There were several issues with Workstation 10 and even 11. I would recommend 12 and for Ubuntu I would also update either to the latest or the new LTS because there were also some issues. In my case I have VMware workstation 12.0.1 and Ubuntu 15.10 64 bit.
            – Luis Alvarado
            Dec 7 '15 at 13:13






          • 1




            From the resize2fs manpage: The resize2fs program does not manipulate the size of partitions.
            – ACK_stoverflow
            Aug 17 '17 at 17:44


















          up vote
          6
          down vote













          The answer depends on whether you can unmount the partition to shrink, or not. In your case, you probably cannot unmount the partition. In Linux (UNIX/MAC OS), mounting a partition refers to using the file system and mapping it to the mount point (in your case /). Unmounting means that you stop using the filesystem, and remove the mapping to the mount point. You cannot unmount the filesystem containing your running OS.



          If the partition can be unmounted



          Lets assume you want to shrink a 200GB ext4 partition on /dev/sda4 mounted to /data. It currently contains music and movies or similar, so you can temporarily unmount it. You want to create a 4GB swap.



          sudo umount /dev/sda4


          to unmount the partition.



          sudo resize2fs /dev/sda4 196G


          to resize the ext4 filesystem to 196 GB, assuming that there is enough space. Now, you have to shrink the partition. I currently belive you need to use cfdisk to delete the existing partition, and recreate a smaller partition in its place. You can then also create a new partition for the swap.



          sudo cfdisk /dev/sda 


          will give you a text-based gui to inspect your partition table. I would recommend you to print the partition table to a file or screen at that point, and take note of the current configuration as backup. You can then select /dev/sda4 and delete the partition. In its place, free space will be displayed. Use new to create a new partition with 196 GB in its place, and set the type to ext4. Then, move to the trailing free space and create the 4GB swap partition with type swap. Note: I did not test these commands, as I can't play around with my / at the moment.



          If the partition cannot be unmounted



          You cannot shrink a mounted ext3/4 partition (see manpage of resize2fs). As you are running your OS from /, you cannot unmount /. That means you have to boot another OS (e.g. from USB key) to do the changes.



          In your case, it is a remote server (on KVM most likely), so you might not be able to boot from USB/ a live OS image. There might be other ways to change the partitioning from your vServer provider through an admin GUI. I believe that is your best bet currently.






          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "89"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f390769%2fhow-do-i-resize-partitions-using-command-line-without-using-a-gui-on-a-server%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            30
            down vote



            accepted
            +100










            You cannot shrink/edit a partition if any of the partition on the storage device is mounted. So in order to unmount and edit the root filesystem, the OS need to be shutdown. Then boot into a live system and edit the partition as described in other answers.



            Alternative solution : swap file



            As an alternative to creating an entire partition, a swap file offers the ability to vary its size on-the-fly, and is more easily removed altogether. Swap file can be hot plugable. i.e can be added and removed without unmounting/turning off the OS.





            1. Create a 512 MB file called /swapfile. This will be our swap file.



              fallocate -l 512M /swapfile  


              OR



              dd if=/dev/zero of=/swapfile bs=1M count=512



            2. Set the right permissions (because a world-readable swap file is a huge local vulnerability):



              chmod 600 /swapfile



            3. After creating the correctly sized file, format it to swap:



              mkswap /swapfile



            4. Activate the swap file:



              swapon /swapfile



            5. Edit /etc/fstab and add an entry for the swap file:



              /swapfile none swap defaults 0 0



            More details at arch linux wiki.






            share|improve this answer























            • I think this is the most easiest and efficient answer =)! Are there implications in using a file instead of a partition though?
              – Alaa Ali
              Jul 28 '14 at 13:18






            • 2




              Slightly Low Performance if file is fragmented. There is no performance advantage to either a contiguous swap file or a partition, both are treated the same way.
              – totti
              Jul 28 '14 at 13:47






            • 1




              I think this is the best solution. As you are not formatting, shrinking or otherwise changing partition(tables), there is no risk of data-loss.
              – R110
              Jul 31 '14 at 13:14















            up vote
            30
            down vote



            accepted
            +100










            You cannot shrink/edit a partition if any of the partition on the storage device is mounted. So in order to unmount and edit the root filesystem, the OS need to be shutdown. Then boot into a live system and edit the partition as described in other answers.



            Alternative solution : swap file



            As an alternative to creating an entire partition, a swap file offers the ability to vary its size on-the-fly, and is more easily removed altogether. Swap file can be hot plugable. i.e can be added and removed without unmounting/turning off the OS.





            1. Create a 512 MB file called /swapfile. This will be our swap file.



              fallocate -l 512M /swapfile  


              OR



              dd if=/dev/zero of=/swapfile bs=1M count=512



            2. Set the right permissions (because a world-readable swap file is a huge local vulnerability):



              chmod 600 /swapfile



            3. After creating the correctly sized file, format it to swap:



              mkswap /swapfile



            4. Activate the swap file:



              swapon /swapfile



            5. Edit /etc/fstab and add an entry for the swap file:



              /swapfile none swap defaults 0 0



            More details at arch linux wiki.






            share|improve this answer























            • I think this is the most easiest and efficient answer =)! Are there implications in using a file instead of a partition though?
              – Alaa Ali
              Jul 28 '14 at 13:18






            • 2




              Slightly Low Performance if file is fragmented. There is no performance advantage to either a contiguous swap file or a partition, both are treated the same way.
              – totti
              Jul 28 '14 at 13:47






            • 1




              I think this is the best solution. As you are not formatting, shrinking or otherwise changing partition(tables), there is no risk of data-loss.
              – R110
              Jul 31 '14 at 13:14













            up vote
            30
            down vote



            accepted
            +100







            up vote
            30
            down vote



            accepted
            +100




            +100




            You cannot shrink/edit a partition if any of the partition on the storage device is mounted. So in order to unmount and edit the root filesystem, the OS need to be shutdown. Then boot into a live system and edit the partition as described in other answers.



            Alternative solution : swap file



            As an alternative to creating an entire partition, a swap file offers the ability to vary its size on-the-fly, and is more easily removed altogether. Swap file can be hot plugable. i.e can be added and removed without unmounting/turning off the OS.





            1. Create a 512 MB file called /swapfile. This will be our swap file.



              fallocate -l 512M /swapfile  


              OR



              dd if=/dev/zero of=/swapfile bs=1M count=512



            2. Set the right permissions (because a world-readable swap file is a huge local vulnerability):



              chmod 600 /swapfile



            3. After creating the correctly sized file, format it to swap:



              mkswap /swapfile



            4. Activate the swap file:



              swapon /swapfile



            5. Edit /etc/fstab and add an entry for the swap file:



              /swapfile none swap defaults 0 0



            More details at arch linux wiki.






            share|improve this answer














            You cannot shrink/edit a partition if any of the partition on the storage device is mounted. So in order to unmount and edit the root filesystem, the OS need to be shutdown. Then boot into a live system and edit the partition as described in other answers.



            Alternative solution : swap file



            As an alternative to creating an entire partition, a swap file offers the ability to vary its size on-the-fly, and is more easily removed altogether. Swap file can be hot plugable. i.e can be added and removed without unmounting/turning off the OS.





            1. Create a 512 MB file called /swapfile. This will be our swap file.



              fallocate -l 512M /swapfile  


              OR



              dd if=/dev/zero of=/swapfile bs=1M count=512



            2. Set the right permissions (because a world-readable swap file is a huge local vulnerability):



              chmod 600 /swapfile



            3. After creating the correctly sized file, format it to swap:



              mkswap /swapfile



            4. Activate the swap file:



              swapon /swapfile



            5. Edit /etc/fstab and add an entry for the swap file:



              /swapfile none swap defaults 0 0



            More details at arch linux wiki.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jul 28 '14 at 13:21









            Alaa Ali

            21.6k96893




            21.6k96893










            answered Jul 28 '14 at 13:08









            totti

            4,87212942




            4,87212942












            • I think this is the most easiest and efficient answer =)! Are there implications in using a file instead of a partition though?
              – Alaa Ali
              Jul 28 '14 at 13:18






            • 2




              Slightly Low Performance if file is fragmented. There is no performance advantage to either a contiguous swap file or a partition, both are treated the same way.
              – totti
              Jul 28 '14 at 13:47






            • 1




              I think this is the best solution. As you are not formatting, shrinking or otherwise changing partition(tables), there is no risk of data-loss.
              – R110
              Jul 31 '14 at 13:14


















            • I think this is the most easiest and efficient answer =)! Are there implications in using a file instead of a partition though?
              – Alaa Ali
              Jul 28 '14 at 13:18






            • 2




              Slightly Low Performance if file is fragmented. There is no performance advantage to either a contiguous swap file or a partition, both are treated the same way.
              – totti
              Jul 28 '14 at 13:47






            • 1




              I think this is the best solution. As you are not formatting, shrinking or otherwise changing partition(tables), there is no risk of data-loss.
              – R110
              Jul 31 '14 at 13:14
















            I think this is the most easiest and efficient answer =)! Are there implications in using a file instead of a partition though?
            – Alaa Ali
            Jul 28 '14 at 13:18




            I think this is the most easiest and efficient answer =)! Are there implications in using a file instead of a partition though?
            – Alaa Ali
            Jul 28 '14 at 13:18




            2




            2




            Slightly Low Performance if file is fragmented. There is no performance advantage to either a contiguous swap file or a partition, both are treated the same way.
            – totti
            Jul 28 '14 at 13:47




            Slightly Low Performance if file is fragmented. There is no performance advantage to either a contiguous swap file or a partition, both are treated the same way.
            – totti
            Jul 28 '14 at 13:47




            1




            1




            I think this is the best solution. As you are not formatting, shrinking or otherwise changing partition(tables), there is no risk of data-loss.
            – R110
            Jul 31 '14 at 13:14




            I think this is the best solution. As you are not formatting, shrinking or otherwise changing partition(tables), there is no risk of data-loss.
            – R110
            Jul 31 '14 at 13:14












            up vote
            25
            down vote













            First of all is important to know that you cannot resize to shrink your root partition if you are using it (This is called online shrinking). You can only grow it online. This is supported by the resize2fs command. I will assume the following:




            • You don't want to loose your information on the root partition.

            • You don't have physical access to the hard drive in order to use a LiveCD. This can apply to a virtual environment or a remote one. In the case of a virtual one you can still manage to boot from a LiveCD if you set the VM to boot from a LiveCD. This is assumming the VM supports outputting the Desktop GUI from where you would run the Gparted app to resize easily. But since this is less likely I assume you can not.


            There are 2 type of partitions that you can resize, the LVM partitions or Logical Volume Manager partitions which support Online resizing (Shrinking/Growing) since the creation of the galaxy and the standard partitions most of us use. Right now the only one that has almost 100% support of complete online resizing (Shrink/Grow) is the btrfs filesystem (Which is still in development). I will explain how to do the normal partitions most of us use in the ext4 filesystem.



            Resizing (Growing) the Partition



            To grow your partition you can do it with the root mounted. To do this simply do:



            sudo resize2fs /dev/sda1


            Provided you already have the empty space ready to be merged. Afterwards I recommend rebooting for the changes to take effect correctly. The command above would resize to the maximum permitted. If you wish to resize to a particular size then simply add the size at the end:



            sudo resize2fs /dev/sda1 25G


            Note that if you want to specify 25.4 GB, you can not use the ".". you would need to go down one unit of measure. In this case from GB to MB, so it would look like this:



            sudo resize2fs /dev/sda1 25400M


            This way you will have a partition of 25.4G



            Resizing (Shrinking) the Partition



            Shrinking the partition is a two step process which involves:




            • Reducing the size of the file system by the amount needed.

            • Reducing the size of the underlying block device to match that of the file system.


            Before reducing the capacity of a file system you need to reduce the size of the block device (Which can be a partition or a logical volume). Since this is not available for any of the ext* file systems you won't be able to shrink it from 20 GB to 19.5 GB to create the 500 MB swap one.



            Even Ext4 does not support online shrinking. If you try to do it you will get the following:



            enter image description here



            Your only bet as far as I know is to either:




            • Install another Ubuntu version on the same server (On another partition) that can then be used to shrink the root partition of the original Ubuntu Server.


            • Install Ubuntu server from scratch with the size you actually want



            • Use the Ubuntu Server Live Image to resize the partition. For this case, you will need to get to this screen:



              enter image description here



              And choose the Resize option as shown in the image above. From there you will select what the new size will be since from here you can unmount the unit and shrink it if you want.




            As an additional help here is the gparted filesystem suppor http://gparted.org/features.php which gives a very detailed list of supported ones and includes if they have full online resizing. Btrfs is amongst them.






            share|improve this answer























            • I'm curious, are there any other filesystems that support online shrinking? zfs or btrfs maybe?
              – Seth
              Aug 2 '14 at 0:25










            • I have added a link at the bottom. The kernel also needs to support this if the filesystem is to resize online correctly.
              – Luis Alvarado
              Aug 2 '14 at 15:48










            • I have ubuntu server on vmware machine and its disk size is 120G. But after use first solution, I get this error: The filesystem is already 27262720 blocks long. Nothing to do!
              – Dr.jacky
              Dec 6 '15 at 12:30






            • 1




              Thank you @Mr.Hyde. There were several issues with Workstation 10 and even 11. I would recommend 12 and for Ubuntu I would also update either to the latest or the new LTS because there were also some issues. In my case I have VMware workstation 12.0.1 and Ubuntu 15.10 64 bit.
              – Luis Alvarado
              Dec 7 '15 at 13:13






            • 1




              From the resize2fs manpage: The resize2fs program does not manipulate the size of partitions.
              – ACK_stoverflow
              Aug 17 '17 at 17:44















            up vote
            25
            down vote













            First of all is important to know that you cannot resize to shrink your root partition if you are using it (This is called online shrinking). You can only grow it online. This is supported by the resize2fs command. I will assume the following:




            • You don't want to loose your information on the root partition.

            • You don't have physical access to the hard drive in order to use a LiveCD. This can apply to a virtual environment or a remote one. In the case of a virtual one you can still manage to boot from a LiveCD if you set the VM to boot from a LiveCD. This is assumming the VM supports outputting the Desktop GUI from where you would run the Gparted app to resize easily. But since this is less likely I assume you can not.


            There are 2 type of partitions that you can resize, the LVM partitions or Logical Volume Manager partitions which support Online resizing (Shrinking/Growing) since the creation of the galaxy and the standard partitions most of us use. Right now the only one that has almost 100% support of complete online resizing (Shrink/Grow) is the btrfs filesystem (Which is still in development). I will explain how to do the normal partitions most of us use in the ext4 filesystem.



            Resizing (Growing) the Partition



            To grow your partition you can do it with the root mounted. To do this simply do:



            sudo resize2fs /dev/sda1


            Provided you already have the empty space ready to be merged. Afterwards I recommend rebooting for the changes to take effect correctly. The command above would resize to the maximum permitted. If you wish to resize to a particular size then simply add the size at the end:



            sudo resize2fs /dev/sda1 25G


            Note that if you want to specify 25.4 GB, you can not use the ".". you would need to go down one unit of measure. In this case from GB to MB, so it would look like this:



            sudo resize2fs /dev/sda1 25400M


            This way you will have a partition of 25.4G



            Resizing (Shrinking) the Partition



            Shrinking the partition is a two step process which involves:




            • Reducing the size of the file system by the amount needed.

            • Reducing the size of the underlying block device to match that of the file system.


            Before reducing the capacity of a file system you need to reduce the size of the block device (Which can be a partition or a logical volume). Since this is not available for any of the ext* file systems you won't be able to shrink it from 20 GB to 19.5 GB to create the 500 MB swap one.



            Even Ext4 does not support online shrinking. If you try to do it you will get the following:



            enter image description here



            Your only bet as far as I know is to either:




            • Install another Ubuntu version on the same server (On another partition) that can then be used to shrink the root partition of the original Ubuntu Server.


            • Install Ubuntu server from scratch with the size you actually want



            • Use the Ubuntu Server Live Image to resize the partition. For this case, you will need to get to this screen:



              enter image description here



              And choose the Resize option as shown in the image above. From there you will select what the new size will be since from here you can unmount the unit and shrink it if you want.




            As an additional help here is the gparted filesystem suppor http://gparted.org/features.php which gives a very detailed list of supported ones and includes if they have full online resizing. Btrfs is amongst them.






            share|improve this answer























            • I'm curious, are there any other filesystems that support online shrinking? zfs or btrfs maybe?
              – Seth
              Aug 2 '14 at 0:25










            • I have added a link at the bottom. The kernel also needs to support this if the filesystem is to resize online correctly.
              – Luis Alvarado
              Aug 2 '14 at 15:48










            • I have ubuntu server on vmware machine and its disk size is 120G. But after use first solution, I get this error: The filesystem is already 27262720 blocks long. Nothing to do!
              – Dr.jacky
              Dec 6 '15 at 12:30






            • 1




              Thank you @Mr.Hyde. There were several issues with Workstation 10 and even 11. I would recommend 12 and for Ubuntu I would also update either to the latest or the new LTS because there were also some issues. In my case I have VMware workstation 12.0.1 and Ubuntu 15.10 64 bit.
              – Luis Alvarado
              Dec 7 '15 at 13:13






            • 1




              From the resize2fs manpage: The resize2fs program does not manipulate the size of partitions.
              – ACK_stoverflow
              Aug 17 '17 at 17:44













            up vote
            25
            down vote










            up vote
            25
            down vote









            First of all is important to know that you cannot resize to shrink your root partition if you are using it (This is called online shrinking). You can only grow it online. This is supported by the resize2fs command. I will assume the following:




            • You don't want to loose your information on the root partition.

            • You don't have physical access to the hard drive in order to use a LiveCD. This can apply to a virtual environment or a remote one. In the case of a virtual one you can still manage to boot from a LiveCD if you set the VM to boot from a LiveCD. This is assumming the VM supports outputting the Desktop GUI from where you would run the Gparted app to resize easily. But since this is less likely I assume you can not.


            There are 2 type of partitions that you can resize, the LVM partitions or Logical Volume Manager partitions which support Online resizing (Shrinking/Growing) since the creation of the galaxy and the standard partitions most of us use. Right now the only one that has almost 100% support of complete online resizing (Shrink/Grow) is the btrfs filesystem (Which is still in development). I will explain how to do the normal partitions most of us use in the ext4 filesystem.



            Resizing (Growing) the Partition



            To grow your partition you can do it with the root mounted. To do this simply do:



            sudo resize2fs /dev/sda1


            Provided you already have the empty space ready to be merged. Afterwards I recommend rebooting for the changes to take effect correctly. The command above would resize to the maximum permitted. If you wish to resize to a particular size then simply add the size at the end:



            sudo resize2fs /dev/sda1 25G


            Note that if you want to specify 25.4 GB, you can not use the ".". you would need to go down one unit of measure. In this case from GB to MB, so it would look like this:



            sudo resize2fs /dev/sda1 25400M


            This way you will have a partition of 25.4G



            Resizing (Shrinking) the Partition



            Shrinking the partition is a two step process which involves:




            • Reducing the size of the file system by the amount needed.

            • Reducing the size of the underlying block device to match that of the file system.


            Before reducing the capacity of a file system you need to reduce the size of the block device (Which can be a partition or a logical volume). Since this is not available for any of the ext* file systems you won't be able to shrink it from 20 GB to 19.5 GB to create the 500 MB swap one.



            Even Ext4 does not support online shrinking. If you try to do it you will get the following:



            enter image description here



            Your only bet as far as I know is to either:




            • Install another Ubuntu version on the same server (On another partition) that can then be used to shrink the root partition of the original Ubuntu Server.


            • Install Ubuntu server from scratch with the size you actually want



            • Use the Ubuntu Server Live Image to resize the partition. For this case, you will need to get to this screen:



              enter image description here



              And choose the Resize option as shown in the image above. From there you will select what the new size will be since from here you can unmount the unit and shrink it if you want.




            As an additional help here is the gparted filesystem suppor http://gparted.org/features.php which gives a very detailed list of supported ones and includes if they have full online resizing. Btrfs is amongst them.






            share|improve this answer














            First of all is important to know that you cannot resize to shrink your root partition if you are using it (This is called online shrinking). You can only grow it online. This is supported by the resize2fs command. I will assume the following:




            • You don't want to loose your information on the root partition.

            • You don't have physical access to the hard drive in order to use a LiveCD. This can apply to a virtual environment or a remote one. In the case of a virtual one you can still manage to boot from a LiveCD if you set the VM to boot from a LiveCD. This is assumming the VM supports outputting the Desktop GUI from where you would run the Gparted app to resize easily. But since this is less likely I assume you can not.


            There are 2 type of partitions that you can resize, the LVM partitions or Logical Volume Manager partitions which support Online resizing (Shrinking/Growing) since the creation of the galaxy and the standard partitions most of us use. Right now the only one that has almost 100% support of complete online resizing (Shrink/Grow) is the btrfs filesystem (Which is still in development). I will explain how to do the normal partitions most of us use in the ext4 filesystem.



            Resizing (Growing) the Partition



            To grow your partition you can do it with the root mounted. To do this simply do:



            sudo resize2fs /dev/sda1


            Provided you already have the empty space ready to be merged. Afterwards I recommend rebooting for the changes to take effect correctly. The command above would resize to the maximum permitted. If you wish to resize to a particular size then simply add the size at the end:



            sudo resize2fs /dev/sda1 25G


            Note that if you want to specify 25.4 GB, you can not use the ".". you would need to go down one unit of measure. In this case from GB to MB, so it would look like this:



            sudo resize2fs /dev/sda1 25400M


            This way you will have a partition of 25.4G



            Resizing (Shrinking) the Partition



            Shrinking the partition is a two step process which involves:




            • Reducing the size of the file system by the amount needed.

            • Reducing the size of the underlying block device to match that of the file system.


            Before reducing the capacity of a file system you need to reduce the size of the block device (Which can be a partition or a logical volume). Since this is not available for any of the ext* file systems you won't be able to shrink it from 20 GB to 19.5 GB to create the 500 MB swap one.



            Even Ext4 does not support online shrinking. If you try to do it you will get the following:



            enter image description here



            Your only bet as far as I know is to either:




            • Install another Ubuntu version on the same server (On another partition) that can then be used to shrink the root partition of the original Ubuntu Server.


            • Install Ubuntu server from scratch with the size you actually want



            • Use the Ubuntu Server Live Image to resize the partition. For this case, you will need to get to this screen:



              enter image description here



              And choose the Resize option as shown in the image above. From there you will select what the new size will be since from here you can unmount the unit and shrink it if you want.




            As an additional help here is the gparted filesystem suppor http://gparted.org/features.php which gives a very detailed list of supported ones and includes if they have full online resizing. Btrfs is amongst them.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 2 '14 at 15:47

























            answered Jul 26 '14 at 2:25









            Luis Alvarado

            143k135482649




            143k135482649












            • I'm curious, are there any other filesystems that support online shrinking? zfs or btrfs maybe?
              – Seth
              Aug 2 '14 at 0:25










            • I have added a link at the bottom. The kernel also needs to support this if the filesystem is to resize online correctly.
              – Luis Alvarado
              Aug 2 '14 at 15:48










            • I have ubuntu server on vmware machine and its disk size is 120G. But after use first solution, I get this error: The filesystem is already 27262720 blocks long. Nothing to do!
              – Dr.jacky
              Dec 6 '15 at 12:30






            • 1




              Thank you @Mr.Hyde. There were several issues with Workstation 10 and even 11. I would recommend 12 and for Ubuntu I would also update either to the latest or the new LTS because there were also some issues. In my case I have VMware workstation 12.0.1 and Ubuntu 15.10 64 bit.
              – Luis Alvarado
              Dec 7 '15 at 13:13






            • 1




              From the resize2fs manpage: The resize2fs program does not manipulate the size of partitions.
              – ACK_stoverflow
              Aug 17 '17 at 17:44


















            • I'm curious, are there any other filesystems that support online shrinking? zfs or btrfs maybe?
              – Seth
              Aug 2 '14 at 0:25










            • I have added a link at the bottom. The kernel also needs to support this if the filesystem is to resize online correctly.
              – Luis Alvarado
              Aug 2 '14 at 15:48










            • I have ubuntu server on vmware machine and its disk size is 120G. But after use first solution, I get this error: The filesystem is already 27262720 blocks long. Nothing to do!
              – Dr.jacky
              Dec 6 '15 at 12:30






            • 1




              Thank you @Mr.Hyde. There were several issues with Workstation 10 and even 11. I would recommend 12 and for Ubuntu I would also update either to the latest or the new LTS because there were also some issues. In my case I have VMware workstation 12.0.1 and Ubuntu 15.10 64 bit.
              – Luis Alvarado
              Dec 7 '15 at 13:13






            • 1




              From the resize2fs manpage: The resize2fs program does not manipulate the size of partitions.
              – ACK_stoverflow
              Aug 17 '17 at 17:44
















            I'm curious, are there any other filesystems that support online shrinking? zfs or btrfs maybe?
            – Seth
            Aug 2 '14 at 0:25




            I'm curious, are there any other filesystems that support online shrinking? zfs or btrfs maybe?
            – Seth
            Aug 2 '14 at 0:25












            I have added a link at the bottom. The kernel also needs to support this if the filesystem is to resize online correctly.
            – Luis Alvarado
            Aug 2 '14 at 15:48




            I have added a link at the bottom. The kernel also needs to support this if the filesystem is to resize online correctly.
            – Luis Alvarado
            Aug 2 '14 at 15:48












            I have ubuntu server on vmware machine and its disk size is 120G. But after use first solution, I get this error: The filesystem is already 27262720 blocks long. Nothing to do!
            – Dr.jacky
            Dec 6 '15 at 12:30




            I have ubuntu server on vmware machine and its disk size is 120G. But after use first solution, I get this error: The filesystem is already 27262720 blocks long. Nothing to do!
            – Dr.jacky
            Dec 6 '15 at 12:30




            1




            1




            Thank you @Mr.Hyde. There were several issues with Workstation 10 and even 11. I would recommend 12 and for Ubuntu I would also update either to the latest or the new LTS because there were also some issues. In my case I have VMware workstation 12.0.1 and Ubuntu 15.10 64 bit.
            – Luis Alvarado
            Dec 7 '15 at 13:13




            Thank you @Mr.Hyde. There were several issues with Workstation 10 and even 11. I would recommend 12 and for Ubuntu I would also update either to the latest or the new LTS because there were also some issues. In my case I have VMware workstation 12.0.1 and Ubuntu 15.10 64 bit.
            – Luis Alvarado
            Dec 7 '15 at 13:13




            1




            1




            From the resize2fs manpage: The resize2fs program does not manipulate the size of partitions.
            – ACK_stoverflow
            Aug 17 '17 at 17:44




            From the resize2fs manpage: The resize2fs program does not manipulate the size of partitions.
            – ACK_stoverflow
            Aug 17 '17 at 17:44










            up vote
            6
            down vote













            The answer depends on whether you can unmount the partition to shrink, or not. In your case, you probably cannot unmount the partition. In Linux (UNIX/MAC OS), mounting a partition refers to using the file system and mapping it to the mount point (in your case /). Unmounting means that you stop using the filesystem, and remove the mapping to the mount point. You cannot unmount the filesystem containing your running OS.



            If the partition can be unmounted



            Lets assume you want to shrink a 200GB ext4 partition on /dev/sda4 mounted to /data. It currently contains music and movies or similar, so you can temporarily unmount it. You want to create a 4GB swap.



            sudo umount /dev/sda4


            to unmount the partition.



            sudo resize2fs /dev/sda4 196G


            to resize the ext4 filesystem to 196 GB, assuming that there is enough space. Now, you have to shrink the partition. I currently belive you need to use cfdisk to delete the existing partition, and recreate a smaller partition in its place. You can then also create a new partition for the swap.



            sudo cfdisk /dev/sda 


            will give you a text-based gui to inspect your partition table. I would recommend you to print the partition table to a file or screen at that point, and take note of the current configuration as backup. You can then select /dev/sda4 and delete the partition. In its place, free space will be displayed. Use new to create a new partition with 196 GB in its place, and set the type to ext4. Then, move to the trailing free space and create the 4GB swap partition with type swap. Note: I did not test these commands, as I can't play around with my / at the moment.



            If the partition cannot be unmounted



            You cannot shrink a mounted ext3/4 partition (see manpage of resize2fs). As you are running your OS from /, you cannot unmount /. That means you have to boot another OS (e.g. from USB key) to do the changes.



            In your case, it is a remote server (on KVM most likely), so you might not be able to boot from USB/ a live OS image. There might be other ways to change the partitioning from your vServer provider through an admin GUI. I believe that is your best bet currently.






            share|improve this answer



























              up vote
              6
              down vote













              The answer depends on whether you can unmount the partition to shrink, or not. In your case, you probably cannot unmount the partition. In Linux (UNIX/MAC OS), mounting a partition refers to using the file system and mapping it to the mount point (in your case /). Unmounting means that you stop using the filesystem, and remove the mapping to the mount point. You cannot unmount the filesystem containing your running OS.



              If the partition can be unmounted



              Lets assume you want to shrink a 200GB ext4 partition on /dev/sda4 mounted to /data. It currently contains music and movies or similar, so you can temporarily unmount it. You want to create a 4GB swap.



              sudo umount /dev/sda4


              to unmount the partition.



              sudo resize2fs /dev/sda4 196G


              to resize the ext4 filesystem to 196 GB, assuming that there is enough space. Now, you have to shrink the partition. I currently belive you need to use cfdisk to delete the existing partition, and recreate a smaller partition in its place. You can then also create a new partition for the swap.



              sudo cfdisk /dev/sda 


              will give you a text-based gui to inspect your partition table. I would recommend you to print the partition table to a file or screen at that point, and take note of the current configuration as backup. You can then select /dev/sda4 and delete the partition. In its place, free space will be displayed. Use new to create a new partition with 196 GB in its place, and set the type to ext4. Then, move to the trailing free space and create the 4GB swap partition with type swap. Note: I did not test these commands, as I can't play around with my / at the moment.



              If the partition cannot be unmounted



              You cannot shrink a mounted ext3/4 partition (see manpage of resize2fs). As you are running your OS from /, you cannot unmount /. That means you have to boot another OS (e.g. from USB key) to do the changes.



              In your case, it is a remote server (on KVM most likely), so you might not be able to boot from USB/ a live OS image. There might be other ways to change the partitioning from your vServer provider through an admin GUI. I believe that is your best bet currently.






              share|improve this answer

























                up vote
                6
                down vote










                up vote
                6
                down vote









                The answer depends on whether you can unmount the partition to shrink, or not. In your case, you probably cannot unmount the partition. In Linux (UNIX/MAC OS), mounting a partition refers to using the file system and mapping it to the mount point (in your case /). Unmounting means that you stop using the filesystem, and remove the mapping to the mount point. You cannot unmount the filesystem containing your running OS.



                If the partition can be unmounted



                Lets assume you want to shrink a 200GB ext4 partition on /dev/sda4 mounted to /data. It currently contains music and movies or similar, so you can temporarily unmount it. You want to create a 4GB swap.



                sudo umount /dev/sda4


                to unmount the partition.



                sudo resize2fs /dev/sda4 196G


                to resize the ext4 filesystem to 196 GB, assuming that there is enough space. Now, you have to shrink the partition. I currently belive you need to use cfdisk to delete the existing partition, and recreate a smaller partition in its place. You can then also create a new partition for the swap.



                sudo cfdisk /dev/sda 


                will give you a text-based gui to inspect your partition table. I would recommend you to print the partition table to a file or screen at that point, and take note of the current configuration as backup. You can then select /dev/sda4 and delete the partition. In its place, free space will be displayed. Use new to create a new partition with 196 GB in its place, and set the type to ext4. Then, move to the trailing free space and create the 4GB swap partition with type swap. Note: I did not test these commands, as I can't play around with my / at the moment.



                If the partition cannot be unmounted



                You cannot shrink a mounted ext3/4 partition (see manpage of resize2fs). As you are running your OS from /, you cannot unmount /. That means you have to boot another OS (e.g. from USB key) to do the changes.



                In your case, it is a remote server (on KVM most likely), so you might not be able to boot from USB/ a live OS image. There might be other ways to change the partitioning from your vServer provider through an admin GUI. I believe that is your best bet currently.






                share|improve this answer














                The answer depends on whether you can unmount the partition to shrink, or not. In your case, you probably cannot unmount the partition. In Linux (UNIX/MAC OS), mounting a partition refers to using the file system and mapping it to the mount point (in your case /). Unmounting means that you stop using the filesystem, and remove the mapping to the mount point. You cannot unmount the filesystem containing your running OS.



                If the partition can be unmounted



                Lets assume you want to shrink a 200GB ext4 partition on /dev/sda4 mounted to /data. It currently contains music and movies or similar, so you can temporarily unmount it. You want to create a 4GB swap.



                sudo umount /dev/sda4


                to unmount the partition.



                sudo resize2fs /dev/sda4 196G


                to resize the ext4 filesystem to 196 GB, assuming that there is enough space. Now, you have to shrink the partition. I currently belive you need to use cfdisk to delete the existing partition, and recreate a smaller partition in its place. You can then also create a new partition for the swap.



                sudo cfdisk /dev/sda 


                will give you a text-based gui to inspect your partition table. I would recommend you to print the partition table to a file or screen at that point, and take note of the current configuration as backup. You can then select /dev/sda4 and delete the partition. In its place, free space will be displayed. Use new to create a new partition with 196 GB in its place, and set the type to ext4. Then, move to the trailing free space and create the 4GB swap partition with type swap. Note: I did not test these commands, as I can't play around with my / at the moment.



                If the partition cannot be unmounted



                You cannot shrink a mounted ext3/4 partition (see manpage of resize2fs). As you are running your OS from /, you cannot unmount /. That means you have to boot another OS (e.g. from USB key) to do the changes.



                In your case, it is a remote server (on KVM most likely), so you might not be able to boot from USB/ a live OS image. There might be other ways to change the partitioning from your vServer provider through an admin GUI. I believe that is your best bet currently.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 6 at 8:47









                Zanna

                49k13123234




                49k13123234










                answered Jul 26 '14 at 1:39









                noleti

                3,5881823




                3,5881823






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f390769%2fhow-do-i-resize-partitions-using-command-line-without-using-a-gui-on-a-server%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Ellipse (mathématiques)

                    Quarter-circle Tiles

                    Mont Emei