Install Ubuntu from another Linux / LiveCD

This article will focus on installing Ubuntu from another Linux without using an ISO image. This is necessary primarily to create customized test environments. There is enough information on such a procedure on the Internet, it is easily googled, but, as it turned out, at some points the existing instructions are outdated, yes, and they all have a known fatal flaw.





So, the problem statement: there is a naked virtual machine with Internet access and an EFI BIOS, there is some Linux (in our case it is SystemRescue LiveCD ), you need to get Ubuntu installed. And all actions should be easily automated so that they can be executed in the form of a script.





First of all, we boot into our SysRCD. We will work via SSH, and in order for it to work, you need to set the root password and enable SSH in iptables (initially, any incoming connections are prohibited in SysRCD):





iptables -I INPUT -p tcp --dport 22 -j ACCEPT
passwd
      
      



And we connect via SSH:





ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@192.168.56.110
      
      



Now you need to prepare partitions on your hard drive. Since our system is EFI, the partition table will be GPT, we need an EFI FAT partition, and the system itself will be on an ext4 partition. The EFI partition can be very small - literally 10 MB, but for the stable operation of system updates it is better to make it at least 32 MB. And an important note! All instructions say that the partition must be in FAT32 format, but in practice VirtualBox refuses to work with a small EFI partition in this format (well, or mkfs.vfat does not format small FAT32 partitions correctly - experiments are needed here)! Plus, there are nuances with disk size, cluster size and compatibility with EFI bios. Therefore, we will format it in FAT16. We will split using parted.

In order to mark a partition as a service EFI in parted, it needs to set the "esp" flag.





parted
unit KiB
mktable gpt
mkpart fat16 1024KiB 32MiB
name 1 EFI  
set 1 esp on
mkpart ext4 32MiB -1s
  - .
name 2 LINUX
quit
      
      



. , EFI- FAT16, VirtualBox.





# VirtualBox EFI      FAT32!  FAT16.
mkfs.fat -F 16 -n EFI /dev/sda1
mkfs.ext4 -O ^64bit -L LINUX /dev/sda2
      
      



debootstrap. DEB- . Debian DEB- . LiveCD . DEB- AR, "tar.gz".





mkdir /tmp/1
cd /tmp/1
wget http://ftp.ru.debian.org/debian/pool/main/d/debootstrap/debootstrap_1.0.123_all.deb
#   -    deb-    .
ar -p *.deb data.tar.gz | tar -xz -C /
      
      



Ubuntu 20.04 "Focal" - deboostrap. , .





, , - . "/tmp/".





mkdir /tmp/newroot
cd /tmp
mount -o relatime /dev/sda2 newroot
/usr/sbin/debootstrap --arch=amd64 --include=wget,nano focal /tmp/newroot https://mirror.linux-ia64.org/ubuntu/
      
      



, . /etc/apt/sources.list





( RELEASE=focal ; echo "
deb http://ru.archive.ubuntu.com/ubuntu/ ${RELEASE} main restricted
# deb-src http://ru.archive.ubuntu.com/ubuntu/ ${RELEASE} main restricted
deb http://ru.archive.ubuntu.com/ubuntu/ ${RELEASE}-updates main restricted
# deb-src http://ru.archive.ubuntu.com/ubuntu/ ${RELEASE}-updates main restricted
deb http://ru.archive.ubuntu.com/ubuntu/ ${RELEASE} universe
# deb-src http://ru.archive.ubuntu.com/ubuntu/ ${RELEASE} universe
deb http://ru.archive.ubuntu.com/ubuntu/ ${RELEASE}-updates universe
# deb-src http://ru.archive.ubuntu.com/ubuntu/ ${RELEASE}-updates universe
deb http://ru.archive.ubuntu.com/ubuntu/ ${RELEASE} multiverse
# deb-src http://ru.archive.ubuntu.com/ubuntu/ ${RELEASE} multiverse
deb http://ru.archive.ubuntu.com/ubuntu/ ${RELEASE}-updates multiverse
# deb-src http://ru.archive.ubuntu.com/ubuntu/ ${RELEASE}-updates multiverse
deb http://ru.archive.ubuntu.com/ubuntu/ ${RELEASE}-backports main restricted universe multiverse
# deb-src http://ru.archive.ubuntu.com/ubuntu/ ${RELEASE}-backports main restricted universe multiverse
deb http://archive.canonical.com/ubuntu ${RELEASE} partner
# deb-src http://archive.canonical.com/ubuntu ${RELEASE} partner
deb http://security.ubuntu.com/ubuntu focal-security main restricted
# deb-src http://security.ubuntu.com/ubuntu ${RELEASE}-security main restricted
deb http://security.ubuntu.com/ubuntu ${RELEASE}-security universe
# deb-src http://security.ubuntu.com/ubuntu ${RELEASE}-security universe
deb http://security.ubuntu.com/ubuntu ${RELEASE}-security multiverse
# deb-src http://security.ubuntu.com/ubuntu ${RELEASE}-security multiverse
" >newroot/etc/apt/sources.list )
      
      



chroot- :





mount --bind /dev newroot/dev
mount --bind /dev/pts newroot/dev/pts 
mount -t sysfs sys newroot/sys 
mount -t proc proc newroot/proc
LANG=C.UTF-8 chroot newroot
      
      



. , CP866 ( "IBM866"), windows-legacy .





/etc/locale.gen , CP866/IBM866 - , .





locale-gen en_US.UTF-8
echo "
en_GB ISO-8859-1
en_GB.UTF-8 UTF-8
en_US ISO-8859-1
en_US.UTF-8 UTF-8
ru_RU.CP1251 CP1251
ru_RU.IBM866 IBM866
ru_RU.KOI8-R KOI8-R
ru_RU.UTF-8 UTF-8
" >/etc/locale.gen
#      .
locale-gen
      
      



mc, aptitude, .





apt-get update
apt-get upgrade
apt-get install -y aptitude mc man
#    .
apt-get install -y language-pack-en
      
      



EFI /boot/efi. /etc/fstab.





echo "
 <file system>                           <mount point>          <type>  <options>                    <dump> <pass>
# / was on /dev/sda2 during installation
UUID=`blkid -o value -s UUID /dev/sda2`   /                      ext4    errors=remount-ro,relatime      0     2
# /boot/efi was on /dev/sda1 during installation
UUID=`blkid -o value -s UUID /dev/sda1`   /boot/efi              vfat    umask=0033                      0     1
" >/etc/fstab
#  EFI-.
mount /boot/efi
      
      



.





echo '
LANG="C"
LANGUAGE="en_US:C:ru_RU"
LC_CTYPE="ru_RU.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_COLLATE="ru_RU.UTF-8"
LC_NUMERIC="C.UTF-8"
LC_TIME="C.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_PAPER="ru_RU.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="ru_RU.UTF-8"
LC_MEASUREMENT="ru_RU.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
' >/etc/default/locale
      
      



. "dpkg-reconfigure tzdata". , . "timedatectl list-timezones".





ln -sf /usr/share/zoneinfo/Europe/Moscow /etc/localtime
echo "Europe/Moscow" >/etc/timezone
echo "test" >etc/hostname
#     :
timedatectl set-timezone "Europe/Moscow" 
#     timedatectl list-timezones
      
      



, UTC. , "0" UTC.





timedatectl set-local-rtc 0
      
      



, . :





apt-get install -y linux-image-virtual-hwe-20.04-edge linux-image-extra-virtual-hwe-20.04-edge linux-headers-virtual-hwe-20.04-edge
      
      



, , GRUB, SSH :





apt-get install -y console-setup console-common ssh
apt-get install -y net-tools bind9utils build-essential pixz pigz pv htop
apt-get install -y grub-efi-amd64
      
      



:





# Consult the keyboard(5) manual page.
XKBMODEL="pc105"
XKBLAYOUT="us,ru"
XKBVARIANT=","
XKBOPTIONS="grp:rctrl_toggle,lv3:ralt_switch,grp_led:scroll"
BACKSPACE="guess"
' >/etc/default/keyboard

echo '
# CONFIGURATION FILE FOR SETUPCON
# Consult the console-setup(5) manual page.
ACTIVE_CONSOLES="/dev/tty[1-6]"
CHARMAP="UTF-8"
CODESET="guess"
FONTFACE="Terminus"
FONTSIZE="8x16"
VIDEOMODE=
" >/etc/default/console-setup
      
      



:

dpkg-reconfigure console-common

dpkg-reconfigure console-data

dpkg-reconfigure keyboard-configuration





GRUB EFI-:





grub-install --target=x86_64-efi --recheck --efi-directory=/boot/efi /dev/sda
      
      



, GRUB /etc/default/grub GRUB :





update-grub2
      
      



, :





update-initramfs -u
      
      



root root SSH . , SSH-.

SSH- /etc/ssh/sshd_config :

PermitRootLogin yes

root .





#   root
passwd
echo "PermitRootLogin yes" >>/etc/ssh/sshd_config
      
      



#   
echo "test1">/etc/hostname
      
      



. netplan networkd. MAC- . , DHCP ( NET- ). , IPv6 "link-local: [ ]" .





echo "
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      match:
        macaddress: 08:00:27:2e:69:24
      addresses:
        - 192.168.56.10/24
      gateway4: 192.168.56.1
" >/etc/netplan/eth0.yaml

echo "
network:
  version: 2
  renderer: networkd
  ethernets:
    eth1:
      match:
        macaddress: 08:00:27:e4:46:31
      # Let's disable IPV6 for this interface.
      link-local: [ ] 
      dhcp4: yes
" >/etc/netplan/eth1.yaml
      
      



:





adduser user
usermod -G 'adm,dialout,sudo,cdrom,dip,plugdev,users' user
      
      



apt:





apt-get clean
      
      



! .





VirtualBox, , "Guest Additions CD Image" - :





sudo mkdir /cdrom
sudo mount -o loop,ro /dev/cdrom /cdrom
sudo /cdrom/VBoxLinuxAdditions.run
sudo umount /cdrom
      
      



That's all. Next, you need to connect via SSH and fill in the SSH user keys. Then remove the " PermitRootLogin yes " setting from / etc / ssh / sshd_config .





If anyone is interested, this Ubuntu 20.04 image takes up 2.2 GB of disk space.








All Articles