LabelSession.hpp 7.75 KB
Newer Older
1
2
/*
 * @project        The CERN Tape Archive (CTA)
3
 * @copyright      Copyright(C) 2003-2021 CERN
4
5
6
7
 * @license        This program is free software: you can redistribute it and/or modify
 *                 it under the terms of the GNU General Public License as published by
 *                 the Free Software Foundation, either version 3 of the License, or
 *                 (at your option) any later version.
Daniele Kruse's avatar
Daniele Kruse committed
8
 *
9
10
11
12
 *                 This program is distributed in the hope that it will be useful,
 *                 but WITHOUT ANY WARRANTY; without even the implied warranty of
 *                 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *                 GNU General Public License for more details.
Daniele Kruse's avatar
Daniele Kruse committed
13
 *
14
15
16
 *                 You should have received a copy of the GNU General Public License
 *                 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
Daniele Kruse's avatar
Daniele Kruse committed
17
18
19
20

#pragma once

#include "castor/legacymsg/TapeLabelRqstMsgBody.hpp"
Victor Kotlyar's avatar
Victor Kotlyar committed
21
22
#include "common/log/LogContext.hpp"
#include "common/log/Logger.hpp"
23
#include "common/processCap/ProcessCap.hpp"
24
#include "castor/tape/tapeserver/daemon/Session.hpp"
25
#include "castor/tape/tapeserver/daemon/VolumeInfo.hpp"
26
#include "castor/tape/tapeserver/drive/DriveInterface.hpp"
27
#include "castor/tape/tapeserver/SCSI/Device.hpp"
28
#include "castor/tape/tapeserver/system/Wrapper.hpp"
29
#include "castor/tape/tapeserver/daemon/LabelSessionConfig.hpp"
30
#include "castor/tape/tapeserver/daemon/EncryptionControl.hpp"
31
32
#include "mediachanger/MediaChangerFacade.hpp"
#include "tapeserver/daemon/TapedProxy.hpp"
Daniele Kruse's avatar
Daniele Kruse committed
33
34

#include <memory>
Daniele Kruse's avatar
Daniele Kruse committed
35
36
37
38
39

namespace castor {
namespace tape {
namespace tapeserver {
namespace daemon {
40
41
42
43
44
45
46

/**
 * Class responsible for handling a tape label session.
 */
class LabelSession: public Session {
public:
  
Daniele Kruse's avatar
Daniele Kruse committed
47
  /**
48
49
   *  Constructor 
   *
50
   * @param capUtils Object providing support for UNIX capabilities.
51
   * @param tapeserver Proxy object representing the tapeserverd daemon.
52
   * @param mc Object representing the media changer.
53
54
55
56
57
58
59
60
   * @param clientRequest The request to label a tape received from the label
   * tape command.
   * @param log Object representing the API to the CASTOR logging system.
   * @param sysWrapper Object representing the operating system.
   * @param driveConfig the configuration of the tape-drive to be used to
   * label a tape.
   * @param force The flag that, if set to true, allows labeling a non-blank
   * tape.
61
62
63
   * @param lbp The flag that, if set to true, allows labeling a tape with 
   *            logical block protection. This parameter comes from
   *            castor-tape-label command line tool.
64
65
66
   * @param labelSessionConfig 
   * @param externalEncryptionKeyScript path to the operator provided script
   * for encryption key management control.
Daniele Kruse's avatar
Daniele Kruse committed
67
   */
68
  LabelSession(
69
    cta::server::ProcessCap &capUtils,
70
    cta::tape::daemon::TapedProxy &tapeserver,
71
    cta::mediachanger::MediaChangerFacade &mc,
72
    const legacymsg::TapeLabelRqstMsgBody &clientRequest, 
Victor Kotlyar's avatar
Victor Kotlyar committed
73
    cta::log::Logger &log,
74
    System::virtualWrapper &sysWrapper,
75
    const cta::tape::daemon::TpconfigLine &driveConfig,
76
77
    const bool force,
    const bool lbp,
78
79
    const LabelSessionConfig &labelSessionConfig,
    const std::string & externalEncryptionKeyScript);
80
81
82
83
84
85
86
87
  
  /** 
   * Execute the session and return the type of action to be performed
   * immediately after the session has completed.
   *
   * @return Returns the type of action to be performed after the session has
   * completed.
   */
88
  EndOfSessionAction execute() throw();
Daniele Kruse's avatar
Daniele Kruse committed
89
    
90
private:
91
92
93
94

  /**
   * Object providing support for UNIX capabilities.
   */
95
  cta::server::ProcessCap &m_capUtils;
96
97
98
99

  /**
   * Proxy object representing the tapeserverd daemon.
   */
100
  cta::tape::daemon::TapedProxy &m_tapeserver;
101
102
103
104
    
  /**
   * The object representing the media changer.
   */
105
  cta::mediachanger::MediaChangerFacade &m_mc;
106
107
108
109
110
111
112
113
114

  /**
   * The label request message body
   */
  legacymsg::TapeLabelRqstMsgBody m_request;
  
  /**
   * The logging object     
   */
Victor Kotlyar's avatar
Victor Kotlyar committed
115
  cta::log::Logger & m_log;
116
117
118
119
120
121
122
123
124
    
  /**
   * The system wrapper used to find the device and instantiate the drive object
   */
  System::virtualWrapper &m_sysWrapper;
  
  /**
   * The configuration of the tape drive to be used to label a tape.
   */
125
  const cta::tape::daemon::TpconfigLine m_driveConfig;
126
  
127
128
129
130
131
  /**
   * The configuration parameters from castor.conf specific for of the tape drive to be used to label a tape.
   */
  const LabelSessionConfig m_labelSessionConfig;
  
132
133
134
135
  /**
   * The flag that, if set to true, allows labeling a non-blank tape
   */
  const bool m_force;
136
137
138
139
140
141
  
  /**
   * The flag that, if set to true, allows labeling a tape with logical 
   * block protection
   */
  const bool m_lbp;
142

143
144
145
146
147
  /** 
   * Encryption helper object 
   */
  EncryptionControl m_encryptionControl;

148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
  /** 
   * Execute the session and return the type of action to be performed
   * immediately after the session has completed.
   *
   * @return Returns the type of action to be performed after the session has
   * completed.
   */
  EndOfSessionAction exceptionThrowingExecute();

  /**
   * Sets the capabilities of the process and logs the result.
   *
   * @param capabilities The string representation of the capabilities.
   */
  void setProcessCapabilities(const std::string &capabilities);
163
    
164
165
166
167
168
  /**
   * Performs some meta-data checks that need to be done before deciding to
   * mount the tape for labeling.
   */ 
  void performPreMountChecks();
169

170
171
172
173
174
175
  /**
   * A meta-data check that sees if the user of the client is either the
   * owner of the tape pool containing the tape to be labelled or is an ADMIN
   * user within the CUPV privileges database.
   */
  void checkClientIsOwnerOrAdmin();
176

177
178
179
180
181
182
  /**
   * Returns a Drive object representing the tape drive to be used to label
   * a tape.
   *
   * @return The drive object.
   */
183
  std::unique_ptr<drive::DriveInterface> createDrive();
184

185
186
187
188
189
190
191
192
193
194
195
196
  /**
   * Mounts the tape to be labelled.
   */
  void mountTape();
  
  /**
   * Waits for the tape to be loaded into the tape drive.
   *
   * @param drive Object representing the drive hardware.
   * @param timeoutSecond The number of seconds to wait for the tape to be
   * loaded into the tape drive. 
   */
197
  void waitUntilTapeLoaded(drive::DriveInterface &drive,
198
    const int timeoutSecond);
199

200
  /**
201
202
203
   * Rewinds the specified tape drive.
   *
   * @param drive The tape drive.
204
   */
205
  void rewindDrive(drive::DriveInterface &drive);
206

207
  /**
208
209
210
   * Notifies the tape-server parent-process of the specified user error.
   *
   * @param message The error message.
211
   */
212
  void notifyTapeserverOfUserError(const std::string message);
213
214
  
  /**
215
216
217
218
219
   * Writes the label file to the tape.
   *
   * This method assumes the tape has been rewound.
   *
   * @param drive The tape drive.
220
   */
221
222
  void writeLabelToTape(drive::DriveInterface &drive);

223
224
225
226
227
228
229
230
231
  /**
   * Writes the label file with logical block protection to the tape.
   *
   * This method assumes the tape has been rewound.
   *
   * @param drive The tape drive.
   */
  void writeLabelWithLbpToTape(drive::DriveInterface &drive);

232
  /**
233
234
235
236
237
   * Unloads the specified tape from the specified tape drive.
   *
   * @param vid The volume identifier of the tape to be unloaded.  Please note
   * that the value of this field is only used for logging purposes.
   * @param drive The tape drive.
238
   */
239
240
  void unloadTape(const std::string &vid, drive::DriveInterface &drive);

241
  /**
242
243
244
   * Dismounts the specified tape.
   *
   * @param vid The volume identifier of the tape to be dismounted.
245
   */
246
247
  void dismountTape(const std::string &vid);

248
  /**
249
250
251
252
   * Returns the string representation of the specified boolean value.
   *
   * @param value The boolean value.
   * @return The string representation.
253
   */
254
  const char *boolToStr(const bool value);
255

256
}; // class LabelSession
257
258
259
260
261

} // namespace daemon
} // namespace tapeserver
} // namespace tape
} // namespace castor