72d6c3ab3c77cf637cc8bf7ccb96b2694bebba51
[openwrt-10.03/.git] / package / block-mount / files / block.sh
1 #!/bin/sh
2 # Copyright 2010 Vertical Communications
3 # This is free software, licensed under the GNU General Public License v2.
4 # See /LICENSE for more information.
5 #
6
7 . /etc/functions.sh
8
9 reset_block_cb() {
10         mount_cb() { 
11                 dmc_mount_cfg="$1"
12                 shift # skip optional param
13                 dmc_target="$2"
14                 dmc_mount_device="$3"
15                 dmc_fstype="$4"
16                 dmc_options="$5"
17                 dmc_enabled="$6"
18                 dmc_enabled_fsck="$7"
19                 dmc_uuid="$8"
20                 dmc_label="$9"
21                 shift
22                 dmc_is_rootfs="$9"
23                 return 0
24         }
25         swap_cb() { 
26                 dsc_swap_cfg="$1"
27                 shift # skip optional param
28                 dsc_swap_device="$2"
29                 dsc_enabled="$3"
30                 dsc_uuid="$4"
31                 dsc_label="$5"
32                 return 0
33         }
34 }
35 reset_block_cb
36
37 reset_dev_section_cb() {
38         mount_dev_section_cb() {
39                 dmds_mount_cfg="$1"
40                 dmds_mount_target="$2"
41                 dmds_mount_device="$3"
42                 dmds_mount_fstype="$4"
43                 dmds_mount_options="$5"
44                 dmds_mount_enabled="$6"
45                 dmds_mount_enabled_fsck="$7"
46                 dmds_mount_uuid="$8"
47                 dmds_mount_label="$9"
48                 shift
49                 dmds_is_rootfs="$9"
50                 return 0
51         }
52         swap_dev_section_cb() { 
53                 dsds_swap_cfg="$1"
54                 dsds_swap_device="$2"
55                 dsds_swap_enabled="$3"
56                 dsds_swap_uuid="$4"
57                 dsds_swap_label="$5"
58                 return 0
59         }
60 }
61 reset_dev_section_cb
62
63 config_get_mount() {
64         local gm_cfg="$1"
65         local gm_param="$2"
66         local gm_target
67         local gm_device
68         local gm_fstype
69         local gm_options
70         local gm_enabled
71         local gm_enabled_fsck
72         local gm_uuid
73         local gm_label
74         local gm_is_rootfs
75         config_get gm_target "$1" target
76         config_get gm_device "$1" device
77         config_get gm_fstype "$1" fstype 'auto'
78         config_get gm_options "$1" options 'rw'
79         config_get_bool gm_enabled "$1" enabled 1
80         config_get_bool gm_enabled_fsck "$1" enabled_fsck 0
81         config_get gm_uuid "$1" uuid
82         config_get gm_label "$1" label
83         config_get_bool gm_is_rootfs "$1" is_rootfs 0
84         mount_cb "$gm_cfg" "$gm_param" "$gm_target" "$gm_device" "$gm_fstype" "$gm_options" "$gm_enabled" "$gm_enabled_fsck" "$gm_uuid" "$gm_label" "$gm_is_rootfs"
85 }
86
87 config_get_swap() {
88         local gs_cfg="$1"
89         local gs_param="$2"
90         local gs_device
91         local gs_enabled
92         local gs_uuid
93         local gs_label
94         config_get gs_device "$1" device
95         config_get_bool gs_enabled "$1" enabled 1
96         config_get gs_uuid "$1" uuid
97         config_get gs_label "$1" label
98
99         swap_cb "$gs_cfg" "$gs_param" "$gs_device" "$gs_enabled" "$gs_uuid" "$gs_label"
100 }
101
102 config_get_automount() {
103         config_load fstab
104         config_get_bool from_fstab "automount" from_fstab 1
105         config_get_bool anon_mount "automount" anon_mount 1
106         config_get_bool anon_fsck "automount" anon_fsck 0
107 }
108
109 config_get_autoswap() {
110         config_load fstab
111         config_get_bool from_fstab "autoswap" from_fstab 1
112         config_get_bool anon_swap "autoswap" anon_swap 0
113 }
114
115 config_create_swap_fstab_entry() {
116         local device="$1"
117         local enabled="$2"
118
119         [ -n "$device" ] || return 0
120
121         local fstabnew
122         
123         mkdir -p /var/lock
124         lock -w /var/lock/fstab.lck && {
125                 lock /var/lock/fstab.lck
126                 fstabnew="$(mktemp -t '.fstab.XXXXXXXX')"
127                 cat /tmp/fstab | grep -E -v "^$device[[:blank:]]" >>"$fstabnew"
128                 [ "$enabled" -eq 1 ] && echo "$device   none    swap    sw      0       0" >> "$fstabnew"
129                 cat "$fstabnew" >/tmp/fstab
130                 rm -f $fstabnew
131                 lock -u /var/lock/fstab.lck
132         }
133 }
134
135 config_create_mount_fstab_entry() {
136         local device="$1"
137         local target="$2"
138         local fstype="$3"
139         local options="$4"
140         local enabled="$5"
141         options="${options:-rw}"
142         [ "$enabled" -eq 0 ] && options="noauto,$options"
143         [ -n "$target" ] || return 0
144         [ -n "$device" ] || return 0
145
146         local fstabnew
147         
148         mkdir -p /var/lock
149         lock -w /var/lock/fstab.lck && {
150                 lock /var/lock/fstab.lck
151                 fstabnew="$(mktemp -t '.fstab.XXXXXXXX')"
152                 cat /tmp/fstab | grep -E -v "^$device[[:blank:]]" | grep -v "$target" >>"$fstabnew"
153                 echo "$device   $target $fstype $options        0       0" >>"$fstabnew"
154                 cat "$fstabnew" >/tmp/fstab             
155                 rm -f $fstabnew
156                 lock -u /var/lock/fstab.lck
157         }
158 }
159
160 libmount_find_token() {
161         local token="$1"
162         local value="$2"
163         local device
164         device="$(blkid | grep "$token=\"$value\"" | cut -f1 -d:)"
165         echo "$device"
166 }
167
168 libmount_find_device_by_id() {
169         local uuid="$1"
170         local label="$2"
171         local device="$3"
172         local cfg_device="$4"
173         local found_device
174         
175         if [ -n "$uuid" ]; then
176                 found_device="$(libmount_find_token "UUID" "$uuid")"
177         elif [ -n "$label" ]; then
178                 found_device="$(libmount_find_token "LABEL" "$label")"
179         elif [ "$device" = "$cfg_device" ]; then
180                 found_device="$device"
181         elif [ -z "$device" ] && [ -e "$cfg_device" ]; then
182                 found_device="$cfg_device"
183         fi
184         [ -n "$device" ] && [ "$device" != "$found_device" ] && {
185                 found_device=""
186         }
187         echo "$found_device"
188 }
189
190 config_get_mount_section_by_device() {
191         local msbd_device="$1"
192         local msbd_mount_cfg=
193         local msbd_target=
194         local msbd_mount_device=
195         local msbd_fstype=
196         local msbd_options=
197         local msbd_enabled=
198         local msbd_enabled_fsck=
199         local msbd_uuid=
200         local msbd_label=
201         local msbd_is_rootfs
202         local msbd_blkid_fstype_match=
203         mount_cb() {
204                 local mc_cfg="$1"
205                 local mc_device="$2"
206                 shift
207                 local mc_target="$2"
208                 local mc_cfgdevice="$3"
209                 local mc_fstype="$4"
210                 local mc_uuid="$8"
211                 local mc_label="$9"
212                 shift
213                 local mc_is_rootfs="$9"
214                 local mc_found_device=""
215                 
216                 mc_found_device="$(libmount_find_device_by_id "$mc_uuid" "$mc_label" "$mc_device" "$mc_cfgdevice")"
217                 if [ -n "$mc_found_device" ]; then
218                         msbd_mount_cfg="$mc_cfg"
219                         msbd_target="$mc_target"
220                         msbd_mount_device="$mc_found_device"
221                         msbd_fstype="$mc_fstype"
222                         msbd_options="$4"
223                         msbd_enabled="$5"
224                         msbd_enabled_fsck="$6"
225                         msbd_uuid="$7"
226                         msbd_label="$8"
227                         msbd_is_rootfs="$9"
228                 fi
229                 return 0        
230         }
231         config_foreach config_get_mount mount "$msbd_device"
232         [ -n "$msbd_mount_device" ] && config_create_mount_fstab_entry "$msbd_mount_device" "$msbd_target" "$msbd_fstype" "$msbd_options" "$msbd_enabled" 
233         mount_dev_section_cb "$msbd_mount_cfg" "$msbd_target" "$msbd_mount_device" "$msbd_fstype" "$msbd_options" "$msbd_enabled" "$msbd_enabled_fsck" "$msbd_uuid" "$msbd_label" "$msbd_is_rootfs"
234         reset_block_cb
235 }
236
237 config_get_swap_section_by_device() {
238         local ssbd_device="$1"
239         local ssbd_swap_cfg=
240         local ssbd_swap_device=
241         local ssbd_enabled=
242         local ssbd_uuid=
243         local ssbd_label=
244         swap_cb() {
245                 local sc_cfg="$1"
246                 local sc_device="$2"
247                 local sc_uuid="$5"
248                 local sc_label="$6"
249                 local sc_cfgdevice="$3"
250                 local sc_found_device
251
252                 sc_found_device="$(libmount_find_device_by_id "$sc_uuid" "$sc_label" "$sc_device" "$sc_cfgdevice")"
253                 if [ -n "$sc_found_device" ]; then
254                         ssbd_swap_cfg="$sc_cfg"
255                         ssbd_swap_device="$sc_found_device"
256                         ssbd_enabled="$4"
257                         ssbd_uuid="$5"
258                         ssbd_label="$6"
259                 fi
260                 return 0        
261         }
262         config_foreach config_get_swap swap "$ssbd_device"
263         [ -n "$ssbd_swap_device" ] && config_create_swap_fstab_entry "$ssbd_swap_device" "$ssbd_enabled"
264         swap_dev_section_cb "$ssbd_swap_cfg" "$ssbd_swap_device" "$ssbd_enabled" "$ssbd_uuid" "$ssbd_label"
265         reset_block_cb
266 }
267
268