Customized Linux CD/USB

Live CD project

1) Step one
Set a virtual machine and load the live CD “livedvd-amd64-multilib-20140826.iso”

Get the gentoo live cd from

# cd ~ 
# wget  https://bouncer.gentoo.org/fetch/gentoo-20140826-livedvd/amd64/wgethttps://ftp.snt.utwente.nl/pub/os/linux/gentoo/releases/amd64/20140826/livedvd-amd64-multilib-20140826.iso

Characteristics of my Virtual Machine are below. You can modify those setting according to your host machine resources

enter image description here

Load the Virtual Machine using the default option

enter image description here

once you are inside of the VM, there are three things that you have to change

1) Active the sshd

# sudo /etc/init.d/sshd start

2) Change the roort password

# sudo passwd root 

3) Get your ip

# ifconfig 
# emerge -av squashfs-tools <---- help to create the Filesystem later

*From now, you can access the virtual machine from your favorite terminal (I’m using Debian), if you are in windows, use putty.

Once you get inside of your live cd, you can type

# lsblk

As you can see, I created a 40G HD

NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0      2:0    1    4K  0 disk 
sda      8:0    0   40G  0 disk <------Here! 
sr0     11:0    1  2.8G  0 rom  /mnt/cdrom
loop0    7:0    0  2.6G  1 loop /mnt/livecd

Well, in order to access those 40G and build our customized LiveCD, we have to create a partition and give it format.

I’m going to use cfdisk in order to create the partition

cfdisk /dev/sda

Give format to the new partition sda1

mkfs.ext4 /dev/sda1

Create a new folder and mount the partition

# mkdir -p /mnt/liveCD
# mount /dev/sda1 /mnt/liveCD
# mount

vartmp on /var/tmp type tmpfs (rw)
tmp on /tmp type tmpfs (rw)
distfiles on /usr/portage/distfiles type tmpfs (rw)
/dev/sda1 on /mnt/liveCD type ext4 (rw)  <----here!

Get into the work folder

# cd /mnt/liveCD/

We need to use a linux template in order to create our linux. We could use the gentoo LiveCD that we have downloaded but it is to big (almost 3Gs). So lets get something smaller (430M).

# wget https://sourceforge.net/projects/systemrescuecd/files/sysresccd-x86/4.5.3/systemrescuecd-x86-4.5.3.iso

Note: This ISO suppose to be a hybrid version of linux (32bit/64bits). However, it is not. It has a bunch of kernels for different platforms included the 64bits kernel but the squash.img was created from the stage3/32bits. So if you try to compile something for 64 bits, it will not work. Don’t worry, we are going to download our stage3/64bits and our own kernel.

Let’s have fun!

Create a new directory. This is going to be actually our new livecd, everything we put inside of this directory is going to be wrapped into the final ISO

   # mkdir -p /mnt/liveCD/newlivecd
   # mkdir -p /mnt/liveCD/iso

Mount the systemrescuecd iso to /mnt/liveCD/iso

mount -o loop systemrescuecd-x86-4.5.2.iso iso/
mount: /dev/loop1 is write-protected, mounting read-only

Access to the “iso/” folder and copy all the files/folders to the new folder “newlivecd/”

# cd /mnt/liveCD/iso
# cp -R boot/ ../newlivecd
# cp -R efi/ ../newlivecd
# cp -R isolinux/ ../newlivecd

Now, get access to the “newlivecd/”

# cd /mnt/liveCD/newlivecd

The purpose of this tutorial is understand how cdlives works, so I’m going to delete a bunch of stuff from the systemrescuecd. Lets start with something simple but functional. ( I eliminated the hash tags so you can copy the following lines and paste it into your terminal)

Get into the folder isolinux/

# cd /mnt/liveCD/newlivecd/isolinux

Delete the following file/folders

rm altker32
rm altker64
rm chain.c32
rm f1boot.msg
rm f2images.msg
rm f3params.msg
rm f4arun.msg
rm f5troubl.msg
rm f6pxe.msg
rm f7net.msg
rm ifcpu64.c32
rm initram.igz
rm kbdmap.c32
rm -rf maps
rm memdisk
rm menu.c32
rm netboot
rm pxelinux.0
rm reboot.c32
rm rescue32
rm rescue64
rm isolinux.old
rm isolinux.cfg

Create a new file

# vi isolinux.cfg

Paste the following code into the file and save it

UI vesamenu.c32
MENU TITLE Gentoo Linux - Mrwhitebp EDITION
MENU BACKGROUND back.png
MENU RESOLUTION 800 600
MENU WIDTH 77
MENU MARGIN 10
MENU ROWS 12
MENU HSHIFT 10
MENU VSHIFT 5
menu color title        1;36;44    #b5b3ff #00000000 std
menu color sel          7;37;40    #e0ffffff #311f39 all
menu color tabmsg       31;40      #90ffffff #311f39 std
menu color timeout      1;37;40    #ff0000 #00000000 std
menu color help         37;40      #c0ffffff #00000000 std
menu color msg07        37;40  
default gentoo-x86_64
timeout 150
# ------------------------------------------------------------------------------

label gentoo-x86_64
MENU LABEL Gentoo x86_64(Mrwhite)
kernel /isolinux/gentoo
append root=/dev/ram0 init=/linuxrc aufs looptype=squashfs loop=/image.squashfs cdroot initrd=/isolinux/gentoo.igz nomodeset
TEXT HELP
Boot Gentoo x86_64
ENDTEXT

Now create the file “livecd” required to mount the filesystem (legacy and Efi)

# cd /mnt/liveCD/newlivecd
# touch livecd
# cd /mnt/liveCD/newlivecd/boot
# touch livecd

Now, it is time to download the stage3 from the Gentoo website

# cd /mnt/liveCD/newlivecd

The stage3 is published every day so do not follow the link below. Basically, go to the Gentoo website and download the stage3 64 bits. here is the link

https://www.gentoo.org/downloads/

Now, we are going to create our squashfs that represents the linux filesystem.

# mkdir  -p /mnt/liveCD/newlivecd/squashfs

Decompress the stage3 file into the squashfs folder

# tar -axvpf stage3-amd64-[].tar.bz2 -C squashfs/

Get into the folder squashfs

# cd /mnt/liveCD/newlivecd/squashfs

Now , we have to mount a few things for compilation purposes (be careful with the slashes, they are meaning a lot)

# mount --rbind /proc/ proc/
# mount --rbind /dev dev/
# mount --rbind /sys  sys/
# cp /etc/resolv.conf /mnt/liveCD/newlivecd/squashfs/etc/
# cp /proc/config.gz .

It is time to Chroot our new system

# chroot . /bin/bash
# source /etc/profile
# export PS1="(chroot) $PS1"
# ping www.yahoo.com

Make sure that your Internet is working and run the following command in order to get portage

# emerge-webrsync
# emerge -avuDN --with-bdeps y --keep-going world

Install vim ( personal preferences)

# emerge -av vim

Now, install the kernel sources (personal preferences)

# emerge -av aufs-sources
# update-env

There are some requirements for the kernel so make sure to add the following packages and then install the kernel again

# echo "=sys-fs/aufs-headers-4.0_p20150420 ~amd64" >> /etc/portage/package.accept_keywords
# echo "=sys-kernel/aufs-sources-4.0.5 ~amd64" >> /etc/portage/package.accept_keywords
# echo "=sys-fs/aufs-util-4.0_p20150420 ~amd64" >> /etc/portage/package.accept_keywords
# emerge -av aufs-sources

Now let’s copy the configuration file from our livecd, it is going to save us a lot of time of kernel tuning.

# cd /
# zcat config.gz > usr/src/linux/.config
# cd /usr/src/linux
# make olddefconfig

In case you want to add extra functionality edit the kernel

# make menuconfig
# make
# make modules
# make modules_install
# make install
# emerge -av genkernel 
# genkernel intramfs

Ok, We have built our modules and kernel. We will back later!

Now, lets install software into it. In order to avoid errors during the compilation, we have to edit the make.conf file

# vi /etc/portage/make.conf

add the following line at the end of the file

MAKEOPTS="-j1" 

Let’s try LVM2

# emerge -av lvm2

After finish the install Linux volume manager, you can comment the previous line into the make.conf. Otherwise you will be compiling software with only one processor.

Let’s try more software

# emerge -av cryptsetup
# emerge -av mdadm
# emerge -av squashfs-tools
# emerge -av efibootmgr
# emerge -av gdisk
# emerge -av gptfdisk
# emerge -av cdrkit
# emerge -av dhcpcd 
# rc-update add dhcpcd boot 
# emerge -av sshd  
# rc-update add sshd boot  
# passwd root  <-----change the root password 

Now, it is time to install zfs. Copy the following line and paste the on the terminal

# echo "=sys-fs/zfs-kmod-0.6.4 ~amd64" >> /etc/portage/package.accept_keywords
# echo "=sys-kernel/spl-0.6.4 ~amd64" >> /etc/portage/package.accept_keywords
# echo "=sys-fs/zfs-0.6.4 ~amd64" >> /etc/portage/package.accept_keywords 
# emerge -av zfs 
# touch /etc/mtab
# zpool status

You can continue adding more software….

Now, we are going to build our ISO

First, exit from the CHROOT

# exit 
# cp boot/* ../isolinux/  <---copying the kernel and the initramfs
# umount -l proc/
# umount -l sys/
# umount -l dev/
# cd /mnt/liveCD/newlivecd/ 
# tar -zxvf squashfs_back.tar.gz squashfs/ <---- Backup your filesystem just in case
# cp -R squashfs_back.tar.gz ../   <---- move it out from our work directory

We have to install squashfs also in the gentoo cdlive

   # rm stage3-amd64-20150604.tar.bz2 <-----delete the stage3 file
   # mksquashfs squashfs/ image.squashfs -comp xz
   # rm -rf squashfs/

So, we have our filesyetm ready and our folder should looks like this

# ls 
boot  efi  image.squashfs  isolinux  livecd  

We have to rename the kernel and the initramfs according to our isolinux.cfg file

# mv initramfs-genkernel-x86_64-4.0.5-aufs gentoo.igz
# mv  vmlinuz-4.0.5-aufs gentoo 
# mv  System.map-4.0.5-aufs System.map-gentoo 
# mv config-4.0.5-aufs config-gentoo

Now, we have to genereta our grub file for EFI boots

go to the directory

# cd /mnt/liveCD/newlivecd/boot/grub
# rm grub-452.cfg

Create the file grub.cfg and paste into the file the following code

# vi gru.cfg 

Code below

# Global options
set timeout=90
set default=0

# Display settings
if loadfont /boot/grub/font.pf2 ; then
        set gfxmode=auto
        insmod efi_gop
        insmod efi_uga
        insmod gfxterm
        insmod videotest
        insmod videoinfo
        terminal_output gfxterm
fi

menuentry "Gentoo zfs and efi (64bit, odin blanco)" {
set gfxpayload=keep
linux /isolinux/gentoo root=/dev/ram0 init=/linuxrc aufs looptype=squashfs loop=/image.squashfs cdroot vconsole.keymap=$bootkeymap locale=$bootlang $modeset_opt $nox $acpi_opt console=tty1 nomodeset
        initrd /isolinux/gentoo.igz

}

It is time to create the ISO and cross our fingers

The struture directory under /mnt/liveCD/newlivecd, should looks like this

boot/  efi/  image.squashfs  isolinux/  livecd 

Change to the folder

# /mnt/liveCD/newlivecd/boot/grub
# cp /mnt/cdrom/isolinux/efiboot.img /mnt/liveCD/newlivecd/isolinux/
# cd /mnt/liveCD 
# mkisofs -o gentoo_light_zfs_mrwhitebp_64bits.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -eltorito-alt-boot -b isolinux/efiboot.img -no-emul-boot -U -J -joliet-long -r -v -T newlivecd/

Let’s try the new iso.

I’m copy the ISO from the Virtual machine to my debian box

# scp gentoo_light_zfs_mrwhitebp_64bits.iso mrwbp@192.168.1.117:/home/mrwbp

Create another virtual machine in vmware or virtuabox and use the new ISO

enter image description here

We are logged into the new ISO and tested the connection

enter image description here

we are also able to test zfs, there are not pools available but the zfs support is already there.

enter image description here

Finally, we have to test EFI, so I’m going to make some changes on the virtual machine in order to boot with EFi BIOS.

edit the file *.vmx on your vmware (I don’t know about virtualbox sorry)

# vi gentoo-zfs-64.vmx 

Add the following lines and save it

firmware = "efi"
bios.forceSetupOnce = "FALSE"

Run the virtual machine again, You will see diffent menu because Grub is responding now.

I’m going to burn this img to a USB and try to boot my laptop, someone at work told me that 64 iso should work on a 32 bits computers. Lets check it out.

I don’t have CD’s so I need to convert this ISO to a USB compatible. In order to do that I’m going to install a packages in debian

# apt-get install syslinux-utils
# isohybrid path/to/image.iso  <---converting the ISO
# dd if=path/to/image.iso of=/dev/sde <--writing to the the USB

enter image description here