📜 ⬆️ ⬇️

Creating a live usb for backup / restore system

Every time when I had to make a backup of the system, I looked for an unused USB flash drive, which I downloaded to the ArchLinux installation image and loaded into it. This method was a very unpleasant moment - I had to look for an unused USB flash drive with a size of> 512 MiB , despite the fact that a 64 MB SD card was always at hand, which would be enough for backup and recovery needs. But there was one problem with the SD card - a rare distribution could boot from it on my Eee PC 900. After trying with a dozen different small distributions, none of which came up to me completely, I decided to make my live system.

Training


What you need:


The size of the carrier does not matter - the smallest memory card I could find was 64 megabytes in size, which is more than enough to create a full-fledged backup / restore system.
As for the kernel, I use my own kernel, in which the necessary modules are compiled, and the initrd support is disabled. If you already have a kernel that does not require additional modules for working with disks, you can use it. If you don’t have such a kernel, download the kernel source code from kernel.org and build it manually. In this article I will not describe how to build the kernel.
We agree that the flash drive device is / dev / sdc.

Busybox


I just want to say that I used BusyBox, a utility that replaces a very large set of other utilities, albeit with a slightly reduced functionality. Its focus is that, depending on how you launch it, this is how it will behave. For example, if you make a symbolic link to busybox with the name ln, then we get ln. If the link is called dd, we get dd. With a weight of 1.5 megabytes and no dependencies - this is just a miracle.
If you run busybox without parameters, you will see a list of utilities available in your version, whose functions can be replaced by busybox. If the size is very important to you, then you can compile BusyBox from source, disabling unnecessary functionality and thereby reducing the size of the binary file. I used a ready package from repositories.
')

Device preparation:


Using fdisk, create one partition on the flash drive device and make it bootable.
fdisk /dev/sdc
First delete all partitions on this device . Most likely there is only one partition, so type d once. Then create a new primary partition . Press n , [Enter] , p , [Enter] , 1 , [Enter] , [Enter] , [Enter] . Make this partition bootable - enter a . To write a new partition table to the device - press w .
Now create a new file system on the first partition of the flash drive. I chose Ext2 as a file system - its support has been around for a long time, and journaling in this kind of system does not make much sense. To format a partition in Ext2, run the following command:
mke2fs -m 0 -N 2000 /dev/sdc1
-m 0 will disable space reservations (whatever that means)
-N 2000 will provide enough inods .

We collect system


Mount the new partition in a separate directory (for example, / newroot):
mkdir /newroot
mount /dev/sdc1 /newroot

Every Linux distribution has a set of required directories. Create the following directories:
/bin
/boot
/dev
/etc
/proc
/sbin
/var

Optional are / sys, / lib, / var, / mnt, / usr and / home - you can create them later if they become necessary.

/ dev


First, fill in the / dev directory with the necessary devices. These are console, kmem, mem, null, ram0 and tty1. Copy them directly from your working system using cp -dpR /dev/DEVICE /newroot/dev where DEVICE is each of the above devices. The -dpR options ensure that the files themselves are copied, not their contents, and also retain all access rights to them. If you don’t like this way - you can make it more time consuming, but perhaps a more correct way - with the help of ls -l, find out the main and smaller numbers of each device and create them with the help of mknod .
In addition to the above-mentioned mandatory devices, also copy your hard disk devices ( hd * and sd * ), as well as a dozen terminal devices ( tty * ). You may also need loop * . Copy the rest of the device files as needed.

/ etc


System settings should be stored in this directory. The required files here will be rc (in different systems called differently), inittab, fstab, passwd, group and shadow. Although, the group may not be mandatory, but I did not try to verify this.
After you copy these files to your new system, you will need to edit the following files:

inittab - runs it / bin / init. It determines the behavior of the system at different levels of performance. You can read more about the format of entries in the inittab file in man inittab However, there is a very important point: unlike the standard inittab, the version for busybox should not contain the id and runlevel fields. My primitive inittab looks like this:
# / etc / inittab

:: sysinit: / etc / rc

# Spawn some gettys
:: respawn: / sbin / getty -L 115200 tty1 vt100
:: respawn: / sbin / getty -L 115200 tty2 vt100
:: respawn: / sbin / getty -L 115200 tty3 vt100
:: respawn: / sbin / getty -L 115200 tty4 vt100

# Stuff to restarting the init process
:: restart: / sbin / init

# Stuff to do before rebooting
:: ctrlaltdel: / sbin / reboot


fstab - everything is as usual here. Just specify as a root system not your hard disk, but a flash drive.

passwd and shadow - in these files, leave only an entry for root. The rest you do not need. In addition, you may want to change the default home directory and shell. Usually / bin / bash or / bin / zsh is registered as a shell, but in busybox there is only primitive sh. Of course, no one bothers to make a symbolic link bash -> sh.

rc is a boot script written in your inittab. It can be super complex and include other rc scripts, but for simple systems it can be greatly simplified (example of my rc):
#! / bin / sh

# Uncomment next line, if you have
# / etc / rc.conf

PS1 = [rescue-system]:
PATH = / sbin: / bin: / usr / bin

/ bin / mount -av
/ bin / hostname localhost


/ bin


Busybox is installed in this directory. This can be achieved in different ways. For example, if you build it manually, then add the parameter --prefix = / newroot during configuration. If you are using the package manager, then read the help for it - there should be an option that allows you to specify the root directory for the package being installed. For example, in ArchLinux, in pacman this is the key -r:
pacman -S busybox -r /newroot -b /var/lib/pacman The -b parameter is required because pacman cannot find the package base inside / newroot.
After installation, go to the / newroot / bin directory. Run busybox and decide which built-in utilities you need and which ones don't. For each of the utilities, make a symbolic link to the busybox with the corresponding utility name. For example:
ln -s ./busybox ./cd
Be sure to make links with the names sh, login and mount!

/ sbin


Go to this directory and create the necessary links to ../bin/busybox. In general, the division into two directories - / bin and / sbin - in this system does not make much sense, since the user is only one, and that root.
Be sure to make links with the names init, getty, poweroff, shutfown, reboot, halt!

/ boot


In this directory, copy only your kernel and name it vmlinuz.

Install GRUB


Installing the GRUB bootloader is done with the grub-install command:
grub-install --root-directory=/newroot /dev/sdc1
After the installation is complete, create a simple GRUB menu file (/boot/grub/menu.lst):
title boot rescue blob
root (hd0,0)
kernel / boot / vmlinuz rootdelay = 6 root = / dev / sdc1 rw

In the root line, you may have to change the device, but on my Eee PC 900, when selecting the boot from the memory card in the BIOS, GRUB identified it that way.
In addition, an important parameter is rootdelay - without it, the kernel did not have time to find the device of the card reader and I received:
Kernel Panic: VFS: Unable to mount root fs on / dev / sdc


Conclusion


That's all. Now you can reboot and try out the new system. Of course, with BusyBox you are unlikely to get a super functional system, but the system will be capable of much in terms of operating system maintenance. In addition, since this version of live-OS does not use compressed images of the file system, then all the changes that you make to it will remain after the reboot, so you can safely supplement the functionality with scripts right during use and not be afraid to lose your work.
Of course, there is a lot more that can be optimized, and I will gradually deal with this, however, the system is quite ready for basic backup disk creation and restoration.

Reference materials:
Documentation of the kernel boot command line options , Old Linux boot disk creation instructions .

This is a cross-post with welinux.ru

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


All Articles