📜 ⬆️ ⬇️

Installing Debian on a physically accessible system without a monitor and keyboard

I had only a laptop on my hands for a long time and I recently got hold of a system unit. Without a monitor and keyboard, in other words, a headless system. Thus the problem arose :

Install it on a physically accessible Debian machine, without having the ability to connect a monitor or keyboard to it, but with some set of software and hardware resources.

Namely:
  1. the target machine with the POST order already set up in the BIOS Setup "NO ERROR" or "ALL, NO KEYBOARD" (otherwise, the procedure will not even start downloading at least something *) and loading set up first from one sata device, then from another (the remaining details of the boot order were not important)
  2. a laptop
  3. local network (in my case, a wi-fi router with a wired connection to the target machine and a wireless one with a laptop. But the details are not fundamental)
  4. Internet access (inexpensive and preferably fast)
  5. Half-collapsible collapsible external hard disk 2.5 "with internal sata-interface and external usb

The task can be considered completed if Debian is installed on the internal hard disk of the target machine, and the last one with power on will be accessible via SSH without user intervention.

Solution scheme


The optimal solution to this problem was the following sequence of actions:
  1. revitalize a part of the external hard disk drive enough to install Debian there
  2. on a laptop, bind an usb-connected external hard drive to a vmdk file using the VBoxManage software from the Oracle VirtualBox virtualization package, install it on an external Debian hard disk and configure it
  3. disassemble the external hard drive and connect it to the target machine via the sata interface, the first in the download queue, boot the machine and connect to it from the laptop via SSH
  4. clone the system from a 2.5 "disk to a native 3.5" target machine and reconfigure grub2, controlling the target machine from the laptop via SSH

Alternatives

Generally speaking, this problem has many solutions. Instead of the proposed scheme in my place, another user, in most cases more experienced and prepared, could choose one of the following solutions:
  1. Modify the existing Debian distribution so that it automatically installs and configures the system without a single question to the user when booting from a CD / DVD, including splitting the hard disk of the target machine into partitions as necessary. To do this, you will need to write a special preseed-file with answers to all installer requests and hope that you have all correctly taken into account. Read more in the official documentation .
  2. To set up a DHCP server on a laptop (or use a router), some web server to publish a preseed file and a tftp server to transfer the initial boot code pxelinux.0 over the network to the target machine - that is, use PXE boot ( for which BIOS boot support must be enabled. This is the most powerful approach that does not use an important strategic advantage of my situation - physical access to the target machine.
  3. Find a suitable live-cd Linux distribution or change an existing one (for example, using the simple-cdd utility or others) so that it boots without a single issue and automatically picks up the SSH server, and then manually install the Debian distribution (this process is also well described in documentation )
  4. To use some other way, however, in most cases it will turn out to be either a combination of the described ones, or it will go beyond the available resources: for example, the existence of a usb-sata adapter for a 3.5 "drive or at least such a trifle as an adapter “Dad-dad” for sata (a 3.5 "drive could be powered from the power supply unit of the target machine and use a controller from a 2.5" external drive to connect it via usb to a laptop or directly connect a 3.5 "drive to a laptop, for example, instead of Slim-DVD -drive.When with uncomplicated skill it is it is possible - but only if there is such an adapter)

A bit more

Now I will describe in more detail the steps that led me to success and, I hope, will lead you in a similar situation.
')
First stage

First, let's connect an external disk (capacity 160 GB, one partition for the whole disk, ext3 file system) to a laptop with a Linux system installed and check it for errors with automatic correction of the latter (hereinafter we will read the commands with the prefix # executed as root. We also assume that the external hard disk is determined by the laptop as a sdc device):
# fsck -f -y -c /dev/sdc1 

-f - check the disk even if it is marked as not containing errors
-y - answer “yes” to all questions automatically. That is, just fix all the errors
-c - search and mark broken disk blocks. For these purposes, there is a special utility badblocks, but with the help of this option you automatically send its fsck report.
Note that you do not specify a disk, but a specific partition on it.
In my case, the test "died" at 20% of execution due to the huge number of errors, which is why I made the simple decision to create a 20 GB partition at the beginning of the disk (this is more than enough to install Debian). To repartition a disk, it is convenient to use the cfdisk utility:
 # cfdisk /dev/sdc 

In interactive mode, you simply configure the disk as necessary: ​​I created a 20-GB primary partition at the beginning of the disk, leaving the rest of the space empty (and also marked it as bootable beforehand . Don't forget to write the changes to the disk before exiting the program). Then we create a file system on it:
 # mkfs.ext3 /dev/sdc1 

And again we check that everything is fine:
 # fsck -f -y -c /dev/sdc1 


Second phase

Suppose you have VirtualBox installed. Then create a vmdk disk image that actually displays all operations with it on a physical disk (we assume that you are in a directory convenient to you, for example, ~ / VirtualBox \ VMs \ desktop):
 # vboxmanage internalcommands createrawvmdk -filename usb-hdd160.vmdk -rawdisk /dev/sdc 

Now create a new virtual machine with the name “desktop”:
 # vboxmanage createvm --name desktop --ostype Debian --register 

Give her the appropriate amount of RAM:
 # vboxmanage modifyvm desktop --memory 512 

Equip a controller to connect hard drives and CD / DVD-drives:
 # vboxmanage storagectl desktop --name "sata" --add sata 

And connect our external hard drive:
 # vboxmanage storageattach desktop --storagectl "sata" --port 0 --type hdd --medium ./usb-hdd160.vmdk 

In order to disconnect a disk (it will come in handy again and again), you need to select “nothing” as the medium:
 # vboxmanage storageattach desktop --storagectl "sata" --port 0 --type hdd --medium none 

Now it's time to think about where we are going to install Debian. If I had a normal Internet channel, I chose a “small installation image”, if you wish, you can download a full-size CD or even a DVD. Details here . Also, due to the installation in the virtual machine, you will have to limit to the i386 version - 64-bit guests VirtualBox only supports on hosts with hardware virtualization support VT-x or AMD-V - only not the cheapest processors can boast this. So let the Debian iso-image be in the current directory, for example, debian-6.0.5-i386-netinst.iso. Then, in the image and likeness of connecting a hard disk, we will connect it too:
 # vboxmanage storageattach desktop --storagectl "sata" --port 1 --type dvddrive --medium ./debian-6.0.5-i386-netinst.iso 

You can run the car:
 # vboxmanage startvm desktop 

(some of these actions can also be performed from the VirtualBox GUI. When starting it, do not forget that the machines are created by default for only one user, so run the GUI also as root. As an ordinary user, you can hardly perform all these manipulations since working with a hard disk requires privileged access. Use sudo)
Now you need to go through all the stages of the installation, specify the passwords for root and the user, and also, and it is critically important, choose an SSH server among the installation templates. Due to the very fragile state of the disk, I minimized my actions when it was broken down - I mounted the existing partition as / and did not select the swap partition - it can be connected already on the target machine. After the installation is complete, you can still work on the system, adjust something to your taste, as long as you can access it from the keyboard and monitor, check that everything is really loading by disabling the iso image, etc.

Third stage

At the third stage, all you need is to connect the external drive to the target machine and turn it on. The router helped me at this stage because I looked at the ip of the machine on its status page and opened an SSH session from a laptop at this address:
 # ssh root@192.168.0.103 

Your car address is likely to be different.

Fourth stage

If everything worked out, it remains to transfer the system from an external disk to an internal one. Regarding this stage, there are especially many opinions (including not quite correct ones), how to implement it, I will offer one. First, given the work blindly, it will be very useful to do everything described in this section, first on a laptop in a virtual machine. Following this wonderful guide and the comments I have already made, you can connect another virtual hard disk to the desktop virtual machine and, considering that this is an internal drive of the target machine, practice on it. Ultimately, you should be able to unmount the external drive and still boot your Debian from the virtual one. To further bring the situation closer to the “combat” situation, you can work with the virtual machine via SSH from the host machine (laptop). To do this, for example, you can leave the network settings (NAT) of the virtual machine by default, but relocate the ports:
 # vboxmanage modifyvm desktop --natpf1 "guestssh,tcp,,2222,,22" 

(details here )
Now any call to 2222 (the number is arbitrary, but it is desirable that it exceed 1024 - only root can access the smaller ports) the port of your laptop will be sent to port 22 (standard SSH) of the virtual machine. You can open a session from the host:
 # ssh -p 2222 root@localhost 

(most likely, localhost leads to 127.0.0.1). Secondly, it is not necessary to copy the entire disk with dd, because it takes a long time and will not go smoothly in most practical cases. You can use cp, rsync to copy the file system, but the special utility dump / restore is most suitable (it works extremely quickly and reliably), and to restore the boot order you don’t get rid of copying with the MBR dd - modern Debian uses grub2 who likes to address disks by unique UUID numbers - you will leave in the system all links to an external hard disk.
So, you need to do the following (the target machine is loaded from an external disk, let it be / dev / sda, the internal disk is / dev / sdb, you work from a laptop via SSH ):
Using internal cfdisk, split the internal disk in the same way (or almost the same - the partition into which you will copy should contain the data intended for it):
 # cfdisk /dev/sdb 

Create a file system on it:
 # mkfs.ext3 /dev/sdb1 

Mount it:
 # mount /dev/sdb1 /mnt 

And copy the external disk file system into it:
 # (dump -0f - /) | ( cd /mnt; restore -rf - ) 

Now you need to configure grub2. First, chroot into the copied file system:
 # mount --bind /dev /mnt/dev # mount --bind /proc /mnt/proc # chroot /mnt 

Now we are actually in the Debian environment on the internal disk of the target machine. Update the configuration of hard drives (now there is garbage left by the virtual machine):
 # grub-mkdevicemap 

- it will automatically be written to the /boot/grub/device.map file. Update grub2 configuration:
 # update-grub 

Perform its installation on the internal disk:
 # grub-install /dev/sdb 

(note, you specify a disk, not a partition on it)
And again it is worth updating the configuration:
 # update-grub 

Now cancel chroot and turn off the system:
 # exit # halt 

Now unplug the external drive and start the target machine again. You must re-access it via SSH from a laptop.
Now you can create and connect a swap partition: allocate a place to it using cfdisk, then execute
 # mkswap /dev/sdb2 # sync; sync; sync # swapon /dev/sdb2 

(sync flushes system buffers)
It is possible to start a home section, etc. (you can see here ).
The system is ready to go.

Related Links:
  1. www.linuxreport.org/content/view/67/36
  2. wiki grub2 in Russian


PS Regarding the resuscitation of an external hard drive, I asked questions on the forums cyberforum.ru and oszone.net . I would be grateful for any ideas presented in any place convenient for you.

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


All Articles