一种实现 ubuntu “一键还原”的思路

系统安装、升级讨论
版面规则
我们都知道新人的确很菜,也喜欢抱怨,并且带有浓厚的Windows习惯,但既然在这里询问,我们就应该有责任帮助他们解决问题,而不是直接泼冷水、简单的否定或发表对解决问题没有任何帮助的帖子。乐于分享,以人为本,这正是Ubuntu的精神所在。
头像
Tenyears
帖子: 2245
注册时间: 2005-06-30 15:46
来自: 成都

#16

帖子 Tenyears » 2008-07-09 20:38

看戏
Humanity to others // 己所不欲,勿施与人
Laptop: ThinkPad X220 --- Win7 Home / Ubuntu 12.04
Desktop: Win7/Ubuntu 12.04
Server: Ubuntu12.04
头像
skyx
论坛版主
帖子: 9202
注册时间: 2006-12-23 13:46
来自: Azores Islands
联系:

#17

帖子 skyx » 2008-07-09 20:38

:shock:
no security measure is worth anything if an attacker has physical access to the machine
头像
ptptptptptpt
帖子: 3711
注册时间: 2006-09-19 18:16

Re: 一种实现 ubuntu “一键还原”的思路

#18

帖子 ptptptptptpt » 2008-07-09 21:58

啊哟,没想到这么热烈,谢谢,谢谢,请耐心等下

yaska 写了:
pt兄,我估计你的方法 就是prayaya livesystem现在用的技术。

把系统做成一个iso,然后用aufs联合一个loop image文件,当然也可以是linux分区上的目录。

iso是只读的,所以数据都保存在loop文件里面,所谓的还原,把loop文件去掉就可以了。

嗯,还有其他新东西的话,大家一起讨论吧。。
没错,相信很多人一眼就能看出怎么回事,不过我更愿意说是本论坛 gnix_oag 倡导过的技术( viewtopic.php?t=21688 ),或者更应该说,是 slax 一直以来就在使用的技术。

06年时 unionfs 问题多多,我一直不大信任它。后来有了 aufs ,据说稳定性好很多,现在也似乎用的越来越广泛,才又又有了兴趣

系统没必要做成 iso 或 squishfs 映像,就用正常安装在分区中的系统,启动时有几种模式可选:1,正常启动,可以添加、删除软件;2,保护模式,就是用 联合文件系统 ,以只读方式挂载系统分区,3,还原模式……


也不用 loop image文件 ,直接用一个分区作为可写部分,需要还原时,直接格式化该分区
头像
yaska
帖子: 1169
注册时间: 2007-10-28 21:35

#19

帖子 yaska » 2008-07-09 22:20

代码: 全选

系统没必要做成 iso 或 squishfs 映像,就用正常安装在分区中的系统 ,  以只读方式挂载系统分区
这些都必须在initrd里面做。 进入系统之后,就无法把可写部分,用aufs挂载到 / 了。

以只读方式挂载系统分区,这个我倒没想过,具体怎么实现?

代码: 全选

用一个分区作为可写部分
ubuntu mid edtion 安装到硬盘就采用这种方式,一个分区放squashfs文件系统,initrd,vmlinuz,另外一个分区用来保存数据。


细节,要的是细节。 我很感兴趣,呵呵
上次由 yaska 在 2008-07-09 22:27,总共编辑 2 次。
头像
yjcong
帖子: 2470
注册时间: 2006-02-28 3:11

#20

帖子 yjcong » 2008-07-09 22:24

lz的思路不错。 如果能在启动后有可添加的功能就好了。 最好再有几个记录点,用以还原
一梦三年,
松风依旧,
萝月何曾老.


灵幽听微, 谁观玉颜?
灼灼春华, 绿叶含丹.
头像
yaska
帖子: 1169
注册时间: 2007-10-28 21:35

#21

帖子 yaska » 2008-07-09 22:37

loop文件有个好处,就是可以放到windows分区,可以随时删除。
头像
ptptptptptpt
帖子: 3711
注册时间: 2006-09-19 18:16

#22

帖子 ptptptptptpt » 2008-07-10 7:13

yaska 写了:

代码: 全选

系统没必要做成 iso 或 squishfs 映像,就用正常安装在分区中的系统 ,  以只读方式挂载系统分区
这些都必须在initrd里面做。 进入系统之后,就无法把可写部分,用aufs挂载到 / 了。

以只读方式挂载系统分区,这个我倒没想过,具体怎么实现?

代码: 全选

用一个分区作为可写部分
ubuntu mid edtion 安装到硬盘就采用这种方式,一个分区放squashfs文件系统,initrd,vmlinuz,另外一个分区用来保存数据。


细节,要的是细节。 我很感兴趣,呵呵
显然是要在initrd里面做,稍微定制一下就行—— livecd 不就是把 squashfs文件系统 和 内存盘 联合吗,把内存盘用一个分区替换,不就行了吗

压成 squashfs文件系统 的好处是 —— 空间占用小,读取速度快,因为是压缩格式,同一个文件,读取时数据量小,但会增大cpu负荷,因为要解压,不过这点负荷对现在的cpu来说只是毛毛雨

另一好处是 squashfs 映像 不可写,系统文件更加安全。但同时也是缺点,配置好的系统,如果想做些更改,就得重新压制 squashfs 映像,比较麻烦

还有一个缺点,squashfs 映像须放在另一个文件系统中,fat32 也好,ntfs也好,ext3也好,逻辑上多了一层,安全性上差了一点

综合考虑,mid设备上使用 squashfs 映像 是不错的选择 ,而日常用的系统,我认为还是分区安装较为合适,当然并非绝对。
头像
ptptptptptpt
帖子: 3711
注册时间: 2006-09-19 18:16

#23

帖子 ptptptptptpt » 2008-07-10 7:19

yaska 写了:loop文件有个好处,就是可以放到windows分区,可以随时删除。
不觉得这是好处,反而是缺点。放到 win 分区,降低了I/O 性能,增大了被误删的可能

而使用一个 linux 分区,在 win 资源管理器中根本看不见,感觉比较舒服

又想了一下,还是在 linux 分区中建个文件夹 (如 ubuntu.rwfs),来作为可写部分。好处是,一方面可作为一个定位标识,另一方面,还原起来也简单,rm -R 之即可,比格式化分区简单
头像
windwiny
帖子: 2254
注册时间: 2007-03-13 17:26

#24

帖子 windwiny » 2008-07-10 10:35

倒,前几天我写的帖子没人看,临时安装软件的


一个很简单的脚本就能解决问题,不过我没写
头像
ptptptptptpt
帖子: 3711
注册时间: 2006-09-19 18:16

#25

帖子 ptptptptptpt » 2008-07-10 18:03

刚才试验了下,可以用,能起到还原效果。但使用感受不好,有时卡,通知区域 还老打哆嗦

如果用内存盘作为可写部分,相信会好很多,缺点是比较占内存,可写空间也不会很大(受内存容量限制),而且每次重启都会还原

用的 unionfs ,不知 aufs 会不会好些。

步骤如下:

简单起见,系统只分一个区,即 / 。

1,定制initrd。新建两个目录 /tmp-rootmnt 和 /rwfs 。简单修改了 /scripts/local ,改后内容如下:

代码: 全选

# Local filesystem mounting			-*- shell-script -*-

# Parameter: Where to mount the filesystem
mountroot ()
{
	[ "$quiet" != "y" ] && log_begin_msg "Running /scripts/local-top"
	run_scripts /scripts/local-top
	[ "$quiet" != "y" ] && log_end_msg

	# If the root device hasn't shown up yet, give it a little while
	# to deal with removable devices
	if [ ! -e "${ROOT}" ] || ! /lib/udev/vol_id "${ROOT}" >/dev/null 2>&1; then
		log_begin_msg "Waiting for root file system..."

		# Default delay is 180s
		if [ -z "${ROOTDELAY}" ]; then
			slumber=180
		else
			slumber=${ROOTDELAY}
		fi
		if [ -x /sbin/usplash_write ]; then
			/sbin/usplash_write "TIMEOUT ${slumber}" || true
		fi

		slumber=$(( ${slumber} * 10 ))
		while [ ! -e "${ROOT}" ] || ! /lib/udev/vol_id "${ROOT}" >/dev/null 2>&1; do
			/bin/sleep 0.1
			slumber=$(( ${slumber} - 1 ))
			[ ${slumber} -gt 0 ] || break
		done

		if [ ${slumber} -gt 0 ]; then
			log_end_msg 0
		else
			log_end_msg 1 || true
		fi
		if [ -x /sbin/usplash_write ]; then
			/sbin/usplash_write "TIMEOUT 15" || true
		fi
	fi

	# We've given up, but we'll let the user fix matters if they can
	while [ ! -e "${ROOT}" ] || ! /lib/udev/vol_id "${ROOT}" >/dev/null 2>&1; do
		echo "	Check root= bootarg cat /proc/cmdline"
		echo "	or missing modules, devices: cat /proc/modules ls /dev"
		panic -r "ALERT!  ${ROOT} does not exist.  Dropping to a shell!"
	done

	# Get the root filesystem type if not set
	if [ -z "${ROOTFSTYPE}" ]; then
		eval $(fstype < ${ROOT})
	else
		FSTYPE=${ROOTFSTYPE}
	fi
	if [ "$FSTYPE" = "unknown" ] && [ -x /lib/udev/vol_id ]; then
		FSTYPE=$(/lib/udev/vol_id -t ${ROOT})
		[ -z "$FSTYPE" ] && FSTYPE="unknown"
	fi

	[ "$quiet" != "y" ] && log_begin_msg "Running /scripts/local-premount"
	run_scripts /scripts/local-premount
	[ "$quiet" != "y" ] && log_end_msg

	if [ ${readonly} = y ] && \
	   [ -z "$LOOP" ]; then
		roflag=-r
	else
		roflag=-w
	fi

	# FIXME This has no error checking
	modprobe -Qb ${FSTYPE}

	# FIXME This has no error checking
	# Mount root
	mount ${roflag} -t ${FSTYPE} ${ROOTFLAGS} ${ROOT} /tmp-rootmnt
	mountroot_status="$?"
	if [ "$LOOP" ]; then
		if [ "$mountroot_status" != 0 ]; then
			if [ ${FSTYPE} = ntfs ] || [ ${FSTYPE} = vfat ]; then
				panic "
Could not mount the partition ${ROOT}.
This could also happen if the file system is not clean because of an operating
system crash, an interrupted boot process, an improper shutdown, or unplugging
of a removable device without first unmounting or ejecting it.  To fix this,
simply reboot into Windows, let it fully start, log in, run 'chkdsk /r', then
gracefully shut down and reboot back into Windows. After this you should be
able to reboot again and resume the installation.
(filesystem = ${FSTYPE}, error code = $mountroot_status)
"
			fi
		fi
	
		mkdir -p /host
		mount -o move /tmp-rootmnt /host

		while [ ! -e "/host/${LOOP#/}" ]; do
			panic "ALERT!  /host/${LOOP#/} does not exist.  Dropping to a shell!"
		done

		# Get the loop filesystem type if not set
		if [ -z "${LOOPFSTYPE}" ]; then
			eval $(fstype < "/host/${LOOP#/}")
		else
			FSTYPE="${LOOPFSTYPE}"
		fi
		if [ "$FSTYPE" = "unknown" ] && [ -x /lib/udev/vol_id ]; then
			FSTYPE=$(/lib/udev/vol_id -t "/host/${LOOP#/}")
			[ -z "$FSTYPE" ] && FSTYPE="unknown"
		fi

		if [ ${readonly} = y ]; then
			roflag=-r
		else
			roflag=-w
		fi

		# FIXME This has no error checking
		modprobe -Qb loop
		modprobe -Qb ${FSTYPE}

		# FIXME This has no error checking
		mount ${roflag} -o loop -t ${FSTYPE} ${LOOPFLAGS} "/host/${LOOP#/}" /tmp-rootmnt

		if [ -d /tmp-rootmnt/host ]; then
			mount -o move /host /tmp-rootmnt/host
		fi
	fi




for i in 1 2 3 4 5 6 7 8 9 ; do 
	for device in /dev/[hs]d[a-z][0-9]*; do
			mount -t reiserfs ${device} -o rw,noatime /rwfs || continue 
			if [ -d /rwfs/ubunturwfs ]; then
				break
			else 
				umount /rwfs
			fi
	done

	if [ -d /rwfs/ubunturwfs ]; then
		break
	else 
		/bin/sleep 3
	fi	
done

    modprobe -Qb unionfs

    mount -t unionfs -o noatime,dirs=/rwfs/ubunturwfs=rw:/tmp-rootmnt=ro unionfs "$rootmnt" || panic "${UNIONFS} mount failed"



	[ "$quiet" != "y" ] && log_begin_msg "Running /scripts/local-bottom"
	run_scripts /scripts/local-bottom
	[ "$quiet" != "y" ] && log_end_msg
}
2,创建一个 reiserfs (目前只支持这一种格式)分区,在其中建立 ubunturwfs 目录。


3,把 fstab 中 挂载跟分区 那一行注释掉

menu.lst 只需修改 initrd 行,指向定制的 initrd 文件。
上次由 ptptptptptpt 在 2008-07-10 18:38,总共编辑 1 次。
头像
ptptptptptpt
帖子: 3711
注册时间: 2006-09-19 18:16

#26

帖子 ptptptptptpt » 2008-07-10 18:33

现在是在 aufs 中,流畅多了!!与正常启动几乎感觉不到差别

把楼上脚本中所有的 unionfs 换成 aufs 即可。
头像
ptptptptptpt
帖子: 3711
注册时间: 2006-09-19 18:16

#27

帖子 ptptptptptpt » 2008-07-10 18:46

其实这个东东说成 ”一键还原“ 并不确切,倒更像是装了还原卡
头像
yaska
帖子: 1169
注册时间: 2007-10-28 21:35

#28

帖子 yaska » 2008-07-10 19:23

lz再加把劲,支持同步数据,就是把rw分区的数据转移到系统分区来。。这样,比windows的ghost先进多了。。
头像
ptptptptptpt
帖子: 3711
注册时间: 2006-09-19 18:16

#29

帖子 ptptptptptpt » 2008-07-10 20:51

yaska 写了:lz再加把劲,支持同步数据,就是把rw分区的数据转移到系统分区来。。这样,比windows的ghost先进多了。。
这个。。。感觉有些复杂哦

权衡一下 实现的复杂性 和 所能带来的好处,这个,似乎不太必要吧
头像
yaska
帖子: 1169
注册时间: 2007-10-28 21:35

#30

帖子 yaska » 2008-07-10 22:19

ptptptptptpt 写了:
yaska 写了:lz再加把劲,支持同步数据,就是把rw分区的数据转移到系统分区来。。这样,比windows的ghost先进多了。。
这个。。。感觉有些复杂哦

权衡一下 实现的复杂性 和 所能带来的好处,这个,似乎不太必要吧

反正就2个分区,用rsync同步一下,然后把可读部分清空。 呵呵 突然想到的。
回复