source: src-sh/pbi-manager/pbime/pbimount @ 011295d

releng/10.0
Last change on this file since 011295d was 011295d, checked in by Kris Moore <kris@…>, 8 months ago

Fix a critical bug in destroying the PBI container, which can cause
system lockups / hangs in VFS

  • Property mode set to 100755
File size: 6.2 KB
Line 
1#!/bin/sh
2
3NULLFS="/usr/pbi/.mount_nullfs"
4MNTLOCK="/usr/pbi/.mntLock"
5MNTSLEEP="sleep 0.5"
6
7# Grab list of current mounted file-systems
8mntOutput="`mount`"
9
10is_mounted() {
11   if [ ! -d "${1}" ] ; then return 1; fi
12   echo ${mntOutput} | grep -q "on ${1} ("
13   return $?
14}
15
16check_pbidir() {
17  if [ -d "${1}/pbimeta" ] ; then return ; fi
18  echo "Error: Invalid PBI: ${1}"
19  exit 1
20}
21
22get_pdir() {
23  pDir="${1}"
24  if [ ! -d "${pDir}/virtbase" ] ; then
25    mkdir -p ${pDir}/virtbase
26  fi
27  if [ ! -d "${pDir}/run" ] ; then
28    mkdir -p ${pDir}/run
29  fi
30}
31
32mount_pbifile() {
33   if [ ! -d "$pDir" ] ; then
34      mkdir -p "${pDir}"
35   else
36      # Check to see if it is already mounted
37      mount | grep -q "on ${pDir} (ufs,"
38      if [ $? -eq 0 ] ; then return; fi
39   fi
40   MD=`mdconfig -a -t vnode -f ${1}`
41   mount -r -t ufs /dev/${MD}.uzip "${pDir}"
42}
43
44check_nvidia_linux()
45{
46   # Check if this PBI even has a linux compat layer with GL support
47   if [ ! -d "${1}/linux/usr/lib" ] ; then return; fi
48   ls ${1}/linux/usr/lib/libGL* 2>/dev/null >/dev/null
49   if [ $? -ne 0 ] ; then return ; fi
50
51   # Ok, we have linux compat in the PBI, is the system using nvidia?
52   pkg info -e nvidia-driver
53   if [ $? -ne 0 ] ; then return ; fi
54
55   # We have nvidia, lets do our linux lib population now
56   for i in `pkg info -l nvidia-driver 2>/dev/null | grep 'compat/linux/usr/lib' | awk '{print $1}'`
57   do
58      cp ${i} ${1}/linux/usr/lib/
59   done
60}
61
62do_mount_lock() {
63
64   # At this point we have waited 15 seconds, if another mount still hasn't finished
65   # by now then there are other serious problems
66   if [ $1 -gt 10 ] ; then
67      exit 1
68   fi
69
70   # Check if we have another mount going now
71   if [ -e "${MNTLOCK}" ] ; then
72      pgrep -qF ${MNTLOCK} 2>/dev/null
73      if [ $? -ne 0 ] ; then
74         # Expired run it seems
75         rm -f ${MNTLOCK} 2>/dev/null
76      else
77         # Another mount is running, wait a bit for it to finish
78         sleep 1.5
79         do_mount_lock "`expr $1 + 1`"
80         return
81      fi
82   fi
83
84   echo "$$" > ${MNTLOCK}
85}
86
87undo_mount_lock() {
88  rm -f ${MNTLOCK} >/dev/null 2>/dev/null
89}
90
91mount_dirs() {
92
93   do_mount_lock "1"
94
95   # Check some of the common items to mount into the PBI container
96   is_mounted "${pDir}/virtbase" || ( $NULLFS / "${pDir}/virtbase" ; $MNTSLEEP ; check_nvidia_linux "${1}" )
97
98   is_mounted "${pDir}/virtbase/dev" || (mount -t devfs devfs "${pDir}/virtbase/dev" ; $MNTSLEEP )
99   is_mounted "${pDir}/virtbase/dev/fd" || (mount -t fdescfs fdesc "${pDir}/virtbase/dev/fd" ; $MNTSLEEP )
100   is_mounted "${pDir}/virtbase/usr/local" || ($NULLFS ${1}/local "${pDir}/virtbase/usr/local" ; $MNTSLEEP )
101   is_mounted "${pDir}/virtbase/var/run" || ($NULLFS ${pDir}/run "${pDir}/virtbase/var/run" ; $MNTSLEEP )
102   if [ -d "/var/run/dbus" ] ; then
103     is_mounted "${pDir}/virtbase/var/run/dbus" || ( mkdir ${pDir}/virtbase/var/run/dbus 2>/dev/null ; $NULLFS /var/run/dbus "${pDir}/virtbase/var/run/dbus" ; $MNTSLEEP )
104   fi
105
106   # If the host has Linux compat
107   if [ -d "${pDir}/virtbase/compat/linux" ] ; then
108     is_mounted "${pDir}/virtbase/compat/linux" || ($NULLFS ${1}/linux "${pDir}/virtbase/compat/linux" ; $MNTSLEEP )
109   fi
110
111   # Sort out the items to mount into the PBI container
112   for i in `mount | grep -e "(zfs" -e "(smbfs" -e "(nfs" -e "(ufs" -e "(msdos" -e "(ntfs" -e "(fusefs" | grep -e "on /" | grep -v "/usr/jails" | grep -v "/usr/pbi" | grep -v "/usr/local" | awk '{print $3}'`
113   do
114     if [ "$i" = "/" ] ; then continue ; fi
115     is_mounted "${pDir}/virtbase${i}" || ($NULLFS ${i} "${pDir}/virtbase${i}" ; $MNTSLEEP )
116   done
117
118   # Mount any system-wide fonts / cursors / icons into the PBI container
119   if [ -d "${pDir}/virtbase/usr/local/etc/fonts" -a -d "/usr/local/etc/fonts" ] ; then
120      is_mounted "${pDir}/virtbase/usr/local/etc/fonts" || ($NULLFS /usr/local/etc/fonts "${pDir}/virtbase/usr/local/etc/fonts" ; $MNTSLEEP )
121   fi
122   if [ -d "${pDir}/virtbase/usr/local/lib/X11/fonts" -a -d "/usr/local/lib/X11/fonts" ] ; then
123      is_mounted "${pDir}/virtbase/usr/local/lib/X11/fonts" || ($NULLFS /usr/local/lib/X11/fonts "${pDir}/virtbase/usr/local/lib/X11/fonts" ; $MNTSLEEP )
124   fi
125   if [ -d "${pDir}/virtbase/usr/local/lib/X11/icons" -a -d "/usr/local/lib/X11/icons" ] ; then
126      is_mounted "${pDir}/virtbase/usr/local/lib/X11/icons" || ($NULLFS /usr/local/lib/X11/icons "${pDir}/virtbase/usr/local/lib/X11/icons" ; $MNTSLEEP )
127   fi
128   if [ -d "${pDir}/virtbase/usr/local/share/icons" -a -d "/usr/local/share/icons" ] ; then
129      is_mounted "${pDir}/virtbase/usr/local/share/icons" || ($NULLFS /usr/local/share/icons "${pDir}/virtbase/usr/local/share/icons" ; $MNTSLEEP )
130   fi
131
132   # Check if we need to mount linprocfs in the PBI container
133   if [ -d "${pDir}/virtbase/compat/linux/proc" ] ; then
134      is_mounted "${pDir}/virtbase/compat/linux/proc" || (mount -t linprocfs linproc "${pDir}/virtbase/compat/linux/proc" ; $MNTSLEEP )
135   fi
136
137   undo_mount_lock
138
139   sleep 1
140   sync
141
142   # Copy over the system command
143   cp /usr/pbi/.pbisyscmd "${pDir}/virtbase/usr/local/bin/pbisyscmd"
144   cp /usr/pbi/.pbisyscmd "${pDir}/virtbase/usr/local/bin/openwith"
145   cp /usr/pbi/.pbisyscmd "${pDir}/virtbase/usr/local/bin/xdg-open"
146   chmod 755 "${pDir}/virtbase/usr/local/bin/pbisyscmd" 
147
148   # Copy the custom ldconfig
149   cp /usr/pbi/.ldconfig "${pDir}/virtbase/var/run/ldconfig"
150   chroot "${pDir}/virtbase" /var/run/ldconfig start >/dev/null 2>/dev/null
151
152   # Start devfs to ensure /dev/* get proper permissions
153   chroot "${pDir}/virtbase" /etc/rc.d/devfs start >/dev/null 2>/dev/null
154
155   sync
156}
157
158umount_pbidir() {
159   for i in `mount | grep "on ${pDir}/virtbase/usr/local" | tail -r | awk '{print $3}'`
160   do
161     #echo "Unmounting ${i}"
162     umount -f ${i}
163     sleep 0.5
164   done
165   for i in `mount | grep "on ${pDir}/virtbase/compat/linux" | tail -r | awk '{print $3}'`
166   do
167     #echo "Unmounting ${i}"
168     umount -f ${i}
169     sleep 0.5
170   done
171}
172
173
174# Doing an unmount?
175if [ "$1" = "umount" ] ; then
176   get_pdir "$2"
177   umount_pbidir "$2"
178   exit 0
179fi
180
181if [ -z "$1" -o -z "$2" ] ; then
182   echo "Missing args: mountdir / pbidir"
183   exit 1
184fi
185
186MNTBASEDIR="`dirname $2`"
187
188if [ ! -d "$MNTBASEDIR" ] ; then
189   mkdir -p ${MNTBASEDIR}
190   echo "Do not manually remove this directory!" > ${MNTBASEDIR}/DONOTDELETE
191fi
192
193# Lets begin the mount process
194get_pdir "$1"
195check_pbidir "${2}"
196mount_dirs "${2}"
Note: See TracBrowser for help on using the repository browser.