summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorerdgeist <erdgeist@erdgeist.org>2006-03-21 16:33:05 +0000
committererdgeist <erdgeist@erdgeist.org>2006-03-21 16:33:05 +0000
commit6c388abea89dddcf8c1c8b56b7d8a7f5edcfb2d5 (patch)
treeabd888104c38833251f309811637f1f74fb20fed
parent761bf8e2e9c40b48e8cde4d74116a46f3308fdf6 (diff)
ezjail now comes with image jails. HEADS UP! A lot of testing is ahead.
-rwxr-xr-xezjail-admin118
1 files changed, 98 insertions, 20 deletions
diff --git a/ezjail-admin b/ezjail-admin
index f968e5e..d905279 100755
--- a/ezjail-admin
+++ b/ezjail-admin
@@ -28,6 +28,16 @@ ezjail_fdescfs_enable=${ezjail_fdescfs_enable:-"YES"}
28# define our bail out shortcut 28# define our bail out shortcut
29exerr () { echo -e "$*"; exit 1; } 29exerr () { echo -e "$*"; exit 1; }
30 30
31# define detach strategy for image jails
32detach_images () {
33 # unmount and detach memory disc
34 if [ "${newjail_img_device}" ]; then
35 umount ${newjail_root}
36 [ "${newjail_image}" = "crypto" ] && gbde detach /dev/${newjail_img_device}
37 mdconfig -d -u ${newjail_img_device}
38 fi
39}
40
31# check for command 41# check for command
32[ "$1" ] || exerr "Usage: `basename -- $0` [create|delete|list|update] {params}" 42[ "$1" ] || exerr "Usage: `basename -- $0` [create|delete|list|update] {params}"
33 43
@@ -35,11 +45,14 @@ case "$1" in
35######################## ezjail-admin CREATE ######################## 45######################## ezjail-admin CREATE ########################
36create) 46create)
37 shift 47 shift
38 args=`getopt xf:r: $*` || exerr "Usage: `basename -- $0` create [-f flavour] [-r jailroot] [-x] jailname jailip" 48 args=`getopt f:r:s:xic $*` || exerr "Usage: `basename -- $0` create [-f flavour] [-r jailroot] [-s size] [-xic] jailname jailip"
39 49
40 newjail_root= 50 newjail_root=
41 newjail_flavour= 51 newjail_flavour=
42 newjail_softlink= 52 newjail_softlink=
53 newjail_image=
54 newjail_imagesize=
55 newjail_device=
43 newjail_fill="YES" 56 newjail_fill="YES"
44 57
45 set -- ${args} 58 set -- ${args}
@@ -48,14 +61,20 @@ create)
48 -x) newjail_fill="NO"; shift;; 61 -x) newjail_fill="NO"; shift;;
49 -r) newjail_root="$2"; shift 2;; 62 -r) newjail_root="$2"; shift 2;;
50 -f) newjail_flavour="$2"; shift 2;; 63 -f) newjail_flavour="$2"; shift 2;;
64 -i) newjail_image="simple"; shift;;
65 -s) newjail_imagesize="$2"; shift 2;;
66 -c) newjail_image="crypto"; shift;;
51 --) shift; break;; 67 --) shift; break;;
52 esac 68 esac
53 done 69 done
54 newjail_name=$1; newjail_ip=$2 70 newjail_name=$1; newjail_ip=$2
55 71
56 # we need at least a name and an ip for new jail 72 # we need at least a name and an ip for new jail
57 [ "${newjail_name}" -a "${newjail_ip}" -a $# = 2 ] || exerr "Usage: `basename -- $0` create [-f flavour] [-r jailroot] [-x] jailname jailip" 73 [ "${newjail_name}" -a "${newjail_ip}" -a $# = 2 ] || exerr "Usage: `basename -- $0` create [-f flavour] [-r jailroot] [-x] jailname jailip"
58 74
75 # check for sanity of settings concerning the image feature
76 [ "${newjail_image}" -a "$newjail_fill" = "YES" -a ! "${newjail_imagesize}" ] && exerr "Image jails need an image size."
77
59 # check, whether ezjail-update has been called. existence of 78 # check, whether ezjail-update has been called. existence of
60 # ezjail_jailbase is our indicator 79 # ezjail_jailbase is our indicator
61 [ -d ${ezjail_jailbase} ] || exerr "Error: base jail does not exist. Please run 'ezjail-admin update' first." 80 [ -d ${ezjail_jailbase} ] || exerr "Error: base jail does not exist. Please run 'ezjail-admin update' first."
@@ -103,35 +122,87 @@ create)
103 # All sanity checks that may lead to errors are hopefully passed here 122 # All sanity checks that may lead to errors are hopefully passed here
104 # 123 #
105 124
125 if [ "${newjail_image}" ]; then
126 # Strip trailing slashes from jail root, those would confuse image path
127 newjail_img=${newjail_root%/}; while [ "${newjail_img}" -a -z "${newjail_img%%*/}" ]; do newjail_img=${newjail_img%/}; done
128 [ -z "${newjail_img}" ] && exerr "Error: Could not determine image file name, something is wrong with the jail root: ${newjail_root}."
129
130 # Location of our image and crypto image lock file
131 newjail_lock=${newjail_img}.lock
132 newjail_img=${newjail_img}.img
133
134 # If NOT exist, create image
135 if [ "$newjail_fill" = "YES" ]; then
136 [ -e "${newjail_img}" ] && exerr "Error: a file exists at the location ${newjail_img}, preventing our own image file to be created."
137 [ "${newjail_image}" = "crypto" -a -e "${newjail_lock}" ] && exerr "Error: a file exists at the location ${newjail_lock}, preventing our own crypto image lock file to be created."
138
139 # Now create jail disc image
140 touch "${newjail_img}"
141 dd if=/dev/random of="${newjail_img}" bs="${newjail_imagesize}" count=1 || exerr "Error: Could not (or not fully) create the image file. You might want to check (and possibly remove) the file ${newjail_img}. The image size provided was ${newjail_imagesize}."
142
143 # And attach device
144 newjail_img_device=`mdconfig -a -t vnode -f ${newjail_img}`
145
146 if [ "${newjail_image}" = "crypto" ]; then
147 # Initialise crypto image
148 # XXX TODO: catch error and detach memory disc
149 echo "Initialising crypto device. Enter a new passphrase twice..."
150 gbde init /dev/${newjail_img_device} -L ${newjail_lock}
151
152 # XXX TODO: catch error and detach memory disc
153 echo "Attaching crypto device. Enter the passphrase..."
154 gbde attach /dev/${newjail_img_device} -l ${newjail_lock}
155 newjail_device=${newjail_img_device}.bde
156 else
157 newjail_device=${newjail_img_device}
158 fi
159
160 # Format memory image
161 newfs /dev/${newjail_device}
162 # Create mount point and mount
163 mkdir -p ${newjail_root}
164 mount /dev/${newjail_device} ${newjail_root}
165 else
166 [ -e ${newjail_root} -a ! -d ${newjail_root} ] && exerr "Error: Could not create mount point for your jail image. A file exists at its location. (For existing image jails, call this tool without the .img suffix when specifying jail root.)"
167 [ -d ${newjail_root} ] || mkdir -p ${newjail_root}
168 fi
169 fi
170
106 # now take a copy of our template jail 171 # now take a copy of our template jail
107 if [ "${newjail_fill}" = "YES" ]; then 172 if [ "${newjail_fill}" = "YES" ]; then
108 mkdir -p ${newjail_root} && cd ${ezjail_jailtemplate} && \ 173 mkdir -p ${newjail_root} && cd ${ezjail_jailtemplate} && find * | cpio -p -v ${newjail_root} > /dev/null
109 find * | cpio -p -v ${newjail_root} > /dev/null 174 [ $? = 0 ] || detach_images || exerr "Error: Could not copy template jail."
110 [ $? = 0 ] || exerr "Error: Could not copy template jail."
111 fi 175 fi
112 176
113 # if a soft link is necessary, create it now 177 # if a soft link is necessary, create it now
114 [ "${newjail_softlink}" ] && ln -s ${newjail_root} ${newjail_softlink} 178 [ "${newjail_softlink}" ] && ln -s ${newjail_root} ${newjail_softlink}
115 179
116 # if the automount feature is not disabled, this fstab entry for new jail 180 # if the automount feature is not disabled, this fstab entry for new jail
117 # will be obeyed 181 # will be obeyed
118 echo ${ezjail_jailbase} ${newjail_root}/basejail nullfs ro 0 0 > /etc/fstab.${newjail_nname} 182 echo -n > /etc/fstab.${newjail_nname}
183 [ "${newjail_image}" ] && \
184 echo ${newjail_root}.device ${newjail_root} ufs rw 0 0 >> /etc/fstab.${newjail_nname}
185 echo ${ezjail_jailbase} ${newjail_root}/basejail nullfs ro 0 0 >> /etc/fstab.${newjail_nname}
119 186
120 # now, where everything seems to have gone right, create control file in 187 # now, where everything seems to have gone right, create control file in
121 # ezjails config dir 188 # ezjails config dir
122 mkdir -p ${ezjail_jailcfgs} 189 mkdir -p ${ezjail_jailcfgs}
123 echo export jail_${newjail_nname}_hostname=\"${newjail_name}\" > ${ezjail_jailcfgs}/${newjail_nname} 190 echo export jail_${newjail_nname}_hostname=\"${newjail_name}\" > ${ezjail_jailcfgs}/${newjail_nname}
124 echo export jail_${newjail_nname}_ip=\"${newjail_ip}\" >> ${ezjail_jailcfgs}/${newjail_nname} 191 echo export jail_${newjail_nname}_ip=\"${newjail_ip}\" >> ${ezjail_jailcfgs}/${newjail_nname}
125 echo export jail_${newjail_nname}_rootdir=\"${newjail_root}\" >> ${ezjail_jailcfgs}/${newjail_nname} 192 echo export jail_${newjail_nname}_rootdir=\"${newjail_root}\" >> ${ezjail_jailcfgs}/${newjail_nname}
126 echo export jail_${newjail_nname}_exec=\"/bin/sh /etc/rc\" >> ${ezjail_jailcfgs}/${newjail_nname} 193 echo export jail_${newjail_nname}_exec=\"/bin/sh /etc/rc\" >> ${ezjail_jailcfgs}/${newjail_nname}
127 echo export jail_${newjail_nname}_mount_enable=\"${ezjail_mount_enable}\" >> ${ezjail_jailcfgs}/${newjail_nname} 194 echo export jail_${newjail_nname}_mount_enable=\"${ezjail_mount_enable}\" >> ${ezjail_jailcfgs}/${newjail_nname}
128 echo export jail_${newjail_nname}_devfs_enable=\"${ezjail_devfs_enable}\" >> ${ezjail_jailcfgs}/${newjail_nname} 195 echo export jail_${newjail_nname}_devfs_enable=\"${ezjail_devfs_enable}\" >> ${ezjail_jailcfgs}/${newjail_nname}
129 echo export jail_${newjail_nname}_devfs_ruleset=\"devfsrules_jail\" >> ${ezjail_jailcfgs}/${newjail_nname} 196 echo export jail_${newjail_nname}_devfs_ruleset=\"devfsrules_jail\" >> ${ezjail_jailcfgs}/${newjail_nname}
130 echo export jail_${newjail_nname}_procfs_enable=\"${ezjail_procfs_enable}\" >> ${ezjail_jailcfgs}/${newjail_nname} 197 echo export jail_${newjail_nname}_procfs_enable=\"${ezjail_procfs_enable}\" >> ${ezjail_jailcfgs}/${newjail_nname}
131 echo export jail_${newjail_nname}_fdescfs_enable=\"${ezjail_fdescfs_enable}\" >> ${ezjail_jailcfgs}/${newjail_nname} 198 echo export jail_${newjail_nname}_fdescfs_enable=\"${ezjail_fdescfs_enable}\" >> ${ezjail_jailcfgs}/${newjail_nname}
199 [ "${newjail_image}" ] && \
200 echo export jail_${newjail_nname}_image=\"${newjail_img}\" >> ${ezjail_jailcfgs}/${newjail_nname}
201 [ "${newjail_image}" = "crypto" ] && \
202 echo export jail_${newjail_nname}_cryptimage=\"YES\" >> ${ezjail_jailcfgs}/${newjail_nname}
132 203
133 # Final steps for flavour installation 204 # Final steps for flavour installation
134 if [ "${newjail_flavour}" ]; then 205 if [ "${newjail_fill}" = "YES" -a "${newjail_flavour}" ]; then
135 # install files and config to new jail 206 # install files and config to new jail
136 cd ${ezjail_flavours}/${newjail_flavour} && find * | cpio -p -v ${newjail_root} > /dev/null 207 cd ${ezjail_flavours}/${newjail_flavour} && find * | cpio -p -v ${newjail_root} > /dev/null
137 [ $? = 0 ] || echo "Warning: Could not fully install flavour." 208 [ $? = 0 ] || echo "Warning: Could not fully install flavour."
@@ -144,6 +215,9 @@ create)
144 fi 215 fi
145 fi 216 fi
146 217
218 # Detach (crypto and) memory discs
219 detach_images
220
147 # 221 #
148 # For user convenience some scenarios commonly causing headaches are checked 222 # For user convenience some scenarios commonly causing headaches are checked
149 # 223 #
@@ -160,7 +234,7 @@ create)
160 newjail_listener=`sockstat -4 -l | grep \*:[[:digit:]]` 234 newjail_listener=`sockstat -4 -l | grep \*:[[:digit:]]`
161 [ $? = 0 ] && echo -e "Warning: Some services already seem to be listening on all IP, (including ${newjail_ip})\n This may cause some confusion, here they are:\n${newjail_listener}" 235 [ $? = 0 ] && echo -e "Warning: Some services already seem to be listening on all IP, (including ${newjail_ip})\n This may cause some confusion, here they are:\n${newjail_listener}"
162 IFS=${TIFS} 236 IFS=${TIFS}
163 237
164 ;; 238 ;;
165######################## ezjail-admin DELETE ######################## 239######################## ezjail-admin DELETE ########################
166delete) 240delete)
@@ -190,6 +264,7 @@ delete)
190 # fetch information about the jail to be gone by parsing our records 264 # fetch information about the jail to be gone by parsing our records
191 . ${ezjail_jailcfgs}/${oldjail_nname} 265 . ${ezjail_jailcfgs}/${oldjail_nname}
192 eval oldjail_rootdir=\"\$jail_${oldjail_nname}_rootdir\" 266 eval oldjail_rootdir=\"\$jail_${oldjail_nname}_rootdir\"
267 eval oldjail_image=\"\$jail_${oldjail_nname}_image\"
193 268
194 # if jail is still running, refuse to go any further 269 # if jail is still running, refuse to go any further
195 [ -f /var/run/jail_${oldjail_nname}.id ] && exerr "Error: Jail appears to be still running, stop it first.\n(/var/run/jail_${oldjail_nname}.id exists)." 270 [ -f /var/run/jail_${oldjail_nname}.id ] && exerr "Error: Jail appears to be still running, stop it first.\n(/var/run/jail_${oldjail_nname}.id exists)."
@@ -204,7 +279,10 @@ delete)
204 [ -L ${oldjail_softlink} ] && rm ${oldjail_softlink} 279 [ -L ${oldjail_softlink} ] && rm ${oldjail_softlink}
205 280
206 # if wiping the jail was requested, remove it 281 # if wiping the jail was requested, remove it
207 [ "${oldjail_wipe}" ] && rm -rf ${oldjail_rootdir} 282 if [ "${oldjail_wipe}" ]; then
283 [ "${oldjail_image}" ] && rm -f ${oldjail_image} ${oldjail_image%.img}.lock ${oldjail_image%.img}.device
284 rm -rf ${oldjail_rootdir}
285 fi
208 286
209 ;; 287 ;;
210######################## ezjail-admin LIST ######################## 288######################## ezjail-admin LIST ########################