]>
Commit | Line | Data |
---|---|---|
dd9cef56 SR |
1 | ///! Daemon binary to run inside a micro-VM for secure single file restore of disk images |
2 | use anyhow::{bail, format_err, Error}; | |
d32a8652 | 3 | use lazy_static::lazy_static; |
9a06eb16 | 4 | use log::{info, error}; |
dd9cef56 SR |
5 | |
6 | use std::os::unix::{ | |
7 | io::{FromRawFd, RawFd}, | |
8 | net, | |
9 | }; | |
10 | use std::path::Path; | |
d32a8652 | 11 | use std::sync::{Arc, Mutex}; |
dd9cef56 SR |
12 | |
13 | use tokio::sync::mpsc; | |
14 | use tokio_stream::wrappers::ReceiverStream; | |
15 | ||
16 | use proxmox::api::RpcEnvironmentType; | |
17 | use proxmox_backup::client::DEFAULT_VSOCK_PORT; | |
18 | use proxmox_backup::server::{rest::*, ApiConfig}; | |
19 | ||
20 | mod proxmox_restore_daemon; | |
21 | use proxmox_restore_daemon::*; | |
22 | ||
23 | /// Maximum amount of pending requests. If saturated, virtio-vsock returns ETIMEDOUT immediately. | |
24 | /// We should never have more than a few requests in queue, so use a low number. | |
25 | pub const MAX_PENDING: usize = 32; | |
26 | ||
27 | /// Will be present in base initramfs | |
28 | pub const VM_DETECT_FILE: &str = "/restore-vm-marker"; | |
29 | ||
d32a8652 SR |
30 | lazy_static! { |
31 | /// The current disks state. Use for accessing data on the attached snapshots. | |
32 | pub static ref DISK_STATE: Arc<Mutex<DiskState>> = { | |
33 | Arc::new(Mutex::new(DiskState::scan().unwrap())) | |
34 | }; | |
35 | } | |
36 | ||
dd9cef56 SR |
37 | /// This is expected to be run by 'proxmox-file-restore' within a mini-VM |
38 | fn main() -> Result<(), Error> { | |
39 | if !Path::new(VM_DETECT_FILE).exists() { | |
309e14eb TL |
40 | bail!( |
41 | "This binary is not supposed to be run manually, use 'proxmox-file-restore' instead." | |
42 | ); | |
dd9cef56 SR |
43 | } |
44 | ||
45 | // don't have a real syslog (and no persistance), so use env_logger to print to a log file (via | |
46 | // stdout to a serial terminal attached by QEMU) | |
47 | env_logger::from_env(env_logger::Env::default().default_filter_or("info")) | |
48 | .write_style(env_logger::WriteStyle::Never) | |
49 | .init(); | |
50 | ||
d32a8652 SR |
51 | // scan all attached disks now, before starting the API |
52 | // this will panic and stop the VM if anything goes wrong | |
9a06eb16 | 53 | info!("scanning all disks..."); |
d32a8652 SR |
54 | { |
55 | let _disk_state = DISK_STATE.lock().unwrap(); | |
56 | } | |
57 | ||
9a06eb16 TL |
58 | info!("disk scan complete, starting main runtime..."); |
59 | ||
dd9cef56 SR |
60 | proxmox_backup::tools::runtime::main(run()) |
61 | } | |
62 | ||
63 | async fn run() -> Result<(), Error> { | |
a26ebad5 SR |
64 | watchdog_init(); |
65 | ||
dd9cef56 SR |
66 | let auth_config = Arc::new( |
67 | auth::ticket_auth().map_err(|err| format_err!("reading ticket file failed: {}", err))?, | |
68 | ); | |
69 | let config = ApiConfig::new("", &ROUTER, RpcEnvironmentType::PUBLIC, auth_config)?; | |
70 | let rest_server = RestServer::new(config); | |
71 | ||
72 | let vsock_fd = get_vsock_fd()?; | |
73 | let connections = accept_vsock_connections(vsock_fd); | |
74 | let receiver_stream = ReceiverStream::new(connections); | |
75 | let acceptor = hyper::server::accept::from_stream(receiver_stream); | |
76 | ||
77 | hyper::Server::builder(acceptor).serve(rest_server).await?; | |
78 | ||
79 | bail!("hyper server exited"); | |
80 | } | |
81 | ||
82 | fn accept_vsock_connections( | |
83 | vsock_fd: RawFd, | |
84 | ) -> mpsc::Receiver<Result<tokio::net::UnixStream, Error>> { | |
85 | use nix::sys::socket::*; | |
86 | let (sender, receiver) = mpsc::channel(MAX_PENDING); | |
87 | ||
88 | tokio::spawn(async move { | |
89 | loop { | |
90 | let stream: Result<tokio::net::UnixStream, Error> = tokio::task::block_in_place(|| { | |
91 | // we need to accept manually, as UnixListener aborts if socket type != AF_UNIX ... | |
92 | let client_fd = accept(vsock_fd)?; | |
93 | let stream = unsafe { net::UnixStream::from_raw_fd(client_fd) }; | |
94 | stream.set_nonblocking(true)?; | |
95 | tokio::net::UnixStream::from_std(stream).map_err(|err| err.into()) | |
96 | }); | |
97 | ||
98 | match stream { | |
99 | Ok(stream) => { | |
100 | if sender.send(Ok(stream)).await.is_err() { | |
101 | error!("connection accept channel was closed"); | |
102 | } | |
103 | } | |
104 | Err(err) => { | |
105 | error!("error accepting vsock connetion: {}", err); | |
106 | } | |
107 | } | |
108 | } | |
109 | }); | |
110 | ||
111 | receiver | |
112 | } | |
113 | ||
114 | fn get_vsock_fd() -> Result<RawFd, Error> { | |
115 | use nix::sys::socket::*; | |
116 | let sock_fd = socket( | |
117 | AddressFamily::Vsock, | |
118 | SockType::Stream, | |
119 | SockFlag::empty(), | |
120 | None, | |
121 | )?; | |
122 | let sock_addr = VsockAddr::new(libc::VMADDR_CID_ANY, DEFAULT_VSOCK_PORT as u32); | |
123 | bind(sock_fd, &SockAddr::Vsock(sock_addr))?; | |
124 | listen(sock_fd, MAX_PENDING)?; | |
125 | Ok(sock_fd) | |
126 | } |