2019-06-29 17:40:21 +00:00
# How to create a custom Ubuntu live from scratch
2019-06-26 17:26:09 +00:00
2019-06-29 17:35:10 +00:00
This procedure works and can create a bootable and installable Ubuntu Live (along with the automatic hardware detection and configuration) from scratch.
2019-06-26 17:26:09 +00:00
## Prerequisites (GNU/Linux Debian/Ubuntu)
Install applications we need to build the environment.
```
sudo apt-get install \
debootstrap \
squashfs-tools \
2019-06-28 12:54:09 +00:00
xorriso \
grub-pc-bin \
grub-efi-amd64-bin \
mtools
2019-06-26 17:26:09 +00:00
```
```
mkdir $HOME/live-ubuntu-from-scratch
```
## Bootstrap and Configure Ubuntu
2019-06-29 17:35:10 +00:00
* Checkout bootstrap
```
sudo debootstrap \
--arch=amd64 \
--variant=minbase \
bionic \
$HOME/live-ubuntu-from-scratch/chroot \
http://us.archive.ubuntu.com/ubuntu/
```
> **debootstrap** is used to create a Debian base system from scratch, without requiring the availability of **dpkg** or **apt**. It does this by downloading .deb files from a mirror site, and carefully unpacking them into a directory which can eventually be **chrooted** into.
* Configure external mount points
```
sudo mount --bind /dev $HOME/live-ubuntu-from-scratch/chroot/dev
sudo mount --bind /run $HOME/live-ubuntu-from-scratch/chroot/run
```
As we will be updating and installing packages (among them grub), these mount points are necessary inside the chroot environment, so we can be able to finish the installations without errors.
2019-06-26 17:26:09 +00:00
2019-06-29 17:35:10 +00:00
## Define chroot environment
2019-06-26 17:26:09 +00:00
2019-06-29 17:35:10 +00:00
*A chroot on Unix operating systems is an operation that changes the apparent root directory for the current running process and its children. A program that is run in such a modified environment cannot name (and therefore normally cannot access) files outside the designated directory tree. The term "chroot" may refer to the chroot(2) system call or the chroot(8) wrapper program. The modified environment is called a chroot jail.*
> Reference: https://en.wikipedia.org/wiki/Chroot
2019-06-29 17:38:31 +00:00
1. **Access chroot environment**
2019-06-29 17:35:10 +00:00
```
sudo chroot $HOME/live-ubuntu-from-scratch/chroot
```
2019-06-26 17:26:09 +00:00
2019-06-29 17:40:21 +00:00
2. **Configure mount points, home and locale**
2019-06-27 04:42:44 +00:00
```
mount none -t proc /proc
2019-06-26 19:12:53 +00:00
2019-06-27 04:42:44 +00:00
mount none -t sysfs /sys
2019-06-26 19:12:53 +00:00
2019-06-27 04:42:44 +00:00
mount none -t devpts /dev/pts
2019-06-26 19:12:53 +00:00
2019-06-27 04:42:44 +00:00
export HOME=/root
2019-06-26 19:12:53 +00:00
2019-06-27 04:42:44 +00:00
export LC_ALL=C
```
2019-06-26 19:12:53 +00:00
2019-06-29 17:40:21 +00:00
These mount points are necessary inside the chroot environment, so we can be able to finish the installations without errors.
2019-06-29 17:35:10 +00:00
3. **Set a custom hostname**
2019-06-27 04:42:44 +00:00
```
2019-06-29 19:41:47 +00:00
echo "ubuntu-fs-live" > /etc/hostname
2019-06-27 04:42:44 +00:00
```
2019-06-26 19:12:53 +00:00
2019-06-29 17:35:10 +00:00
4. **Configure apt sources.list**
2019-06-27 04:42:44 +00:00
```
cat < < EOF > /etc/apt/sources.list
deb http://us.archive.ubuntu.com/ubuntu/ bionic main restricted universe multiverse
2019-06-27 19:43:34 +00:00
deb-src http://us.archive.ubuntu.com/ubuntu/ bionic main restricted universe multiverse
2019-06-26 19:12:53 +00:00
2019-06-27 04:42:44 +00:00
deb http://us.archive.ubuntu.com/ubuntu/ bionic-security main restricted universe multiverse
2019-06-27 19:43:34 +00:00
deb-src http://us.archive.ubuntu.com/ubuntu/ bionic-security main restricted universe multiverse
2019-06-26 19:45:45 +00:00
2019-06-27 04:42:44 +00:00
deb http://us.archive.ubuntu.com/ubuntu/ bionic-updates main restricted universe multiverse
2019-06-27 19:43:34 +00:00
deb-src http://us.archive.ubuntu.com/ubuntu/ bionic-updates main restricted universe multiverse
2019-06-27 04:42:44 +00:00
EOF
```
2019-06-26 19:45:45 +00:00
2019-06-27 04:42:44 +00:00
4. **Update indexes packages**
```
apt-get update
```
2019-06-26 19:45:45 +00:00
2019-06-27 04:42:44 +00:00
5. **Install systemd**
```
apt-get install -y systemd-sysv
```
2019-06-29 17:35:10 +00:00
> **systemd** is a system and service manager for Linux. It provides aggressive parallelization capabilities, uses socket and D-Bus activation for starting services, offers on-demand starting of daemons, keeps track of processes using Linux control groups, maintains mount and automount points and implements an elaborate transactional dependency-based service control logic.
2019-06-26 19:45:45 +00:00
2019-06-27 04:42:44 +00:00
5. **Configure machine-id and divert**
```
2019-06-29 18:46:38 +00:00
dbus-uuidgen > /etc/machine-id
2019-06-26 19:45:45 +00:00
2019-06-29 18:46:38 +00:00
ln -fs /etc/machine-id /var/lib/dbus/machine-id
2019-06-29 17:35:10 +00:00
```
> The `/etc/machine-id` file contains the unique machine ID of the local system that is set during installation or boot. The machine ID is a single newline-terminated, hexadecimal, 32-character, lowercase ID. When decoded from hexadecimal, this corresponds to a 16-byte/128-bit value. This ID may not be all zeros.
2019-06-27 04:42:44 +00:00
2019-06-29 17:35:10 +00:00
```
dpkg-divert --local --rename --add /sbin/initctl
2019-06-28 12:54:09 +00:00
2019-06-29 17:35:10 +00:00
ln -s /bin/true /sbin/initctl
2019-06-27 04:42:44 +00:00
```
2019-06-29 17:35:10 +00:00
> **dpkg-divert** is the utility used to set up and update the list of diversions.
2019-06-26 19:45:45 +00:00
6. **Install packages needed for Live System**
2019-06-27 04:42:44 +00:00
```
apt-get install -y \
ubuntu-standard \
casper \
lupin-casper \
discover \
laptop-detect \
os-prober \
network-manager \
2019-06-27 19:56:19 +00:00
resolvconf \
2019-06-27 04:42:44 +00:00
net-tools \
wireless-tools \
wpagui \
locales \
linux-generic
```
2019-06-29 19:37:11 +00:00
1. Configure grub
< p align = "center" >
< img src = "images/grub-configure-01.png" >
< / p >
< p align = "center" >
< img src = "images/grub-configure-02.png" >
< / p >
< p align = "center" >
< img src = "images/grub-configure-03.png" >
< / p >
2019-06-27 19:54:51 +00:00
7. **Graphical installer**
2019-06-27 04:42:44 +00:00
```
apt-get install -y \
ubiquity \
ubiquity-casper \
ubiquity-frontend-gtk \
ubiquity-slideshow-ubuntu \
ubiquity-ubuntu-artwork
```
2019-06-26 19:45:45 +00:00
2019-06-29 19:37:11 +00:00
1. Configure keyboard
< p align = "center" >
< img src = "images/keyboard-configure-01.png" >
< / p >
< p align = "center" >
< img src = "images/keyboard-configure-02.png" >
< / p >
2. Console setup
< p align = "center" >
< img src = "images/console-configure-01.png" >
< / p >
2019-06-27 19:54:51 +00:00
8. **Install window manager**
2019-06-27 04:42:44 +00:00
```
apt-get install -y \
2019-06-27 14:02:56 +00:00
plymouth-theme-ubuntu-logo \
2019-06-27 04:42:44 +00:00
ubuntu-gnome-desktop \
ubuntu-gnome-wallpapers
```
2019-06-26 19:45:45 +00:00
2019-06-28 13:24:54 +00:00
9. **Install useful applications**
2019-06-27 04:42:44 +00:00
```
apt-get install -y \
clamav-daemon \
terminator \
apt-transport-https \
curl \
vim \
nano
```
2019-06-26 19:45:45 +00:00
2019-06-27 19:54:51 +00:00
10. **Install Visual Studio Code**
2019-06-26 19:45:45 +00:00
1. Download and install the key
2019-06-27 04:42:44 +00:00
```
curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
2019-06-26 19:45:45 +00:00
2019-06-27 04:42:44 +00:00
install -o root -g root -m 644 microsoft.gpg /etc/apt/trusted.gpg.d/
echo "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main" > /etc/apt/sources.list.d/vscode.list
2019-06-26 19:45:45 +00:00
2019-06-27 04:42:44 +00:00
rm microsoft.gpg
```
2019-06-26 19:45:45 +00:00
2019-06-29 17:35:10 +00:00
2. Then update the package cache and install the package using
2019-06-27 04:42:44 +00:00
```
apt-get update
apt-get install -y code
```
2019-06-26 19:45:45 +00:00
2019-06-27 19:54:51 +00:00
11. **Install Google Chrome**
2019-06-26 19:45:45 +00:00
1. Download and install the key
2019-06-27 04:42:44 +00:00
```
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
2019-06-26 19:45:45 +00:00
2019-06-27 04:42:44 +00:00
echo "deb http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google-chrome.list
```
2019-06-26 19:45:45 +00:00
2019-06-29 17:35:10 +00:00
2. Then update the package cache and install the package using
2019-06-27 04:42:44 +00:00
```
apt-get update
2019-06-26 19:45:45 +00:00
2019-06-27 04:42:44 +00:00
apt-get install google-chrome-stable
```
2019-06-26 19:45:45 +00:00
2019-06-27 19:54:51 +00:00
12. **Install Java JDK 8**
2019-06-26 19:45:45 +00:00
```
apt-get install -y \
openjdk-8-jdk \
openjdk-8-jre
```
2019-06-27 19:54:51 +00:00
13. **Remove unused applications**
2019-06-26 19:45:45 +00:00
```
apt-get purge -y \
transmission-gtk \
transmission-common \
gnome-mahjongg \
gnome-mines \
gnome-sudoku \
aisleriot \
hitori
```
2019-06-27 19:54:51 +00:00
14. **Remove unused packages**
2019-06-26 19:45:45 +00:00
```
apt-get autoremove -y
```
2019-06-27 19:54:51 +00:00
15. **Reconfigure packages**
2019-06-29 17:38:31 +00:00
1. Generate locales
2019-06-27 19:54:51 +00:00
```
dpkg-reconfigure locales
```
2019-06-29 17:38:31 +00:00
1. *Select locales*
2019-06-29 17:35:10 +00:00
< p align = "center" >
< img src = "images/locales-select.png" >
< / p >
2019-06-29 17:38:31 +00:00
2. *Select default locale*
2019-06-29 17:35:10 +00:00
< p align = "center" >
< img src = "images/locales-default.png" >
< / p >
2019-06-29 17:38:31 +00:00
2. Reconfigure resolvconf
2019-06-27 19:54:51 +00:00
```
2019-06-27 21:02:56 +00:00
dpkg-reconfigure resolvconf
2019-06-27 19:54:51 +00:00
```
2019-06-29 17:38:31 +00:00
1. *Confirm changes*
2019-06-29 17:35:10 +00:00
< p align = "center" >
< img src = "images/resolvconf-confirm-01.png" >
< / p >
< p align = "center" >
< img src = "images/resolvconf-confirm-02.png" >
< / p >
< p align = "center" >
< img src = "images/resolvconf-confirm-03.png" >
< / p >
2019-06-29 17:38:31 +00:00
3. Configure network-manager
2019-06-27 19:54:51 +00:00
```
cat < < EOF > /etc/NetworkManager/NetworkManager.conf
[main]
rc-manager=resolvconf
plugins=ifupdown,keyfile
dns=dnsmasq
[ifupdown]
managed=false
EOF
```
2019-06-29 17:38:31 +00:00
4. Reconfigure network-manager
2019-06-27 19:54:51 +00:00
```
dpkg-reconfigure network-manager
```
15. **Cleanup the chroot environment**
2019-06-26 19:45:45 +00:00
1. If you installed software, be sure to run
2019-06-27 04:42:44 +00:00
```
2019-06-29 18:46:38 +00:00
truncate -s 0 /etc/machine-id
2019-06-27 04:42:44 +00:00
```
2019-06-26 19:45:45 +00:00
2. Remove the diversion
2019-06-27 04:42:44 +00:00
```
rm /sbin/initctl
2019-06-26 19:45:45 +00:00
2019-06-27 04:42:44 +00:00
dpkg-divert --rename --remove /sbin/initctl
```
2019-06-26 19:45:45 +00:00
3. Clean up
2019-06-27 04:42:44 +00:00
```
apt-get clean
rm -rf /tmp/* ~/.bash_history
2019-06-26 19:45:45 +00:00
2019-06-27 04:42:44 +00:00
umount /proc
umount /sys
umount /dev/pts
2019-06-26 19:45:45 +00:00
2019-06-27 04:42:44 +00:00
export HISTSIZE=0
exit
```
2019-06-26 19:45:45 +00:00
## Unbind mount points
```
sudo umount $HOME/live-ubuntu-from-scratch/chroot/dev
2019-06-26 19:12:53 +00:00
2019-06-26 19:45:45 +00:00
sudo umount $HOME/live-ubuntu-from-scratch/chroot/run
2019-06-26 19:12:53 +00:00
```
2019-06-26 19:27:43 +00:00
## Create the CD image directory and populate it
1. Access build directory
```
cd $HOME/live-ubuntu-from-scratch
```
2. Create directories
```
mkdir -p image/{casper,isolinux,install}
```
2. Copy kernel images
```
2019-06-26 19:36:21 +00:00
sudo cp chroot/boot/vmlinuz-**-**-generic image/casper/vmlinuz
2019-06-26 19:27:43 +00:00
2019-06-27 02:27:28 +00:00
sudo cp chroot/boot/initrd.img-**-**-generic image/casper/initrd
2019-06-26 19:27:43 +00:00
```
2019-06-28 12:54:09 +00:00
3. Copy memtest binary
2019-06-26 19:27:43 +00:00
```
2019-06-26 19:36:21 +00:00
sudo cp chroot/boot/memtest86+.bin image/install/memtest
2019-06-26 19:27:43 +00:00
```
2019-06-27 02:27:28 +00:00
2019-06-28 12:54:09 +00:00
## Grub configuration
2019-06-27 02:27:28 +00:00
1. Access build directory
```
cd $HOME/live-ubuntu-from-scratch
```
2019-06-28 12:54:09 +00:00
2. Create image/isolinux/grub.cfg
2019-06-27 02:27:28 +00:00
```
2019-06-28 12:54:09 +00:00
cat < < EOF > image/isolinux/grub.cfg
2019-06-27 02:27:28 +00:00
2019-06-28 12:54:09 +00:00
search --set=root --file /ubuntu
2019-06-27 02:27:28 +00:00
2019-06-28 12:54:09 +00:00
insmod all_video
set default="0"
set timeout=30
menuentry "Try Ubuntu without installing" {
linux /casper/vmlinuz boot=casper quiet splash ---
initrd /casper/initrd
}
menuentry "Install Ubuntu" {
linux /casper/vmlinuz boot=casper only-ubiquity quiet splash ---
initrd /casper/initrd
}
menuentry "Check disc for defects" {
linux /casper/vmlinuz boot=casper integrity-check quiet splash ---
initrd /casper/initrd
}
menuentry "Test memory" {
2019-06-29 22:43:26 +00:00
linux16 /install/memtest
2019-06-28 12:54:09 +00:00
}
2019-06-27 02:27:28 +00:00
EOF
```
2019-06-28 12:54:09 +00:00
2019-06-27 02:27:28 +00:00
## Create manifest
2019-06-26 22:13:38 +00:00
1. Access build directory
```
cd $HOME/live-ubuntu-from-scratch
```
2019-06-27 02:27:28 +00:00
2. Generate manifest
```
sudo chroot chroot dpkg-query -W --showformat='${Package} ${Version}\n' | sudo tee image/casper/filesystem.manifest
2019-06-26 22:13:38 +00:00
2019-06-27 02:27:28 +00:00
sudo cp -v image/casper/filesystem.manifest image/casper/filesystem.manifest-desktop
2019-06-26 22:13:38 +00:00
2019-06-27 14:49:19 +00:00
sudo sed -i '/ubiquity/d' image/casper/filesystem.manifest-desktop
sudo sed -i '/casper/d' image/casper/filesystem.manifest-desktop
sudo sed -i '/discover/d' image/casper/filesystem.manifest-desktop
sudo sed -i '/laptop-detect/d' image/casper/filesystem.manifest-desktop
sudo sed -i '/os-prober/d' image/casper/filesystem.manifest-desktop
2019-06-27 02:27:28 +00:00
```
2019-06-26 22:13:38 +00:00
2019-06-27 02:27:28 +00:00
## Compress the chroot
2019-06-26 22:13:38 +00:00
1. Access build directory
```
cd $HOME/live-ubuntu-from-scratch
```
2019-06-27 02:27:28 +00:00
2. Create squashfs
2019-06-26 22:13:38 +00:00
```
2019-06-27 02:27:28 +00:00
sudo mksquashfs chroot image/casper/filesystem.squashfs
2019-06-26 22:13:38 +00:00
```
2019-06-27 02:27:28 +00:00
3. Write the filesystem.size
2019-06-26 22:13:38 +00:00
```
2019-06-27 02:27:28 +00:00
printf $(sudo du -sx --block-size=1 chroot | cut -f1) > image/casper/filesystem.size
2019-06-26 22:13:38 +00:00
```
2019-06-27 02:27:28 +00:00
## Create diskdefines
2019-06-26 22:13:38 +00:00
1. Access build directory
```
cd $HOME/live-ubuntu-from-scratch
```
2019-06-27 02:27:28 +00:00
2. Create file image/README.diskdefines
```
cat < < EOF > image/README.diskdefines
#define DISKNAME Ubuntu from scratch
#define TYPE binary
#define TYPEbinary 1
#define ARCH amd64
#define ARCHamd64 1
#define DISKNUM 1
#define DISKNUM1 1
#define TOTALNUM 0
#define TOTALNUM0 1
EOF
```
2019-06-26 22:13:38 +00:00
2019-06-27 02:27:28 +00:00
## Recognition as an Ubuntu from scratch
2019-06-26 22:13:38 +00:00
1. Access build directory
```
cd $HOME/live-ubuntu-from-scratch
```
2019-06-27 02:27:28 +00:00
2. Create an empty file named "ubuntu" and a hidden ".disk" folder.
2019-06-26 22:13:38 +00:00
```
2019-06-27 02:27:28 +00:00
touch image/ubuntu
mkdir image/.disk
2019-06-26 22:13:38 +00:00
```
2019-06-28 12:54:09 +00:00
## Create ISO Image for a LiveCD (BIOS + UEFI)
2019-06-26 22:13:38 +00:00
2019-06-28 12:54:09 +00:00
1. Access image directory
2019-06-26 22:13:38 +00:00
```
2019-06-28 12:54:09 +00:00
cd $HOME/live-ubuntu-from-scratch/image
2019-06-26 22:13:38 +00:00
```
2019-06-28 12:54:09 +00:00
2. Create a grub UEFI image
2019-06-26 22:13:38 +00:00
```
2019-06-28 12:54:09 +00:00
grub-mkstandalone \
--format=x86_64-efi \
--output=isolinux/bootx64.efi \
--locales="" \
--fonts="" \
"boot/grub/grub.cfg=isolinux/grub.cfg"
2019-06-26 22:13:38 +00:00
```
2019-06-28 12:54:09 +00:00
4. Create a FAT16 UEFI boot disk image containing the EFI bootloader
```
(
cd isolinux & & \
dd if=/dev/zero of=efiboot.img bs=1M count=10 & & \
sudo mkfs.vfat efiboot.img & & \
mmd -i efiboot.img efi efi/boot & & \
mcopy -i efiboot.img ./bootx64.efi ::efi/boot/
)
```
2019-06-27 02:27:28 +00:00
2019-06-28 12:54:09 +00:00
5. Create a grub BIOS image
2019-06-26 22:13:38 +00:00
```
2019-06-28 12:54:09 +00:00
grub-mkstandalone \
--format=i386-pc \
--output=isolinux/core.img \
2019-06-29 22:43:26 +00:00
--install-modules="linux16 linux normal iso9660 biosdisk memdisk search tar ls" \
--modules="linux16 linux normal iso9660 biosdisk search" \
2019-06-28 12:54:09 +00:00
--locales="" \
--fonts="" \
"boot/grub/grub.cfg=isolinux/grub.cfg"
```
6. Combine a bootable Grub cdboot.img
```
cat /usr/lib/grub/i386-pc/cdboot.img isolinux/core.img > isolinux/bios.img
```
2019-06-29 22:22:52 +00:00
7. Generate md5sum.txt
2019-06-28 12:54:09 +00:00
```
2019-06-29 22:22:52 +00:00
sudo /bin/bash -c "(find . -type f -print0 | xargs -0 md5sum | grep -v "\./md5sum.txt" > md5sum.txt)"
2019-06-27 02:27:28 +00:00
```
2019-06-28 12:54:09 +00:00
7. Create iso from the image directory using the command-line
2019-06-27 02:27:28 +00:00
```
2019-06-28 12:54:09 +00:00
sudo xorriso \
-as mkisofs \
-iso-level 3 \
-full-iso9660-filenames \
-volid "Ubuntu from scratch" \
2019-06-29 18:09:21 +00:00
-eltorito-boot boot/grub/bios.img \
-no-emul-boot \
-boot-load-size 4 \
-boot-info-table \
--eltorito-catalog boot/grub/boot.cat \
2019-06-28 12:54:09 +00:00
--grub2-boot-info \
--grub2-mbr /usr/lib/grub/i386-pc/boot_hybrid.img \
-eltorito-alt-boot \
2019-06-29 18:09:21 +00:00
-e EFI/efiboot.img \
-no-emul-boot \
2019-06-28 12:54:09 +00:00
-append_partition 2 0xef isolinux/efiboot.img \
-output "../ubuntu-from-scratch.iso" \
-graft-points \
"." \
/boot/grub/bios.img=isolinux/bios.img \
/EFI/efiboot.img=isolinux/efiboot.img
2019-06-27 14:02:56 +00:00
```