用debootstrap来安装debian/ubuntu的脚本

sh/bash/dash/ksh/zsh等Shell脚本
回复
wkt
帖子: 849
注册时间: 2006-09-07 22:51
送出感谢: 0
接收感谢: 0
联系:

用debootstrap来安装debian/ubuntu的脚本

#1

帖子 wkt » 2007-05-06 20:46

脚本主要通过debootstrap来安装basesystem
然后就chroot 进入 $root 安装内核 gcc xorg gonme fcitx 等
脚本有3个 两个是必须的
本来有1个 但如果这样用gedit编辑就动起来很慢
就分成3个
脚本从debinstall.sh 的do_main开始(我喜欢c的做法)

脚本运行的条件是有个linux和debootstrap
手册说unix(只要有ar和wget)也可以了但我没有试过
我只在debian和ubuntu下用过
也就是debian装ubuntu ro ubtuntu to debian
这样不会让我在安装时不能使用电脑了
当然了 我写这脚本主要是方便自己同时也是学习一下shell
所以还是有问题的 很多问题是因为软件包依赖我不是清楚

1. acpid 等包在apt-get 哪里老是报错
让它不报错的方法在fun.sh 的set_things 函数那里(哪个东西很怪,事实证明有效)
2 .安装之后 ubuntu 在gnome的动起来时的那个东西不是很象(绝对是gnome的)ubuntu的
但gdm的界面跟一般方法安装的一样
3 . 它安装的软件看起来比一般方法少
我想这个时我要的
debinstall.sh

代码: 全选

#!/bin/bash
export LC_ALL=C
root=
rootdev=
arch=
target=
url=
desktop=
x=
xorg="x-window-system-core x11-common xfonts-utils"
hostname=`hostname`
tmpout=">>/tmp/install.log"
release=
debug=

usage()
{
cat <<eof
Usage: debinstall.sh [root=..] [rootdev=..] [arch=i386/..] [url=..] [target=..]
	others:destop x=yes/no cleanroot=no/yes --help
eof
return 0

}

args() ###arguements analysis
{
	echo "argsing ..."
	while [ ! "x$1" == "x" ]
	do
		case $1 in
			root=*)root=${1#root=} 
			shift
				;;
			rootdev=*)rootdev=${1#rootdev=}
			shift
				;;
			arch=*)arch=${1#arch=}
			shift
				;;
			target=*)target=${1#target=}
			shift
				;;
			url=*)url=${1#url=}
			shift
				;;
			desktop=*)desktop=${1#desktop=}
			shift
				;;
			x=*)x=${1#x=}
			shift
			;;
			cleanroot=*)cleanroot=${1#cleanroot=}
			shift
				;;
			release=*)release=${1#release=}
			shift
				;;
			*debug*)debug="debug"
			#	echo $debug
			shift
				;;
			--help)usage
			shift				
				;;
			*)
			shift	
				;;
		esac
done

}

arch_set()
{
	local ch
	echo "Set Arch"
	if [ "x$arch" == "x" ];then
		echo -n "Enter you computer arch(default=i386):"
		read ch
		arch="i386"
		if [ "x$ch" == "x" ];then
			echo "arch=$arch"
		else
		arch=$ch
		fi
	fi
	echo ""
}

target_set()
{
	echo "Set the target "
	echo ""
	local count
 if [ "x$target" == "x" ];then
		if [ -d /usr/lib/debootstrap/scripts ];then
			/bin/ls /usr/lib/debootstrap/scripts |grep -v root|grep -v build > /tmp/ls.out
		else
			echo "debootstrap isn't installed "
			return -244
		fi	
		count=1	
		exec </tmp/ls.out
		while read ch
			do
				echo "$count. $ch "
				let count=$count+1
		done >/tmp/target.out
		exec <&1
		while true
			do
			echo "This is some targets:"
			cat /tmp/target.out
			echo -n "Your chioce:"
			read count
#			echo $count
			if [ "x$count" == "x" ];then
				count="1"
				echo "your chioce is 1"
			fi
			case $count in
				[1-9][0-9]|[1-9])	target=$(head -n $count /tmp/target.out |tail -n 1|awk '{print $2}')
					;;
				*)
					target=$count				
					;;
			esac
#			echo "count=$count  target=$target"
			count=$(cat /tmp/ls.out|grep  $target )
#			echo "count=$count  target=$target"
			if [ "x$count" == "x$target" ];then
				break
			fi
			echo -e "\033[0;0;32mIcorrect target  !Try ones more \033[0;0;0m" 
		done	
	else
		echo "target=$target"
	fi
	echo ""
}

root_set()
{
	local ch	
	echo "Set root"
	echo ""
	if [ "x$root" == "x" ];then
		root="/tmp/$arch"
		##echo "Enter empty  directory !"
		echo -n "That root device will mount to(default=$root):"
		read ch
		if [ ! "x$ch" == "x" ];then
			root=$ch
		fi
 	else
		echo "root=$root"
	fi
	if [ ! -d $root ] && [ -e $rootdev ];then
		echo -n "do mount root ..."	
		mkdir -p $root
		mount $rootdev $root
		echo -n "Done."
	fi	
	echo ""
}

rootdev_set()
{
	echo "Set rootdev "
	echo ""
	if [ "x$rootdev" == "x" ];then
		echo "Please input a root partition special file !(examples:/dev/sd* /dev/hd*)"
		echo -n "(default is you needn't root partition):"
		read rootdev
	else
		if [ "x$rootdev" == "xno" ]||[ ! -e $rootdev ];then
			echo "Your secletion is no root partition ."
		fi
	fi
	echo -e "rootdev=$rootdev\n"	
}

url_set()
{
	if [ ! "x$url" == "x" ];then
			echo "url=$url"		
			return
	fi
	echo -e " Select mirrors\n"
	echo "1    url=http://archive.ubuntu.org.cn/ubuntu"
	echo "2    url=ftp://ftp.linuxforum.net/debian"
	echo "3    url=file:/cdrom"
	echo "4    url=http://debian.cn99.com/debian"
	echo "5    url=http://apt.debian.org.tw/"
	echo "6    url=http://mirror.dlut.edu.cn/debian/"
	echo "7    url=http://mirror.rootguide.org/ubuntu/"
	echo -e "100  Enter yourself !\n"
	echo -n "Your chioce :"
	read ch
	case  $ch in
		1)  url=http://archive.ubuntu.org.cn/ubuntu ;;
		2)  url=ftp://ftp.linuxforum.net/debian ;;
		3)  url=file:/cdrom ;;
		4)  url=http://debian.cn99.com/debian ;;
		5)  url=http://apt.debian.org.tw/ ;;
		6)  url=http://mirror.dlut.edu.cn/debian ;;
		7)  url=http://mirror.rootguide.org/ubuntu/ ;;
		100)	echo -n "your url boy/gril :" ;
					read url ;;
		*) url=ftp://ftp.linuxforum.net/debian    ;;
	esac
	echo "" 
}

 

base_install()
{
if [ "x$base" == "xno" ];then
	return
fi
tmpout=""
rootdev_set
arch_set
target_set
root_set
clean_root
url_set
test_url
echo "Installing the base system ..."
echo -e "Please wait ...\n"
if debootstrap --arch $arch $target $root $url  $tmpout;then
	echo "Base system is OK !"
#	echo "But I can't not promise .See detail in /tmp/install.log "
else
return $?
fi
echo ""
}

fstab_set()
{
	if [ "x$rootdev" == "x" ];then
		return 1
	fi
	if [ -e $rootdev ];then	
		ftype=`fstpye $rootdev` ##there are some problem here ftpye should not be 
	else																##ntfs swap vfat auto ro extened 
		return 2
	fi
	if [ -e $root/etc/fstab ] && grep proc $root/etc/fstab;then
		return -1
	fi		
cat <<eof>$root/etc/fstab
$rootdev	/		$ftype	defaults		0	0
none       /proc	proc		defaults        0       0
none       /dev/shm     tmpfs		defaults,size=144m       0    0
eof
}

host_set()
{
	if [ -e $root/etc/hosts ];then
		return
	fi
cat <<eof>$root/etc/hosts
127.0.0.1  localhost  $hostname
127.0.0.1  $hostname.domain  $hostname
eof
cp -r /etc/network/interfaces $root/etc/network/interfaces
echo $hostname >$root/etc/hostname
 if grep [Dd]ebian $root/etc/issue >/dev/null 2>&1 ;then
		ap="contrib"
		release="debian"
	fi
	if grep [Uu]buntu $root/etc/issue >/dev/null 2>&1 ;then
		ap="restricted universe multiverse"
		release="ubuntu"
	fi
cat <<END>$root/etc/apt/sources.list
deb $url $target main $ap
END
####I have no ideal about how-to know debia/ubuntu so ...
}

kernel_install()
{
	if [  "x$kernel" == "xno" ];then
		return 
	fi 
	echo -e "\033[0;0;32mBegin to kernel installation\033[0;0;0m"   ###right now I only know the green code
	local count linux ch one
	LC_ALL=C chroot $root mount -t proc none /proc
	LC_ALL=C chroot $root apt-get update
	LC_ALL=C chroot $root apt-cache search linux-image|grep ^linux-image >/tmp/kernel.list
	count=1
	exec </tmp/kernel.list	
	while read one
		do
			echo "$count.  $one"
			let count=$count+1
	done>/tmp/kernel.out
		exec <&1
	while true
		do
			echo -e "Which kernel would you like ?\n"
			cat /tmp/kernel.out
			echo ""
			echo -n "Your chioce:"
			read ch 
			case $ch in
				[1-9][0-9]|[1-9]) linux=$(head -n $ch /tmp/kernel.out |tail -n 1|awk '{print $2}')  ;;
				*) linux=$ch  ;;
			esac
			if [ "x$linux" == "x" ];then
				linux=$(head -n 1 /tmp/kernel.out |tail -n 1|awk '{print $2}')
			fi			
			ch=$(cat /tmp/kernel.out|awk '{print $2}'| grep $linux\$ 2>/dev/null)
			if [ "x$ch" == "x$linux" ];then
				LC_ALL=C chroot $root apt-get install --yes  -q 	$linux
				break
			fi
			echo -e "\033[0;0;32mIcorrect kernel name try againt ! \033[0;0;0m"
	done
echo ""
}


useful_install()
{
	if  [ ! -e /proc/version ];then
		LC_ALL=C chroot $root mount -t proc none /proc
	fi
	LC_ALL=C chroot $root apt-get update
	LC_ALL=C chroot $root apt-get install --yes build-essential man manpages nano
	LC_ALL=C chroot $root apt-get install --yes -q alsa-base  alsa-utils locales       ##be sure locales is installed
}

x_set()
{
	echo -e "Set X:\n"
	if [ "x$x" == "x" ];then
		echo "would you like  the X-Window system ?"
		echo -n "(default=yes yes/no):"		
		read ch
		x="yes"
		if [ "x$ch" == "x" ];then
			echo "x=$x"
		else
		x=$ch
		case $x in
			[Yy]|[yY][eE][Ss])
				x="yes"
				;;
			[nN]|[nN][oO])
				x="no"
				;;	
			*)
			x=yes
			;;
		esac
		fi
	else
		echo "x=$x"
	fi
	if [ "$x" == "no" ];then
		return 1
	fi
 desktop_set
}

desktop_set()
{
	echo -e "Set Desktop:\n"
	if [ X$x == X"no" ];then
	return 1
	fi
	if [ "x$desktop" == "x" ];then
		echo "What would you like to install for you desktop ?"
		echo -n "gnome/kde/xfce4(defaulf=gnome):"
		desktop=gnome
		read ch
		if [ "x$ch" == "x" ];then
			echo "desktop is $desktop"
		else
		desktop=$ch
		fi
	else
		echo -e "desktop=$desktop\n"
	fi
}

desktop_install()
{
	local ttf human language extras dm
	case $desktop in
		gnome*)
		dm=gdm ;;
		kde*)
		dm=kdm ;;
		xfce*)
		dm=gdm ;; ###I don't know xfce dm so gdm
		*)
		dm=gdm ;; ###gdm it's what I like !!
	esac
	ttf="ttf-arphic-uming ttf-dejavu" ##I don't know what ttf-dejavu do but both debian&ubuntu install it 
	locales="/etc/locale.gen"
	extras="libgnomevfs2-bin libgnomevfs2-extra "
	if [ "x$release" == "xubuntu" ] &&( [ "x$desktop"  == "xgnome" ] || [ "x$desktop" == "xgnome-core" ]  );then
		human="human-theme human-icon-theme human-cursors-theme ${target}-wallpapers ${target}-session-splashes "    ###ubuntu must be a human themes
		language="language-pack-gnome-zh language-pack-gnome-zh-base language-pack-zh-base language-pack-zh mozilla-firefox-locale-zh-cn"
		locales="/var/lib/locales/supported.d/locales"
		extras="  gnome-media gnome-volume-manager  acpi gnome-applets gedit gnome-terminal ${target}-gdm-themes"	
	fi
	LC_ALL=C chroot $root apt-get update >/dev/null 2>&1
	echo -e "\033[0;0;32mInstalling the xorg Xserver  $xorg.... \033[0;0;0m"
	LC_ALL=C chroot $root apt-get install --yes -q $xorg xfonts-base
	echo -e "\033[0;0;32mDone.\033[0;0;0m"
	echo -e "\033[0;0;32mInstall $dm $desktop $human $ttf $extras $language .........\033[0;0;0m"
	LC_ALL=C chroot $root apt-get install --yes --force-yes -q $dm $desktop $human  $ttf $extras $language
	LC_ALL=C chroot $root apt-get install --yes samba samba-common firefox eog  gthumb
	echo -e "\033[0;0;32mDone.\033[0;0;0m"
	 	
}


do_main() ##just like a c 
{
. funs.sh
if [ -e install.set ];then
. ./install.set
fi
args $@
if [ "x$debug" == "xdebug" ];then
	return
fi
echo -e "\033[0;0;32mHi,boy/girl we will go to debian/ubuntu installation"
echo -e "And what will be installed that depend on your url and target\033[0;0;0m"
echo ""  
if ! is_root ;then ### I think that can only the root debootstrap !! 
	return $?
fi
if base_install 2>&1;then
	echo "base_install is OK !!"
else
	echo "Error when install base return code $?"
	echo "See some messages in /tmp/install.log"
	return -1
fi
host_set
fstab_set
kernel_install
user_add
echo -n "Installing gcc g++ alsa ... "
if useful_install > /tmp/usefull.log 2>&1;then
	echo "Done ."
else
	echo "Error . return code $?"
	echo "See messages in /tmp/usefull.log"
fi
	
if x_set ;then
	echo "X-Window will be installed later"
else
	echo "Error when set X return code $?"
	return -3
fi
echo ""
echo "Installing the desktop environment ...."
if  desktop_install || unset_all;then
	set_locales
	echo "Things seem to be OK !!"
	unset_all
else
	echo "Things don't go well. return code $?"
fi
return $?

}

do_main $@ ###script do things begin here
上次由 wkt 在 2007-05-08 15:50,总共编辑 2 次。
wkt
帖子: 849
注册时间: 2006-09-07 22:51
送出感谢: 0
接收感谢: 0
联系:

#2

帖子 wkt » 2007-05-06 20:50

fun.sh

代码: 全选

#!/bin/bash

function fstype()    ####this is a function that copyed by someone on linuxfans.org 
{
	
FILE="$(LANG=C LC_ALL=C LC_MESSAGES=C file -Lkbs "$1")" 
# echo $FILE
  if [ "$FILE" = "data" ]; then 
# could still be ReiserFS, since "file" only reads 64k in Debian 
    FILE="$(dd if="$1" skip=16397 ibs=4 count=2 2>/dev/null)" 
#    echo FILE is $FILE
  fi 
  case "$FILE" in 
   *[Rr][Ee][Ii][Ss][Ee][Rr]*)  echo "reiserfs";; 
   *ISO\ 9660*)                  echo "iso9660";; 
   *[Mm][Ii][Nn][Ii][Xx]*)      echo "minix";; 
   *[Xx][Ff][Ss]*)              echo "xfs";; 
   *[Jj][Ff][Ss]*)              echo "jfs";; 
   *[Ee][Xx][Tt]3*)             echo "ext3";; 
   *[Ee][Xx][Tt]2*)             echo "ext2";; 
   *[Ss][Ww][Aa][Pp]*)           echo "swap";; 
   *[Nn][Tt][Ff][Ss]*)          echo "ntfs";; 
   *[Ff][Aa][Tt]*)              echo "vfat";; 
   *[eE][Xx][Tt][Ee][Nn][Dd][Ee][Dd]*) echo "extended" ;;
   *)                           echo "auto";; 
esac
}


is_root()
{
	user=`whoami`
	if [ ! "$user" == "root" ];then
		echo "It's should be root !!"
		return 254
	fi			
}

user_add()
{
	local ch ch1 ch2 chroot
	ch1=""
	ch3=""
	chroot="chroot"
	ch=$1
	if [ "x$1" != "x" ];then
		chroot="sudo"
	fi
	echo users=$users
	if [ "x$users" == "xno" ];then
		return
	fi
	while [ "x$ch" == "x" ]
	do
		echo -n "Enter A username:"
		read ch
	done
	ch1="disk audio floppy admin"
	LC_ALL=C $chroot $root adduser  --system --shell /bin/bash  $ch
	LC_ALL=C $chroot $root passwd $ch
	for ch3 in $ch1
		do
			LC_ALL=C $chroot $root adduser $ch $ch3
	done >/dev/null
}


clean_root()
{
	local exp
	if [ "x$cleanroot" == "xno" ];then
			echo -e "Root isn't be clean .\n"
			echo "root=$root"			
			return 0
	fi
	if [ ! -d $root ];then
		echo  "$root isn't exits .so create it ."
		mkdir -p $root
	fi
	if [ "x$1" != "x" ];then
			exp="|$1"
	fi
 	list=`/bin/ls $root|grep -E -v "proc|home|lost|root$exp"`
	for one in $list
		do
			echo "removing  $one ...."
			rm -r $root/$one
	done

}

test_url()
{
	local tmp
	if echo $url |grep "file:" >/dev/null 2>&1;then
		tmp=${url#file://}
		echo "tmp=$tmp"
		mkdir -p $root$tmp
		cd=$(df|grep -E "*.iso"|awk '{print $1}')
		mount $cd $root$tmp -o loop,ro
	fi
}

set_locales()
{
echo "Install the inputmethod "
LC_ALL=C chroot $root apt-get install  --yes im-switch fcitx
rm -f $root/var/lib/locales/supported.d/*
cat <<eof>$root$locales
en_US ISO-8859-1
en_US.ISO-8859-15 ISO-8859-15
en_US.UTF-8 UTF-8
zh_CN GB2312
#zh_CN.GB18030 GB18030 ##It take too much time
zh_CN.GBK GBK
zh_CN.UTF-8 UTF-8
eof
	echo "Set locales"
	LC_ALL=C chroot $root locale-gen 
	LC_ALL=C chroot $root im-switch -z all_ALL -s fcitx
	echo "locales done"
	echo "Done ."	
}

unset_all()
{
 unset 	ch url release arch root target one desktop cleanroot base kernel user locales
return 1
}

set_things(){
 
{
echo "going on"
cat <<eof
apt-get --purge --yes remove  acpid
apt-get --yes -f install gnome-core
eof
}|chroot $root

}

after_install()
{
		LC_ALL=C chroot $root apt-get autoclean
}	
install.set

代码: 全选

target=feisty    ###debian's target is sarge etch ... 
url=http://mirror.rootguide.org/ubuntu/
release=ubuntu
cleanroot=no
rootdev=/dev/sda6
root=/media/sda6
desktop=gnome    ##if you like ubuntu may be ubuntu-desktop is good idea
arch=i386
x=yes
kernel=yes
base=yes
users=yes
没有install.set照样可以
但我觉那不太自动化
上次由 wkt 在 2007-05-08 15:54,总共编辑 1 次。
wkt
帖子: 849
注册时间: 2006-09-07 22:51
送出感谢: 0
接收感谢: 0
联系:

#3

帖子 wkt » 2007-05-06 21:07

回复

回到 “Shell脚本”