Custom Linux Mint bootable image (USB/CD)

There have been a number of posts on how to create your own Linux Mint bootable image. There was a need to create one recently, and I noted a number of things that were addressed by others, but not in one document. This post is an attempt to create one document that will outline how to create a custom Linux Mint image, using Linux mint 18.1 as of this writing, and write this to a bootable USB flash drive.


Most of the initial part of this post was lifted from this page.

First off, we need to create a local copy of Linux mint, and chroot into that environment. From there, we can modify the base and add/remove applications and set some defaults. We would then squash that, make a new image and write it to a disk, with some caveats. You will need some prerequisites, so first:


sudo aptitude install syslinux squashfs-tools genisoimage

You can call the directory we are going to work in anything you want, for this post lets call it mylivecd. Download linuxmint-18.1-cinnamon-64bit (or change to suit your needs).


mkdir ~/mylivecd
cp /path_to_the_downloaded_iso_directory/linuxmint-18.1-cinnamon-64bit.iso ~/mylivecd
cd ~/mylivecd
mkdir mnt
sudo mount -o loop linuxmint-18.1-cinnamon-64bit.iso mnt
mkdir extracted
sudo rsync --exclude=/casper/filesystem.squashfs -a mnt/ extracted
sudo unsquashfs mnt/casper/filesystem.squashfs
sudo mv squashfs-root edit

You can copy over your /etc/resolv.conf, and your /etc/hosts, or you can edit them directly. I prefer to edit directly as my resolv.conf usually has something wonky in it from a stray VPN connection.

Don’t blindly do this if you have custom networks/hosts.


sudo cp /etc/resolv.conf edit/etc/
sudo cp /etc/hosts edit/etc/

You can copy/paste the following (chroot and mount filesystems!):


sudo mount --bind /dev/ edit/dev
sudo chroot edit
mount -t proc none /proc
mount -t sysfs none /sys
mount -t devpts none /dev/pts
export HOME=/root
export LC_ALL=C
dbus-uuidgen > /var/lib/dbus/machine-id
dpkg-divert --local --rename --add /sbin/initctl
ln -s /bin/true /sbin/initctl

Now, you are in your chroot, and you are ready to get customizing. If you want to do anything to the system, you need to do it here in this terminal. It is a good idea to open a new terminal so you can copy/paste some configs if you need to… just remember which one is the chroot. If you want to be totally clear, change your prompt in your chroot terminal:


export PS1="(chroot) $PS1"

So, whatever you need/want to install, do it now. However, for good measure, first do:


apt-get update

Now, “Player one, go NUTS!”. Install whatever you like. Whatever you need, install and configure it now.

If you want to add a menu item to the linux mint cinnamon menu, reference this page. In a nutshell, you would:


cd /usr/share/applications

Copy an existing application to yours. For example, if Hexchat is installed, and you want an icon for epic5:


cp hexchat.desktop epic5.desktop

Since they are both internet utils, it will already be in the menu that you want. Edit epic5.desktop, and change the relevant parts from HexChat to epic5. Pay attention to the application caller:

change Exec=hexchat %U to Exec=epic5

and be sure to do a search and replace on HexChat (e.g. in vim :%s/HexChat/epic5/)

For me a “todo item” is to remove the “Install Linux Mint” Icon from the desktop, as I was giving this USB to people to use as a plug-in development environment. To do this (which is not necessary):

cd /usr/share/applications
rm ubiquity.desktop

Note, if you wish to install later, you would do this from a shell in cinnamon:

sh -c 'ubiquity gtk_ui'

Which is the command the icon is executing.

***Begin totally optional part***
One thing that is interesting to do is change the default Firefox so that it shows your own homepage, or has bookmarks that might be more relevant. The easiest way (so far) to do this was to create a new profile on an existing linux box (you are doing this from linux we presume). In your own linux box (not the chroot):


/usr/bin/firefox -profileditor

Create a new profile, start that profile, edit that profile in firefox (e.g. your own homepage, bookmarks, search engines, etc) then save that profile (you can just exit). Now, you have to get that profile over to the chroot. One way is to use an intermediary… so, lets just say that the user that created the profile was you, and the profile is now called fgjw5nes.mint (since you called it mint).


cd .mozilla/firefox
tar -cvf myffprofile.tar fgjw5nes.mint
scp myffprofile.tar :~

Now back in your chroot, scp the file over there. We will also just obliterate the default mozilla profile since we are not going to use it. Lets assume the default profile in the chroot is called mwad0hks.default.


cd /etc/skel/.mozilla/firefox
scp :~/myffprofile.tar .
tar -xvf myffprofile.tar
mv fgjw5nes.mint mwad0hks.default

Now what you have done is created a local firefox profile on your local machine, saved it, and copied it to the chroot environment. You only need to do that if you want custom firefox by default.

***End totally optional part***

Finally, if you want to install any deb packages by hand, you need to download them to a directory in the chroot and install them from there. e.g.


dpkg -i *.deb

***Begin totally optional part***
After you are done, if you wish to install a new kernel, do it now as per this document. After that you would need to do this (you only need to do this if you installed a new kernel!)


cp edit/boot/vmlinuz* extracted/casper/vmlinuz
cp edit/boot/initrd.img* extracted/casper/initrd.lz

***End totally optional part***

Make sure the system is up to date:


apt install ubiquity-frontend-gtk

Finish up, unmount, get out, su again, fix files, squash the fs, make new MD5SUMS. Best to copy/paste this:


aptitude clean
rm -r /var/cache/apt/archives/*
rm -r /mydir
rm -rf /tmp/* ~/.bash_history
rm /var/lib/dbus/machine-id
rm /sbin/initctl
dpkg-divert --rename --remove /sbin/initctl
umount /proc || umount -lf /proc
umount /sys
umount /dev/pts
exit
sudo umount edit/dev
sudo su
chmod +w extracted/casper/filesystem.manifest
chroot edit dpkg-query -W --showformat='${Package} ${Version}\n' > extracted/casper/filesystem.manifest
cp extracted/casper/filesystem.manifest extracted/casper/filesystem.manifest-desktop
sed -i '/ubiquity/d' extracted/casper/filesystem.manifest-desktop
sed -i '/casper/d' extracted/casper/filesystem.manifest-desktop
rm extracted/casper/filesystem.squashfs
mksquashfs edit extracted/casper/filesystem.squashfs -comp xz
printf $(sudo du -sx --block-size=1 edit | cut -f1) > extracted/casper/filesystem.size
cd extracted

Here we should rename the disk. Not totally mandatory, but…

vi README.diskdefines

Rename DISKNAME to something like Linux Mint 18.1 “mycoolname” – Release amd64
Save that file.

rm MD5SUMS
find -type f -print0 | sudo xargs -0 md5sum | grep -v isolinux/boot.cat | sudo tee MD5SUMS

Oh-Kay, now we have our files and all ready to make our ISO. Ready? We will call this Linux-Mint-custom.iso The exit again is because you are root. Be careful.


mkisofs -D -r -V "$IMAGE_NAME" -cache-inodes -J -l -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -o ../Linux-Mint-custom.iso .
chmod 777 ../Linux-Mint-custom.iso
exit

Now, there is one problem with this image. If you write/convert this to a bootable USB using a tool such as usb-creator-gtk (which is what I recommend for now), you will find that the USB is not bootable. Let us fix that.

Install usb-creator-gtk:


sudo apt-get install usb-creator-gtk

Run it, insert a USB stick big enough (recall earlier, we wrote the size to extracted/casper/filesystem.size) and choose that file (your Linux-Mint-custom.iso). Have it write it to the USB stick. After it is done, re-attach it (you can pull it and re-insert it if you like). Now find where that stick is mounted:


mount

You should see your entry for the USB stick (under Mint it would be something like /media/user/. Lets assume it is called usbstick.

You now need to do a:


cp /usr/lib/syslinux/vesamenu.c32 /media/user/usbstick/syslinux/

Recall, you need to replace user and usbstick in the above command with your values. Now the USB stick should boot and behave just like a normal Linux Mint install cd/media, but it is with the changes you made.

TODO0: In the start, when we copy resolv.conf and hosts over, we should copy /usr/lib/syslinux/vesamenu.c32 to mylivecd/extracted/isolinux/ At this point, it is not clear if this is the directory that will get transferred over to syslinux.

TODO1: In this post, the mkisofs does not use -D “Do not use deep directory relocation”, which the man page gives a warning about using (it violates the ISO9660 standard). Is it necessary?

This entry was posted in Desktops. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *