1# Volume Management 2 3The volumeManager module provides APIs for volume and disk management, including obtaining volume information, mounting or unmounting volumes, partitioning disks, and formatting volumes. 4 5> **NOTE**<br> 6> 7> - The initial APIs of this module are supported since API version 9. 8> - API version 9 is a canary version for trial use. The APIs of this version may be unstable. 9> - The APIs of this module are system APIs and cannot be called by third-party applications. 10 11## Modules to Import 12 13```js 14import volumemanager from "@ohos.volumeManager"; 15``` 16 17## volumemanager.getAllVolumes 18 19getAllVolumes(): Promise<Array<Volume>> 20 21Asynchronously obtains information about all available volumes. This API uses a promise to return the result. 22 23**Required permissions**: ohos.permission.STORAGE_MANAGER 24 25**System capability**: SystemCapability.FileManagement.StorageService.Volume 26 27**Return value** 28 29 | Type | Description | 30 | ---------------------------------- | -------------------------- | 31 | Promise<[Volume](#volume)[]> | Promise used to return the execution result.| 32 33**Example** 34 35 ```js 36 volumemanager.getAllVolumes().then(function(volumes){ 37 // Do something. 38 }); 39 ``` 40 41## volumemanager.getAllVolumes 42 43getAllVolumes(callback: AsyncCallback<Array<Volume>>): void 44 45Asynchronously obtains information about all available volumes. This API uses a callback to return the result. 46 47**Required permissions**: ohos.permission.STORAGE_MANAGER 48 49**System capability**: SystemCapability.FileManagement.StorageService.Volume 50 51**Parameters** 52 53 | Name | Type | Mandatory| Description | 54 | -------- | ------------------------------------------------- | ---- | ------------------------------------ | 55 | callback | callback:AsyncCallback<[Volume](#volume)[]> | Yes | Callback invoked to return the volume information obtained.| 56 57**Example** 58 59 ```js 60 let uuid = ""; 61 volumemanager.getAllVolumes(function(error, volumes){ 62 // Do something 63 }); 64 ``` 65 66 67## volumemanager.mount 68 69mount(volumeId: string): Promise<boolean> 70 71Asynchronously mounts a volume. This API uses a promise to return the result. 72 73**Required permissions**: ohos.permission.MOUNT_UNMOUNT_MANAGER 74 75**System capability**: SystemCapability.FileManagement.StorageService.Volume 76 77**Parameters** 78 79 | Name | Type | Mandatory| Description| 80 | -------- | ------ | ---- | ---- | 81 | volumeId | string | Yes | Volume ID.| 82 83**Return value** 84 85 | Type | Description | 86 | ---------------------- | ---------- | 87 | Promise<boolean> | Promise used to return the execution result.| 88 89**Example** 90 91 ```js 92 let volumeId = ""; 93 volumemanager.mount(volumeId).then(function(flag){ 94 // Do something 95 }); 96 ``` 97 98## volumemanager.mount 99 100mount(volumeId: string, callback:AsyncCallback<boolean>):void 101 102Asynchronously obtains the available space of the specified volume. This API uses a callback to return the result. 103 104**Required permissions**: ohos.permission.MOUNT_UNMOUNT_MANAGER 105 106**System capability**: SystemCapability.FileManagement.StorageService.Volume 107 108**Parameters** 109 110 | Name | Type | Mandatory| Description | 111 | -------- | ------------------------------------- | ---- | -------------------- | 112 | volumeId | string | Yes | Volume ID. | 113 | callback | callback:AsyncCallback<boolean> | Yes | Callback invoked to return the execution result.| 114 115**Example** 116 117 ```js 118 let volumeId = ""; 119 volumemanager.mount(volumeId, function(error, flag){ 120 // Do something 121 }); 122 ``` 123 124## volumemanager.unmount 125 126unmount(volumeId: string): Promise<boolean> 127 128Asynchronously unmounts a volume. This API uses a promise to return the result. 129 130**Required permissions**: ohos.permission.MOUNT_UNMOUNT_MANAGER 131 132**System capability**: SystemCapability.FileManagement.StorageService.Volume 133 134**Parameters** 135 136 | Name | Type | Mandatory| Description| 137 | -------- | ------ | ---- | ---- | 138 | volumeId | string | Yes | Volume ID.| 139 140**Return value** 141 142 | Type | Description | 143 | ---------------------- | ---------- | 144 | Promise<boolean> | Promise used to return the execution result.| 145 146**Example** 147 148 ```js 149 let volumeId = ""; 150 volumemanager.unmount(volumeId).then(function(flag){ 151 // Do something 152 }); 153 ``` 154 155## volumemanager.unmount 156 157unmount(volumeId: string, callback:AsyncCallback<boolean>):void 158 159Asynchronously unmounts a volume. This API uses a callback to return the result. 160 161**Required permissions**: ohos.permission.MOUNT_UNMOUNT_MANAGER 162 163**System capability**: SystemCapability.FileManagement.StorageService.Volume 164 165**Parameters** 166 167 | Name | Type | Mandatory| Description | 168 | -------- | ------------------------------------- | ---- | -------------------- | 169 | volumeId | string | Yes | Volume ID. | 170 | callback | callback:AsyncCallback<boolean> | Yes | Callback invoked to return the execution result.| 171 172**Example** 173 174 ```js 175 let volumeId = ""; 176 volumemanager.unmount(volumeId, function(error, flag){ 177 // Do something 178 }); 179 ``` 180 181## volumemanager.getVolumeByUuid 182 183getVolumeByUuid(uuid: string): Promise<Volume> 184 185Asynchronously obtains volume information based on the universally unique identifier (UUID). This API uses a promise to return the result. 186 187**Required permissions**: ohos.permission.STORAGE_MANAGER 188 189**System capability**: SystemCapability.FileManagement.StorageService.Volume 190 191**Parameters** 192 193 | Name | Type | Mandatory| Description| 194 | -------- | ------ | ---- | ---- | 195 | uuid | string | Yes | UUID of the volume.| 196 197**Return value** 198 199 | Type | Description | 200 | ---------------------------------- | -------------------------- | 201 | Promise<[Volume](#volume)> | Promise used to return the volume information obtained.| 202 203**Example** 204 205 ```js 206 let uuid = ""; 207 volumemanager.getVolumeByUuid(uuid).then(function(volume) { 208 console.info("getVolumeByUuid successfully:" + JSON.stringify(volume)); 209 }).catch(function(error){ 210 console.info("getVolumeByUuid failed with error:"+ error); 211 }); 212 ``` 213 214## volumemanager.getVolumeByUuid 215 216getVolumeByUuid(uuid: string, callback: AsyncCallback<Volume>): void 217 218Asynchronously obtains volume information based on the UUID. This API uses a callback to return the result. 219 220**Required permissions**: ohos.permission.STORAGE_MANAGER 221 222**System capability**: SystemCapability.FileManagement.StorageService.Volume 223 224**Parameters** 225 226 | Name | Type | Mandatory| Description | 227 | -------- | ------------------------------------------------ | ---- | -------------------- | 228 | uuid | string | Yes | UUID of the volume. | 229 | callback | callback:AsyncCallback<[Volume](#volume)> | Yes | Callback invoked to return the volume information obtained.| 230 231**Example** 232 233 ```js 234 let uuid = ""; 235 volumemanager.getVolumeByUuid(uuid, (error, volume) => { 236 // Do something. 237 }); 238 ``` 239 240## volumemanager.getVolumeById 241 242getVolumeById(volumeId: string): Promise<Volume> 243 244Asynchronously obtains volume information based on the volume ID. This API uses a promise to return the result. 245 246**Required permissions**: ohos.permission.STORAGE_MANAGER 247 248**System capability**: SystemCapability.FileManagement.StorageService.Volume 249 250**Parameters** 251 252 | Name | Type | Mandatory | Description| 253 | -------- | ------ | ---- | ---- | 254 | volumeId | string | Yes | Volume ID.| 255 256**Return value** 257 258 | Type | Description | 259 | ---------------------------------- | -------------------------- | 260 | Promise<[Volume](#volume)> | Promise used to return the volume information obtained.| 261 262**Example** 263 264 ```js 265 let volumeId = ""; 266 volumemanager.getVolumeById(volumeId).then(function(volume) { 267 console.info("getVolumeById successfully:" + JSON.stringify(volume)); 268 }).catch(function(error){ 269 console.info("getVolumeById failed with error:"+ error); 270 }); 271 ``` 272 273## volumemanager.getVolumeById 274 275getVolumeById(volumeId: string, callback: AsyncCallback<Volume>): void 276 277Asynchronously obtains volume information based on the volume ID. This API uses a callback to return the result. 278 279**Required permissions**: ohos.permission.STORAGE_MANAGER 280 281**System capability**: SystemCapability.FileManagement.StorageService.Volume 282 283**Parameters** 284 285 | Name | Type | Mandatory| Description | 286 | -------- | ------------------------- | ---- | ----------------------------- | 287 | volumeId | string | Yes | Volume ID. | 288 | callback | callback:AsyncCallback<[Volume](#volume)> | Yes | Callback invoked to return the volume information obtained. | 289 290**Example** 291 292 ```js 293 let volumeId = ""; 294 volumemanager.getVolumeById(volumeId, (error, volume) => { 295 // Do something. 296 }); 297 ``` 298 299## volumemanager.setVolumeDescription 300 301setVolumeDescription(uuid: string, description: string): Promise<void> 302 303Asynchronously sets the volume description based on the UUID. This API uses a promise to return the result. 304 305**Required permissions**: ohos.permission.MOUNT_UNMOUNT_MANAGER 306 307**System capability**: SystemCapability.FileManagement.StorageService.Volume 308 309**Parameters** 310 311 | Name | Type | Mandatory| Description| 312 | --------- | ------ | ---- | ---- | 313 | uuid | string | Yes | UUID of the volume.| 314 | description | string | Yes | Volume description.| 315 316**Return value** 317 318 | Type | Description | 319 | ---------------------- | -------------------------- | 320 | Promise<void> | Promise used to return the result. | 321 322**Example** 323 324 ```js 325 let uuid = ""; 326 let description = ""; 327 volumemanager.setVolumeDescription(uuid, description).then(function() { 328 console.info("setVolumeDescription successfully"); 329 }).catch(function(error){ 330 console.info("setVolumeDescription failed with error:"+ error); 331 }); 332 ``` 333 334## volumemanager.setVolumeDescription 335 336setVolumeDescription(uuid: string, description: string, callback: AsyncCallback<void>): void 337 338Asynchronously sets the volume description based on the UUID. This API uses a callback to return the result. 339 340**Required permissions**: ohos.permission.MOUNT_UNMOUNT_MANAGER 341 342**System capability**: SystemCapability.FileManagement.StorageService.Volume 343 344**Parameters** 345 346 | Name | Type | Mandatory| Description | 347 | ---------- | --------------------------------------- | ---- | ---------------- | 348 | uuid | string | Yes | UUID of the volume. | 349 | description | string | Yes | Volume description. | 350 | callback | callback:AsyncCallback<void> | Yes | Callback invoked to return the result.| 351 352**Example** 353 354 ```js 355 let uuid = ""; 356 let description = ""; 357 volumemanager.setVolumeDescription(uuid, description, (error, bool) => { 358 // Do something. 359 }); 360 ``` 361 362## volumemanager.format 363 364format(volumeId: string, fsType: string): Promise<void> 365 366Asynchronously formats a volume. This API uses a promise to return the result. 367 368**Required permissions**: ohos.permission.MOUNT_FORMAT_MANAGER 369 370**System capability**: SystemCapability.FileManagement.StorageService.Volume 371 372**Parameters** 373 374 | Name | Type | Mandatory| Description| 375 | ----------- | ------ | ---- | ---- | 376 | volumeId | string | Yes | Volume ID.| 377 | fsType | string | Yes | File system type.| 378 379**Return value** 380 381 | Type | Description | 382 | ---------------------- | ---------- | 383 | Promise<void> | Promise used to return the result.| 384 385**Example** 386 387 ```js 388 let volumeId = ""; 389 let fsType = ""; 390 volumemanager.format(volumeId, fsType).then(function() { 391 console.info("format successfully"); 392 }).catch(function(error){ 393 console.info("format failed with error:"+ error); 394 }); 395 ``` 396 397## volumemanager.format 398 399format(volumeId: string, fsType: string, callback: AsyncCallback<void>): void 400 401Asynchronously formats a volume. This API uses a callback to return the result. 402 403**Required permissions**: ohos.permission.MOUNT_FORMAT_MANAGER 404 405**System capability**: SystemCapability.FileManagement.StorageService.Volume 406 407**Parameters** 408 409 | Name | Type | Mandatory| Description | 410 | -------- | ------------------------- | ---- | ----------------------------- | 411 | volumeId | string | Yes | Volume ID. | 412 | fsType | string | Yes | File system type.| 413 | callback | callback:AsyncCallback<void> | Yes | Called after the volume is formatted. | 414 415**Example** 416 417 ```js 418 let volumeId = ""; 419 let fsType = ""; 420 volumemanager.format(volumeId, fsType, (error, bool) => { 421 // Do something. 422 }); 423 ``` 424 425## volumemanager.partition 426 427partition(diskId: string, type: number): Promise<void> 428 429Asynchronously partitions a disk. This API uses a promise to return the result. 430 431**Required permissions**: ohos.permission.MOUNT_FORMAT_MANAGER 432 433**System capability**: SystemCapability.FileManagement.StorageService.Volume 434 435**Parameters** 436 437 | Name | Type | Mandatory| Description| 438 | ----------- | ------ | ---- | ---- | 439 | diskId | string | Yes | ID of the disk to which the volume belongs.| 440 | type | number | Yes | Partition type. | 441 442**Return value** 443 444 | Type | Description | 445 | --------------------- | ----------------------- | 446 | Promise<void> | Promise used to return the result. | 447 448**Example** 449 450 ```js 451 let diskId = ""; 452 let type = 0; 453 volumemanager.partition(diskId, type).then(function() { 454 console.info("partition successfully"); 455 }).catch(function(error){ 456 console.info("partition failed with error:"+ error); 457 }); 458 ``` 459 460## volumemanager.partition 461 462partition(diskId: string, type: number, callback: AsyncCallback<void>): void 463 464Asynchronously partitions a disk. This API uses a callback to return the result. 465 466**Required permissions**: ohos.permission.MOUNT_FORMAT_MANAGER 467 468**System capability**: SystemCapability.FileManagement.StorageService.Volume 469 470**Parameters** 471 472 | Name | Type | Mandatory| Description | 473 | -------- | --------------------------------------- | ---- | ---------------- | 474 | diskId | string | Yes | ID of the disk to which the volume belongs. | 475 | type | number | Yes | Partition type. | 476 | callback | callback:AsyncCallback<void> | Yes | Callback invoked to return the result. | 477 478**Example** 479 480 ```js 481 let diskId = ""; 482 let type = 0; 483 volumemanager.partition(diskId, type, (error, bool) => { 484 // Do something. 485 }); 486 ``` 487 488## Volume 489 490**System capability**: SystemCapability.FileManagement.StorageService.Volume 491 492### Attributes 493 494| Name | Type | Description | 495| ----------- | ------- | -------------------- | 496| id | string | Volume ID. | 497| uuid | string | UUID of the volume. | 498| diskId | string | ID of the disk to which the volume belongs. | 499| description | string | Description of the volume. | 500| removable | boolean | Whether the volume is a removable storage device.| 501| state | number | Volume state. | 502| path | string | Mount address of the volume. | 503