📜 ⬆️ ⬇️

Remote reinstallation of linux using a Debian example

There are many features that simplify the life of a system administrator. Various remote control systems allow you to install the OS on a pristine-clean server and today this is not a problem at all. Even the presence of a second hard drive makes the problem of reinstalling the operating system into a commonplace. Therefore, in this article we will consider the most severe of cases: let there be only one completely broken hard drive on the server, and our task is to remotely reinstall the OS.

Raise our eyebrows, marveling at how flexible our favorite OS is, roll up our sleeves and get down to business.

The basis of this method is the idea that we can use the SWAP-section to install a temporary operating system, and when it gets to its feet - to do the main one. The main thing and the only thing we need is a swap of at least 420 MB in size (this is exactly how much lenny takes with everything needed).

The following assumptions are used in the article:
- you have debian, and you plan to install debian,
- grub installed in the MBR is used.

A brief plan of our work will look like this:

  1. convert swap partition to ext3;
  2. install a clean OS on it;
  3. reboot into it;
  4. make the necessary changes on the main section;
  5. copy the clean OS from the temporary to the main section;
  6. boot from the main partition, turn on swap.

Preparation section.

The first thing we will do is see if we have this swap section at all:

 # free -m ... Swap: 470 0 470 

As you can see - there is, and the size (displayed in MB) fully meets the requirements. It remains to find out how we have a disk:

 # fdisk -l /dev/sda ... Device Boot Start End Blocks Id System /dev/sda1 * 1 462 3710983+ 83 Linux /dev/sda2 463 522 481950 5 Extended /dev/sda5 463 522 481918+ 82 Linux swap / Solaris 

It can be seen that on sda1 - the current OS, on sda5 - swap. Confusing is difficult, but anything can happen.

Turn off swap:

 # swapoff -a 

You can make sure that we turned it off by doing the same free:

 # free ... Swap: 0 0 0 

Update our partition table:

 # fdisk /dev/sda Command (m for help): t Partition number (1-5): 5 Hex code (type L to list codes): 83 Changed system type of partition 5 to 83 (Linux) Command (m for help): w The partition table has been altered! WARNING: Re-reading the partition table failed with error 16:    . The kernel still uses the old table. The new table will be used at the next reboot. Syncing disks. 

We were carefully told that the kernel will not see the changes before the reboot, but we don’t need it yet. Now is the time to prepare the file system on our old new section. For example, ext3:

 # mke2fs -j /dev/sda5 Writing inode tables: done Creating journal (4096 blocks): done 

We will mount the section somewhere and on this its preparation will be completed.

 # mkdir /mnt/temp # mount /dev/sda5 /mnt/temp # df -h      %   /dev/sda1 3,5G 600M 2,8G 18% / ... /dev/sda5 456M 11M 422M 3% /mnt/temp 

Installing a "temporary" OS.

It should be noted here that it is temporary only in its location. In order not to chase away all the actions to build a clean system in the future, we simply copy it to the main section.

We use a great tool for getting a minimal installation - debootstrap. Hereinafter, we will assume that there are no problems with the Internet on the server (otherwise, what kind of server is it after this?), So we’ll download everything from the repository.

 # aptitude install debootstrap ...   debootstrap (1.0.10lenny1) ... 

It is enough to transfer four parameters to this utility: the desired architecture, the release name, the installation directory and a link to the complete mirror. The architecture can be identified by the output `uname -a`, the distribution kit is chosen to your taste, the directory in this case is where we mounted our section, and the link to the mirror can be found here: http://www.debian.org/mirror/list . It turns out that the startup line looks like this:

 # debootstrap --arch i386 lenny /mnt/temp http://ftp.ru.debian.org/debian/ 

After pressing enter, the process of downloading and installing packages will begin, with a fairly average Internet connection (~ 10 Mbit) it takes about 5-10 minutes - I did not even have time to finish my tea. At the end you will see a message that the system has been successfully installed:

 I: Base system installed successfully. 

Now begins one of the most crucial procedures: we need to properly configure the new system. Any sys-admin probably had nightmares about how he loses remote control over the machine, and in the end we started all this in order not to go to the data center. Therefore, we postpone a cup of coffee and concentrate.

First, copy all the important settings. Probably everyone will find quite important files that are not where they should be. I, for example, have a certain /etc/rc.routes with all non-standard routes. The main thing is not to forget anything. It seems to me that it’s absolutely pointless to give here a list, but for me it looks like this:

 # cp /etc/{resolv.conf,hosts,rc.local} /mnt/temp/etc # cp /etc/network/interfaces /mnt/temp/etc/network # cp /etc/your-stuff /mnt/temp/etc 

In fstab, we write the most necessary, proc and our root partition:

 # cat > /mnt/temp/etc/fstab << "#EOF" > proc /proc proc defaults 0 0 > /dev/sda5 / ext3 errors=remount-ro 0 1 > #EOF 

Now we will mount the dev-environment, go to the front of our temporary system and immediately mount the proc:

 # mount --bind /dev /mnt/temp/dev # chroot /mnt/temp /bin/bash # mount -t proc proc /proc 

Obviously, we will need a package manager, I think it’s not necessary to talk about setting it up separately.

 # wget http://debian.soar.name/sources.list -O /etc/apt/sources.list # aptitude update 

Set the time zone:

 # dpkg-reconfigure tzdata 

We also need the following packages:

 # aptitude install locales # dpkg-reconfigure locales # aptitude install console-data # aptitude install ssh # aptitude install sudo 

Immediately, in order not to forget, we will create a user and assign a password to it, otherwise we will not be allowed into ssh later:

 # adduser --ingroup users soar # visudo 

Now reinstall the bootloader. First you need to create all bootloader files on the new disk:

 # aptitude install grub # grub-install /dev/sda 

After that, you need to initialize the MBR to boot from our new section. To do this, everything is in the same place, in the thread, enter the hornbeam console and write the following:

 # grub grub> root (hd0,<TAB> Possible partitions are: Partition num: 0, Filesystem type is ext2fs, partition type 0x83 Partition num: 4, Filesystem type is ext2fs, partition type 0x83 

A peculiar autocomplete on taboo will tell us which sections are at our disposal. As you can see everything is one less than the name in the system. Initialize the download from our sda5:

 grub> root (hd0,4) Filesystem type is ext2fs, partition type 0x83 grub> setup (hd0) ... Done. grub> quit 

The loader is installed where necessary. Now select and install the appropriate kernel:

 # aptitude search linux-image # aptitude install linux-image-2.6.26-2-686 

During the installation, we will be asked to "Create a symbolic link to the current kernel image?", To which we will answer in the affirmative. They will also inform that we are installing a kernel that requires initrd support from the boot loader, and will clarify whether we have changed our mind.
We answer "No" and the installation ends. It remains to update the bootloader menu:

 # update-grub Found kernel: /boot/vmlinuz-2.6.26-2-686 Updating /boot/grub/menu.lst ... done 

We leave the trunk, collect nerves in a fist and send the server to the first reboot:

 # exit # reboot 

If everything was done correctly, the machine will reboot into our temporary system. We can log into ssh as the user we just added.

Transferring the system to the main section.

Format and mount our old section:

 # mke2fs -j /dev/sda1 Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done # mkdir /mnt/temp # mount /dev/sda1 /mnt/temp 

By the way, at this stage you can also perform disk maintenance: for example, repartition the primary partition and check the file system.

It remains to copy our clean OS to the main section:

 # cp -a -x / /mnt/temp/ 

Update fstab. This time let's get it right:

 # cat > /mnt/temp/etc/fstab << "#EOF" > # /etc/fstab: static file system information. > # > # <file system> <mount point> <type> <options> <dump> <pass> > proc /proc proc defaults 0 0 > /dev/sda1 / ext3 defaults,errors=remount-ro 0 0 > #EOF 

Once again, you need to update the hornbeam. This time - to download already from our main section:

 # mount --bind /dev /mnt/temp/dev # chroot /mnt/temp/ /bin/bash # grub-install /dev/sda # grub grub> root (hd0,0) Filesystem type is ext2fs, partition type 0x83 grub> setup (hd0) Checking if "/boot/grub/stage1" exists... yes Checking if "/boot/grub/stage2" exists... yes Checking if "/boot/grub/e2fs_stage1_5" exists... yes Running "embed /boot/grub/e2fs_stage1_5 (hd0)"... 17 sectors are embedded. succeeded Running "install /boot/grub/stage1 (hd0) (hd0)1+17 p (hd0,0)/boot/grub/stage2 /boot/grub/menu.lst"... succeeded Done. grub> quit # update-grub Updating /boot/grub/menu.lst ... done 

It is worth noting that in this place, unlike the first case, I refused to update menu.lst, and there were still links to sda5. Why this is happening - I never figured it out, so I manually edited this file:

 # sed -i -e 's/sda5/sda1/g' /boot/grub/menu.lst # sed -i -e 's/(hd0,4)/(hd0,0)/g' /boot/grub/menu.lst 

Exit the thread and do the second reboot:

 # exit # reboot 

After the reboot, you can make sure that we are again in our native main partition:

 # df -h      %   /dev/sda1 3,5G 436M 2,9G 13% / 

It remains to create and enable swap:

 # fdisk /dev/sda Command (m for help): t Partition number (1-5): 5 Hex code (type L to list codes): 82 Changed system type of partition 5 to 82 (Linux swap / Solaris) ... The new table will be used at the next reboot. Syncing disks. # cat >> /etc/fstab << "#EOF" > /dev/sda5 none swap sw 0 0 > #EOF # mkswap /dev/sda5 # swapon -a 

Make sure everything is fine:

 # free -m Swap: 470 0 470 

Well, in the end, if you edited / boot / grub/menu.lst hand to hand - you should still run the script to update it again:

 # update-grub Updating /boot/grub/menu.lst ... done 

Summing up.

This method is certainly not the easiest way to reinstall the system, however, in many cases, it becomes the only possible one. Moreover, the whole procedure takes about 25 minutes, so if your DC is not across the road, then in any case, you get quite a good time saving. It turned out to be surprisingly many letters - I tried to paint in detail and clearly, but in fact the operation is simple and fast enough.

Source: https://habr.com/ru/post/101702/

All Articles