shared 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #
  2. # shared 1.31 2002/08/19 03:21:11 (David Hinds)
  3. #
  4. usage ()
  5. {
  6. echo "usage: $0 [action] [device name]"
  7. echo " actions: start check stop suspend resume"
  8. exit 1
  9. }
  10. is_true ()
  11. {
  12. [ "$1" = "y" -o "$1" = "Y" -o "$1" = "yes" -o "$1" = "YES" ]
  13. }
  14. do_fuser ()
  15. {
  16. is_true $NO_FUSER && return 1
  17. fuser $*
  18. }
  19. do_eval () { eval $* ; }
  20. match () { case $1 in $2) return 0; ;; *) return 1; ;; esac ; }
  21. log ()
  22. {
  23. if [ -n "$VERBOSE" ] ; then
  24. echo "$*" ; eval $*
  25. else
  26. # if command fails, print the command followed by its output
  27. RET=0
  28. LOG=/var/run/cardmgr.out
  29. do_eval $* >$LOG 2>&1
  30. if [ $? -ne 0 ] ; then RET=$? ; echo "$*" ; fi
  31. cat $LOG 1>&2 ; rm -f $LOG
  32. return $RET
  33. fi
  34. }
  35. add_blkdev ()
  36. {
  37. # usage: add_blkdev [device]
  38. if [ -n "$LINK" ] ; then
  39. if [ -L $LINK ] ; then rm $LINK ; fi
  40. ln -s $1 $LINK || return 1
  41. fi
  42. if is_true $DO_FSTAB ; then
  43. echo "$1 $MOUNTPT ${FSTYPE:-auto} ${OPTS:-defaults} 0 0" >> /etc/fstab
  44. fi
  45. if is_true $DO_FSCK ; then
  46. log /sbin/fsck -Ta $1
  47. if [ $? -gt 1 ] ; then return 1 ; fi
  48. fi
  49. if is_true $DO_MOUNT ; then
  50. O=${OPTS:+-o $OPTS} ; FS=${FSTYPE:+-t $FSTYPE}
  51. log mount -v $O $FS $1 $MOUNTPT || return 1
  52. fi
  53. return 0
  54. }
  55. add_parts ()
  56. {
  57. # usage: add_parts [address] [parts]
  58. RET=0
  59. for PART in ${2:-""} ; do
  60. # Get mount options for particular partitions
  61. if [ -n "$PART" ] ; then
  62. ADDRESS="$1,$PART"
  63. unset INFO DO_FSTAB DO_FSCK DO_MOUNT LINK
  64. . $0.opts
  65. [ -n "$VERBOSE" -a -n "$INFO" ] && echo "$INFO"
  66. fi
  67. add_blkdev /dev/$DEVICE$PART || RET=1
  68. done
  69. return $RET
  70. }
  71. rm_blkdev ()
  72. {
  73. # usage: rm_blkdev [device]
  74. test -b $1 || return 1
  75. do_fuser -k -m $1 > /dev/null
  76. if mount | grep -q "$1 on" ; then
  77. log umount -v $1 || return 1
  78. fi
  79. if is_true $DO_FSTAB ; then
  80. grep -v $1 /etc/fstab > /etc/fstab.N
  81. mv /etc/fstab.N /etc/fstab
  82. fi
  83. if [ -L "$LINK" ] ; then rm $LINK ; fi
  84. }
  85. rm_parts ()
  86. {
  87. # usage: rm_parts [address] [parts]
  88. BLK=/dev/$DEVICE
  89. test -b $BLK || return 1
  90. for PART in ${2:-""} ; do
  91. if [ -n "$PART" ] ; then
  92. ADDRESS="$1,$PART"
  93. unset DO_FSTAB DO_FSCK DO_MOUNT
  94. . $0.opts
  95. fi
  96. if is_true $DO_FSTAB ; then
  97. grep -v "$BLK$PART " /etc/fstab > /etc/fstab.N
  98. mv /etc/fstab.N /etc/fstab
  99. fi
  100. done
  101. do_fuser -k -m /dev/${DEVICE}* > /dev/null
  102. LIST=`mount | sed -ne "s+^\($BLK[0-9]*\) .*+\1+p"`
  103. if [ -n "$LIST" ] ; then
  104. for MT in $LIST ; do
  105. log umount -v $MT || return 1
  106. done
  107. fi
  108. }
  109. chk_simple ()
  110. {
  111. # usage: chk_simple [new-address]
  112. ADDRESS=""
  113. OLD=`set | grep -v '^OLD=' | md5sum`
  114. ADDRESS="$1"
  115. . $0.opts
  116. ADDRESS=""
  117. NEW=`set | grep -v '^OLD=' | md5sum`
  118. [ "$OLD" = "$NEW" ]
  119. }
  120. chk_parts ()
  121. {
  122. # usage: chk_parts [new-address]
  123. OLD_ADDR="$ADDRESS"
  124. chk_simple $1 || return 1
  125. if [ -z "$PARTS" ] ; then return 0 ; fi
  126. for PART in $PARTS ; do
  127. ADDRESS="$OLD_ADDR,$PART"
  128. . $0.opts
  129. chk_simple "$1,$PART" || return 1
  130. done
  131. }
  132. grep_stab ()
  133. {
  134. # this should be cheaper than invoking "grep"
  135. local DEV
  136. while read SOCKET CLASS DRIVER INSTANCE DEV MAJOR MINOR ; do
  137. if [ "$SOCKET" != "Socket" -a "$1" = "$DEV" ] ; then return 0 ; fi
  138. done
  139. return 1
  140. }
  141. get_info ()
  142. {
  143. if [ -f /var/lib/pcmcia/scheme ] ; then
  144. SCHEME=`cat /var/lib/pcmcia/scheme`
  145. elif [ -f /var/run/pcmcia-scheme ] ; then
  146. SCHEME=`cat /var/run/pcmcia-scheme`
  147. fi
  148. if [ -z "$SCHEME" ] ; then SCHEME="default" ; fi
  149. if [ -z "$CLASS" -o -z "$MANFID" ] ; then
  150. if [ -d /var/lib/pcmcia ] ; then
  151. STAB=/var/lib/pcmcia/stab
  152. else
  153. STAB=/var/run/stab
  154. fi
  155. grep_stab $1 < $STAB || usage
  156. eval `/sbin/cardctl info $SOCKET`
  157. fi
  158. }
  159. # A bit of stuff to do right away...
  160. if [ $# -lt 2 ] ; then usage ; fi
  161. ACTION=$1 ; DEVICE=$2 ; NEW_SCHEME=$3