_       _       
  (_) ___ | |_ ____
  | |/ _ \| __|_  /
  | | (_) | |_ / / 
 _/ |\___/ \__/___|
|__/               

Using Debian Live to create a custom live USB Linux distribution

date: 13 Dec 2023

Introduction

As a starting point, I adapted the examples provided here:

Preparing to use Debian Live

First, boot the PC into Debian 11 (Bullseye) using the standard USB live image (or an existing custom live image, which is what I use).

sudo apt update
sudo apt install live-build

If you're going to build the custom image on a removable drive, you may need to remount it with the appropriate flags set, as shown below (replace the path shown with the one for your device).

sudo mount -o remount,exec,dev /media/user/[YOUR DEVICE NAME GOES HERE]

Custom build configuration

mkdir MyDebLive
cd MyDebLive/

The following command creates a configuration for the build process.

lb config -d bullseye --archive-areas "main contrib non-free" --parent-archive-areas "main contrib non-free" --bootappend-live "boot=live locales=en_GB.UTF-8 keyboard-layouts=gb timezone=Europe/Dublin user-default-groups=user,cdrom,floppy,sudo,audio,dip,video,plugdev,netdev,scanner,dialout"

Notes:

  1. Added "--archive-areas" and "--parent-archive-areas" options to include non-free, so that wifi firmware would be included. (Note: also added firmware-linux to package list.)
  2. Added dialout group to list of live user's groups using "--bootappend-live" option so that serial port can be accessed by e.g. arduino.

Selecting packages for custom build

Create a file my.list.chroot containing the list of packages to be included in the custom build.

echo "task-xfce-desktop firmware-linux build-essential printer-driver-all v4l2loopback-dkms exfat-utils live-build vim-gtk geany gedit tilde micro hexedit inkscape gimp imagemagick ristretto feh gphoto2 ffmpeg mplayer vlc mpg123 audacity blender kdenlive obs-studio screenkey gromit-mpx chromium falkon lynx w3m elinks wget curl filezilla freerdp2-x11 gcc clang git gdb freeglut3-dev gcc-arm-none-eabi gdb-multiarch openocd python3-matplotlib python3-scipy python3-serial php octave julia tmux xterm stterm kitty pv xclip htop bpytop fzf gdu ncdu bat ranger mc trash-cli neofetch wmctrl xdotool testdisk cmatrix hollywood pandoc pandoc-citeproc texlive texlive-xetex fonts-ibm-plex fonts-ebgaramond-extra fonts-ebgaramond fonts-adf-universalis fonts-beteckna fonts-linuxlibertine gpxviewer gpsbabel" > config/package-lists/my.list.chroot

Changes in next version:

  1. Add figlet
  2. Add wine for e.g. LTspice (just "sudo apt install wine" works fine with portable LTspice zip version)
  3. Maybe add psutils? (for the likes of psnup)
  4. Maybe add texlive-extra-utils (for pdfjam, which allows multiple pdf pages to be combined into one)

Changes in 13-Jan-2023 version:

  1. Re-organized packages in list for better readability and to hopefully fix file associations.
  2. Added freerdp2-x11 back in.
  3. Replaced vim with vim-gtk to enable clipboard functionality (still runs in terminal).
  4. Added terminal emulators: xterm stterm kitty
  5. Added mpg123 for internet radio.
  6. Removed tldr, because it required additional step to actually download documentation before it worked.
  7. Added steps to create .vimrc and .tmux.conf in the home directory of the live system.

Changes in 13-Dec-2022 version:

  1. Additional packages for pandoc references: pandoc-citeproc
  2. Additional packages for ARM development: gcc-arm-none-eabi gdb-multiarch openocd (add Frank Duignan's build script, etc. also?)
  3. System monitors / process control: htop, bpytop
  4. Useful free font family from IBM: fonts-ibm-plex
  5. Terminal text editors: vim tilde micro
  6. Terminal multiplexer: tmux
  7. Fuzzy find tool: fzf
  8. Disk usage analyzers: gdu, ncdu
  9. Alternative to cat with extra stuff: bat
  10. File managers: ranger, mc
  11. Command line apps: trash-cli, neofetch (display system info summary)
  12. GNU debugger: gdb
  13. Terminal browser(s): lynx, w3m, elinks
  14. Eye candy: cmatrix, hollywood?
  15. utilities to copy text to system clipboard (e.g. in Vim): xclip
  16. Community driven man pages alternative: tldr (need to run "tldr -u" after install to update content)
  17. Removed these: texstudio texlive-latex-extra darktable openfortivpn freerdp2-x11

Automatic login script to apply final desktop tweaks

Add some additional commands to a setup script, "setup.sh", that will be placed in the default user's home directory. This can be used to make some final tweaks to the desktop (e.g. set number of workspaces, remove desktop icon) at login time:

mkdir -p config/includes.chroot/etc/skel
cat > config/includes.chroot/etc/skel/setup.sh << 'EOF'
#!/bin/sh

# Set number of workspaces to two
xfconf-query -c xfwm4 -p /general/workspace_count -s 2

# Hide desktop icons
xfconf-query -c xfce4-desktop --create -t bool -p /desktop-icons/file-icons/show-filesystem -s false
xfconf-query -c xfce4-desktop --create -t bool -p /desktop-icons/file-icons/show-home -s false
xfconf-query -c xfce4-desktop --create -t bool -p /desktop-icons/file-icons/show-removable -s false
xfconf-query -c xfce4-desktop --create -t bool -p /desktop-icons/file-icons/show-trash -s false

# Identify connected monitor and store name in shell variable
monitor=`xrandr -q | sed -n '0,\|[[:space:]]connected[[:space:]]| s/^\([^[:space:]]*\)[[:space:]]\+connected[[:space:]].*$/monitor\1/ p'`

# Disable wallpaper on connected monitor (probably sufficient to set workspace0 only)
xfconf-query -c xfce4-desktop -pn /backdrop/screen0/$monitor/workspace0/image-style -t 'int' -s 0
xfconf-query -c xfce4-desktop -pn /backdrop/screen0/$monitor/workspace1/image-style -t 'int' -s 0

# Disable wallpaper (NB this version not working)
#for property in $(xfconf-query -c xfce4-desktop -l | grep image-style); do xfconf-query -c xfce4-desktop -p $property -s 0 ; done
#xfconf-query -c xfce4-desktop -pn /backdrop/screen0/monitor0/image-style -t 'int' -s 0
#xfconf-query -c xfce4-desktop -pn /backdrop/screen0/monitor1/image-style -t 'int' -s 0

# Enable touchpad tap to click
xfconf-query -c pointers -pn /SynPS2_Synaptics_TouchPad/Properties/libinput_Tapping_Enabled -t 'int' -s 1
xfconf-query -c pointers -pn /DELL0A2000_06CBCE65_Touchpad/Properties/libinput_Tapping_Enabled -t 'int' -s 1

# Create browser profiles one and two
firefox -CreateProfile one
firefox -CreateProfile two

# Set up shortcuts
echo "firefox -P one -private-window about:profiles &" > ~/one
echo "firefox -P two -private-window about:profiles &" > ~/two

chmod 755 ~/one ~/two
EOF

Set the mode of "setup.sh" so that it's executable:

chmod +x config/includes.chroot/etc/skel/setup.sh

To run the above setup script automatically at login, autostart it by creating the file MyDebLive.setup.desktop, as follows:

mkdir -p config/includes.chroot/etc/skel/.config/autostart
cat > config/includes.chroot/etc/skel/.config/autostart/MyDebLive.setup.desktop << 'EOF'
[Desktop Entry]
Encoding=UTF-8
Version=0.9.4
Type=Application
Name=MyDebLive.setup
Comment=
Exec=/home/user/setup.sh
OnlyShowIn=XFCE;
RunHook=0
StartupNotify=false
Terminal=false
Hidden=false
EOF

Add dot files for vim and tmux

Create ".vimrc":

cat > config/includes.chroot/etc/skel/.vimrc << 'EOF'
" Disable compatibility with vi
set nocompatible

" Filetype detection and configuration
filetype on
filetype plugin on
filetype indent on

" Syntax highlighting
syntax on

" Remap up and down arrow keys to move cursor
" one screen line in normal and visual modes
:noremap <Up> gk
:noremap <Down> gj

" Remap up and down arrow keys to move cursor
" one screen line in insert mode
:inoremap <Up> <C-o>gk
:inoremap <Down> <C-o>gj

" Enable line numbers
set number
"set nonumber

" Set tab width
set shiftwidth=4
set tabstop=4
set expandtab

" Set colour scheme
colorscheme ron 

" Disable Vim mouse interaction, which re-enables
" regular terminal highlighting with mouse
set mouse=
EOF

Create ".tmux.conf":

cat > config/includes.chroot/etc/skel/.tmux.conf << 'EOF'
set-option -g escape-time 10
set-option default-terminal tmux-256color
EOF

Note: The default terminal type in tmux is "screen", which seems to be causing a problem with the micro editor. Specifically, using the shift key and e.g. arrow keys to select text is not working. There are a few workarounds that seem to get it working. One is to start micro (within tmux) as follows...

TERM=tmux micro main.c

or like this...

TERM=tmux-256color micro main.c

The alternative is to set it in ".tmux.conf" as shown above.

Fix for wireless keyboard scrolling post-resume from suspend

Use a heredoc to create a file "/lib/systemd/system-sleep/post-suspend.sh" in the live system that will reload the usbhid module every time the system resumes after suspend. The file permissions are also changed to make the file executable.

mkdir -p config/includes.chroot/lib/systemd/system-sleep
cat > config/includes.chroot/lib/systemd/system-sleep/post-suspend.sh << 'EOF'
#!/bin/bash
if [ "${1}" == "post" ]; then
modprobe -r usbhid && modprobe usbhid
fi
EOF
chmod +x config/includes.chroot/lib/systemd/system-sleep/post-suspend.sh

EXPERIMENTAL: Configure GRUB boot menu

Build the live image

As root, build the bootable live image. This may take several minutes.

sudo lb build

Write the image to a USB key

Still as root, copy the completed image to a USB drive. This may take several minutes to complete. The sync command ensures that all data has been written to the USB drive before it is removed.

sudo cp live-image-amd64.hybrid.iso /dev/sdb
sudo sync

The USB key can now be unplugged and should be ready to use.

Create a backup copy of the iso with today's date added to the filename:

mv live-image-amd64.hybrid.iso `date +%Y.%m.%d_live-image-amd64.hybrid.iso`