Added tag 0.9.13 for changeset 635bd08c02fba6f96a6775f6e9581bac4de82e7b
[grml-scripts.git] / usr_bin / grml-bind
1 #!/bin/sh
2 # Filename:      grml-bind
3 # Purpose:       Program to copy config files and mount them back for editing
4 # Authors:       grml-team (grml.org), (c) Michael Gebetsroither <gebi@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 # Latest change: Thu Feb 10 06:25:01 CET 2005 [gebi]
8 ################################################################################
9
10
11 ###
12 ### __INCLUDES
13 ###
14 . /etc/grml/sh-lib
15 #. /etc/grml/sysexits-sh
16
17
18
19 ###
20 ### __VARIABLES
21 ###
22
23 # real vars (script defaults)
24 target_="/ramdisk/grml-bind"
25 runsFromHd && target_="/tmp/grml-bind"   # where to save the file
26 file_ro_=""   # secured input filename (source)
27 target_path_="" #secured path to target + filename
28
29 # command parameter vars
30 verbose_=0
31 remove_mapping_=0   # user wants to remove the mapping
32 create_mapping=0    # user wants to create the mapping
33
34
35
36 ###
37 ### __FUNCTIONS
38 ###
39
40 # print program usage
41 function printUsage
42 {
43 cat << EOF
44 Usage: "grml-bind" [OPTIONS] <file you want to edit>
45
46 grml-bind is a program to prepare a file on a read-only fs for writing
47 It copies the file to a ramdisk and mount --bind it back.
48 Very usefull for knoppix-like distributions.
49
50 OPTIONS:
51    -c     create the file-mapping
52    -r     remove the file-mapping
53    -v     verbose (show what is going on, v++)
54    -h     this help text
55    ()     create the file-mapping
56
57 EOF
58 }
59
60
61 # function to create the mapping
62 function createMapping
63 {
64   isExistent "$file_ro_" die
65   isNotExistent "$target_path_" die
66
67   # --preserve=mode,ownership,timestamps can be a problem for normal user
68   # but neverless mount --bind requires extended privilegs, so who cares?
69   execute "cp -rL \"$file_ro_\" \"$target_\"" warn \
70     "Problms copying data to ramdisk"
71
72   # mount--bind the file from ramdisk back over the r/o file
73   execute "mount --bind \"$target_path_\" \"$file_ro_\"" warn || \
74     execute "rm -r \"$target_path_\"" die
75   
76   exit 0
77 }
78
79
80 # remove the mapping
81 function removeMapping
82 {
83   isExistent "$target_path_" die \
84     "You have not edited this file yet: \"$target_path_\""
85
86   execute "umount \"$file_ro_\"" warn
87   execute "rm -r \"$target_path_\"" die
88
89   exit 0
90 }
91
92
93 ###
94 ### __START
95 ###
96
97 # parse commandline
98 while getopts vrhc opt_ ; do
99   case "$opt_" in
100     r) remove_mapping_=1 ;;
101     c) create_mapping=1 ;;
102     h) printUsage; exit ;;
103     v) let verbose_=$verbose_+1 ;;
104     ?) printUsage; exit 64 ;;
105   esac
106 done
107 shift $(($OPTIND - 1))  # set ARGV to the first not parsed commandline parameter
108 setVerbose $verbose_
109
110
111 ###
112 # Working part
113 ###
114
115 setExitFunction "printUsage"
116 # hmmm user has not given us a filename/path
117 if [ -z "$1" ]; then
118   die "you need to give me a file/path" 64
119 fi
120
121 # hmmm user gave us the wrong number of arguments
122 if [ "$#" -ne 1 ]; then
123   die "wrong number of arguments" 64
124 fi
125 resetExitFunction
126
127 checkRoot die
128
129 # does our target-path exist? if not create it
130 if [ ! -d $target_ ]; then
131   warn "target path \"$target_\" does not exist => creating"
132   execute "mkdir -p $target_" die
133   runsFromHd && execute "chmod 751 $target_" die
134 fi
135
136 # secure the input file/path
137 file_ro_=`secureInput "$1"`
138
139 # check if input file is a link, if yes resolve it
140 if [ -L $file_ro_ ]; then
141   file_ro_=`execute "readlink \"$file_ro_\""`
142 fi
143
144 #create all possible pathformats to absolut paths
145 normalized_source_path_=`relToAbs "$file_ro_"`
146 file_ro_=$normalized_source_path_
147 target_path_=$target_/`execute "basename \"$normalized_source_path_\""`
148
149 # do what the user wants, maybee *g*
150 if [ $remove_mapping_ -eq 1 ]; then # ok, the user wants to remove his mapping
151   removeMapping
152 else # no, user wants to create the mapping
153   createMapping
154 fi
155
156 # END OF FILE ################################################################################