1 use anyhow
::{bail, Error}
;
2 use serde_json
::{json, Value}
;
4 use proxmox
::{sortable, identity}
;
6 list_subdirs_api_method
, Router
, RpcEnvironment
, RpcEnvironmentType
, SubdirMap
, Permission
,
8 use proxmox_schema
::api
;
9 use proxmox_sys
::task_log
;
12 UPID_SCHEMA
, NODE_SCHEMA
, BLOCKDEVICE_NAME_SCHEMA
,
13 PRIV_SYS_AUDIT
, PRIV_SYS_MODIFY
,
16 use crate::tools
::disks
::{
17 DiskUsageInfo
, DiskUsageType
, DiskManage
, SmartData
,
18 get_disks
, get_smart_data
, get_disk_usage_info
, inititialize_gpt_disk
,
20 use proxmox_rest_server
::WorkerTask
;
33 description
: "Skip smart checks.",
45 description
: "Local disk list.",
52 permission
: &Permission
::Privilege(&["system", "disks"], PRIV_SYS_AUDIT
, false),
58 usage_type
: Option
<DiskUsageType
>,
59 ) -> Result
<Vec
<DiskUsageInfo
>, Error
> {
61 let mut list
= Vec
::new();
63 for (_
, info
) in get_disks(None
, skipsmart
)?
{
64 if let Some(ref usage_type
) = usage_type
{
65 if info
.used
== *usage_type
{
73 list
.sort_by(|a
, b
| a
.name
.cmp(&b
.name
));
86 schema
: BLOCKDEVICE_NAME_SCHEMA
,
89 description
: "If true returns only the health status.",
99 permission
: &Permission
::Privilege(&["system", "disks"], PRIV_SYS_AUDIT
, false),
102 /// Get SMART attributes and health of a disk.
105 healthonly
: Option
<bool
>,
106 ) -> Result
<SmartData
, Error
> {
108 let healthonly
= healthonly
.unwrap_or(false);
110 let manager
= DiskManage
::new();
111 let disk
= manager
.disk_by_name(&disk
)?
;
112 get_smart_data(&disk
, healthonly
)
123 schema
: BLOCKDEVICE_NAME_SCHEMA
,
126 description
: "UUID for the GPT table.",
137 permission
: &Permission
::Privilege(&["system", "disks"], PRIV_SYS_MODIFY
, false),
140 /// Initialize empty Disk with GPT
141 pub fn initialize_disk(
143 uuid
: Option
<String
>,
144 rpcenv
: &mut dyn RpcEnvironment
,
145 ) -> Result
<Value
, Error
> {
147 let to_stdout
= rpcenv
.env_type() == RpcEnvironmentType
::CLI
;
149 let auth_id
= rpcenv
.get_auth_id().unwrap();
151 let info
= get_disk_usage_info(&disk
, true)?
;
153 if info
.used
!= DiskUsageType
::Unused
{
154 bail
!("disk '{}' is already in use.", disk
);
157 let upid_str
= WorkerTask
::new_thread(
158 "diskinit", Some(disk
.clone()), auth_id
, to_stdout
, move |worker
|
160 task_log
!(worker
, "initialize disk {}", disk
);
162 let disk_manager
= DiskManage
::new();
163 let disk_info
= disk_manager
.disk_by_name(&disk
)?
;
165 inititialize_gpt_disk(&disk_info
, uuid
.as_deref())?
;
174 const SUBDIRS
: SubdirMap
= &sorted
!([
175 // ("lvm", &lvm::ROUTER),
176 ("directory", &directory
::ROUTER
),
177 ("zfs", &zfs
::ROUTER
),
179 "initgpt", &Router
::new()
180 .post(&API_METHOD_INITIALIZE_DISK
)
183 "list", &Router
::new()
184 .get(&API_METHOD_LIST_DISKS
)
187 "smart", &Router
::new()
188 .get(&API_METHOD_SMART_STATUS
)
192 pub const ROUTER
: Router
= Router
::new()
193 .get(&list_subdirs_api_method
!(SUBDIRS
))