Implement different error kinds

This commit is contained in:
Roey Darwish Dror 2018-11-04 16:20:01 +02:00
parent a250e749d1
commit c89763c689
2 changed files with 46 additions and 18 deletions

View File

@ -16,8 +16,38 @@ pub enum ErrorKind {
#[fail(display = "Could not find {}", _0)] #[fail(display = "Could not find {}", _0)]
NoTool(&'static str), NoTool(&'static str),
#[fail(display = "Error creating a temporary directory")]
TmpDirError,
#[fail(display = "Partitioning error")] #[fail(display = "Partitioning error")]
Creation, Partitioning,
#[fail(display = "Error formatting filesystems")]
Formatting,
#[fail(display = "Error mounting filesystems")]
Mounting,
#[fail(display = "Error creating the boot directory")]
CreateBoot,
#[fail(display = "Pacstrap error")]
Pacstrap,
#[fail(display = "fstab error")]
Fstab,
#[fail(display = "Post installation configuration error")]
PostInstallation,
#[fail(display = "Initramfs error")]
Initramfs,
#[fail(display = "Bootloader error")]
Bootloader,
#[fail(display = "Error calling sync")]
Sync,
} }
impl Fail for Error { impl Fail for Error {

View File

@ -44,7 +44,6 @@ enum App {
fn create(disk: PathBuf) -> Result<(), Error> { fn create(disk: PathBuf) -> Result<(), Error> {
let sgdisk = Tool::find("sgdisk")?; let sgdisk = Tool::find("sgdisk")?;
let sync = Tool::find("sync")?; let sync = Tool::find("sync")?;
let partprobe = Tool::find("partprobe")?;
let pacstrap = Tool::find("pacstrap")?; let pacstrap = Tool::find("pacstrap")?;
let arch_chroot = Tool::find("arch-chroot")?; let arch_chroot = Tool::find("arch-chroot")?;
let genfstab = Tool::find("genfstab")?; let genfstab = Tool::find("genfstab")?;
@ -62,7 +61,7 @@ fn create(disk: PathBuf) -> Result<(), Error> {
return Err(ErrorKind::NotUSB.into()); return Err(ErrorKind::NotUSB.into());
} }
let mount_point = tempdir().context(ErrorKind::Creation)?; let mount_point = tempdir().context(ErrorKind::TmpDirError)?;
info!("Partitioning the disk"); info!("Partitioning the disk");
sgdisk sgdisk
@ -76,8 +75,7 @@ fn create(disk: PathBuf) -> Result<(), Error> {
"--typecode=1:EF02", "--typecode=1:EF02",
"--typecode=2:EF00", "--typecode=2:EF00",
]).arg(&disk) ]).arg(&disk)
.run(ErrorKind::Creation)?; .run(ErrorKind::Partitioning)?;
partprobe.execute().run(ErrorKind::Creation)?;
thread::sleep(Duration::from_millis(1000)); thread::sleep(Duration::from_millis(1000));
@ -86,12 +84,12 @@ fn create(disk: PathBuf) -> Result<(), Error> {
.execute() .execute()
.arg("-F32") .arg("-F32")
.arg(format!("{}-part2", disk.display())) .arg(format!("{}-part2", disk.display()))
.run(ErrorKind::Creation)?; .run(ErrorKind::Formatting)?;
mkbtrfs mkbtrfs
.execute() .execute()
.arg("-f") .arg("-f")
.arg(format!("{}-part3", disk.display())) .arg(format!("{}-part3", disk.display()))
.run(ErrorKind::Creation)?; .run(ErrorKind::Formatting)?;
info!("Mounting filesystems to {}", mount_point.path().display()); info!("Mounting filesystems to {}", mount_point.path().display());
mount_stack mount_stack
@ -100,10 +98,10 @@ fn create(disk: PathBuf) -> Result<(), Error> {
&mount_point.path(), &mount_point.path(),
Filesystem::Btrfs, Filesystem::Btrfs,
Some("compress=zstd"), Some("compress=zstd"),
).context(ErrorKind::Creation)?; ).context(ErrorKind::Mounting)?;
let boot_point = mount_point.path().join("boot"); let boot_point = mount_point.path().join("boot");
fs::create_dir(&boot_point).context(ErrorKind::Creation)?; fs::create_dir(&boot_point).context(ErrorKind::CreateBoot)?;
mount_stack mount_stack
.mount( .mount(
@ -111,7 +109,7 @@ fn create(disk: PathBuf) -> Result<(), Error> {
&boot_point, &boot_point,
Filesystem::Vfat, Filesystem::Vfat,
None, None,
).context(ErrorKind::Creation)?; ).context(ErrorKind::Mounting)?;
info!("Bootstrapping system"); info!("Bootstrapping system");
pacstrap pacstrap
@ -125,31 +123,31 @@ fn create(disk: PathBuf) -> Result<(), Error> {
"intel-ucode", "intel-ucode",
"networkmanager", "networkmanager",
"btrfs-progs", "btrfs-progs",
]).run(ErrorKind::Creation)?; ]).run(ErrorKind::Pacstrap)?;
let fstab = genfstab let fstab = genfstab
.execute() .execute()
.arg("-U") .arg("-U")
.arg(mount_point.path()) .arg(mount_point.path())
.run_text_output(ErrorKind::Creation)? .run_text_output(ErrorKind::Fstab)?
.replace("relatime", "noatime"); .replace("relatime", "noatime");
debug!("fstab:\n{}", fstab); debug!("fstab:\n{}", fstab);
fs::write(mount_point.path().join("etc/fstab"), fstab).context(ErrorKind::Creation)?; fs::write(mount_point.path().join("etc/fstab"), fstab).context(ErrorKind::Fstab)?;
arch_chroot arch_chroot
.execute() .execute()
.arg(mount_point.path()) .arg(mount_point.path())
.args(&["systemctl", "enable", "NetworkManager"]) .args(&["systemctl", "enable", "NetworkManager"])
.run(ErrorKind::Creation)?; .run(ErrorKind::PostInstallation)?;
info!("Generating initramfs"); info!("Generating initramfs");
fs::write(mount_point.path().join("etc/mkinitcpio.conf"), MKINITCPIO) fs::write(mount_point.path().join("etc/mkinitcpio.conf"), MKINITCPIO)
.context(ErrorKind::Creation)?; .context(ErrorKind::Initramfs)?;
arch_chroot arch_chroot
.execute() .execute()
.arg(mount_point.path()) .arg(mount_point.path())
.args(&["mkinitcpio", "-p", "linux"]) .args(&["mkinitcpio", "-p", "linux"])
.run(ErrorKind::Creation)?; .run(ErrorKind::Initramfs)?;
info!("Installing the Bootloader"); info!("Installing the Bootloader");
arch_chroot arch_chroot
@ -157,11 +155,11 @@ fn create(disk: PathBuf) -> Result<(), Error> {
.arg(mount_point.path()) .arg(mount_point.path())
.args(&["bash", "-c"]) .args(&["bash", "-c"])
.arg(format!("grub-install --target=i386-pc --boot-directory /boot {} && grub-install --target=x86_64-efi --efi-directory /boot --boot-directory /boot --removable && grub-mkconfig -o /boot/grub/grub.cfg", disk.display())) .arg(format!("grub-install --target=i386-pc --boot-directory /boot {} && grub-install --target=x86_64-efi --efi-directory /boot --boot-directory /boot --removable && grub-mkconfig -o /boot/grub/grub.cfg", disk.display()))
.run(ErrorKind::Creation)?; .run(ErrorKind::Bootloader)?;
info!("Unmounting filesystems"); info!("Unmounting filesystems");
drop(mount_stack); drop(mount_stack);
sync.execute().run(ErrorKind::Creation)?; sync.execute().run(ErrorKind::Sync)?;
Ok(()) Ok(())
} }