随笔-9  评论-21  文章-63  trackbacks-0

Contents

[hide]

Introduction

This article will demonstrate how to create a custom stage4 archive. A stage4 archive is an image of your entire root partition. The primary reason to create a stage4 archive is to provide for quick recovery in the event of disk failure. A stage4 archive is the same as a stage3, only you can select the CFLAGS you want to use and what other software you want installed. You can adapt this method to suit your personal uses.

If you want a generic stage4 that you can install on multiple systems, use genkernel to set up your kernel. This will ensure that on boot, the kernel will work like the one on the livecd. You may also want to use less restrictive CFLAGS (i.e., MCPU instead of MARCH). You may still have to modify /etc/fstab and the USE flags after extracting the stage4, to suit the user.

Step1

Install Gentoo .

Step2

Configure all drivers (i.e. sound, video and USB) and install any software you want to be included on your stage4. For example, I would install X, Xfce4, Sun's JDK, CVS, Emacs, Thunderbird, and Firefox.

Step3

Make a copy of your /boot partition:

root# mount /bootroot# cp -R /boot /bootcpyroot# umount /boot

Alternatively, you can just mount /boot before you tar everything up. However, there may be problems with this alternate method if you use the stage4 on systems with different hardware configurations.

Step4

Clear out temporary files in /usr/portage/distfiles and /var/tmp to save space.

Warning: Do NOT remove "/var/tmp/portage/homedir"! It will result in problems with portage.

I would use caution in clearing /var/tmp. On my system, config information was stored in there for sound and portage. This caused problems later, when I restored my system and realized that I was missing some important information. It may be better to have a slightly larger tarball than to find that the restored system is incomplete.

Step5

After everything is set up correctly from the previous sections, we will create the archive:

root# tar cjpf /path/to/save/at/stage4.tar.bz2 / --exclude=stage4.tar.bz2 --exclude=/proctar options we used: c - create archive j - use bzip2 compression p - preserve file attributes (don't leave this out!!) f - specify file name

Be sure to look at the tar manpage

It will probably take a long time to create the archive depending on how much you have installed. I usually save the archive on a spare disk I have in my system that I use for backups. You can also burn it to a cd or dvd.

If it is too large to fit on a cd you will have to split it up. Large tar files can be split up with 'split'. The parts can be joined later with 'cat'. ('man split', and 'man cat' for more info).

fdavid suggested:

  • Large tar files can be split with a small utility called split. The parts can be joined later with cat.
  • The exclude option of tar must be heavily utilized, because there are many directories which must be excluded when backing up your root. Exclude can also go for /usr/portage, if you want a smaller archive. Excluding this might be prudent, since the contents of /usr/portage will be outdated when you restore the archive.

The directories I exclude when backing up my root:

  • /mnt
  • /proc
  • /sys
  • /tmp
  • + any directory, which is backed up separately.

Small script, which I use for making backup of my /home. (The /dev/hdX corresponds to the /mnt/tarbackup directory in /etc/fstab.)

File: backupHome.sh
#! /bin/bash# Backup script for Gentoo Linux# Author: fdavid# Date: 2003.11.29.# Making backup of home partition to cds# options for the archivetarOptions="--create --absolute-names --preserve-permissions --gzip --file"# name of the archivearchive=/mnt/tarbackup/$(date +%Y%m%d)home.tar.gz# mount the backup partitionmount /dev/hdaXsleep 5# create the archivetar ${tarOptions} ${archive} /home/;echo archive is done# split the archive to cd size (use: "cat ${archive}.* >> ${archive}" to join the parts)split --bytes=700000000 ${archive} ${archive}.echo splitting is done# unmountsleep 5umount /dev/hdaX

BrianW added:

  • Cleaned up the script a bit.
  • Made it so the --exclude= for the archive doesn't have to be edited if you modify $stage4Location
  • Added --exclude=/var/tmp/*
  • Added --verbose to the tar options
  • Added code to remove /bootcpy
  • Moved the --exclude directories/files out of $tarOptions into their own variable's (thanks kamilian )

File: mkstage4.sh
#! /bin/bash##  Backup script for Gentoo Linux##  Author: BrianW##  Date: 2004.10.26.##  Adapted from backupHome.sh by fdavid##  Adapted from mkstage4.sh by nianderson##  This is a script to create a custom stage 4 tarball (System and boot backup)##  I use this script to make a snapshot of my system. Meant to be done weekly in my case##  Please check the options and adjust to your specifics.echo -=- Starting the Backup Script...echo -=-echo -=- Setting the variables...##  The location of the stage 4 tarball.##  Be sure to include a trailing /stage4Location=/##  The name of the stage 4 tarball.archive=$stage4Location$(hostname)-stage4.tar.bz2##  Directories/files that will be exluded from the stage 4 tarball.####  Add directories that will be recursively excluded, delimited by a space.##  Be sure to omit the trailing /dir_excludes="/dev /proc /sys /tmp /usr/portage /var/tmp"####  Add files that will be excluded, delimited by a space.##  You can use the * wildcard for multiple matches.##  There should always be $archive listed or bad things will happen.file_excludes="$archive"####  Combine the two *-excludes variables into the $excludes variableexcludes="$(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)"##  The options for the stage 4 tarball.tarOptions="$excludes --create --absolute-names --preserve-permissions --bzip2 --verbose --totals --file"echo -=- Done!echo -=-##  Mounting the boot partitionecho -=- Mounting boot partition, then sleeping for 5 seconds...mount /bootsleep 5echo -=- Done!echo -=-##  Creating a copy of the boot partition (copy /boot to /bootcpy).##  This will allow the archiving of /boot without /boot needing to be mounted.##  This will aid in restoring the system.echo -=- Copying /boot to /bootcpy ...cp -R /boot /bootcpyecho -=- Done!echo -=-##  Unmounting /bootecho -=- Unmounting /boot then sleeping for 5 seconds...umount /bootsleep 5echo -=- Done!echo -=-##  Creating the stage 4 tarball.echo -=- Creating custom stage 4 tarball \=\=\> $archiveecho -=-echo -=- Running the following command:echo -=- tar ${tarOptions} ${archive} /tar ${tarOptions} ${archive} /;echo -=- Done!##  Split the stage 4 tarball in cd size tar files.##  To combine the tar files after copying them to your##  chroot do the following: "cat *.tar.bz2 >> stage4.tar.bz2".##  Uncomment the following lines to enable this feature.#echo -=- Splitting the stage 4 tarball into CD size tar files...#split --bytes=700000000 ${archive} ${archive}.#echo -=- Done!##  Removing the directory /bootcpy.##  You may safely uncomment this if you wish to keep /bootcpy.echo -=- Removing the directory /bootcpy ...rm -rf /bootcpyecho -=- Done!echo -=-##  This is the end of the line.echo -=- The Backup Script has completed!

Here is a little script to install from a stage4 created by either of the above two scripts. (Limited testing reported by rpcyan thanks please continue testing) should work though. A few changes were made by rpcyan to get the install script working correctly Please contact me with comments or improvements. nianderson

File: installstage4.sh
#! /bin/bash# Backup script for Gentoo Linux# Author: nianderson# Date: 2004.09.15.##Used to install a stage4 created with mkstage4.sh#assumes you have already partitioned and formated your drive#assumes your booted from gentoo live cd#Define Disk layout#if using ide disks use hdax if using scsi use sdaxrootPartition=/dev/sda3bootPartition=/dev/sda1#where to mount the disk partitionsmntRootPartition=/mnt/gentoomntBootPartition=/mnt/gentoo/boot#URL of stage4#I put a copy of the tar on a webserver so i can#easily get it when a reinstall is neededurlToStage4=http://domain.com/stage4=hostname-stage4.tar.bz2#mount root partitionecho mounting root partition $rootPartition to $mntRootPartitionmount $rootPartition $mntRootPartitionsleep 5echo#not sure about this part yet#wget the stage4 to the mounted root partitioncd $mntRootPartitionecho wget $urlToStage4$stage4 to $mntRootPartitionwget $urlToStage4$stage4sleep 5#untar the stage4echo extract stage4tar xjpf $stage4sleep 5echo#mount boot partitonecho mounting $bootPartition to $mntBootPartitionmkdir $mntbootPartitionmount $bootPartition $mntBootPartitionsleep 5echo#copy boot copy back to bootecho copy bootcpy back to bootcp -R $mntRootPartition/bootcpy $mntBootPartitionsleep 5#remove stage4 filerm -rf $mntRootPartition/$stage4echo you need to check your fstab and install grub or lilo thenecho all should be wellecho Removing bootcpyrm -rf /bootcpyecho Enjoy


For explanation and further infos refer to the Gentoo Post. A howto (Wiki) of the script can be found here.

File: mkstage4.sh
#!/bin/bash# Backup script for Gentoo Linux# Author: Reto Glauser aka blinkeye# Homepage: http://blinkeye.ch# Mailto: stage4 at blinkeye dot ch# Date: 23.03.2005# If you need further infos check out this post: http://forums.gentoo.org/viewtopic.php?p=1751698#1751698version=v1.2# these are the commands we actually need for the backupcommand_list="echo tar hostname date split"# verify that each command we use existsfor command in $command_list; do	path=`which $command | grep "no $command in"`		if [ ! -x `which $command` -a "$path" ]; then		echo -e "\n\nERROR: $command not found! Check your commands and/or your \$PATH"		exit -1	fidone# options for the tar commandtarOptions="--create --absolute-names --preserve-permissions --totals --bzip2 --ignore-failed-read --verbose --file"# where to put the stage4stage4Location=/mnt/backups/stage4# name prefixstage4prefix=$(hostname)-stage4-`date +\%d.\%m.\%Y`# these files/directories are always excludeddefault_exclude_list="--exclude=/tmp/*--exclude=/var/tmp/*--exclude=/lost+found/*--exclude=/dev/*--exclude=/proc/*--exclude=/mnt/*--exclude=/sys/*--exclude=/usr/portage/*--exclude=/var/log/*--exclude=$stage4Location"# depending on your choice these files or directories will additionally be excludedcustom_exclude_list="--exclude=/usr/src/*--exclude=/opt/mathematica--exclude=/usr/share/smssend--exclude=/home/*"# check the folder/files stored in $default_exclude_list existfor exclude in $default_exclude_list; do	if [ ! -e "`echo "$exclude" | cut -d'=' -f2 | cut -d'*' -f1`"  ]; then		echo -e "\n\nERROR: `echo "$exclude" | cut -d'=' -f2` not found! Check your \$default_exclude_list"	fidone# check the folder/files stored in $custom_exclude_list existfor exclude in $custom_exclude_list; do	if [ ! -e "`echo "$exclude" | cut -d'=' -f2 | cut -d'*' -f1`"  ]; then		echo -e "\n\nERROR: `echo "$exclude" | cut -d'=' -f2` not found! Check your \$custom_exclude_list"	fidone# print out the version echo -e "\nBackup script $version" echo -e "==================="# how do you want to backup?echo -e "\nWhat do you want to do? (Use CONTROL-C to abort)\n(1) Minimal backup(2) Interactive backup"while [ "$option" != '1' -a "$option" != '2'  ]; do	echo -en "\nPlease enter your option: "	read optiondonecase $option in1)	stage4Name=$stage4Location/$stage4prefix-minimal	final_command="tar $default_exclude_list $custom_exclude_list $tarOptions $stage4Name.tar.bz2 / /var/log/emerge.log"	;;2)	for folder in $custom_exclude_list; do		echo -en "Do you want to backup" `echo "$folder" | cut -d'=' -f2`"? (y/n) "		read answer		while [ "$answer" != 'y' -a "$answer" != 'n' ]; do			echo "please enter y or n"			read answer		done		if [ "$answer" == 'n' ]; then			default_exclude_list="$default_exclude_list $folder"		fi	done		stage4Name=$stage4Location/$stage4prefix-custom	final_command="tar $default_exclude_list $tarOptions $stage4Name.tar.bz2 /  /var/log/emerge.log"	;;esac# show what will be doneecho -e "\n* creating the stage4 at $stage4Location with the following options:\n\n"$final_command# everything is set, are you sure to continue?echo -ne "\nDo you want to continue? (y/n) "read answerwhile [ "$answer" != 'y' ] && [ "$answer" != 'n' ]; do			echo "please enter y or n"			read answerdoneif [ "$answer" == 'y' ]; then	# mount boot	echo -e "\n* mount boot"	mount /boot >/dev/null 2>&1			# if necessary, create the stage4Location	if [ ! -d "$stage4Location" ] ; then		echo "* creating directory $stage4Location"		mkdir -p $stage4Location	fi		# check whether the file already exists	if [ -a "$stage4Name.tar.bz2" ]; then		echo -en "\nDo you want to overwrite $stage4Name.tar.bz2? (y/n) "		read answer		while [ "$answer" != 'y' ] && [ "$answer" != 'n' ]; do			echo "please enter y or n"			read answer		done		if [ "$answer" == 'n' ]; then			echo -e "\n* There's nothing to do ... Exiting"			exit 0;		fi	fi		# do the backup	time $final_command	# copy the current world file to the stage4 location	echo -e "\n* creating stage4 overview $stage4Name.txt"	cp /var/lib/portage/world $stage4Name.txt >/dev/null 2>&1		# we finished, clean up	echo "* stage4 is done"	echo "* umounting boot"	umount /bootelse	echo -e "\n* There's nothing to do ... Exiting"fi#Uncomment the following command if you want to split the archive in cd size chunks:#split --suffix-length=1 --bytes=670m $stage4Name.tar.bz2 "$stage4Name".tar.bz2_ && echo "* splitting is done"

Install from a stage4

  1. Boot live CD
  2. Create partitions, make filesystems and mount filesystems
    root# cd /mnt/gentoo
  3. Copy your stage4 archive(s) to disk (if it is on another CD type "gentoo docache" at the boot prompt. Then you'll be able to umount/mount other CDs.)
    tar -xvjpf stage4.tar.bz2
  4. root# cp -R bootcpy /mnt/gentoo/boot
    (Double check the boot dir after you copy the files over!)
  5. root# rm -rf bootcpy
  6. Check /etc/fstab
  7. You must "link" /dev to /mnt/gentoo/dev with following command (given outside chroot):
    mount -o bind /dev /mnt/gentoo/dev
  8. Chroot into /mnt/gentoo:
    chroot /mnt/gentoo /bin/bash
  9. grub or lilo

Partition table tip

Code: To store partition table info
dd if=/dev/(your_disk) of=mbr.save count=1 bs=512sfdisk -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).

Code: To restore partition info
dd if=mbr.save of=/dev/(your_disk)sfdisk /dev/(your_disk) < partitions.save

LILO tip

To install LILO in the MBR after untaring stage, mount it as:

mount -o bind /dev /mnt/gentoo/dev

GRUB tip

After chrooting:

root# grubgrub> root (hd0,0)grub> setup (hd0)grub> quit

(hd may need to be changed depending on your setup)

Privacy tip

I recommend deleting every "~/.bash_history" before executing the stage4 commands/scripts. This can be done by simply typing:

Singleuser (root) environments:
root@gentoo# rm ~/.bash_history && tar cpjf ...
or
root@gentoo# rm ~/.bash_history && sh <your-script>.sh

Multiuser (>root) environments:
Just add ".bash_history" to the excluded files list in your stage4 scripts or "--exclude='.bash_history'" to command line.


 

posted on 2006-12-03 20:16 Flutist 阅读(328) 评论(1)  编辑 收藏 引用 所属分类: 学习文章
只有注册用户登录后才能发表评论。