File tree Expand file tree Collapse file tree 2 files changed +45
-1
lines changed Expand file tree Collapse file tree 2 files changed +45
-1
lines changed Original file line number Diff line number Diff line change
1
+ // Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ use std:: fs:: File ;
5
+ use std:: io:: Error ;
6
+ use std:: os:: raw;
7
+ use sys_util:: ioctl_with_mut_ref;
8
+
9
+ const FS : raw:: c_uint = 0x12 ;
10
+ ioctl_ior_nr ! ( BLKGETSIZE64 , FS , 114 , :: std:: os:: raw:: c_ulong) ;
11
+
12
+ pub fn get_size ( path : & std:: path:: Path ) -> std:: result:: Result < u64 , std:: io:: Error > {
13
+ let mut size: u64 = 0 ;
14
+
15
+ let file = File :: open ( path) ?;
16
+ let ret = unsafe {
17
+ ioctl_with_mut_ref ( & file, BLKGETSIZE64 ( ) , & mut size)
18
+ } ;
19
+
20
+ if ret < 0 {
21
+ return Err ( Error :: last_os_error ( ) ) ;
22
+ } else {
23
+ return Ok ( size)
24
+ }
25
+ }
26
+
27
+ #[ cfg( test) ]
28
+ mod tests {
29
+ use super :: * ;
30
+ use std:: path:: Path ;
31
+
32
+ #[ test]
33
+ fn test_invalid_file ( ) {
34
+ assert ! ( get_size( & Path :: new( "/dev/null" ) ) . is_err( ) ) ;
35
+ }
36
+ }
Original file line number Diff line number Diff line change @@ -31,6 +31,7 @@ extern crate memory_model;
31
31
extern crate net_util;
32
32
extern crate rate_limiter;
33
33
extern crate seccomp;
34
+ #[ macro_use]
34
35
extern crate sys_util;
35
36
36
37
/// Syscalls allowed through the seccomp filter.
@@ -41,11 +42,13 @@ pub mod error;
41
42
pub mod signal_handler;
42
43
/// Wrappers over structures used to configure the VMM.
43
44
pub mod vmm_config;
45
+ mod block_device;
44
46
mod vstate;
45
47
46
48
use std:: collections:: HashMap ;
47
49
use std:: fs:: { metadata, File , OpenOptions } ;
48
50
use std:: io;
51
+ use std:: os:: unix:: fs:: FileTypeExt ;
49
52
use std:: os:: unix:: io:: { AsRawFd , RawFd } ;
50
53
use std:: path:: PathBuf ;
51
54
use std:: process;
@@ -1524,7 +1527,12 @@ impl Vmm {
1524
1527
if drive_config. drive_id == * drive_id {
1525
1528
let metadata = metadata ( & drive_config. path_on_host )
1526
1529
. map_err ( |_| DriveError :: BlockDeviceUpdateFailed ) ?;
1527
- let new_size = metadata. len ( ) ;
1530
+ let new_size = if metadata. file_type ( ) . is_block_device ( ) {
1531
+ block_device:: get_size ( & drive_config. path_on_host )
1532
+ . map_err ( |_| DriveError :: BlockDeviceUpdateFailed ) ?
1533
+ } else {
1534
+ metadata. len ( )
1535
+ } ;
1528
1536
if new_size % virtio:: block:: SECTOR_SIZE != 0 {
1529
1537
warn ! (
1530
1538
"Disk size {} is not a multiple of sector size {}; \
You can’t perform that action at this time.
0 commit comments