Adding casper 1.77+debian-2.
[live-boot-grml.git] / scripts / casper-helpers
1 ## Casper helper functions, used by casper on boot and by casper-snapshot
2
3 if [ "${BUILD_SYSTEM}" == "Ubuntu" ]; then
4     MP_QUIET="-Q"
5 else
6     MP_QUIET="-q"
7 fi
8
9 sys2dev() {
10     sysdev=${1#/sys}
11     echo "/dev/$(udevinfo -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
12 }
13
14 subdevices() {
15     sysblock=$1
16     r=""
17     for dev in "${sysblock}" "${sysblock}"/*; do
18         if [ -e "${dev}/dev" ]; then
19             r="${r} ${dev}"
20         fi
21     done
22     echo ${r}
23 }
24
25 where_is_mounted() {
26     device=$1
27     if grep -q "^$device " /proc/mounts; then
28         grep "^$device " /proc/mounts | read d mountpoint rest
29         echo $mountpoint
30         return 0
31     fi
32     return 1
33 }
34
35 lastline() {
36     while read lines ; do
37         line=${lines}
38     done
39     echo "${line}"
40 }
41
42 base_path ()
43 {
44     testpath="${1}"
45     mounts="$(awk '{print $2}' /proc/mounts)"
46     testpath="$(busybox realpath ${testpath})"
47
48     while true ; do
49         if echo "${mounts}" | grep -qs "^${testpath}" ; then
50             set -- `echo "${mounts}" | grep "^${testpath}" | lastline`
51             echo ${1}
52             break
53         else
54             testpath=`dirname $testpath`
55         fi
56     done
57 }
58
59 used_fs_size ()
60 {
61     # Returns used fs kbytes + 5% more
62     # You could pass a block device as $1 or the mount point as $2
63
64     dev="${1}"
65     mountp="${2}"
66
67     if [ -z "${mountp}" ]; then
68         mountp=$(where_is_mounted "${dev}")
69         if [ "$?" -gt 0 ]; then
70             mountp=/mnt/tmp_fs_size
71             mkdir -p "${mountp}"
72             mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}"
73             doumount=1
74         fi
75     fi
76
77     size=$(du -ks ${mountp} | cut -f1)
78     size=$(expr ${size} + ${size}/20 ) # FIXME: 5% more to be sure
79     needed_space=$(expr ${size} * 1024)
80
81     if [ ! -z "${doumount}" ]; then
82         umount "${mountp}"
83         rmdir "${mountp}"
84     fi
85     echo "${needed_space}"
86 }
87
88 setup_loop() {
89     local fspath=$1
90     local module=$2
91     local pattern=$3
92
93     modprobe "${MP_QUIET}" -b "$module"
94     udevsettle
95
96     for loopdev in $pattern; do
97         if [ "$(cat $loopdev/size)" -eq 0 ]; then
98             dev=$(sys2dev "${loopdev}")
99             losetup "$dev" "$fspath"
100             echo "$dev"
101             return 0
102         fi
103     done
104     panic "No loop devices available"
105 }
106
107 try_mount ()
108 {
109     dev="${1}"
110     mountp="${2}"
111     opts="${3}"
112
113     if where_is_mounted ${dev} > /dev/null; then
114         mount -o remount,"${opts}" ${dev} $(where_is_mounted ${dev}) || panic "Remounting failed"
115         mount -o bind $(where_is_mounted ${dev}) ${mountp} || panic "Cannot bind-mount"
116     else
117         mount -t $(get_fstype "${dev}") -o "${opts}" "${dev}" "${mountp}" || panic "Cannot mount ${dev} on ${mountp}"
118     fi
119 }
120
121 find_cow_device() {
122     pers_label="${1}"
123     cow_backing="/${pers_label}-backing"
124     for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop); do
125         for dev in $(subdevices "${sysblock}"); do
126             devname=$(sys2dev "${dev}")
127             if [ "$(/lib/udev/vol_id -l $devname 2>/dev/null)" = "${pers_label}" ]; then
128                 echo "$devname"
129                 return
130             elif [ "$(get_fstype ${devname})" = "vfat" ]; then # FIXME: all supported block devices should be scanned
131                 mkdir -p "${cow_backing}"
132                 try_mount "${devname}" "${cow_backing}" "rw"
133                 if [ -e "${cow_backing}/${pers_label}" ]; then
134                     echo $(setup_loop "${cow_backing}/${pers_label}" "loop" "/sys/block/loop*")
135                     return 0
136                 else
137                     umount ${cow_backing}
138                 fi
139             fi
140         done
141     done
142 }
143
144 find_files()
145 # return the first of $filenames found on vfat and ext2 devices
146 # FIXME: merge with above function
147 {
148     filenames="${1}"
149     snap_backing="/snap-backing"
150     for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop); do
151         for dev in $(subdevices "${sysblock}"); do
152             devname=$(sys2dev "${dev}")
153             devfstype="$(get_fstype ${devname})"
154             if [ "${devfstype}" = "vfat" ] ||  [ "${devfstype}" = "ext2" ] ; then # FIXME: all supported block devices should be scanned
155                 mkdir -p "${snap_backing}"
156                 try_mount "${devname}" "${snap_backing}" "ro"
157                 for filename in ${filenames}; do
158                     if [ -e "${snap_backing}/${filename}" ]; then
159                         echo "${devname} ${snap_backing} ${filename}"
160                         return 0
161                     fi
162                 done
163                 umount ${snap_backing}
164             fi
165         done
166     done
167 }
168
169