Changing the partitioning scheme of a rental VPS

“Is it possible to make an arbitrary disk partitioning scheme on a virtual server?” Such a question once arose in my head. I must say right away - this is a case when there is no access to the hypervisor (otherwise it would be too easy). At first glance, it may seem that the issue is not resolved. In fact, to repartition a partition, you need to unmount it. In the case of home computers, Live {CD, USB} is used for this. But if you have only ssh at your disposal? A couple of seconds to think it over - and I found a solution. It seemed that one can calm down on this: he himself came up with the problem, he himself decided. But then I jokingly asked the same question to a colleague, being sure that he would also have no difficulties in resolving the issue. The results surprised me. Interviewing others, it turned out that all respondents are sure that it is impossible to repartition the disk on VPS. Only one guy went farthest

If you also do not know the solution to this problem or just want to look at an example of implementation, welcome to the article!

Why all this fuss?

By and large, of course, is not required. The server is still working. But, perhaps, everyone has their own vision of the “correct” breakdown, which may not coincide with what it really is.

My virtual server with CentOS 6.6 x86-64 had this scheme:

# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda3       24G  1,7G   21G   8% /
tmpfs           371M     0  371M   0% /dev/shm
/dev/xvda1      194M   29M  156M  16% /boot
# free -m
Swap:          511          0        511

In my philosophy, 200 meters per boot partition is a waste. I usually use 32 MB (for the love of round numbers). This is enough for me to accommodate 4 cores and still remains. Naturally, I would like to use the virtual server space more efficiently - remove unnecessary from / boot, attach to /. And at the same time change the OS. The company that owns the virtual server provides a fixed list of possible operating systems. Unfortunately, it doesn't have my favorite Gentoo in it. But ... Does it really mean that I can’t use it?

The answer to the main question of life, the universe and this article

So how do you repartition the root partition? Especially for those who did not immediately guess, I left the tips above. Yes, that's right - swap will save us.

Here is a general outline for those who need only the principle itself and do not need explanations:
  • get rid of swap - we get a free partition;
  • create FS on it;
  • we lighten the current root partition to the size of the FS on the former swap;
  • copy data to a new partition;
  • transfer the boot partition;
  • we twirl as we wish the freed sections;
  • if necessary, repeat the actions of root transfer until complete satisfaction.

How to do all this?

First, turn off the swap partition and create a file system on it. Then we remove the line about it from / etc / fstab. Now an interesting step is to make the OS easier. It goes without saying that if you already have some data on the server - pick it up. However, this is likely to be not enough. In my case, the xvda2 partition (where swap was) was a little over 0.5 GB, and the OS that I got after creating the server was a little over 1.5 GB. A little missing. To reduce the size, I sawed out all the applications that I knew about, and which did not affect the ability:
  • server downloads;
  • section changes;
  • creating FS;
  • work with the network;
  • SSH connections.

If your server has a pre-installed control panel (in “my” company you can immediately get ISPmanager) - then there will be guaranteed a lot of superfluous for us at the moment. Save the configs if you wish, and then demolish all these mail-web-ftp-dns-other-servers.

But even without a pre-installed body kit for all occasions in the system, there is a lot of extra at the moment. To get a list of all installed programs in Debian-like, do:

# dpkg --list

And in the red-caps family:

# yum list installed

The method is simple: first go through the entire list and delete what you know, then iterate over each item in the list separately and look at its dependencies. Carefully look at the list of applications that will be deleted along with the selected one, and check if their removal will affect any of the items in the list above.

Do not overdo it!
I was so carried away by cleaning the system that I myself did not notice how I was left without any text editor. Since it was too lazy to install them again, all configuration files had to be edited with sed. The sensations were ... interesting.

It’s good if you have the opportunity to create a backup copy of the image of your virtual server at any time and recover from it. Otherwise, be doubly careful.

By the way, there are two more possibilities to slightly correct the place:
- first of all, change the size of the / boot partition to add the freed up space to the swap partition;
- and more extreme cleaning - removal (or better transfer to another machine) of the contents of / usr - all these doc /, man /, time zone files, fonts and more. Be careful three times when performing these actions and do not proceed with them unless absolutely necessary.

But suppose you did manage. Copy the entire contents of the current root to a new location. And don't forget to adjust the values ​​in / etc / fstab and in the bootloader configuration.

You can also transfer the / boot directory to the new partition if you decide to change the size of the boot partition as well (of course, if you have / boot on a separate partition at all).

There were no problems with Grub2. But Grub legacy (aka Grub 0.97) does not suspect that the partition names may be similar to / dev / xvda2. Teach him this. In the file / sbin / grub-install you need to find the line:

        tmp_disk=`echo "$1" | sed -e 's%\([shv]d[a-z]\)[0-9]*$%\1%' \

And lower:

        tmp_part=`echo "$1" | sed -e 's%.*/[shv]d[a-z]\([0-9]*\)$%\1%' \

In my case, these are lines 99 and 105, but I do not exclude that there may be slight differences in different distributions.
Replace this part:




Now with the installation of grub 0.97 there will be no problems.

If there were no errors when uninstalling programs, installing the bootloader and editing configuration files, then, rebooting, you will get a system that works on one small partition. Now it’s enough to delete the old root partition (xvda3 in my case) and create a new one at the end of the free space. Once again, dragging the root there - we get the opportunity to change partitions from the very beginning of the disk.

Since I decided at the same time to change the OS, instead of copying the files of the root partition back and forth, you can immediately create the file system of the future OS in a new place. Fortunately, Gentoo does not use all of these newfangled graphical installers. In order to settle in a new place, it is enough for her to unpack her suitcases (the archive of the third stage is the minimum OS environment), fix several configs and execute several commands. Unfortunately, I do not know if this approach will work with other distributions.

Partition Change

Be careful when working with partitions. The kernel will not know about the changes you have made until you reboot or inform it. You can do this by using the partprobe command from the parted package. But what’s interesting is that if you managed to do everything without rebooting in CentOS 6.6 x32, then using the same version of the OS, but 64-bit, it turned out that partprobe was unable to tell the kernel about the changes in the partition table and had to reboot. Carefully ensure that you always have a loader somewhere and its configuration is correct, taking into account the current layout scheme.

What is the result?

If, when ordering a VPS, you cannot influence the layout, this does not mean that you cannot implement your vision of the “correct” layout on the server. I changed the size of the boot partition, leaving it the necessary minimum and allocating additional MB for more necessary purposes. Made two additional sections. And he changed the OS to the one that I needed , and not choosing from those that offered me. However, if you decide to repeat it - I strongly recommend that you first check with the company that provides you the server - whether the changes you plan will affect its maintenance. Perhaps some program or script on the physical server serving your VPS relies on very specific partition tables.

Also popular now: