1 use anyhow
::{bail, Error}
;
2 use serde_json
::{json, Value}
;
4 use proxmox
::api
::{api, Permission, RpcEnvironment, RpcEnvironmentType}
;
5 use proxmox
::api
::router
::{Router, SubdirMap}
;
6 use proxmox
::{sortable, identity}
;
7 use proxmox
::{list_subdirs_api_method}
;
9 use crate::config
::acl
::{PRIV_SYS_AUDIT, PRIV_SYS_MODIFY}
;
10 use crate::tools
::disks
::{
11 DiskUsageInfo
, DiskUsageType
, DiskManage
, SmartData
,
12 get_disks
, get_smart_data
, get_disk_usage_info
, inititialize_gpt_disk
,
14 use crate::server
::WorkerTask
;
16 use crate::api2
::types
::{UPID_SCHEMA, NODE_SCHEMA, BLOCKDEVICE_NAME_SCHEMA}
;
29 description
: "Skip smart checks.",
41 description
: "Local disk list.",
48 permission
: &Permission
::Privilege(&["system", "disks"], PRIV_SYS_AUDIT
, false),
54 usage_type
: Option
<DiskUsageType
>,
55 ) -> Result
<Vec
<DiskUsageInfo
>, Error
> {
57 let mut list
= Vec
::new();
59 for (_
, info
) in get_disks(None
, skipsmart
)?
{
60 if let Some(ref usage_type
) = usage_type
{
61 if info
.used
== *usage_type
{
80 schema
: BLOCKDEVICE_NAME_SCHEMA
,
83 description
: "If true returns only the health status.",
93 permission
: &Permission
::Privilege(&["system", "disks"], PRIV_SYS_AUDIT
, false),
96 /// Get SMART attributes and health of a disk.
99 healthonly
: Option
<bool
>,
100 ) -> Result
<SmartData
, Error
> {
102 let healthonly
= healthonly
.unwrap_or(false);
104 let manager
= DiskManage
::new();
105 let disk
= manager
.disk_by_name(&disk
)?
;
106 get_smart_data(&disk
, healthonly
)
117 schema
: BLOCKDEVICE_NAME_SCHEMA
,
120 description
: "UUID for the GPT table.",
131 permission
: &Permission
::Privilege(&["system", "disks"], PRIV_SYS_MODIFY
, false),
134 /// Initialize empty Disk with GPT
135 pub fn initialize_disk(
137 uuid
: Option
<String
>,
138 rpcenv
: &mut dyn RpcEnvironment
,
139 ) -> Result
<Value
, Error
> {
141 let to_stdout
= if rpcenv
.env_type() == RpcEnvironmentType
::CLI { true }
else { false }
;
143 let username
= rpcenv
.get_user().unwrap();
145 let info
= get_disk_usage_info(&disk
, true)?
;
147 if info
.used
!= DiskUsageType
::Unused
{
148 bail
!("disk '{}' is already in use.", disk
);
151 let upid_str
= WorkerTask
::new_thread(
152 "diskinit", Some(disk
.clone()), &username
.clone(), to_stdout
, move |worker
|
154 worker
.log(format
!("initialize disk {}", disk
));
156 let disk_manager
= DiskManage
::new();
157 let disk_info
= disk_manager
.disk_by_name(&disk
)?
;
159 inititialize_gpt_disk(&disk_info
, uuid
.as_deref())?
;
168 const SUBDIRS
: SubdirMap
= &sorted
!([
169 // ("lvm", &lvm::ROUTER),
170 ("directory", &directory
::ROUTER
),
171 ("zfs", &zfs
::ROUTER
),
173 "initgpt", &Router
::new()
174 .post(&API_METHOD_INITIALIZE_DISK
)
177 "list", &Router
::new()
178 .get(&API_METHOD_LIST_DISKS
)
181 "smart", &Router
::new()
182 .get(&API_METHOD_SMART_STATUS
)
186 pub const ROUTER
: Router
= Router
::new()
187 .get(&list_subdirs_api_method
!(SUBDIRS
))