SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2015-09-24 08:10:33

Michele13
#! Member
From: Italy
Registered: 2015-04-16
Posts: 86

Correcting Bugs in a script (mk-livecd-v2.sh)

Hi guys

I wrote a script that should make a LiveCD automatically but it has some problems:

1. It works only with a stable version of debian (don't know why it does not work testing or stable)
2. It cannot delete or move some files inside a directory (all the bugs in the code are signaled inside the code)

Here it is:

#!/bin/bash

DEBIAN_VERSION="$2"
WORKDIR="$PWD/$3"
MIRROR="http://httpredir.debian.org/debian/"
ROOT="$WORKDIR/chroot/"
PACKAGES="grub-pc squashfs-tools aufs-tools man manpages alsa-base alsa-utils alsa-tools live-boot dbus console-data zip unzip bzip2 xz-utils keyboard-configuration tzdata"
KERNEL="linux-image-686-pae"
FIRMWARE="firmware-linux firmware-linux-free firmware-linux-nonfree firmware-b43-installer firmware-b43legacy-installer firmware-atheros firmware-brcm80211 firmware-intelwimax firmware-ipw2x00 firmware-iwlwifi firmware-libertas firmware-myricom firmware-netxen firmware-qlogic firmware-ralink firmware-realtek firmware-zd1211"
KEYRING="debian-archive-keyring"
ISOFILE="$WORKDIR/$4"

function test_function 
{
echo $#
echo $0 $1 $2 $3 $4
}

function usage
{
echo "usage: $0 FUNCTION DEBIAN_VERSION DIRECTORY ISOFILE"
echo ""
echo "list of executable functions:"
echo ""
echo "install (installs the programs that are needed in order to make this script work)"
echo "create (executes all the funcions listed below in order to make a Live CD)"
echo "bootstrap (create the root filesystem of the squashfs file)"
echo "mount_chroot (mounts essential filesystems on the chroot)"
echo "configure (creates essential configuration files)"
echo "install_software (include essential packages in the Live CD)"
echo "clean_chroot (cleans the root filesystem before it is compressed into a squashfs file)"
echo "make_livecd_tree (builds the Live CD final structure)"
echo "make_iso (create a ISO from the Live CD tree)"
exit
}


function check_root
{
if [ $UID -ne 0 ]
then
echo you must run this script as root!
exit
fi
}

# install dependencies

function install
{
apt-get update
apt-get -y install genisoimage debootstrap squashfs-tools grub-pc
exit
}

function bootstrap 
{
mkdir -p $WORKDIR/binary
debootstrap $DEBIAN_VERSION $ROOT $MIRROR
}

function mount_chroot
{
mount none -t proc $ROOT/proc/
mount none -t sysfs $ROOT/sys/
mount none -t devpts $ROOT/dev/pts/

}

function configure
{
# change hostname
echo debian > $ROOT/etc/hostname

# writes sources.list file
rm $ROOT/etc/apt/sources.list

echo "deb http://httpredir.debian.org/debian $DEBIAN_VERSION main contrib non-free" >> $ROOT/etc/apt/sources.list
echo "#deb-src http://httpredir.debian.org/debian $DEBIAN_VERSION main contrib non-free" >> $ROOT/etc/apt/sources.list
echo "" >> $ROOT/etc/apt/sources.list
echo "deb http://httpredir.debian.org/debian $DEBIAN_VERSION-updates main contrib non-free" >> $ROOT/etc/apt/sources.list
echo "#deb-src http://httpredir.debian.org/debian $DEBIAN_VERSION-updates main contrib non-free" >> $ROOT/etc/apt/sources.list
echo "" >> $ROOT/etc/apt/sources.list
echo "deb http://security.debian.org/ $DEBIAN_VERSION/updates main contrib non-free" >> $ROOT/etc/apt/sources.list
echo "#deb-src http://security.debian.org/ $DEBIAN_VERSION/updates main contrib non-free" >> $ROOT/etc/apt/sources.list


# avoids installation of recommended and suggested packages
cat << "EOF" > $ROOT/etc/apt/apt.conf.d/99Recommends

Apt::Install-Recommends "false";
Apt::Install-Suggests "false";
EOF

# set empty root password
chroot $ROOT passwd -d root

}

function install_software
{
# install software
chroot $ROOT apt-get update
chroot $ROOT apt-get -y dist-upgrade
chroot $ROOT apt-get -y install $KEYRING

#ISSUE: this passage still needs the intervention of a user, how can I make it automatic?
chroot $ROOT apt-get -y --install-recommends install $KERNEL $PACKAGES $FIRMWARE 

}

function clean_chroot
{
# clean up $ROOT
chroot $ROOT apt-get clean
umount $ROOT/proc $ROOT/dev/pts $ROOT/sys #BUG: could not umount $ROOT/proc because it's in use
rm -r $ROOT/tmp/*.* #BUG: could not delete temp files
rm $ROOT/var/lib/apt/lists/*.* #BUG: could not delete apt lists downloaded with apt-get update
## chroot $ROOT history -c #this should be a bash functionality
}

function make_livecd_tree
{
# create CD structure

mkdir -p $WORKDIR/binary/boot/grub/
mkdir $WORKDIR/binary/live/

# copy kernel
mv $ROOT/boot/vmlinuz-* $WORKDIR/binary/live/vmlinuz #BUG: could not find the kernel (it's there so why can't I move it?)
mv $ROOT/boot/initrd-* $WORKDIR/binary/live/initrd.img #BUG: could not find the initrd (it's there so why can't I move it?)

# create root squashfs image
mksquashfs $ROOT $WORKDIR/binary/live/000-core.squashfs -b 1M -comp xz -Xbcj x86

# generate grub LiveCD menu
cat << "EOF" > $WORKDIR/binary/boot/grub/grub.cfg
menuentry "Debian (persistence)" {
linux /live/vmlinuz boot=live persistence
initrd /live/initrd.img
}

menuentry "Debian (no persistence)" {
linux /live/vmlinuz boot=live
initrd /live/initrd.img
}
EOF
}

function make_iso
{
grub-mkrescue -o $ISOFILE $WORKDIR/binary/
}

function create 
{
bootstrap
mount_chroot
configure
install_software
clean_chroot
make_livecd_tree
make_iso
}


# if the number of arguments is equal to 0 print usage
if [ "$#" -eq "0" ]
then usage
fi

check_root

# if the first argument is "install" execute the install function
if [ "$1" == "install" ]
 then install
fi


# if the number of arguments is NOT equal to 4 print usage
if [ "$#" -ne "4" ]
then
usage
fi


$1 $@

can you help me to fix them? I'll add more details later, I have to run now smile

Edit: as I was saying with testing it has issues with installing the kernel and aufs-tools. The important thing is that it works at least with stable. If you want yoi can even use it inside bunsen labs, just make sure to write inside the script that I'm the author of this little monster. I release it under GPL, I will add these details later inside it.

Last edited by Michele13 (2015-09-24 08:32:31)

Offline

Be excellent to each other!

#2 2015-09-24 08:55:46

brontosaurusrex
#! Red Menace
Registered: 2012-06-15
Posts: 1,643

Re: Correcting Bugs in a script (mk-livecd-v2.sh)

I'd start with fixing whatever shellcheck.net said, that way you can be mostly sure bugs are probably of logical nature (and not syntax related).

Offline

#3 2015-09-24 11:12:03

Michele13
#! Member
From: Italy
Registered: 2015-04-16
Posts: 86

Re: Correcting Bugs in a script (mk-livecd-v2.sh)

Ok, then what?

Edit: I've tried to fix it myself but I've got no success

Last edited by Michele13 (2015-09-26 13:39:10)

Offline

#4 2015-11-14 12:07:52

Michele13
#! Member
From: Italy
Registered: 2015-04-16
Posts: 86

Re: Correcting Bugs in a script (mk-livecd-v2.sh)

Ok i've fixed those nasty bugs with some workarounds and I'm posting the updated script to you. Don't let this script die, use it and improve it. I'm giving it to you. In my computer it could be deleted any time.

#!/bin/bash

# Copyright (C) 2015 Michele Bucca (michele.bucca@gmail.com)

#This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.



DEBIAN_VERSION="$2"
WORKDIR="$PWD/$3"
MIRROR="http://httpredir.debian.org/debian/"
ROOT="$WORKDIR/chroot"
PACKAGES="grub-pc squashfs-tools aufs-tools man manpages alsa-base alsa-utils alsa-tools live-boot dbus console-data zip unzip bzip2 xz-utils keyboard-configuration tzdata"
#KERNEL_VERSION="3.16.0-4-"
KERNEL="linux-image-686-pae"
FIRMWARE="firmware-linux firmware-linux-free firmware-linux-nonfree firmware-b43-installer firmware-b43legacy-installer firmware-atheros firmware-brcm80211 firmware-intelwimax firmware-ipw2x00 firmware-iwlwifi firmware-libertas firmware-myricom firmware-netxen firmware-qlogic firmware-ralink firmware-realtek firmware-zd1211"
KEYRING="debian-archive-keyring"
ISOFILE="$WORKDIR/$4"

function usage
{
echo "usage: $0 FUNCTION DEBIAN_VERSION DIRECTORY ISOFILE"
echo ""
echo "list of executable functions:"
echo ""
echo "install (installs the programs that are needed in order to make this script work)"
echo "create (executes all the funcions listed below in order to make a Live CD)"
echo "bootstrap (create the root filesystem of the squashfs file)"
echo "mount_chroot (mounts essential filesystems on the chroot)"
echo "configure (creates essential configuration files)"
echo "install_software (include essential packages in the Live CD)"
echo "clean_chroot (cleans the root filesystem before it is compressed into a squashfs file)"
echo "make_livecd_tree (builds the Live CD final structure)"
echo "make_iso (create a ISO from the Live CD tree)"
exit
}


function check_root
{
if [ $UID -ne 0 ]
then
echo you must run this script as root!
exit
fi
}

# install dependencies

function install
{
apt-get update
apt-get -y install genisoimage debootstrap squashfs-tools grub-pc
exit
}

function bootstrap 
{
mkdir -p "$WORKDIR/binary"
debootstrap "$DEBIAN_VERSION" "$ROOT" "$MIRROR"
}

function mount_chroot
{
mount none -t proc "$ROOT/proc/"
mount none -t sysfs "$ROOT/sys/"
mount none -t devpts "$ROOT/dev/pts/"

}

function configure
{
# change hostname
echo debian > "$ROOT/etc/hostname"

# writes sources.list file
rm "$ROOT/etc/apt/sources.list"

echo "deb http://httpredir.debian.org/debian $DEBIAN_VERSION main contrib non-free" >> "$ROOT/etc/apt/sources.list"
echo "#deb-src http://httpredir.debian.org/debian $DEBIAN_VERSION main contrib non-free" >> "$ROOT/etc/apt/sources.list"
echo "" >> "$ROOT/etc/apt/sources.list"
echo "deb http://httpredir.debian.org/debian $DEBIAN_VERSION-updates main contrib non-free" >> "$ROOT/etc/apt/sources.list"
echo "#deb-src http://httpredir.debian.org/debian $DEBIAN_VERSION-updates main contrib non-free" >> "$ROOT/etc/apt/sources.list"
echo "" >> "$ROOT/etc/apt/sources.list"
echo "deb http://security.debian.org/ $DEBIAN_VERSION/updates main contrib non-free" >> "$ROOT/etc/apt/sources.list"
echo "#deb-src http://security.debian.org/ $DEBIAN_VERSION/updates main contrib non-free" >> "$ROOT/etc/apt/sources.list"


# avoids installation of recommended and suggested packages
cat << "EOF" > "$ROOT/etc/apt/apt.conf.d/99Recommends"

Apt::Install-Recommends "false";
Apt::Install-Suggests "false";
EOF

# set empty root password
chroot "$ROOT" passwd -d root

}

function install_software
{
# install software
chroot "$ROOT" apt-get update
chroot "$ROOT" apt-get -y dist-upgrade
chroot "$ROOT" apt-get -y install $KEYRING

#ISSUE: this passage still needs the intervention of a user, how can I make it automatic?
chroot "$ROOT" apt-get -y --install-recommends install $KERNEL $PACKAGES $FIRMWARE

}

function clean_chroot
{
# clean up $ROOT
chroot "$ROOT" apt-get clean
umount -l "$ROOT/proc" #BUG: could not umount $ROOT/proc because it's in use. Using LAZY UMOUNT
umount "$ROOT/dev/pts" 
umount "$ROOT/sys" 
rm -r $ROOT/tmp/
mkdir $ROOT/tmp/
rm -r $ROOT/var/lib/apt/lists/
mkdir $ROOT/var/lib/apt/lists/
## chroot "$ROOT" history -c #this should be a bash functionality
}

function make_livecd_tree
{
# create CD structure

mkdir -p "$WORKDIR/binary/boot/grub/"
mkdir "$WORKDIR/binary/live/"

# move kernel to the live cd
cp $ROOT/vmlinuz $WORKDIR/binary/live/vmlinuz
cp $ROOT/initrd.img $WORKDIR/binary/live/initrd.img

# create root squashfs image
mksquashfs "$ROOT" "$WORKDIR/binary/live/000-core.squashfs" -b 1M -comp xz -Xbcj x86 -e boot

# generate grub LiveCD menu
cat << "EOF" > "$WORKDIR/binary/boot/grub/grub.cfg"
menuentry "Debian (persistence)" {
linux /live/vmlinuz boot=live persistence
initrd /live/initrd.img
}

menuentry "Debian (no persistence)" {
linux /live/vmlinuz boot=live
initrd /live/initrd.img
}
EOF
}

function list 
{
ls $ROOT/boot/vmlinuz-*
ls $ROOT/boot/initrd-*
}

function make_iso
{
grub-mkrescue -o "$ISOFILE" "$WORKDIR/binary/"
}

function create 
{
bootstrap
mount_chroot
configure
install_software
clean_chroot
make_livecd_tree
make_iso
}


# if the number of arguments is equal to 0 print usage
if [ "$#" -eq "0" ]
then usage
fi

check_root

# if the first argument is "install" execute the install function
if [ "$1" == "install" ]
 then install
fi


# if the number of arguments is NOT equal to 4 print usage
if [ "$#" -ne "4" ]
then
usage
fi


"$1" "$@"

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.
Server: acrobat

Debian Logo