#!/bin/bash
clear;
echo "Welcome to Rosspace OS installation CD.";
exit_menu=0;
while [ $exit_menu -ne 1 ]
do
echo "Select what do you want to do.";
echo "[a] start installation routine (DANGEROUS!)";
echo "[b] backup files from the current OS as images to /var/www/";
echo "[c] drop to command line";
echo "[d] reboot";
echo -e "Type your choice and press enter: \c";
read choice
case "$choice" in
a)
exit_menu=1;
;;
b)
file_ok=0;
while [ $file_ok -ne 1 ]
do
if [ -z $1 ]; then
echo "Please enter device name of the hard drive you want to backup.";
echo "Usually Primary Master is hda, Primary Slave is hdb, etc.";
echo "SATA drive 1 is usually sda, SATA drive 2 - sdb and so on.";
echo "Possible devices are:"
ls /dev/hd* 2>/dev/null | grep -v [0-9] 2> /dev/null | sed -e "s/\/dev\///"
ls /dev/sd* 2>/dev/null | grep -v [0-9] 2> /dev/null | sed -e "s/\/dev\///"
echo -e "Enter device name: \c";
read devname;
fi
if [ -b "/dev/$devname" ]; then
if [ -O "/dev/$devname" ]; then
file_ok=1;
else
file_ok=0;
fi
else
file_ok=0;
fi
if [ $file_ok -ne 1 ]; then
echo "The path to file $devname is not correct.";
echo "Possible causes are: you are not root (try sudo $0 $devname), the file doesn't exist (check spelling)";
echo "or the file is not a block device, therefore it is not possible to install OS here.";
fi
done
~/backup.sh ${devname}
;;
c)
echo "Type 'exit' to exit shell and return to main menu."
/bin/bash
;;
d)
reboot;
;;
*)
clear;
;;
esac
done
file_ok=0;
while [ $file_ok -ne 1 ]
do
if [ -z $1 ]; then
echo "Please enter device name of the hard drive you want to install on.";
echo "Usually Primary Master is hda, Primary Slave is hdb, etc.";
echo "SATA drive 1 is usually sda, SATA drive 2 - sdb and so on.";
echo "Possible devices are:"
ls /dev/hd* 2>/dev/null | grep -v [0-9] 2> /dev/null | sed -e "s/\/dev\///"
ls /dev/sd* 2>/dev/null | grep -v [0-9] 2> /dev/null | sed -e "s/\/dev\///"
echo -e "Enter device name: \c";
read devname
else
devname=$1
fi
if [ -b "/dev/$devname" ]; then
if [ -O "/dev/$devname" ]; then
file_ok=1;
else
file_ok=0;
fi
else
file_ok=0;
fi
if [ $file_ok -ne 1 ]; then
echo "The path to file $devname is not correct.";
echo "Possible causes are: you are not root (try sudo $0 $devname), the file doesn't exist (check spelling)";
echo "or the file is not a block device, therefore it is not possible to install OS here.";
fi
done
echo "Here are parameters of the selected hard drive.";
echo "-----------------------------------------------";
fdisk -l /dev/$devname
echo "-----------------------------------------------";
echo "If this is the disk you wanted to use for installation,";
echo "type proceed here, or press RESET to reboot.";
echo "!!!WARNING!!! IF YOU CONTINUE, ALL EXISTING DATA ON /dev/$devname WILL BE LOST.";
echo -e "Type \"proceed\" to start installation: \c";
read start
if [ -z $start ]; then # the line is empty
start="";
fi
while [ "$start" != "proceed" ]
do
echo -e "Type \"proceed\" if you want to proceed with the installation or press RESET to reboot: \c";
read start
done
echo .
echo "If you enter an IP address or a hostname here, the root";
echo "filesystem image will be downloaded from http://name/rootfs.tbz";
echo "as the network is faster than USB1.1."
echo "If you press enter, the file will be taken from this CD";
echo "from the folder where this script resides."
echo -e "Enter an IP address or hostname of your server or press Enter to continue: \c";
read servername
echo "Stage 1: Erasing hard drive /dev/$devname's partition table...";
dd if=/dev/zero of=/dev/$devname bs=512k count=1 > /dev/null
echo "Partition table is erased.";
echo "Stage 2: Creating partitions...";
echo "n
p
1
+128M
n
p
2
t
1
83
t
2
83
a
1
w" | fdisk /dev/$devname
echo "Partitions created successfully."
echo "Stage 3: building a filesystem on the hard drive...";
mke2fs -j /dev/${devname}1
mke2fs -j /dev/${devname}2
echo "Filesystem created successfully.";
echo "Stage 4: creating mountpoint /mnt/$devname and mounting HDD there...";
mkdir /mnt/$devname
mount /dev/${devname}2 /mnt/$devname
echo "Mounted successfully.";
echo "Stage 5: copying filesystem tarball to $devname started on `date`...";
if [ -z $servername ]; then
echo "Copying installation files from CD started...";
cp /media/cdrom/rootfs.tbz /mnt/$devname
cp /media/cdrom/boot.tbz /mnt/$devname
else
cd /mnt/$devname
wget http://$servername/rootfs.tbz
# | echo "There was an error while downloading root tarball.";\
# echo "Tried URL http://$servername/rootfs.tbz";\
# echo "Please check an address and network connection.";\
# echo "The installation cannot continue.";\
# exit 0;
wget http://$servername/boot.tbz #| echo "There was an error getting GRUB boot data.";\
# echo "Tried URL http://$servername/boot.tbz";\
# echo "The system will not be bootable. Check an address and network connection.";\
# exit 0;
fi
echo "Copied successfully on `date`.";
echo "Stage 6: unpacking tarball to $devname...";
cd /mnt/$devname && tar -jxvf rootfs.tbz
echo "Unpacking completed successfully.";
echo "Stage 7: setting hostname for the new machine.";
echo -e "Please enter a hostname for the new machine. It may contain only latin letters and numbers.";
while [ -z $hostname ]
do
echo -e "Please enter a hostname for the new machine. It may contain only latin letters and numbers.";
echo "New system will not work without a correct hostname, so be careful.";
echo -e "Enter hostname: \c"
read hostname
done
echo "Applying $hostname as a name for the new machine...";
echo $hostname > /mnt/$devname/etc/hostname
echo "127.0.0.1 $hostname" > /mnt/$devname/etc/hosts;
echo "Hostname has been set.";
echo "Stage 8: installing GRUB bootloader";
rm -rf /mnt/$devname/boot
mkdir /mnt/boot
mount /dev/${devname}1 /mnt/boot
cp /mnt/$devname/boot.tbz /mnt/boot
grub-mkdevicemap -n -m /mnt/device.map # create device map file at /mnt/device.map without floppy support
cd /mnt/boot
tar -jxf boot.tbz
echo "root `grub-probe -d /dev/${devname}1 -m /mnt/device.map -t drive | sed -e "s/,1)/,0)/" `
setup `grub-probe -d /dev/${devname} -m /mnt/device.map -t drive | sed -e "s/,1)/,0)/" `
quit" | grub --batch --no-floppy
rm /mnt/boot/boot.tbz
echo "default 0
timeout 3
hiddenmenu
title RosspaceOS (Ubuntu 9.04, kernel 2.6.28-11-generic)
uuid `vol_id /dev/${devname}1 --uuid`
kernel /vmlinuz root=UUID=`vol_id /dev/${devname}2 --uuid` ro quiet splash
initrd /initrd
title RosspaceOS (Ubuntu 9.04, kernel 2.6.28-11-generic (recovery mode))
uuid `vol_id /dev/${devname}1 --uuid`
kernel /vmlinuz root=UUID=`vol_id /dev/${devname}2 --uuid` ro single
initrd /initrd
title memtest86+
uuid `vol_id /dev/${devname}1 --uuid`
kernel /memtest86+.bin
" > /mnt/boot/grub/menu.lst
echo "GRUB installation complete.";
echo "Stage 9: creating 200M swap file.";
dd if=/dev/zero of=/mnt/$devname/.swap bs=1M count=200
mkswap /mnt/$devname/.swap
echo -e "Formatting fstab file..."
rm /mnt/$devname/etc/fstab
touch /mnt/$devname/etc/fstab
echo "proc /proc proc defaults 0 0" >> /mnt/$devname/etc/fstab
echo "UUID=`vol_id /dev/${devname}1 --uuid` /boot ext3 ro 0 0" >> /mnt/$devname/etc/fstab
echo "UUID=`vol_id /dev/${devname}2 --uuid` / ext3 relatime,errors=remount-ro 0 1" >> /mnt/$devname/etc/fstab
echo "/dev/scd0 /media/cdrom0 udf,iso9660 user,noauto,exec,utf8 0 0" >> /mnt/$devname/etc/fstab
echo "done."
echo "Stage 10: moving rootfs.tbz, boot.tbz and scripts to www server root...";
mv /mnt/$devname/rootfs.tbz /mnt/$devname/var/www/
mv /mnt/$devname/boot.tbz /mnt/$devname/var/www/
mv /media/cdrom/install.sh /mnt/$devname/var/www/
mv /media/cdrom/backup.sh /mnt/$devname/var/www/
echo "done.";
echo "Stage 11: cleaning up...";
rm -rf /mnt/$devname/tmp/*;
mkdir /mnt/$devname/tmp/
# rm -rf /mnt/$devname/home/user/.mozilla/firefox/*;
mkdir /mnt/$devname/boot
cd /
umount /dev/${devname}1
umount /dev/${devname}2
echo "done."
clear
echo "Installation complete. Reboot now in order to use your new OS.";
echo "Use the command prompt to modify something in your new system. Type exit to reboot.";
/bin/bash
reboot