]>
Commit | Line | Data |
---|---|---|
dd9cef56 | 1 | ///! Daemon binary to run inside a micro-VM for secure single file restore of disk images |
4c1b7761 WB |
2 | use std::fs::File; |
3 | use std::io::prelude::*; | |
dd9cef56 SR |
4 | use std::os::unix::{ |
5 | io::{FromRawFd, RawFd}, | |
6 | net, | |
7 | }; | |
8 | use std::path::Path; | |
d32a8652 | 9 | use std::sync::{Arc, Mutex}; |
dd9cef56 | 10 | |
4c1b7761 WB |
11 | use anyhow::{bail, format_err, Error}; |
12 | use lazy_static::lazy_static; | |
13 | use log::{error, info}; | |
dd9cef56 SR |
14 | use tokio::sync::mpsc; |
15 | use tokio_stream::wrappers::ReceiverStream; | |
16 | ||
17 | use proxmox::api::RpcEnvironmentType; | |
dd9cef56 | 18 | |
4c1b7761 | 19 | use pbs_client::DEFAULT_VSOCK_PORT; |
fd6d2438 DM |
20 | use proxmox_rest_server::ApiConfig; |
21 | ||
22 | use proxmox_backup::server::rest::*; | |
9edf96e6 | 23 | |
dd9cef56 SR |
24 | mod proxmox_restore_daemon; |
25 | use proxmox_restore_daemon::*; | |
26 | ||
27 | /// Maximum amount of pending requests. If saturated, virtio-vsock returns ETIMEDOUT immediately. | |
28 | /// We should never have more than a few requests in queue, so use a low number. | |
29 | pub const MAX_PENDING: usize = 32; | |
30 | ||
31 | /// Will be present in base initramfs | |
32 | pub const VM_DETECT_FILE: &str = "/restore-vm-marker"; | |
33 | ||
d32a8652 SR |
34 | lazy_static! { |
35 | /// The current disks state. Use for accessing data on the attached snapshots. | |
36 | pub static ref DISK_STATE: Arc<Mutex<DiskState>> = { | |
37 | Arc::new(Mutex::new(DiskState::scan().unwrap())) | |
38 | }; | |
39 | } | |
40 | ||
dd9cef56 SR |
41 | /// This is expected to be run by 'proxmox-file-restore' within a mini-VM |
42 | fn main() -> Result<(), Error> { | |
43 | if !Path::new(VM_DETECT_FILE).exists() { | |
309e14eb TL |
44 | bail!( |
45 | "This binary is not supposed to be run manually, use 'proxmox-file-restore' instead." | |
46 | ); | |
dd9cef56 SR |
47 | } |
48 | ||
49 | // don't have a real syslog (and no persistance), so use env_logger to print to a log file (via | |
50 | // stdout to a serial terminal attached by QEMU) | |
51 | env_logger::from_env(env_logger::Env::default().default_filter_or("info")) | |
52 | .write_style(env_logger::WriteStyle::Never) | |
ecd66eca | 53 | .format_timestamp_millis() |
dd9cef56 SR |
54 | .init(); |
55 | ||
3f780ddf TL |
56 | info!("setup basic system environment..."); |
57 | setup_system_env().map_err(|err| format_err!("system environment setup failed: {}", err))?; | |
33d7292f | 58 | |
d32a8652 SR |
59 | // scan all attached disks now, before starting the API |
60 | // this will panic and stop the VM if anything goes wrong | |
9a06eb16 | 61 | info!("scanning all disks..."); |
d32a8652 SR |
62 | { |
63 | let _disk_state = DISK_STATE.lock().unwrap(); | |
64 | } | |
65 | ||
9a06eb16 TL |
66 | info!("disk scan complete, starting main runtime..."); |
67 | ||
d420962f | 68 | pbs_runtime::main(run()) |
dd9cef56 SR |
69 | } |
70 | ||
73e1ba65 TL |
71 | /// ensure we have our /run dirs, system users and stuff like that setup |
72 | fn setup_system_env() -> Result<(), Error> { | |
73 | // the API may save some stuff there, e.g., the memcon tracking file | |
74 | // we do not care much, but it's way less headache to just create it | |
75 | std::fs::create_dir_all("/run/proxmox-backup")?; | |
76 | ||
9edf96e6 TL |
77 | // we now ensure that all lock files are owned by the backup user, and as we reuse the |
78 | // specialized REST module from pbs api/daemon we have some checks there for user/acl stuff | |
79 | // that gets locked, and thus needs the backup system user to work. | |
80 | std::fs::create_dir_all("/etc")?; | |
81 | let mut passwd = File::create("/etc/passwd")?; | |
82 | writeln!(passwd, "root:x:0:0:root:/root:/bin/sh")?; | |
83 | writeln!(passwd, "backup:x:34:34:backup:/var/backups:/usr/sbin/nologin")?; | |
84 | ||
85 | let mut group = File::create("/etc/group")?; | |
86 | writeln!(group, "root:x:0:")?; | |
87 | writeln!(group, "backup:x:34:")?; | |
88 | ||
73e1ba65 TL |
89 | Ok(()) |
90 | } | |
91 | ||
dd9cef56 | 92 | async fn run() -> Result<(), Error> { |
a26ebad5 SR |
93 | watchdog_init(); |
94 | ||
dd9cef56 SR |
95 | let auth_config = Arc::new( |
96 | auth::ticket_auth().map_err(|err| format_err!("reading ticket file failed: {}", err))?, | |
97 | ); | |
98 | let config = ApiConfig::new("", &ROUTER, RpcEnvironmentType::PUBLIC, auth_config)?; | |
99 | let rest_server = RestServer::new(config); | |
100 | ||
101 | let vsock_fd = get_vsock_fd()?; | |
102 | let connections = accept_vsock_connections(vsock_fd); | |
103 | let receiver_stream = ReceiverStream::new(connections); | |
104 | let acceptor = hyper::server::accept::from_stream(receiver_stream); | |
105 | ||
106 | hyper::Server::builder(acceptor).serve(rest_server).await?; | |
107 | ||
108 | bail!("hyper server exited"); | |
109 | } | |
110 | ||
111 | fn accept_vsock_connections( | |
112 | vsock_fd: RawFd, | |
113 | ) -> mpsc::Receiver<Result<tokio::net::UnixStream, Error>> { | |
114 | use nix::sys::socket::*; | |
115 | let (sender, receiver) = mpsc::channel(MAX_PENDING); | |
116 | ||
117 | tokio::spawn(async move { | |
118 | loop { | |
119 | let stream: Result<tokio::net::UnixStream, Error> = tokio::task::block_in_place(|| { | |
120 | // we need to accept manually, as UnixListener aborts if socket type != AF_UNIX ... | |
121 | let client_fd = accept(vsock_fd)?; | |
122 | let stream = unsafe { net::UnixStream::from_raw_fd(client_fd) }; | |
123 | stream.set_nonblocking(true)?; | |
124 | tokio::net::UnixStream::from_std(stream).map_err(|err| err.into()) | |
125 | }); | |
126 | ||
127 | match stream { | |
128 | Ok(stream) => { | |
129 | if sender.send(Ok(stream)).await.is_err() { | |
130 | error!("connection accept channel was closed"); | |
131 | } | |
132 | } | |
133 | Err(err) => { | |
134 | error!("error accepting vsock connetion: {}", err); | |
135 | } | |
136 | } | |
137 | } | |
138 | }); | |
139 | ||
140 | receiver | |
141 | } | |
142 | ||
143 | fn get_vsock_fd() -> Result<RawFd, Error> { | |
144 | use nix::sys::socket::*; | |
145 | let sock_fd = socket( | |
146 | AddressFamily::Vsock, | |
147 | SockType::Stream, | |
148 | SockFlag::empty(), | |
149 | None, | |
150 | )?; | |
151 | let sock_addr = VsockAddr::new(libc::VMADDR_CID_ANY, DEFAULT_VSOCK_PORT as u32); | |
152 | bind(sock_fd, &SockAddr::Vsock(sock_addr))?; | |
153 | listen(sock_fd, MAX_PENDING)?; | |
154 | Ok(sock_fd) | |
155 | } |