[原创]新写的gnix_oag脚本(正在测式,欢迎提意见)

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
gnix_oag
帖子: 1994
注册时间: 2005-05-19 18:38
来自: 湖南永州
联系:

[原创]新写的gnix_oag脚本(正在测式,欢迎提意见)

#1

帖子 gnix_oag » 2007-02-11 21:11

代码: 全选

#!/bin/sh
# set -e
##################################################################################
#
#	gnix_oag 一个从硬盘引导 Ubuntu *Desktop*.iso 的小小脚本
#   
#	作者: 高星
#	Email: gnix.oag@gmail.com
#
#	版本: 2007.2.15
#
#	说明: 本脚本由 /init 导入并调用 mountroot()执行, 但要导入本脚本,
#	必须要把内核参数boot=casper 改成 boot=gnix_oag
#
##################################################################################

################### 定义变量 ########################################### 
#变量初始化及默认值 
export PATH=/root/usr/bin:/root/usr/sbin:/root/bin:/root/sbin:/usr/bin:/usr/sbin:/bin:/sbin
export USERNAME=ubuntu
export USERFULLNAME="Live session user"
export HOST=ubuntu

FD_DIR="." ###默认找的文件夹根目录

###从内核参数中获取变量
for x in $(cat /proc/cmdline 2>/dev/null) ; do
	case $x in 
	FD_DIR=*) FD_DIR="${x#FD_DIR=}" ;;
	fd_dir=*) FD_DIR="${x#fd_dir=}" ;;
	USERNAME=*) 
		export USERNAME="${x#USERNAME=}" ;;
	username=*) 
		export USERNAME="${x#username=}" ;;
	esac
done

################### 一些功能模块 ####################################### 
###获得分区及镜象的文件系统的类型 
get_fstype() {
	local FSTYPE ; local FSSIZE
	eval $(fstype < $1)
	if [ "$FSTYPE" != "unknown" ]; then
		echo $FSTYPE ; return 0
	fi
	/lib/udev/vol_id -t $1 2>/dev/null
}

###安装联合文件系统
setup_unionfs() {
    rofs_dir=""
    for dir in /rofs/* ; do
		if echo $dir | grep -E -q "\.rofs" ;then
		 rofs_dir="${dir}=ro:$rofs_dir"
		else
		 if [ "$need" ]; then
			rofs_dir="$rofs_dir:${dir}=ro"
		 else	
			rofs_dir="$rofs_dir${dir}=ro"
			need=ok     #之后的要加“:”
		 fi
		fi
    done

	if [ "$fd_rwfs" ]; then
		rwfs_dir=$fd_rwfs
	else
		mkdir -p /rwfs/ram.fs
	    mount -t tmpfs tmpfs /rwfs/ram.fs
		rwfs_dir=/rwfs/ram.fs
	fi

	if [ -d /aufs ] ; then
    mount -t aufs -o dirs=$rwfs_dir=rw:${rofs_dir} unionfs "$rootmnt" || panic "Can not mount unionfs aufs "
	else
	 mount -t unionfs -o dirs=$rwfs_dir=rw:${rofs_dir} unionfs "$rootmnt" || panic "Can not mount unionfs"
	fi

	###把相关内容mount到真实的根目录中
    for d in /rofs/* ; do
        mkdir -p "${rootmnt}/hd_rofs/${d##*/}"
        mount -o move "${d}" "${rootmnt}/hd_rofs/${d##*/}"
    done
    for d in $(mount -t iso9660 | cut -d\  -f 3); do
    	mkdir -p "$rootmnt/hd_iso/${d##*/}"
    	mount -o move "${d}" "$rootmnt/hd_iso/${d##*/}"
	done
	mkdir -p "$rootmnt/hd_rwfs/${rwfs_dir##*/}"
	mount -o move ${rwfs_dir} "$rootmnt/hd_rwfs/${rwfs_dir##*/}"
}

###生成 fstab
mk_fstab(){
:>/fstab #新建fstab文件
msfscount=98   ###ms文件系统计数值: 98对应B
###找所有硬盘分区
for dev_name in $(cat /proc/partitions 2>/dev/null | tr -s "[\ ]" | cut -d\  -f5 | tr  "[\n]" "[ ]") ; do 
	fstype="" ; mnt_point="" ; mnt_option=""
	fstype=$(get_fstype "/dev/${dev_name}")
	case "$fstype" in
	ntfs*)
		msfscount=$(expr $msfscount + 1) 
		mnt_point=/media/`echo -e -n "\\x$(printf %x $msfscount)"`_win
		mnt_option="umask=000,user,exec,nls=utf8" ;;
	vfat)
		msfscount=$(expr $msfscount + 1) 
		mnt_point=/media/`echo -e -n "\\x$(printf %x $msfscount)"`_win
		mnt_option="umask=000,shortname=winnt,user,exec,utf8" ;;
	msdos)
		msfscount=$(expr $msfscount + 1) 
		mnt_point=/media/`echo -e -n "\\x$(printf %x $msfscount)"`_win
		mnt_option="umask=000,user,exec,utf8" ;;
    reiserfs|xfs|jfs|ext3|ext2) 
		mnt_point="/media/${dev_name}"
		mnt_option="exec" ;;
	swap)
		mnt_point="none"
		mnt_option="defaults" ;;
	esac
	if [ "$mnt_point" -a "$mnt_option" -a "$fstype" ] ; then
		echo "/dev/${dev_name} ${mnt_point} ${fstype} ${mnt_option} 0 0 " >> /fstab
		mkdir -p /${mnt_point}
	fi
done
###找所有光驱
for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram) ; do 
	hd_dev=$(echo "$(udevinfo -q name -p ${sysblock#/sys} 2>/dev/null || echo ${sysblock##*/})")
	if /lib/udev/cdrom_id /dev/${hd_dev} > /dev/null ; then
		echo "/dev/${hd_dev} /media/${hd_dev} auto  ro,noauto,user,utf8 0 0 " >> /fstab
		mkdir -p /media/${hd_dev}
	fi
done
} 

###搜索磁盘,找相关文件 
fd_file() {
:>/swap_fstab #新建swap_fstab文件
while read  dev_name mntpoint mntfstype mntoptions dump fcsk ; do
	have_file=""  #有没有文件 
	mkdir -p /"${mntpoint##*/}"
	case $mntfstype in 
		ntfs*) 
		ntfs-3g $dev_name /"${mntpoint##*/}" || continue
 		;;
		reiserfs|xfs|jfs|ext3|ext2|vfat|msdos)	
		mount -t $mntfstype -o "${mntoptions##*user,}" $dev_name /"${mntpoint##*/}" || continue
		;;
	esac

	###找*.iso文件
	for iso in $(echo /"${mntpoint##*/}"/$FD_DIR/*.[iI][sS][oO] ) ; do
		if echo $iso | grep "*" > /dev/null ; then break ; fi
		iso_name=$(basename $iso)
		mkdir -p /iso/$iso_name
		if mount -t iso9660 -o loop $iso /iso/$iso_name ; then : ;
		else
			rm -r /iso/$iso_name
			continue
		fi
		for rofs in /iso/$iso_name/casper/*.squashfs ; do
			if echo $rofs | grep "*" > /dev/null ; then break ; fi
			rofs_name=$(basename $rofs)
			mkdir -p /rofs/$rofs_name
			if mount -t $(get_fstype $rofs) -o ro,loop $rofs /rofs/$rofs_name ; then : ;
			else 
				rm -r /rofs/$rofs_name
				continue
			fi
			fd_iso="ok" ; have_file="yes"
		done      	
    	umount /iso/$iso_name && rm -r /iso/$iso_name
	done
     	
   ###找*.rofs文件及硬盘上的*.squashfs
   for rofs in /"${mntpoint##*/}"/$FD_DIR/*.[rR][oO][fF][sS] /"${mntpoint##*/}"/$FD_DIR/*.[sS][qQ][uU][aA][sS][hH][fF][sS] ; do
      if echo $rofs | grep "*" > /dev/null ; then break ; fi
      rofs_name=$(basename $rofs)
      mkdir -p /rofs/$rofs_name
      if mount -t $(get_fstype $rofs) -o ro,loop $rofs /rofs/$rofs_name ; then : ;
      else
         rm -r /rofs/$rofs_name
         continue
      fi
      if echo $rofs | grep "filesystem" > /dev/null ; then fd_iso="ok" ; fi
      have_file="yes"
   done              	

	###找ubuntu.fs文件
	for rwfs in /"${mntpoint##*/}"/$FD_DIR/*[uU][bB][uU][nN][tT][uU].[fF][sS] ; do
		if echo $rwfs | grep "*" > /dev/null ; then break ; fi
		if [ "$fd_rwfs" ] ; then break ; fi
		if [ -f $rwfs ] ; then
			mkdir -p /rwfs/"${rwfs##*/}"
			if mount -t $(get_fstype $rwfs) -o rw,loop $rwfs /rwfs/"${rwfs##*/}" ; then 
			fd_rwfs=/rwfs/"${rwfs##*/}" ; have_file="yes" ; break
			else
			rm -r /rwfs/"${rwfs##*/}"
			fi
		fi
		case $mntfstype in reiserfs|xfs|jfs|ext3|ext2)
		if [ -d $rwfs ] ; then
			mkdir -p /rwfs/"${rwfs##*/}"
			if mount -o bind $rwfs /rwfs/"${rwfs##*/}" ; then
			fd_rwfs=/rwfs/"${rwfs##*/}" ; have_file="yes" ; break
			else
			rm -r /rwfs/"${rwfs##*/}"
			fi 
		fi ; ;;	
		esac
	done

	###找swap.fs文件
	for swap in /"${mntpoint##*/}"/$FD_DIR/*[sS][wW][aA][pP].[fF][sS] ; do
		if echo $swap | grep "*" > /dev/null ; then break ; fi
		have_file="yes"
		echo "${mntpoint}/$FD_DIR/${swap##*/} swap  swap  defaults  0  0" >> /swap_fstab
	done
	for swap in /"${mntpoint##*/}"/*[sS][wW][aA][pP].[fF][sS] ; do
		if echo $swap | grep "*" > /dev/null ; then break ; fi
		have_file="yes"
		echo "${mntpoint}/${swap##*/} swap  swap  defaults  0  0" >> /swap_fstab
	done

	###如果没有找文件就umount 
	if [ -z "$have_file" ] ; then umount /"${mntpoint##*/}" ; fi 
	
done
}


###装入相关内核模块
ld_modules(){
	if [ -x /sbin/usplash_write ] ; then
		/sbin/usplash_write "TIMEOUT 120"
	fi
	[ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-premount"
	run_scripts /scripts/casper-premount
	[ "$quiet" != "y" ] && log_end_msg
	if [ -x /sbin/usplash_write ] ; then
		/sbin/usplash_write "TIMEOUT 1000"
	fi
	#usb设备等待5秒
	for x in $(cat /proc/cmdline 2>/dev/null) ; do
		case "$x" in
		   usb*|USB* ) sleep 5 ;;
		esac
	done
	#文件系统
	modprobe -Qb loop max_loop=255    ;	modprobe -Qb squashfs 
	modprobe -Qb ext3 ;	modprobe -Qb ext2
	modprobe -Qb xfs ;	modprobe -Qb isofs
	modprobe -Qb jfs ;	modprobe -Qb nfs
	modprobe -Qb vfat ;	modprobe -Qb reiserfs
	#ntfs-3g
	if [ -d /ntfs ] ; then
		cp -r /ntfs/* /
		insmod /fuse/fuse.ko
	fi
	#aufs
	if [ -d /aufs ] ; then
		cp -r /aufs/sbin/* /sbin
		insmod /aufs/aufs/aufs.ko
	fi
}

setup_system(){
if [ ! -e $rootmnt/etc/gnix.oag@gmail.com ] ; then
	:>$rootmnt/etc/gnix.oag@gmail.com
	maybe_break casper-bottom
	[ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-bottom"
	run_scripts /scripts/casper-bottom

	#配置 fstab 文件
	cp -r /media/* $rootmnt/media/
	cat /fstab >> $rootmnt/etc/fstab
	if [ -f /swap_fstab ] ; then 
	cat /swap_fstab >> $rootmnt/etc/fstab
	fi

	#修正时间相差8小时
	chroot /root  sed -i -e "s/^UTC=.*\$/UTC=no/" /etc/default/rcS
	cp $rootmnt/usr/share/zoneinfo/Asia/Shanghai $rootmnt/etc/localtime
 
	#不显示桌面上的分区图标
	chroot /root sudo -u "$USERNAME" gconftool-2 -s -t bool /apps/nautilus/desktop/volumes_visible false
	[ "$quiet" != "y" ] && log_end_msg
	fi
} 

################### 主函数  ############################################
mountroot() {
exec 6>&1 ; exec 7>&2 ; exec > casper.log ; exec 2>&1

###装入相关内核模块 
ld_modules

###找文件及挂载相关内容 
for i in 0 1 2 3 4 ; do
mk_fstab
fd_file < /fstab
	if [ "$fd_iso" ]; then
		break
	fi
    sleep 2
done
if [ "$?" -gt 0 ]; then
	panic "Unable to find a medium containing a live file system"
fi
   
###联合所有文件夹    
setup_unionfs

log_end_msg

###配置系统 
setup_system

exec 1>&6 6>&- ; exec 2>&7 7>&-
cp casper.log "${rootmnt}/var/log/"
} 
aufs文件
/aufs/aufs/aufs.ko
/aufs/sbin/umount.aufs
/aufs/sbin/auplink
/aufs/sbin/fsck.aufs
/aufs/sbin/unionctl
/aufs/sbin/aulchown
/aufs/sbin/mount.aufs
/aufs/sbin/aufind.sh
ntfs-3g文件
/ntfs/lib/libpthread.so.0
/ntfs/lib/libfuse.so.2
/ntfs/lib/libntfs-3g.so.0
/ntfs/lib/libfuse.so.2.6.3
/ntfs/lib/libntfs-3g.so.0.0.0
/ntfs/lib/libpthread-2.5.so
/ntfs/bin/ntfs-3g
/ntfs/bin/ntfsmount
/ntfs/bin/fusermount
/ntfs/fuse/fuse.ko
/ntfs/lib/librt.so.1
/ntfs/lib/librt-2.5.so
上次由 gnix_oag 在 2007-04-14 17:24,总共编辑 19 次。
头像
gnix_oag
帖子: 1994
注册时间: 2005-05-19 18:38
来自: 湖南永州
联系:

#2

帖子 gnix_oag » 2007-02-11 21:14

制作 initrd.gz 脚本(占位)
头像
gnix_oag
帖子: 1994
注册时间: 2005-05-19 18:38
来自: 湖南永州
联系:

#3

帖子 gnix_oag » 2007-02-14 9:39

脚本已能正常运行了,还有些细节要改要加,
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

#4

帖子 eexpress » 2007-02-14 9:53

等使用说明 :lol:
● 鸣学
头像
gnix_oag
帖子: 1994
注册时间: 2005-05-19 18:38
来自: 湖南永州
联系:

#5

帖子 gnix_oag » 2007-02-14 19:04

除了 ntfs 外 , 其它功能测式完,
头像
skyx
论坛版主
帖子: 9202
注册时间: 2006-12-23 13:46
来自: Azores Islands
联系:

#6

帖子 skyx » 2007-02-14 19:11

原来的脚本在7.04 herd2上就不行了(6.10和7.04 herd1上ok),这个一定要mark先
no security measure is worth anything if an attacker has physical access to the machine
头像
gnix_oag
帖子: 1994
注册时间: 2005-05-19 18:38
来自: 湖南永州
联系:

#7

帖子 gnix_oag » 2007-02-14 19:23

原来的脚本在7.04 herd2 是可以的, 我试过
头像
skyx
论坛版主
帖子: 9202
注册时间: 2006-12-23 13:46
来自: Azores Islands
联系:

#8

帖子 skyx » 2007-02-14 19:24

gnix_oag 写了:除了 ntfs 外 , 其它功能测式完,

测试是在herd2 和herd3上进行的吧?
no security measure is worth anything if an attacker has physical access to the machine
头像
skyx
论坛版主
帖子: 9202
注册时间: 2006-12-23 13:46
来自: Azores Islands
联系:

#9

帖子 skyx » 2007-02-14 19:25

gnix_oag 写了:原来的脚本在7.04 herd2 是可以的, 我试过
怪了,一样的制作方法,我做了6.10 7.04 herd1 ,是ok的,7.04 herd2 当时死活进不了桌面。
no security measure is worth anything if an attacker has physical access to the machine
头像
gnix_oag
帖子: 1994
注册时间: 2005-05-19 18:38
来自: 湖南永州
联系:

#10

帖子 gnix_oag » 2007-02-15 11:24

skyx 写了:
gnix_oag 写了:原来的脚本在7.04 herd2 是可以的, 我试过
怪了,一样的制作方法,我做了6.10 7.04 herd1 ,是ok的,7.04 herd2 当时死活进不了桌面。
可能是硬件支持的问题
头像
gnix_oag
帖子: 1994
注册时间: 2005-05-19 18:38
来自: 湖南永州
联系:

#11

帖子 gnix_oag » 2007-02-15 19:48

ntfs-3g 程序依赖以下文件:

/lib/modules/$( uname -r )/kernel/fs/fuse/fuse.ko 用 insmod 装入内核

/usr/lib/libfuse.so.2
libfuse2包中的
/usr/lib/libfuse.so.2.5.3 libfuse2包中的

/usr/lib/libntfs-3g.so.0.0.0 libntfs-3g0包中的
/usr/lib/libntfs-3g.so.0 libntfs-3g0包中的

/lib/libpthread-2.4.so libc6 包中的
/lib/libpthread.so.0 libc6 包中的

/usr/bin/fusermount
fuse-utils包中的

可以把这些文件放到 /ntfs_3g 文件夹中
头像
gnix_oag
帖子: 1994
注册时间: 2005-05-19 18:38
来自: 湖南永州
联系:

#12

帖子 gnix_oag » 2007-02-15 21:40

脚本基本完成

在initrd.gz 加入 ntfs 文件夹,以支持 ntfs 分区,


/ntfs/lib/libpthread.so.0
/ntfs/lib/libfuse.so.2
/ntfs/lib/libntfs-3g.so.0
/ntfs/lib/libfuse.so.2.6.3
/ntfs/lib/libntfs-3g.so.0.0.0
/ntfs/lib/libpthread-2.5.so
/ntfs/bin/ntfs-3g
/ntfs/bin/ntfsmount
/ntfs/bin/fusermount
/ntfs/fuse/fuse.ko
/ntfs/lib/librt.so.1
/ntfs/lib/librt-2.5.so



.
上次由 gnix_oag 在 2007-03-31 19:34,总共编辑 2 次。
头像
skyx
论坛版主
帖子: 9202
注册时间: 2006-12-23 13:46
来自: Azores Islands
联系:

#13

帖子 skyx » 2007-02-15 21:43

支持
no security measure is worth anything if an attacker has physical access to the machine
xory
帖子: 13
注册时间: 2007-02-09 10:23

#14

帖子 xory » 2007-02-18 22:46

使用版本: 2007.2.15 , 安装 Herd4 成功,
--未使用*.rofs文件
--采用在ext2分区上建立目录方式:/herd4/ubuntu.fs

但是还是存在一些问题,不知道是脚本的原因还是herd4的问题:
1、系统启动时提示HAL 内部错误
2、无法使用“用户和组”添加管理用户,打开ROOT登录也不行
3、“网络设置”中,没有设备列表,不过可以连接上网

使用了一下,感觉比edgy还要稳定,除了这些BUG (不知道是否硬盘启动问题……)

楼主继续加油!
头像
gnix_oag
帖子: 1994
注册时间: 2005-05-19 18:38
来自: 湖南永州
联系:

#15

帖子 gnix_oag » 2007-04-14 17:26

modprobe -Qb loop max_loop=255

这样就能使用 255 个回环设备。

启动后可用 ls /dev/loop* 看看有没有255个,

呵呵,原来只有8个。
回复