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