LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 3744|回复: 5

HOWTO Custom Stage4中文版

[复制链接]
发表于 2005-5-17 19:53:34 | 显示全部楼层 |阅读模式
HOWTO Custom Stage4
原文:  http://gentoo-wiki.com/HOWTO_Custom_Stage4
翻译:Eric Neon
邮件:ericneon AT gmail DOT com
版权:本文在GNU Free Documentation License 1.2及其后续版本的许可范围内使用。欢迎转载,转载时请注明原文和本译文出处,并请保证本文完整性。



HOWTO Custom Stage4
[[fr:HOWTO Créer un stage4 de Gentoo personnalisé]]



==简介==
在这篇文章中,我将为大家展示怎样创建一个自定义的stage4档案。 一个stage4档案就是你当前根分区的一个完整镜像。我创建stage4的主要原因是当硬件故障发生时, 为快速恢复系统做好准备。stage4和stage3类似,只是你可以选择使用怎样的CFLAGS和安装哪些软件。 你可以修改这种模式,以适应你自己的使用方式。

如果你想要一个可以安装到各种各样的系统上的通用的stage4,请使用genkernel设置你的内核。这将确保在启动时,内核可以像在liveCD上那样的工作。你也可能想使用几个限定性的CFLAGS (例如:用MCPU替换MARCH)。你也可能不得不在释放了stage4之后修改一下 /etc/fstab和 USE标志以适应用户。

==第一步==
安装Gentoo

==第二步==
设置所有的设备(例如:声卡、显卡以及USB),并且安装一些你想要包含到stage4里面的软件。例如,我会安装X、Xfce4、Sun的JDK、CVS、Emacs、Thunderbird、以及Firefox等。

==第三步==
复制一个你的/boot分区:

  1. root# mount /boot
  2. root# cp -R /boot /bootcpy
  3. root# umount /boot
复制代码

作为选择,你可以在你tar所有东西之前挂载 /boot 分区。 但是,如果你在不同硬件配置的系统上使用stage4,这种可选的方式可能会有点问题。

==第四步==
清除 /usr/portage/distfiles和 /var/tmp 中的临时文件以节省空间。

在清除 /var/tmp时,我曾非常小心。在我的系统里, 声卡和portage的配置信息都储存在那里。这会在之后产生些问题,当我还原了我的系统后发现我已经丢掉了一些重要的信息。得到一个尺寸稍微大点的tarball可能比还原的系统很不完整要好一点吧。

==第五步==
当所有事情通过前面的几个步骤都正确配置完成后,我们就要创建档案了:
  1. root# tar cjpf /path/to/save/at/stage4.tar.bz2 / --exclude=stage4.tar.bz2 --exclude=/proc
复制代码

   我们使用的tar选项:

  1.   c - 创建档案
  2.   j - 使用bzip2压缩
  3.   p - 保留文件属性 (不要丢了这个!!)
  4.   f - 指定文件名
复制代码

查看tar的手册以确认无误

依赖你所安装的这么多东西来创建档案也许要花费很长的时间。我通常将档案保存到我的系统中专门用来备份的备用磁盘上。你也可以把它刻成CD或DVD。

如果它太大不能塞进一张CD中,你就不得不把它分割开来。大的tar文件可以用“split”来分割。分割的部分以后可以用“cat”来合并。 (“man split”活用“man cat”获取更多信息)。

fdavid 建议:
* 大的tar文件可以用一个叫做split的小工具来分割。分割的部分以后可以用cat来合并。
* tar文件的解压选项必须慎重使用,因为当恢复到你的根目录时,会有很多目录被释放开。如果你想要得到一个更小的档案的话, /usr/portage也可以被排除在外。既然当你还原档案时/usr/portage会变得过时,不将其包含在内也许是一个明智的做法。

当我备份我的根目录时,这些目录被我排除在外:

  1. *  /mnt
  2. *  /proc
  3. *  /sys
  4. *  /tmp
  5. *+ 以及那些独立备份的目录。
复制代码


小脚本,我用来备份我的/home目录。
(备份到/mnt/tarbackup目录的 /dev/hdX要和 /etc/fstab中所写的保持一致。)
File|backupHome.sh

  1. #! /bin/bash
  2. # Backup script for Gentoo Linux
  3. # Author: fdavid
  4. # Date: 2003.11.29.

  5. # Making backup of home partition to cds

  6. # options for the archive (译注:归档选项)
  7. tarOptions="--create --absolute-names --preserve-permissions --gzip --file"

  8. # name of the archive (译注:档案名称)
  9. archive=/mnt/tarbackup/$(date +%Y%m%d)home.tar.gz

  10. # mount the backup partition  (译注:挂载备份分区)
  11. mount /dev/hdaX
  12. sleep 5

  13. # create the archive  (译注:创建档案)
  14. tar ${tarOptions} ${archive} /home/;
  15. echo archive is done

  16. # split the archive to cd size (use: "cat ${archive}.* >> ${archive}" to join the parts)
  17. split --bytes=700000000 ${archive} ${archive}.
  18. echo splitting is done

  19. # unmount
  20. sleep 5
  21. umount /dev/hdaX
复制代码



BrianW  补充:

*   稍微清理了脚本
* Made it so the --exclude= for the archive doesn't have to be edited if you modify $stage4Location
*   添加了 --exclude=/var/tmp/*
*   为tar添加了 --verbose 选择
*   添加了删除 /bootcpy的代码
*   将$tarOptions输出的“ --exclude 目录/文件”移到了他们自己的变量中。 (感谢 kamilian  )

File|mkstage4.sh

  1. #! /bin/bash
  2. ##  Backup script for Gentoo Linux
  3. ##  Author: BrianW
  4. ##  Date: 2004.10.26.
  5. ##  Adapted from backupHome.sh by fdavid
  6. ##  Adapted from mkstage4.sh by nianderson

  7. ##  This is a script to create a custom stage 4 tarball (System and boot backup)
  8. ##  I use this script to make a snapshot of my system. Meant to be done weekly in my case

  9. ##  Please check the options and adjust to your specifics.

  10. echo -=- Starting the Backup Script...
  11. echo -=-

  12. echo -=- Setting the variables...

  13. ##  The location of the stage 4 tarball.
  14. ##  Be sure to include a trailing /      (译注:注意包含一个根)
  15. stage4Location=/

  16. ##  The name of the stage 4 tarball.     (译注:stage4压缩包的名称)
  17. archive=$stage4Location$(hostname)-stage4.tar.bz2

  18. ##  Directories/files that will be exluded from the stage 4 tarball.
  19. ##
  20. ##  Add directories that will be recursively excluded, delimited by a space.
  21. ##  Be sure to omit the trailing /
  22. dir_excludes="/dev /proc /sys /tmp /usr/portage /var/tmp"
  23. ##
  24. ##  Add files that will be excluded, delimited by a space.
  25. ##  You can use the * wildcard for multiple matches.
  26. ##  There should always be $archive listed or bad things will happen.
  27. file_excludes="$archive"
  28. ##
  29. ##  Combine the two *-excludes variables into the $excludes variable
  30. excludes="$(for i in $dir_excludes; do if [ -d $i ]; then echo -n " --exclude=$i/*"; fi; done) $(for i in $file_excludes; do echo -n " --exclude=$i"; done)"

  31. ##  The options for the stage 4 tarball.   (译注:stage4压缩选项)
  32. tarOptions="$excludes --create --absolute-names --preserve-permissions --bzip2 --verbose --totals --file"

  33. echo -=- Done!
  34. echo -=-

  35. ##  Mounting the boot partition    (译注:挂载boot分区)
  36. echo -=- Mounting boot partition, then sleeping for 5 seconds...
  37. mount /boot
  38. sleep 5
  39. echo -=- Done!
  40. echo -=-

  41. ##  Creating a copy of the boot partition (copy /boot to /bootcpy).  (译注:创建一个/boot分区的拷贝)
  42. ##  This will allow the archiving of /boot without /boot needing to be mounted. (译注:这将允许归档/boot分区的内容,而不必挂载/boot分区)
  43. ##  This will aid in restoring the system.
  44. echo -=- Copying /boot to /bootcpy ...
  45. cp -R /boot /bootcpy
  46. echo -=- Done!
  47. echo -=-

  48. ##  Unmounting /boot   (译注:卸载/boot分区)
  49. echo -=- Unmounting /boot then sleeping for 5 seconds...
  50. umount /boot
  51. sleep 5
  52. echo -=- Done!
  53. echo -=-

  54. ##  Creating the stage 4 tarball.  (译注:创建stage4压缩包)
  55. echo -=- Creating custom stage 4 tarball \=\=\> $archive
  56. echo -=-
  57. echo -=- Running the following command:
  58. echo -=- tar ${tarOptions} ${archive} /
  59. tar ${tarOptions} ${archive} /;
  60. echo -=- Done!

  61. ##  Split the stage 4 tarball in cd size tar files.
  62. ##  To combine the tar files after copying them to your
  63. ##  chroot do the following: "cat *.tar.bz2 >> stage4.tar.bz2".
  64. ##  Uncomment the following lines to enable this feature.
  65. #echo -=- Splitting the stage 4 tarball into CD size tar files...
  66. #split --bytes=700000000 ${archive} ${archive}.
  67. #echo -=- Done!

  68. ##  Removing the directory /bootcpy.  (译注:删除/bootcpy目录)
  69. ##  You may safely uncomment this if you wish to keep /bootcpy.
  70. echo -=- Removing the directory /bootcpy ...
  71. rm -rf /bootcpy
  72. echo -=- Done!
  73. echo -=-

  74. ##  This is the end of the line.
  75. echo -=- The Backup Script has completed!

复制代码



这里有一个短小的但是可以使用的脚本,以安装由上述两个脚本的任意一个所创建的stage4(由rpcyan做过有限的测试,清继续测试)。rpcyan对安装脚本做了一些更正以使其可以正确得运行。
请给我一些批评或者改进的建议。 nianderson
File|installstage4.sh

  1. #! /bin/bash
  2. # Backup script for Gentoo Linux
  3. # Author: nianderson
  4. # Date: 2004.09.15.
  5. #


  6. #Used to install a stage4 created with mkstage4.sh
  7. #assumes you have already partitioned and formated your drive
  8. #assumes your booted from gentoo live cd

  9. #Define Disk layout
  10. #if using ide disks use hdax if using scsi use sdax

  11. rootPartition=/dev/sda3
  12. bootPartition=/dev/sda1

  13. #where to mount the disk partitions
  14. mntRootPartition=/mnt/gentoo
  15. mntBootPartition=/mnt/gentoo/boot

  16. #URL of stage4
  17. #I put a copy of the tar on a webserver so i can
  18. #easily get it when a reinstall is needed
  19. urlToStage4=http://domain.com/
  20. stage4=hostname-stage4.tar.bz2

  21. #mount root partition
  22. echo mounting root partition $rootPartition to $mntRootPartition
  23. mount $rootPartition $mntRootPartition
  24. sleep 5
  25. echo

  26. #not sure about this part yet
  27. #wget the stage4 to the mounted root partition
  28. cd $mntRootPartition
  29. echo wget $urlToStage4$stage4 to $mntRootPartition
  30. wget $urlToStage4$stage4
  31. sleep 5

  32. #untar the stage4 (译注:解压stage4)
  33. echo extract stage4
  34. tar xjpf $stage4
  35. sleep 5
  36.                                                             
  37. echo

  38. #mount boot partiton (译注:挂载boot分区)
  39. echo mounting $bootPartition to $mntBootPartition
  40. mkdir $mntbootPartition
  41. mount $bootPartition $mntBootPartition
  42. sleep 5
  43. echo

  44. #copy boot copy back to boot (译注:将boot分区内容拷贝回boot分区)
  45. echo copy bootcpy back to boot
  46. cp -R $mntRootPartition/bootcpy $mntBootPartition
  47. sleep 5

  48. #remove stage4 file (译注:删除stage4文件)
  49. rm -rf $mntRootPartition/$stage4

  50. echo you need to check your fstab and install grub or lilo then
  51. echo all should be well

  52. echo Removing bootcpy
  53. rm -rf /bootcpy
  54. echo Enjoy

复制代码




BlinkEye  补充: 从这篇文章的作者这里得到灵感,我做了一个备份脚本。最近我完整地重写了一遍,因此我将它发表在这里。查看gentoo论坛中allucid所写的原文以得到详细的解释和进一步的信息。
File|mkstage4.sh

  1. #!/bin/bash
  2. # Backup script for Gentoo Linux
  3. # Author: Reto Glauser aka blinkeye
  4. # Mailto: stage4 at blinkeye dot ch
  5. # Date: 23.03.2005
  6. # If you need further infos check out this post: [url]http://forums.gentoo.org/viewtopic.php?p=1751698#1751698[/url]

  7. version=v1.2

  8. # these are the commands we actually need for the backup
  9. command_list="echo tar hostname date split"

  10. # verify that each command we use exists
  11. for command in $command_list; do
  12.         path=`which $command | grep "no $command in"`
  13.        
  14.         if [ ! -x `which $command` -a "$path" ]; then
  15.                 echo -e "\n\nERROR: $command not found! Check your commands and/or your \$PATH"
  16.                 exit -1
  17.         fi
  18. done

  19. # options for the tar command   (译注:tar压缩命令选项)
  20. tarOptions="--create --absolute-names --preserve-permissions --totals --bzip2 --ignore-failed-read --verbose --file"

  21. # where to put the stage4
  22. stage4Location=/mnt/backups/stage4

  23. # name prefix
  24. stage4prefix=$(hostname)-stage4-`date +\%d.\%m.\%Y`

  25. # these files/directories are always excluded
  26. default_exclude_list="
  27. --exclude=/tmp/*
  28. --exclude=/var/tmp/*
  29. --exclude=/lost+found/*
  30. --exclude=/dev/*
  31. --exclude=/proc/*
  32. --exclude=/mnt/*
  33. --exclude=/sys/*
  34. --exclude=/usr/portage/*
  35. --exclude=/var/log/*
  36. --exclude=$stage4Location"

  37. # depending on your choice these files or directories will additionally be excluded
  38. custom_exclude_list="
  39. --exclude=/usr/src/*
  40. --exclude=/opt/mathematica
  41. --exclude=/usr/share/smssend
  42. --exclude=/home/*"

  43. # check the folder/files stored in $default_exclude_list exist
  44. for exclude in $default_exclude_list; do
  45.         if [ ! -e "`echo "$exclude" | cut -d'=' -f2 | cut -d'*' -f1`"  ]; then
  46.                 echo -e "\n\nERROR: `echo "$exclude" | cut -d'=' -f2` not found! Check your \$default_exclude_list"
  47.         fi
  48. done

  49. # check the folder/files stored in $custom_exclude_list exist
  50. for exclude in $custom_exclude_list; do
  51.         if [ ! -e "`echo "$exclude" | cut -d'=' -f2 | cut -d'*' -f1`"  ]; then
  52.                 echo -e "\n\nERROR: `echo "$exclude" | cut -d'=' -f2` not found! Check your \$custom_exclude_list"
  53.         fi
  54. done

  55. # print out the version
  56. echo -e "\nBackup script $version"
  57. echo -e "==================="


  58. # how do you want to backup?
  59. echo -e "\nWhat do you want to do? (Use CONTROL-C to abort)\n
  60. (1) Minimal backup
  61. (2) Interactive backup"

  62. while [ "$option" != '1' -a "$option" != '2'  ]; do
  63.         echo -en "\nPlease enter your option: "
  64.         read option
  65. done

  66. case $option in
  67. 1)
  68.         stage4Name=$stage4Location/$stage4prefix-minimal
  69.         final_command="tar $default_exclude_list $custom_exclude_list $tarOptions $stage4Name.tar.bz2 / /var/log/emerge.log"
  70.         ;;
  71. 2)
  72.         for folder in $custom_exclude_list; do
  73.                 echo -en "Do you want to backup" `echo "$folder" | cut -d'=' -f2`"? (y/n) "
  74.                 read answer
  75.                 while [ "$answer" != 'y' -a "$answer" != 'n' ]; do
  76.                         echo "please enter y or n"
  77.                         read answer
  78.                 done
  79.                 if [ "$answer" == 'n' ]; then
  80.                         default_exclude_list="$default_exclude_list $folder"
  81.                 fi
  82.         done
  83.        
  84.         stage4Name=$stage4Location/$stage4prefix-custom
  85.         final_command="tar $default_exclude_list $tarOptions $stage4Name.tar.bz2 /  /var/log/emerge.log"
  86.         ;;
  87. esac

  88. # show what will be done
  89. echo -e "\n* creating the stage4 at $stage4Location with the following options:\n\n"$final_command

  90. # everything is set, are you sure to continue?
  91. echo -ne "\nDo you want to continue? (y/n) "
  92. read answer
  93. while [ "$answer" != 'y' ] && [ "$answer" != 'n' ]; do
  94.                         echo "please enter y or n"
  95.                         read answer
  96. done

  97. if [ "$answer" == 'y' ]; then
  98.         # mount boot
  99.         echo -e "\n* mount boot"
  100.         mount /boot >/dev/null 2>&1       
  101.        
  102.         # if necessary, create the stage4Location
  103.         if [ ! -d "$stage4Location" ] ; then
  104.                 echo "* creating directory $stage4Location"
  105.                 mkdir -p $stage4Location
  106.         fi
  107.        
  108.         # check whether the file already exists
  109.         if [ -a "$stage4Name.tar.bz2" ]; then  
  110.                 echo -en "\nDo you want to overwrite $stage4Name.tar.bz2? (y/n) "
  111.                 read answer
  112.                 while [ "$answer" != 'y' ] && [ "$answer" != 'n' ]; do
  113.                         echo "please enter y or n"
  114.                         read answer
  115.                 done
  116.                 if [ "$answer" == 'n' ]; then
  117.                         echo -e "\n* There's nothing to do ... Exiting"
  118.                         exit 0;
  119.                 fi
  120.         fi
  121.        
  122.         # do the backup
  123.         time $final_command

  124.         # copy the current world file to the stage4 location
  125.         echo -e "\n* creating stage4 overview $stage4Name.txt"
  126.         cp /var/lib/portage/world $stage4Name.txt >/dev/null 2>&1
  127.        
  128.         # we finished, clean up
  129.         echo "* stage4 is done"
  130.         echo "* umounting boot"
  131.         umount /boot
  132. else
  133.         echo -e "\n* There's nothing to do ... Exiting"
  134. fi

  135. #Uncomment the following command if you want to split the archive in cd size chunks:
  136. #split --suffix-length=1 --bytes=670m $stage4Name.tar.bz2 "$stage4Name".tar.bz2_ && echo "* splitting is done"
复制代码



==从stage4开始安装==
*   用liveCD引导
*   创建分区,初始化文件系统并且挂载文件系统
*
  1. root# cd /mnt/gentoo
复制代码

*   拷贝你的stage4档案到磁盘中
(如果在另外的CD引导提示时输入了 "gentoo cdcache",那么你就可以卸载/挂载其他的CD。)
*
  1. tar xvjpf stage4.tar.bz2
  2. * root# cp -R bootcpy /mnt/gentoo/boot
复制代码

(当你拷贝了文件之后双击boot目录!)
*
  1. root# rm -rf bootcpy
复制代码

*   fstab
*   grub或lilo
**   你必须用如下命令链接 /dev到 /mnt/gentoo/dev (放弃外部 chroot):
**
  1. mount -o bind /dev /mnt/gentoo/dev
复制代码

*

==分区表技巧==
*  还原分区表信息:

  1. dd if=/dev/(your_disk) of=mbr.save count=1 bs=512
  2. sfdisk -d /dev/(your_disk) > partitions.save
复制代码

The first of those saves the mbr and the second will store all partition info (including logical partitions, which aren't part of the mbr).

*  还原分区信息:

  1. dd if=mbr.save of=/dev/(your_disk)
  2. sfdisk /dev/(your_disk) < partitions.save
复制代码

==LILO技巧==
在解压了stage之后将LILO安装到MBR,这样挂载:
  1. mount -o bind /dev /mnt/gentoo/dev
复制代码


==归功于==
allucid论坛发表的原文   

==反馈==
{{Discussion|HOWTO_Custom_Stage4|}}
发表于 2005-5-17 19:59:26 | 显示全部楼层
好东西  正准备备份呢
回复 支持 反对

使用道具 举报

发表于 2005-5-17 22:43:16 | 显示全部楼层
不错,加个精
回复 支持 反对

使用道具 举报

发表于 2005-5-17 23:00:18 | 显示全部楼层
很棒,但感觉这样做,把事情复杂化了,stage4看上去就是完成备份功能。我用livecd启动挂载相关盘,直接做压缩.
cd /mnt/gentoo/
tar czpivf /th/gentoo.tar.gz ./*

还原时,解压
cd /mnt/gentoo/
tar zxvf /th/gentoo.tar.gz
也是同样效果呀。
回复 支持 反对

使用道具 举报

发表于 2005-8-15 22:59:34 | 显示全部楼层
(当你拷贝了文件之后双击boot目录!)


应该是(当你拷贝了文件之后,仔细检查boot目录!)
回复 支持 反对

使用道具 举报

发表于 2005-8-16 01:10:54 | 显示全部楼层
double check
not double click
hehe
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表