1 #! /bin/sh 2 3 # Nick Urbanik 4 # report information about the computer, reading from proc file system 5 # and Linux hard disk partition (if any) and storing the result in 6 # a directory that may be on a floppy disk. 7 8 # Designed to work with tomsrtbt or from a hard disk. 9 10 # Here is my attempt to find out whether the root device is a hard disk 11 # or a RAM disk. 12 # Now what devices are hard disks? 13 # could be a label (if read fstab, but not output of mount) 14 # could be [hs]d[a-z][1-9][0-9]* 15 # could be md[0-9]+ 16 # return 0 if root partition is a hard disk 17 # return 1 otherwise 18 mounted_on_hd() { 19 for line in "`mount | grep '[ \t]/[ \t]'`" 20 do 21 mount_point=`echo $line | awk '{print $3}'` 22 if [ "$mount_point" = "/" ]; 23 then 24 device="`echo $line | awk '{print $1}'`" 25 is_hd=`echo $device | 26 awk '/^\/dev\/[hs]d[a-z][1-9][0-9]*$/ {print 0; next} 27 /^\/dev\/md[0-9]+/ {print 0; next} 28 {print 1}'` 29 return $is_hd 30 fi; 31 done 32 return 1; 33 } 34 35 # A utility subroutine used by other subroutines 36 # checks that the directory $INFODIR exists. 37 ensure_INFODIR_exists() { 38 subroutine=$1 39 if [ -z "$INFODIR" -o ! -d "$INFODIR" ] 40 then 41 echo $prog: $subroutine called without initialised \$INFODIR 42 exit 1 43 fi 44 } 45 46 # A utility subroutine used by other subroutines 47 # checks that the directory $c exists. 48 ensure_hd_mountpoint_exists() { 49 subroutine=$1 50 if [ -z "$c" -o ! -d "$c" ] 51 then 52 echo $prog: $subroutine called without initialised mount point \$c 53 exit 1 54 fi 55 } 56 57 # reads the output of fdisk, and dmesg, 58 # and copies the content of a number of files in the /proc file system. 59 # Assume that $INFODIR exists 60 report_generic_info() { 61 ensure_INFODIR_exists "report_generic_info" 62 63 fdisk -l > $INFODIR/fdisk 2>&1 64 # print plenty of stuff from proc: 65 # exclude kcore, kmsg and ksyms 66 for f in `ls -1 /proc | grep -v '^k'` 67 do 68 if [ ! -d /proc/$f ]; then 69 echo $f: 70 cat /proc/$f 71 echo ' ' 72 fi 73 done > $INFODIR/proc 74 for f in /proc/sys/kernel/* 75 do 76 if [ ! -d $f ]; then 77 echo $f: 78 cat $f 79 echo ' ' 80 fi 81 done > $INFODIR/proc-kernel 82 dmesg > $INFODIR/dmesg 83 } 84 85 # print the device names of all partitions that fdisk 86 # reported as having the parition type Linux 87 # This assumes non-raid. 88 # problems detecting raid, journalling file systems 89 # 1. tomsrtbt won't handle raid 90 # 2. How determine what raid devices exist? 91 92 print_linux_partition_devices() { 93 if [ -f $INFODIR/fdisk ]; then 94 grep '83 *Linux' $INFODIR/fdisk | awk '{print $1}' 95 else 96 fdisk -l 2> /dev/null|grep '83 *Linux' | awk '{print $1}' 97 fi 98 } 99 100 # print the name of the root partition device on standard output if find it. 101 # Assume the root partition on the hard disk is not mounted 102 # Assume $c exists and is a directory: 103 guess_root_partition() { 104 ensure_hd_mountpoint_exists "guess_root_partition" 105 ensure_INFODIR_exists "guess_root_partition" 106 107 for d in `print_linux_partition_devices` 108 do 109 mount $d $c 110 if [ -f $c/etc/fstab ]; then 111 echo $d 112 fi 113 umount $c 114 done 115 } 116 117 # takes one parameter, the partition device file. 118 # mount the device on $c 119 mount_root_partition() { 120 device=$1 121 if mount | grep $device > /dev/null 2>&1 122 then 123 # false 124 return 1 125 fi 126 ensure_hd_mountpoint_exists "mount_root_partition" 127 128 if mount $device $c 129 then 130 # true 131 return 0 132 else 133 echo $prog: unable to mount $device on $c\; giving up 134 exit 1 135 fi 136 } 137 138 # if we have something mounted on $c, read plenty of Linux system configuration 139 # files from it. We could do the same for windows, but I don't know 140 # enough about the registry. 141 142 copy_config_from_root_partition() { 143 if mount | grep $c > /dev/null 2>&1 144 then 145 : 146 else 147 echo $prog: nothing mounted on $c\; giving up 148 exit 149 fi 150 if $use_floppy; then 151 if mount | grep $a > /dev/null 2>&1 152 then 153 : 154 else 155 echo $prog: no floppy mounted on $a\; giving up 156 exit 157 fi 158 fi 159 ensure_INFODIR_exists "copy_config_from_root_partition" 160 161 for i in fstab lilo.conf conf.modules modules.conf \ 162 sysconfig/network issue resolv.conf hosts profile bashrc \ 163 inittab auto.conf 164 do 165 cp -p $c/etc/$i $INFODIR/$d > /dev/null 2>&1 166 done 167 if [ -d $c/etc/rc.d ] 168 then 169 tar cf - $c/etc/rc.d 2> /dev/null | bzip2 -9 > $INFODIR/rc-d.tar.bz2 170 fi 171 if [ -d "$c/etc/sysconfig" ] 172 then 173 tar cf - $c/etc/sysconfig 2> /dev/null | bzip2 -9 > $INFODIR/sysconfig.tar.bz2 174 fi 175 if [ -f "$c/var/log/messages" ] 176 then 177 # bzip2 -9c $c/var/log/messages > $INFODIR/messages.bz2 178 tail -30000 $c/var/log/messages | bzip2 -9 > $INFODIR/messages.bz2 179 fi 180 df > $INFODIR/df 181 mount > $INFODIR/mount 182 } 183 184 # This subroutine is called automatically whenever the program exits, 185 # either because exit was called (signal zero), or an interrupt, hangup 186 # or terminate signal were sent to this script. 187 188 clean_up() { 189 echo cleaning up... 190 rmdir $a > /dev/null 2>&1 191 if $use_floppy 192 then 193 umount $FD > /dev/null 2>&1 194 fi 195 if [ "$c" = "/tmp/c" ] 196 then 197 umount $c > /dev/null 2>&1 198 rmdir $c 199 fi 200 } 201 202 # tomsrtbt doesn't have a basename. 203 # given a file name like "/this/directory/name/contains/a/file", 204 # will print "file" 205 basen() { 206 dir=$1; 207 echo $dir | awk 'BEGIN{FS="/"}{print $NF}' 208 } 209 210 # find the first part of the hostname without the domain name part. 211 # for example, if hostname gives "nickpc.tycm.vtc.edu.hk", will print 212 # "nickpc" 213 # hostname -s doesnt work on tomsrtbt 214 hostname_short() { 215 hostname | awk '{ sub( "\\..*$", "" ) }{ printf $0 }' 216 } 217 218 # tomsrtbt doesn't have tr, so can't use: 219 # INFODIR=$a/`date | tr ' :' '-'` 220 # take output from the date command (which looks like: 221 # Thu Feb 15 11:55:49 HKT 2001 222 # and replace all spaces and colons by hyphens, so it looks like this: 223 # Thu-Feb-15-11-55-49-HKT-2001 224 # Windows is not happy about the colons as a file name. 225 date_to_dir_name() { 226 date | awk '{ gsub( "[ :]", "-" ); printf $0 }' 227 } 228 229 usage() { 230 cat <&2 270 ;; 271 272 -o) 273 use_floppy=false 274 INFODIR="$2" 275 shift 276 ;; 277 278 -o*) 279 use_floppy=false 280 INFODIR=`echo "$1" | sed 's/-o//'` 281 ;; 282 283 *) 284 echo unknown option $1 285 usage 1 1>&2 286 ;; 287 esac 288 shift 289 done 290 291 mkdir $c 292 mkdir $a > /dev/null 2>&1 293 if $use_floppy; then 294 echo "Getting basic info about this computer..." 295 echo insert a formatted floppy with some disk space, any key to continue... 296 read anykey 297 FD=/dev/fd0 298 if mount -t vfat $FD $a 299 then 300 : 301 else 302 echo $prog: cannot mount floppy on $a\; giving up 303 exit 1 304 fi 305 fi 306 # cleanup on exit, hangup, interrupt, quit, termination 307 trap clean_up 0 1 2 3 15 308 mkdir $INFODIR 309 report_generic_info 310 311 if mounted_on_hd 312 then 313 c=/ 314 copy_config_from_root_partition 315 else 316 for device in `guess_root_partition` 317 do 318 echo your root partition seems to be $device. 319 mount_root_partition $device 320 copy_config_from_root_partition 321 umount $device 322 done 323 fi 324 $use_floppy && umount $a 325 rmdir $a > /dev/null 2>&1