blob: d24212d74ad887f93ab7e414ad59e05730162c3f [file] [log] [blame]
Dean Troyer67787e62012-05-02 11:48:15 -05001# lib/cinder
2# Install and start Cinder volume service
3
4# Dependencies:
5# - functions
Dean Troyer50ac7922012-09-13 14:02:01 -05006# - DEST, DATA_DIR must be defined
Dean Troyer67787e62012-05-02 11:48:15 -05007# SERVICE_{TENANT_NAME|PASSWORD} must be defined
Dean Troyerbc071bc2012-10-01 14:06:44 -05008# ``KEYSTONE_TOKEN_FORMAT`` must be defined
Dean Troyer67787e62012-05-02 11:48:15 -05009
10# stack.sh
11# ---------
12# install_XXX
13# configure_XXX
14# init_XXX
15# start_XXX
16# stop_XXX
17# cleanup_XXX
18
Dean Troyer7903b792012-09-13 17:16:12 -050019# Save trace setting
20XTRACE=$(set +o | grep xtrace)
21set +o xtrace
Dean Troyer67787e62012-05-02 11:48:15 -050022
23
24# Defaults
25# --------
26
27# set up default directories
28CINDER_DIR=$DEST/cinder
Dean Troyer50ac7922012-09-13 14:02:01 -050029CINDERCLIENT_DIR=$DEST/python-cinderclient
30CINDER_STATE_PATH=${CINDER_STATE_PATH:=$DATA_DIR/cinder}
31CINDER_CONF_DIR=/etc/cinder
32CINDER_CONF=$CINDER_CONF_DIR/cinder.conf
Dean Troyerbc071bc2012-10-01 14:06:44 -050033CINDER_AUTH_CACHE_DIR=${CINDER_AUTH_CACHE_DIR:-/var/cache/cinder}
Dean Troyer50ac7922012-09-13 14:02:01 -050034
35# Support entry points installation of console scripts
36if [[ -d $CINDER_DIR/bin ]]; then
Monty Taylor9fbeedd2012-08-17 12:52:27 -040037 CINDER_BIN_DIR=$CINDER_DIR/bin
38else
39 CINDER_BIN_DIR=/usr/local/bin
40fi
Dean Troyer67787e62012-05-02 11:48:15 -050041
42# Name of the lvm volume group to use/create for iscsi volumes
43VOLUME_GROUP=${VOLUME_GROUP:-stack-volumes}
44VOLUME_NAME_PREFIX=${VOLUME_NAME_PREFIX:-volume-}
45
46# cleanup_cinder() - Remove residual data files, anything left over from previous
47# runs that a clean run would need to clean up
48function cleanup_cinder() {
49 # This function intentionally left blank
50 :
51}
52
53# configure_cinder() - Set config files, create data dirs, etc
54function configure_cinder() {
55 setup_develop $CINDER_DIR
56 setup_develop $CINDERCLIENT_DIR
57
58 if [[ ! -d $CINDER_CONF_DIR ]]; then
59 sudo mkdir -p $CINDER_CONF_DIR
60 fi
61 sudo chown `whoami` $CINDER_CONF_DIR
62
63 cp -p $CINDER_DIR/etc/cinder/policy.json $CINDER_CONF_DIR
64
John Griffith4e823ff2012-07-20 13:18:17 -060065 # Set the paths of certain binaries
66 if [[ "$os_PACKAGE" = "deb" ]]; then
67 CINDER_ROOTWRAP=/usr/local/bin/cinder-rootwrap
68 else
69 CINDER_ROOTWRAP=/usr/bin/cinder-rootwrap
70 fi
71
72 # If Cinder ships the new rootwrap filters files, deploy them
73 # (owned by root) and add a parameter to $CINDER_ROOTWRAP
74 ROOTWRAP_CINDER_SUDOER_CMD="$CINDER_ROOTWRAP"
75 if [[ -d $CINDER_DIR/etc/cinder/rootwrap.d ]]; then
76 # Wipe any existing rootwrap.d files first
77 if [[ -d $CINDER_CONF_DIR/rootwrap.d ]]; then
78 sudo rm -rf $CINDER_CONF_DIR/rootwrap.d
79 fi
80 # Deploy filters to /etc/cinder/rootwrap.d
81 sudo mkdir -m 755 $CINDER_CONF_DIR/rootwrap.d
82 sudo cp $CINDER_DIR/etc/cinder/rootwrap.d/*.filters $CINDER_CONF_DIR/rootwrap.d
83 sudo chown -R root:root $CINDER_CONF_DIR/rootwrap.d
84 sudo chmod 644 $CINDER_CONF_DIR/rootwrap.d/*
85 # Set up rootwrap.conf, pointing to /etc/cinder/rootwrap.d
86 sudo cp $CINDER_DIR/etc/cinder/rootwrap.conf $CINDER_CONF_DIR/
87 sudo sed -e "s:^filters_path=.*$:filters_path=$CINDER_CONF_DIR/rootwrap.d:" -i $CINDER_CONF_DIR/rootwrap.conf
88 sudo chown root:root $CINDER_CONF_DIR/rootwrap.conf
89 sudo chmod 0644 $CINDER_CONF_DIR/rootwrap.conf
90 # Specify rootwrap.conf as first parameter to cinder-rootwrap
91 CINDER_ROOTWRAP="$CINDER_ROOTWRAP $CINDER_CONF_DIR/rootwrap.conf"
92 ROOTWRAP_CINDER_SUDOER_CMD="$CINDER_ROOTWRAP *"
93 fi
94
95 TEMPFILE=`mktemp`
96 echo "$USER ALL=(root) NOPASSWD: $ROOTWRAP_CINDER_SUDOER_CMD" >$TEMPFILE
97 chmod 0440 $TEMPFILE
98 sudo chown root:root $TEMPFILE
99 sudo mv $TEMPFILE /etc/sudoers.d/cinder-rootwrap
100
Dean Troyer67787e62012-05-02 11:48:15 -0500101 CINDER_API_PASTE_INI=$CINDER_CONF_DIR/api-paste.ini
102 cp $CINDER_DIR/etc/cinder/api-paste.ini $CINDER_API_PASTE_INI
103 iniset $CINDER_API_PASTE_INI filter:authtoken auth_host $KEYSTONE_AUTH_HOST
104 iniset $CINDER_API_PASTE_INI filter:authtoken auth_port $KEYSTONE_AUTH_PORT
105 iniset $CINDER_API_PASTE_INI filter:authtoken auth_protocol $KEYSTONE_AUTH_PROTOCOL
106 iniset $CINDER_API_PASTE_INI filter:authtoken admin_tenant_name $SERVICE_TENANT_NAME
107 iniset $CINDER_API_PASTE_INI filter:authtoken admin_user cinder
108 iniset $CINDER_API_PASTE_INI filter:authtoken admin_password $SERVICE_PASSWORD
109
Dean Troyerbc071bc2012-10-01 14:06:44 -0500110 if [[ "$KEYSTONE_TOKEN_FORMAT" == "PKI" ]]; then
111 iniset $CINDER_API_PASTE_INI filter:authtoken signing_dir $CINDER_AUTH_CACHE_DIR
112 fi
113
Dean Troyer67787e62012-05-02 11:48:15 -0500114 cp $CINDER_DIR/etc/cinder/cinder.conf.sample $CINDER_CONF
115 iniset $CINDER_CONF DEFAULT auth_strategy keystone
116 iniset $CINDER_CONF DEFAULT verbose True
117 iniset $CINDER_CONF DEFAULT volume_group $VOLUME_GROUP
118 iniset $CINDER_CONF DEFAULT volume_name_template ${VOLUME_NAME_PREFIX}%s
119 iniset $CINDER_CONF DEFAULT iscsi_helper tgtadm
Terry Wilson428af5a2012-11-01 16:12:39 -0400120 local dburl
121 database_connection_url dburl cinder
122 iniset $CINDER_CONF DEFAULT sql_connection $dburl
Dean Troyer67787e62012-05-02 11:48:15 -0500123 iniset $CINDER_CONF DEFAULT api_paste_config $CINDER_API_PASTE_INI
John Griffith4e823ff2012-07-20 13:18:17 -0600124 iniset $CINDER_CONF DEFAULT root_helper "sudo ${CINDER_ROOTWRAP}"
John Griffith43bedda2012-08-21 15:26:15 -0600125 iniset $CINDER_CONF DEFAULT osapi_volume_extension cinder.api.openstack.volume.contrib.standard_extensions
Dean Troyer50ac7922012-09-13 14:02:01 -0500126 iniset $CINDER_CONF DEFAULT state_path $CINDER_STATE_PATH
John Griffith4e823ff2012-07-20 13:18:17 -0600127
Gary Kottonf71bf192012-08-06 11:15:36 -0400128 if is_service_enabled qpid ; then
129 iniset $CINDER_CONF DEFAULT rpc_backend cinder.openstack.common.rpc.impl_qpid
ewindisch3bae7c22012-01-18 11:18:35 -0500130 elif is_service_enabled zeromq; then
131 iniset $CINDER_CONF DEFAULT rpc_backend nova.openstack.common.rpc.impl_zmq
Gary Kottonf71bf192012-08-06 11:15:36 -0400132 elif [ -n "$RABBIT_HOST" ] && [ -n "$RABBIT_PASSWORD" ]; then
133 iniset $CINDER_CONF DEFAULT rabbit_host $RABBIT_HOST
134 iniset $CINDER_CONF DEFAULT rabbit_password $RABBIT_PASSWORD
135 fi
136
James E. Blair213c4162012-11-06 09:38:36 +0100137 if [[ "$CINDER_SECURE_DELETE" == "False" ]]; then
138 iniset $CINDER_CONF DEFAULT secure_delete False
139 fi
140
Chmouel Boudjnah1057bff2012-08-03 11:42:51 +0000141 if [ "$LOG_COLOR" == "True" ] && [ "$SYSLOG" == "False" ]; then
142 # Add color to logging output
143 iniset $CINDER_CONF DEFAULT logging_context_format_string "%(asctime)s %(color)s%(levelname)s %(name)s [%(request_id)s %(user_id)s %(project_id)s%(color)s] %(instance)s%(color)s%(message)s"
144 iniset $CINDER_CONF DEFAULT logging_default_format_string "%(asctime)s %(color)s%(levelname)s %(name)s [-%(color)s] %(instance)s%(color)s%(message)s"
145 iniset $CINDER_CONF DEFAULT logging_debug_format_suffix "from (pid=%(process)d) %(funcName)s %(pathname)s:%(lineno)d"
146 iniset $CINDER_CONF DEFAULT logging_exception_prefix "%(color)s%(asctime)s TRACE %(name)s %(instance)s"
147 fi
Dean Troyer67787e62012-05-02 11:48:15 -0500148}
149
150# init_cinder() - Initialize database and volume group
151function init_cinder() {
152 # Force nova volumes off
153 NOVA_ENABLED_APIS=$(echo $NOVA_ENABLED_APIS | sed "s/osapi_volume,//")
154
Terry Wilson428af5a2012-11-01 16:12:39 -0400155 if is_service_enabled $DATABASE_BACKENDS; then
Dean Troyer67787e62012-05-02 11:48:15 -0500156 # (re)create cinder database
Terry Wilson428af5a2012-11-01 16:12:39 -0400157 recreate_database cinder utf8
Dean Troyer67787e62012-05-02 11:48:15 -0500158
159 # (re)create cinder database
Monty Taylor9fbeedd2012-08-17 12:52:27 -0400160 $CINDER_BIN_DIR/cinder-manage db sync
Dean Troyer67787e62012-05-02 11:48:15 -0500161 fi
162
163 if is_service_enabled c-vol; then
164 # Configure a default volume group called '`stack-volumes`' for the volume
165 # service if it does not yet exist. If you don't wish to use a file backed
166 # volume group, create your own volume group called ``stack-volumes`` before
167 # invoking ``stack.sh``.
168 #
Eoghan Glynn9cb17762012-07-15 10:22:45 +0100169 # By default, the backing file is 5G in size, and is stored in ``/opt/stack/data``.
Dean Troyer67787e62012-05-02 11:48:15 -0500170
171 if ! sudo vgs $VOLUME_GROUP; then
172 VOLUME_BACKING_FILE=${VOLUME_BACKING_FILE:-$DATA_DIR/${VOLUME_GROUP}-backing-file}
Dean Troyer67787e62012-05-02 11:48:15 -0500173 # Only create if the file doesn't already exists
174 [[ -f $VOLUME_BACKING_FILE ]] || truncate -s $VOLUME_BACKING_FILE_SIZE $VOLUME_BACKING_FILE
175 DEV=`sudo losetup -f --show $VOLUME_BACKING_FILE`
176 # Only create if the loopback device doesn't contain $VOLUME_GROUP
177 if ! sudo vgs $VOLUME_GROUP; then sudo vgcreate $VOLUME_GROUP $DEV; fi
178 fi
179
Dean Troyer50ac7922012-09-13 14:02:01 -0500180 mkdir -p $CINDER_STATE_PATH/volumes
Chuck Short3f603d92012-07-28 13:28:33 -0500181
Dean Troyer67787e62012-05-02 11:48:15 -0500182 if sudo vgs $VOLUME_GROUP; then
Vincent Untz0230aa82012-06-14 08:51:01 +0200183 if [[ "$os_PACKAGE" = "rpm" ]]; then
184 # RPM doesn't start the service
185 start_service tgtd
186 fi
187
Dean Troyer67787e62012-05-02 11:48:15 -0500188 # Remove iscsi targets
189 sudo tgtadm --op show --mode target | grep $VOLUME_NAME_PREFIX | grep Target | cut -f3 -d ' ' | sudo xargs -n1 tgt-admin --delete || true
190 # Clean out existing volumes
191 for lv in `sudo lvs --noheadings -o lv_name $VOLUME_GROUP`; do
192 # VOLUME_NAME_PREFIX prefixes the LVs we want
193 if [[ "${lv#$VOLUME_NAME_PREFIX}" != "$lv" ]]; then
194 sudo lvremove -f $VOLUME_GROUP/$lv
195 fi
196 done
197 fi
198 fi
Dean Troyerbc071bc2012-10-01 14:06:44 -0500199
200 if [[ "$KEYSTONE_TOKEN_FORMAT" == "PKI" ]]; then
201 # Create cache dir
202 sudo mkdir -p $CINDER_AUTH_CACHE_DIR
203 sudo chown `whoami` $CINDER_AUTH_CACHE_DIR
204 fi
Dean Troyer67787e62012-05-02 11:48:15 -0500205}
206
207# install_cinder() - Collect source and prepare
208function install_cinder() {
209 git_clone $CINDER_REPO $CINDER_DIR $CINDER_BRANCH
210 git_clone $CINDERCLIENT_REPO $CINDERCLIENT_DIR $CINDERCLIENT_BRANCH
211}
212
Mate Lakata39caac2012-09-03 15:45:53 +0100213# apply config.d approach (e.g. Oneiric does not have this)
214function _configure_tgt_for_config_d() {
215 if [[ ! -d /etc/tgt/conf.d/ ]]; then
216 sudo mkdir /etc/tgt/conf.d
217 echo "include /etc/tgt/conf.d/*.conf" | sudo tee -a /etc/tgt/targets.conf
218 fi
219}
220
Dean Troyer67787e62012-05-02 11:48:15 -0500221# start_cinder() - Start running processes, including screen
222function start_cinder() {
223 if is_service_enabled c-vol; then
224 if [[ "$os_PACKAGE" = "deb" ]]; then
Mate Lakata39caac2012-09-03 15:45:53 +0100225 _configure_tgt_for_config_d
Chuck Short3f603d92012-07-28 13:28:33 -0500226 if [[ ! -f /etc/tgt/conf.d/cinder.conf ]]; then
Eoghan Glynnc6cc5852012-09-25 18:16:59 +0100227 echo "include $CINDER_STATE_PATH/volumes/*" | sudo tee /etc/tgt/conf.d/cinder.conf
Chuck Short3f603d92012-07-28 13:28:33 -0500228 fi
Dean Troyer67787e62012-05-02 11:48:15 -0500229 # tgt in oneiric doesn't restart properly if tgtd isn't running
230 # do it in two steps
231 sudo stop tgt || true
232 sudo start tgt
233 else
234 # bypass redirection to systemctl during restart
235 sudo /sbin/service --skip-redirect tgtd restart
236 fi
237 fi
238
Monty Taylor9fbeedd2012-08-17 12:52:27 -0400239 screen_it c-api "cd $CINDER_DIR && $CINDER_BIN_DIR/cinder-api --config-file $CINDER_CONF"
240 screen_it c-vol "cd $CINDER_DIR && $CINDER_BIN_DIR/cinder-volume --config-file $CINDER_CONF"
241 screen_it c-sch "cd $CINDER_DIR && $CINDER_BIN_DIR/cinder-scheduler --config-file $CINDER_CONF"
Dean Troyer67787e62012-05-02 11:48:15 -0500242}
243
Dean Troyer699a29f2012-09-10 14:10:27 -0500244# stop_cinder() - Stop running processes
Dean Troyer67787e62012-05-02 11:48:15 -0500245function stop_cinder() {
Dean Troyer699a29f2012-09-10 14:10:27 -0500246 # Kill the cinder screen windows
247 for serv in c-api c-sch c-vol; do
248 screen -S $SCREEN_NAME -p $serv -X kill
249 done
Dean Troyer67787e62012-05-02 11:48:15 -0500250
251 if is_service_enabled c-vol; then
252 stop_service tgt
253 fi
254}
Dean Troyer7903b792012-09-13 17:16:12 -0500255
256# Restore xtrace
257$XTRACE