@@ -6,6 +6,7 @@ use std::{
66 collections:: { BTreeMap , BTreeSet } ,
77 ops:: Deref ,
88 path:: { Path , PathBuf } ,
9+ process:: Command ,
910 str:: FromStr ,
1011} ;
1112
@@ -502,6 +503,8 @@ impl<'a> Stage0<'a> {
502503 self . args . device . to_string_lossy ( ) . to_string ( )
503504 } ;
504505
506+ cmd ! ( mkdir -p $mount_point) . context ( "Failed to create mount point" ) ?;
507+
505508 if !initialized {
506509 self . vmm
507510 . notify_q ( "boot.progress" , "initializing data disk" )
@@ -514,10 +517,6 @@ impl<'a> Stage0<'a> {
514517 info ! ( "Skipping disk encryption as requested by kernel cmdline" ) ;
515518 }
516519
517- cmd ! {
518- mkdir -p $mount_point;
519- } ?;
520-
521520 match opts. storage_fs {
522521 FsType :: Zfs => {
523522 info ! ( "Creating ZFS filesystem" ) ;
@@ -561,16 +560,52 @@ impl<'a> Stage0<'a> {
561560 }
562561 }
563562 FsType :: Ext4 => {
564- if cmd ! ( mountpoint -q $mount_point) . is_err ( ) {
565- cmd ! ( mount $fs_dev $mount_point)
566- . context ( "Failed to mount ext4 filesystem" ) ?;
567- }
563+ Self :: mount_e2fs ( & fs_dev, mount_point)
564+ . context ( "Failed to mount ext4 filesystem" ) ?;
568565 }
569566 }
570567 }
571568 Ok ( ( ) )
572569 }
573570
571+ fn mount_e2fs ( dev : & impl AsRef < Path > , mount_point : & impl AsRef < Path > ) -> Result < ( ) > {
572+ let dev = dev. as_ref ( ) ;
573+ let mount_point = mount_point. as_ref ( ) ;
574+ info ! ( "Checking filesystem" ) ;
575+
576+ let e2fsck_status = Command :: new ( "e2fsck" )
577+ . arg ( "-f" )
578+ . arg ( "-p" )
579+ . arg ( dev)
580+ . status ( )
581+ . with_context ( || format ! ( "Failed to run e2fsck on {}" , dev. display( ) ) ) ?;
582+
583+ match e2fsck_status. code ( ) {
584+ Some ( 0 | 1 ) => { }
585+ Some ( code) => {
586+ bail ! (
587+ "e2fsck exited with status {code} while checking {}" ,
588+ dev. display( )
589+ ) ;
590+ }
591+ None => {
592+ bail ! (
593+ "e2fsck terminated by signal while checking {}" ,
594+ dev. display( )
595+ ) ;
596+ }
597+ }
598+
599+ cmd ! {
600+ info "Trying to resize filesystem if needed" ;
601+ resize2fs $dev;
602+ info "Mounting filesystem" ;
603+ mount $dev $mount_point;
604+ }
605+ . context ( "Failed to prepare ext4 filesystem" ) ?;
606+ Ok ( ( ) )
607+ }
608+
574609 fn luks_setup ( & self , disk_crypt_key : & str , name : & str ) -> Result < ( ) > {
575610 let root_hd = & self . args . device ;
576611 let sector_offset = PAYLOAD_OFFSET / 512 ;
0 commit comments