[backfire] merge r27226
[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 /var/lock/fstab.lck
125         fstabnew="$(mktemp -t '.fstab.XXXXXXXX')"
126         cat /tmp/fstab | grep -E -v "^$device[[:blank:]]" >>"$fstabnew"
127         [ "$enabled" -eq 1 ] && echo "$device   none    swap    sw      0       0" >> "$fstabnew"
128         cat "$fstabnew" >/tmp/fstab
129         rm -f $fstabnew
130         lock -u /var/lock/fstab.lck
131 }
132
133 config_create_mount_fstab_entry() {
134         local device="$1"
135         local target="$2"
136         local fstype="$3"
137         local options="$4"
138         local enabled="$5"
139         options="${options:-rw}"
140         [ "$enabled" -eq 0 ] && options="noauto,$options"
141         [ -n "$target" ] || return 0
142         [ -n "$device" ] || return 0
143
144         local fstabnew
145
146         mkdir -p /var/lock
147         lock /var/lock/fstab.lck
148         fstabnew="$(mktemp -t '.fstab.XXXXXXXX')"
149         cat /tmp/fstab | grep -E -v "^$device[[:blank:]]" | grep -v "$target" >>"$fstabnew"
150         echo "$device   $target $fstype $options        0       0" >>"$fstabnew"
151         cat "$fstabnew" >/tmp/fstab             
152         rm -f $fstabnew
153         lock -u /var/lock/fstab.lck
154 }
155
156 libmount_find_token() {
157         local token="$1"
158         local value="$2"
159         local device
160         device="$(blkid | grep "$token=\"$value\"" | cut -f1 -d:)"
161         echo "$device"
162 }
163
164 libmount_find_device_by_id() {
165         local uuid="$1"
166         local label="$2"
167         local device="$3"
168         local cfg_device="$4"
169         local found_device
170         
171         if [ -n "$uuid" ]; then
172                 found_device="$(libmount_find_token "UUID" "$uuid")"
173         elif [ -n "$label" ]; then
174                 found_device="$(libmount_find_token "LABEL" "$label")"
175         elif [ "$device" = "$cfg_device" ]; then
176                 found_device="$device"
177         elif [ -z "$device" ] && [ -e "$cfg_device" ]; then
178                 found_device="$cfg_device"
179         fi
180         [ -n "$device" ] && [ "$device" != "$found_device" ] && {
181                 found_device=""
182         }
183         echo "$found_device"
184 }
185
186 config_get_mount_section_by_device() {
187         local msbd_device="$1"
188         local msbd_mount_cfg=
189         local msbd_target=
190         local msbd_mount_device=
191         local msbd_fstype=
192         local msbd_options=
193         local msbd_enabled=
194         local msbd_enabled_fsck=
195         local msbd_uuid=
196         local msbd_label=
197         local msbd_is_rootfs
198         local msbd_blkid_fstype_match=
199         mount_cb() {
200                 local mc_cfg="$1"
201                 local mc_device="$2"
202                 shift
203                 local mc_target="$2"
204                 local mc_cfgdevice="$3"
205                 local mc_fstype="$4"
206                 local mc_uuid="$8"
207                 local mc_label="$9"
208                 shift
209                 local mc_is_rootfs="$9"
210                 local mc_found_device=""
211                 
212                 mc_found_device="$(libmount_find_device_by_id "$mc_uuid" "$mc_label" "$mc_device" "$mc_cfgdevice")"
213                 if [ -n "$mc_found_device" ]; then
214                         msbd_mount_cfg="$mc_cfg"
215                         msbd_target="$mc_target"
216                         msbd_mount_device="$mc_found_device"
217                         msbd_fstype="$mc_fstype"
218                         msbd_options="$4"
219                         msbd_enabled="$5"
220                         msbd_enabled_fsck="$6"
221                         msbd_uuid="$7"
222                         msbd_label="$8"
223                         msbd_is_rootfs="$9"
224                 fi
225                 return 0        
226         }
227         config_foreach config_get_mount mount "$msbd_device"
228         [ -n "$msbd_mount_device" ] && config_create_mount_fstab_entry "$msbd_mount_device" "$msbd_target" "$msbd_fstype" "$msbd_options" "$msbd_enabled" 
229         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"
230         reset_block_cb
231 }
232
233 config_get_swap_section_by_device() {
234         local ssbd_device="$1"
235         local ssbd_swap_cfg=
236         local ssbd_swap_device=
237         local ssbd_enabled=
238         local ssbd_uuid=
239         local ssbd_label=
240         swap_cb() {
241                 local sc_cfg="$1"
242                 local sc_device="$2"
243                 local sc_uuid="$5"
244                 local sc_label="$6"
245                 local sc_cfgdevice="$3"
246                 local sc_found_device
247
248                 sc_found_device="$(libmount_find_device_by_id "$sc_uuid" "$sc_label" "$sc_device" "$sc_cfgdevice")"
249                 if [ -n "$sc_found_device" ]; then
250                         ssbd_swap_cfg="$sc_cfg"
251                         ssbd_swap_device="$sc_found_device"
252                         ssbd_enabled="$4"
253                         ssbd_uuid="$5"
254                         ssbd_label="$6"
255                 fi
256                 return 0        
257         }
258         config_foreach config_get_swap swap "$ssbd_device"
259         [ -n "$ssbd_swap_device" ] && config_create_swap_fstab_entry "$ssbd_swap_device" "$ssbd_enabled"
260         swap_dev_section_cb "$ssbd_swap_cfg" "$ssbd_swap_device" "$ssbd_enabled" "$ssbd_uuid" "$ssbd_label"
261         reset_block_cb
262 }
263
264