mirror of
https://github.com/philmmanjaro/alma.git
synced 2025-07-25 22:49:28 +02:00
Compare commits
70 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
42af023b54 | ||
|
6f5e2dd748 | ||
|
979d30162f | ||
|
af66a89067 | ||
|
5259bb6105 | ||
|
b3629c3120 | ||
|
7510cce532 | ||
|
3f15f87a4a | ||
|
88df8af4ed | ||
|
4b263f2f33 | ||
|
0c12f01685 | ||
|
7fefb5fde2 | ||
|
fb3808335e | ||
|
ba18f68f8f | ||
|
0bd7be2304 | ||
|
19d26cf0bf | ||
|
20a80c73e2 | ||
|
3f89061369 | ||
|
60438bac5c | ||
|
21e11151c8 | ||
|
dc127ed87a | ||
|
a6984b0b84 | ||
|
0680de7c42 | ||
|
86a7b8f43f | ||
|
67f7266ef9 | ||
|
f594fc2ffc | ||
|
38d0085ba1 | ||
|
fdbe285855 | ||
|
b4ecfd3c2a | ||
|
7bba0c8c5a | ||
|
38e2cfbf5b | ||
|
f8bb291db4 | ||
|
3bc7d478ec | ||
|
ef3d5ef01b | ||
|
433b3300dc | ||
|
841704b39c | ||
|
0405b4022d | ||
|
7922e34634 | ||
|
2580f02a6a | ||
|
98a475feb9 | ||
|
153c408c87 | ||
|
493caad5b5 | ||
|
ac97608fe7 | ||
|
5610aa0335 | ||
|
315f2c5a62 | ||
|
b42f2f41b2 | ||
|
eec0bf1628 | ||
|
4df5629d43 | ||
|
5b2e5d75ec | ||
|
922f9cccfe | ||
|
b2dc7e628a | ||
|
3ca2e01f1f | ||
|
460752adfa | ||
|
b92219af41 | ||
|
b3449b6b3d | ||
|
c04b5f5559 | ||
|
7152901820 | ||
|
c8b151fe5f | ||
|
bc9969a0db | ||
|
14347710ce | ||
|
1f5b28c065 | ||
|
6624f05d1e | ||
|
490ab30f4c | ||
|
19eef3a0e1 | ||
|
1de58314e4 | ||
|
7c88f4527d | ||
|
11c5b04677 | ||
|
750653c222 | ||
|
25cdc44c7c | ||
|
34020614e1 |
27
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
27
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
@ -0,0 +1,27 @@
|
||||
---
|
||||
name: Bug report
|
||||
about: Create a report to help us improve
|
||||
title: ''
|
||||
labels: ''
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
<!-- Please do not open bug reports if you're using Manjaro.
|
||||
ALMA does not support Manjaro. If it works then have fun, but if it doesn't then I won't fix it -->
|
||||
|
||||
**Describe the bug**
|
||||
A clear and concise description of what the bug is.
|
||||
|
||||
**To Reproduce**
|
||||
Steps to reproduce the behavior:
|
||||
1. Go to '...'
|
||||
2. Click on '....'
|
||||
3. Scroll down to '....'
|
||||
4. See error
|
||||
|
||||
**Expected behavior**
|
||||
A clear and concise description of what you expected to happen.
|
||||
|
||||
**Additional context**
|
||||
Add any other context about the problem here.
|
22
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
22
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
---
|
||||
name: Feature request
|
||||
about: Suggest an idea for this project
|
||||
title: ''
|
||||
labels: ''
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
<!-- Before requesting a feature, note that ALMA isn't intended to be a generic Arch Linux installer. It's an installer focused on creating mutable live environments. In addition, avoid asking for new features which can be easily implemented using the preset system -->
|
||||
|
||||
**Is your feature request related to a problem? Please describe.**
|
||||
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
|
||||
|
||||
**Describe the solution you'd like**
|
||||
A clear and concise description of what you want to happen.
|
||||
|
||||
**Describe alternatives you've considered**
|
||||
A clear and concise description of any alternative solutions or features you've considered.
|
||||
|
||||
**Additional context**
|
||||
Add any other context or screenshots about the feature request here.
|
31
.github/workflows/rust.yml
vendored
Normal file
31
.github/workflows/rust.yml
vendored
Normal file
@ -0,0 +1,31 @@
|
||||
name: Rust
|
||||
|
||||
on:
|
||||
push:
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
toolchain: stable
|
||||
profile: minimal
|
||||
override: true
|
||||
components: rustfmt, clippy
|
||||
- uses: actions-rs/cargo@v1
|
||||
name: Check format
|
||||
with:
|
||||
command: fmt
|
||||
args: --all -- --check
|
||||
- uses: actions-rs/cargo@v1
|
||||
name: Run clippy
|
||||
with:
|
||||
command: clippy
|
||||
args: --all-targets --locked -- -D warnings
|
||||
- uses: actions-rs/cargo@v1
|
||||
name: Run tests
|
||||
with:
|
||||
command: test
|
723
Cargo.lock
generated
723
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
27
Cargo.toml
27
Cargo.toml
@ -1,19 +1,20 @@
|
||||
[package]
|
||||
name = "alma"
|
||||
version = "0.9.0"
|
||||
version = "0.10.0"
|
||||
authors = ["Roey Darwish Dror"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
which = "2.0.1"
|
||||
failure = "0.1.5"
|
||||
log = "0.4.6"
|
||||
structopt = "0.2.14"
|
||||
tempfile = "3.0.5"
|
||||
serde = { version = "1.0.92", features = ["derive"] }
|
||||
toml = "0.5.1"
|
||||
byte-unit = "3.0.1"
|
||||
nix = "0.14.1"
|
||||
env_logger = "0.6.2"
|
||||
pretty_env_logger = "0.3.0"
|
||||
dialoguer = "0.4.0"
|
||||
which = "4"
|
||||
log = "0.4"
|
||||
structopt = "0.3"
|
||||
tempfile = "3"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
toml = "0.5"
|
||||
byte-unit = "4.0"
|
||||
nix = "0.19"
|
||||
env_logger = "0.8"
|
||||
pretty_env_logger = "0.4"
|
||||
dialoguer = "0.7"
|
||||
console = "0.13"
|
||||
anyhow = "1"
|
||||
|
36
PKGBUILD
Normal file
36
PKGBUILD
Normal file
@ -0,0 +1,36 @@
|
||||
# Maintainer: James McMurray <jamesmcm03@gmail.com>
|
||||
# Contributor: Roey Darwish Dror <roey.ghost@gmail.com>
|
||||
|
||||
_pkgname="alma"
|
||||
pkgname="alma-git"
|
||||
pkgver=r108.3ca2e01
|
||||
pkgrel=1
|
||||
pkgdesc='Create Arch Linux based live USB'
|
||||
arch=('x86_64')
|
||||
url='https://github.com/philmmanjaro/alma'
|
||||
license=('GPL3')
|
||||
makedepends=('git' 'rust')
|
||||
depends=('gptfdisk' 'parted' 'arch-install-scripts' 'dosfstools' 'coreutils' 'util-linux')
|
||||
optdepends=('cryptsetup: for root filesystem encryption')
|
||||
source=("git+https://github.com/philmmanjaro/${_pkgname}")
|
||||
provides=('alma')
|
||||
conflicts=('alma')
|
||||
sha256sums=('SKIP')
|
||||
|
||||
pkgver() {
|
||||
cd "${srcdir}/${_pkgname}"
|
||||
printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
|
||||
}
|
||||
|
||||
build() {
|
||||
cd "${srcdir}/${_pkgname}"
|
||||
|
||||
cargo build --release
|
||||
}
|
||||
|
||||
package() {
|
||||
cd "${srcdir}/${_pkgname}"
|
||||
|
||||
install -Dm755 target/release/${_pkgname} "${pkgdir}/usr/bin/${_pkgname}"
|
||||
install -Dm644 LICENSE "${pkgdir}/usr/share/licenses/${_pkgname}/LICENSE"
|
||||
}
|
154
README.md
154
README.md
@ -17,8 +17,13 @@ date without having to boot it. Encrypting the root partition is as easy as prov
|
||||
|
||||
You can either build the project using cargo build or install the `alma` package from AUR.
|
||||
|
||||
### Using Arch Linux derivatives
|
||||
|
||||
Using Arch Linux derivatives, is supported with this ALMA fork. You may edit the base.toml as needed.
|
||||
|
||||
## Usage
|
||||
|
||||
### Image creation on removable device
|
||||
``` shell
|
||||
sudo alma create /dev/disk/by-id/usb-Generic_USB_Flash_Disk-0:0
|
||||
```
|
||||
@ -26,17 +31,154 @@ sudo alma create /dev/disk/by-id/usb-Generic_USB_Flash_Disk-0:0
|
||||
This will wipe the entire disk and create a bootable installation of Arch Linux. You can use either
|
||||
removable devices or loop devices. As a precaution, ALMA will not wipe non-removable devices.
|
||||
|
||||
After the installation is done you can either boot from it immediately or use `arch-chroot` to
|
||||
perform further customizations before your first boot.
|
||||
|
||||
Not specifying any path will cause ALMA to interactively prompt the user for a removable device.
|
||||
|
||||
### Disk encryption
|
||||
|
||||
You can enable disk encryption with the `-e` flag:
|
||||
|
||||
``` shell
|
||||
sudo alma create -e /dev/disk/by-id/usb-Generic_USB_Flash_Disk-0:0
|
||||
```
|
||||
|
||||
You will be prompted to enter and confirm the encryption passphrase during image creation.
|
||||
|
||||
### chroot
|
||||
|
||||
After the installation is done you can either boot from it immediately or use `arch-chroot` to
|
||||
perform further customizations before your first boot (e.g. installing wireless device drivers).
|
||||
|
||||
You can run `arch-chroot` via ALMA:
|
||||
|
||||
``` shell
|
||||
sudo alma chroot /dev/disk/by-id/usb-Generic_USB_Flash_Disk-0:0
|
||||
```
|
||||
|
||||
### Create raw image and boot in qemu
|
||||
|
||||
For development and testing it may be useful to generate and boot the image in qemu.
|
||||
|
||||
Creating a 10GiB raw image, with disk encryption:
|
||||
|
||||
``` shell
|
||||
sudo alma create -e --image 10GiB almatest.img
|
||||
```
|
||||
|
||||
If you receive the following error:
|
||||
```
|
||||
Error setting up a loop device: losetup: cannot find an unused loop device
|
||||
```
|
||||
|
||||
Check that you are running ALMA with sudo privileges, and reboot if you have installed a kernel update since your last reboot.
|
||||
|
||||
Mounting the raw image to a loop device:
|
||||
|
||||
``` shell
|
||||
sudo losetup -f ./almatest.img
|
||||
```
|
||||
|
||||
Check loop device:
|
||||
``` shell
|
||||
sudo losetup -j ./almatest.img
|
||||
```
|
||||
```
|
||||
/dev/loop0: [2070]:6865917 (/path/to/image/almatest.img)
|
||||
```
|
||||
Note that your loop device number may differ.
|
||||
|
||||
Run qemu via ALMA:
|
||||
``` shell
|
||||
sudo alma qemu /dev/loop0
|
||||
```
|
||||
|
||||
This will boot the image in qemu.
|
||||
|
||||
## Presets
|
||||
|
||||
Reproducing a build can be easily done using a preset file. Presets file are simple TOML file which
|
||||
contain a list of packages to install, a post-installation script and environment variables required
|
||||
by the preset. See the presets directory for examples.
|
||||
Reproducing a build can be easily done using a preset file.
|
||||
|
||||
Preset files are simple TOML files which contain:
|
||||
* A list of packages to install: `packages = ["mypackage"]`
|
||||
* A post-installation script: `script = """ ... """`
|
||||
* Environment variables required by the preset (e.g. used in the script): `enironment_variables = ["USERNAME"]`
|
||||
* A list of shared directories `shared_directories = ["subdirectory"]` - where subdirectory would be available at `/shared_dirs/subdirectory/` for use in the script of the preset.
|
||||
|
||||
See the presets directory for examples.
|
||||
|
||||
Presets are used via the `--presets` argument (multiple preset files or directories may be provided):
|
||||
|
||||
``` shell
|
||||
sudo ALMA_USER=archie alma create /dev/disk/by-id/usb-Generic_USB_Flash_Disk-0:0 --presets ./presets/user.toml ./presets/custom_preset.toml
|
||||
```
|
||||
|
||||
Preset scripts are executed in the same order they are provided.
|
||||
|
||||
If a directory is provided, then all files and subdirectories in the directory are recursively crawled in alphanumeric order (all files must be ALMA .toml files). This allows you to use the following structure to compose many scripts in a specific order:
|
||||
|
||||
```
|
||||
.
|
||||
├── 00-add_user.toml
|
||||
├── 01-xorg
|
||||
│ ├── 00-install.toml
|
||||
│ └── 01-config.toml
|
||||
└── 02-i3
|
||||
├── 00-install.toml
|
||||
└── 01-copy_dotfiles.toml
|
||||
```
|
||||
|
||||
Example preset TOML:
|
||||
|
||||
``` toml
|
||||
packages = ["sudo"]
|
||||
script = """
|
||||
set -eux
|
||||
useradd -m ${ALMA_USER}
|
||||
passwd ${ALMA_USER}
|
||||
usermod -G wheel -a ${ALMA_USER}
|
||||
echo "%wheel ALL=(ALL) ALL" > /etc/sudoers.d/wheel
|
||||
"""
|
||||
environment_variables = ["ALMA_USER"]
|
||||
```
|
||||
|
||||
Note that shared directories in the preset scripts are mounted as bind mounts, so they are *not* mounted read-only. Any changes the custom script makes to the shared directory will be carried out in the preset shared directory of the host system, so be sure to copy (not move) files from the shared directories.
|
||||
|
||||
### Order of execution
|
||||
|
||||
ALMA installs the packages and presets in the following order:
|
||||
|
||||
1. All non-AUR packages are installed
|
||||
2. If AUR packages are present in the toml files, yay (or another
|
||||
specified AUR helper) is installed
|
||||
3. All AUR packages are installed.
|
||||
4. Preset scripts are executed according to their filenames in
|
||||
alphanumeric order.
|
||||
|
||||
Note this may mean you have to workaround some package installations if
|
||||
they depend on preset scripts.
|
||||
|
||||
For example, at the moment you cannot install Rust-based AUR packages in
|
||||
the `aur_packages` array of the Preset TOMLs if you use rustup,
|
||||
since rustup needs to be given the toolchain to
|
||||
install first. This can be worked around by carrying out the AUR
|
||||
package installation inside the preset script itself in these cases.
|
||||
|
||||
## Troubleshooting
|
||||
### mkinitcpio: /etc/mkinitcpio.d/linux.preset: No such file or directory
|
||||
|
||||
Ensure you have both the `linux` and `base` packages installed. Note
|
||||
that only Arch Linux is supported, not Arch Linux derivatives such as
|
||||
Manjaro.
|
||||
|
||||
### Problem opening /dev/... for reading! Error is 123.
|
||||
|
||||
Delete all partitions on the disk first (e.g. with gparted) and try
|
||||
again.
|
||||
|
||||
## Similar projects
|
||||
|
||||
* [NomadBSD](http://nomadbsd.org/)
|
||||
|
||||
## Useful Resources
|
||||
|
||||
* [Arch Wiki: Installing Arch Linux on a USB key](https://wiki.archlinux.org/index.php/Install_Arch_Linux_on_a_USB_key)
|
||||
* [ValleyCat's Arch Linux USB guide](http://valleycat.org/linux/arch-usb.html?i=1)
|
||||
|
@ -1,37 +0,0 @@
|
||||
stages:
|
||||
- stage: Check
|
||||
jobs:
|
||||
- job: Check
|
||||
displayName: Sanity Check
|
||||
pool:
|
||||
vmImage: 'ubuntu-16.04'
|
||||
container: 'rust:latest'
|
||||
steps:
|
||||
- script: rustup component add clippy rustfmt
|
||||
displayName: Install clippy and rustfmt
|
||||
- script: cargo fmt -- --check
|
||||
displayName: Check Formatting
|
||||
- script: cargo check --all
|
||||
displayName: Check
|
||||
- script: cargo clippy
|
||||
displayName: Clippy
|
||||
|
||||
- stage: Build
|
||||
jobs:
|
||||
- job: BuildDocker
|
||||
displayName: Build Docker Image
|
||||
pool:
|
||||
vmImage: 'ubuntu-16.04'
|
||||
steps:
|
||||
- task: Docker@2
|
||||
displayName: Login to Dockerhub
|
||||
inputs:
|
||||
command: login
|
||||
containerRegistry: Dockerhub
|
||||
- task: Docker@2
|
||||
displayName: Build and Push
|
||||
inputs:
|
||||
command: buildAndPush
|
||||
repository: darwish/alma
|
||||
tags: |
|
||||
latest
|
1
presets/audio.toml
Normal file
1
presets/audio.toml
Normal file
@ -0,0 +1 @@
|
||||
packages = ["alsa-firmware", "alsa-utils", "pavucontrol", "pulseaudio-bluetooth", "pulseaudio-ctl", "pulseaudio-zeroconf"]
|
2
presets/aur_example.toml
Normal file
2
presets/aur_example.toml
Normal file
@ -0,0 +1,2 @@
|
||||
packages = ["clang"]
|
||||
aur_packages = ["bat-cat-git"]
|
1
presets/base.toml
Normal file
1
presets/base.toml
Normal file
@ -0,0 +1 @@
|
||||
packages = ["linux61", "linux61-broadcom-wl", "linux-firmware", "grub", "efibootmgr", "intel-ucode", "amd-ucode", "networkmanager"]
|
5
presets/copy_file.toml
Normal file
5
presets/copy_file.toml
Normal file
@ -0,0 +1,5 @@
|
||||
script = """
|
||||
ls /shared_dirs/copy_file_example/
|
||||
cp /shared_dirs/copy_file_example/testfile.txt /root/
|
||||
"""
|
||||
shared_directories = ["copy_file_example"]
|
1
presets/copy_file_example/testfile.txt
Normal file
1
presets/copy_file_example/testfile.txt
Normal file
@ -0,0 +1 @@
|
||||
test
|
@ -1 +0,0 @@
|
||||
packages = ['arch-install-scripts']
|
@ -1,6 +1,11 @@
|
||||
packages = ["plasma-desktop", "dolphin", "gwenview", "konsole", "ttf-dejavu", "sddm"]
|
||||
packages = ["plasma-desktop", "plasma-nm", "plasma-pa", "dolphin", "gwenview", "konsole", "ttf-dejavu", "sddm", "kate", "xdg-desktop-portal-kde", "discover", "falkon"]
|
||||
script = """
|
||||
set -exu
|
||||
|
||||
systemctl enable sddm
|
||||
|
||||
# Set theme
|
||||
mkdir -p /etc/sddm.conf.d
|
||||
echo "[Theme]" > /etc/sddm.conf.d/00_theme_settings.conf
|
||||
echo "Current=breeze" >> /etc/sddm.conf.d/00_theme_settings.conf
|
||||
"""
|
||||
|
9
presets/pamac.toml
Normal file
9
presets/pamac.toml
Normal file
@ -0,0 +1,9 @@
|
||||
packages = ["pamac-gtk", "pamac-snap-plugin", "pamac-flatpak-plugin"]
|
||||
script = """
|
||||
set -exu
|
||||
|
||||
systemctl enable apparmor
|
||||
systemctl enable snapd.apparmor
|
||||
systemctl enable snapd
|
||||
"""
|
||||
|
1
presets/plasma-steam/00-base.toml
Symbolic link
1
presets/plasma-steam/00-base.toml
Symbolic link
@ -0,0 +1 @@
|
||||
../base.toml
|
1
presets/plasma-steam/01-xorg.toml
Symbolic link
1
presets/plasma-steam/01-xorg.toml
Symbolic link
@ -0,0 +1 @@
|
||||
../xorg.toml
|
1
presets/plasma-steam/02-user.toml
Symbolic link
1
presets/plasma-steam/02-user.toml
Symbolic link
@ -0,0 +1 @@
|
||||
../user.toml
|
1
presets/plasma-steam/03-kde.toml
Symbolic link
1
presets/plasma-steam/03-kde.toml
Symbolic link
@ -0,0 +1 @@
|
||||
../kde.toml
|
1
presets/plasma-steam/04-steam.toml
Symbolic link
1
presets/plasma-steam/04-steam.toml
Symbolic link
@ -0,0 +1 @@
|
||||
../steam.toml
|
1
presets/plasma-steam/05-audio.toml
Symbolic link
1
presets/plasma-steam/05-audio.toml
Symbolic link
@ -0,0 +1 @@
|
||||
../audio.toml
|
1
presets/plasma/00-base.toml
Symbolic link
1
presets/plasma/00-base.toml
Symbolic link
@ -0,0 +1 @@
|
||||
../base.toml
|
1
presets/plasma/01-xorg.toml
Symbolic link
1
presets/plasma/01-xorg.toml
Symbolic link
@ -0,0 +1 @@
|
||||
../xorg.toml
|
1
presets/plasma/02-user.toml
Symbolic link
1
presets/plasma/02-user.toml
Symbolic link
@ -0,0 +1 @@
|
||||
../user.toml
|
1
presets/plasma/03-kde.toml
Symbolic link
1
presets/plasma/03-kde.toml
Symbolic link
@ -0,0 +1 @@
|
||||
../kde.toml
|
1
presets/plasma/04-audio.toml
Symbolic link
1
presets/plasma/04-audio.toml
Symbolic link
@ -0,0 +1 @@
|
||||
../audio.toml
|
13
presets/steam.toml
Normal file
13
presets/steam.toml
Normal file
@ -0,0 +1,13 @@
|
||||
packages = ["steam", "gamescope-session-git", "gamescope-plus", "wget", "vulkan-icd-loader", "lib32-vulkan-icd-loader", "vulkan-intel", "lib32-vulkan-intel", "vulkan-radeon", "lib32-vulkan-radeon", "ttf-liberation", "adobe-source-sans-pro-fonts", "adobe-source-han-sans-jp-fonts", "adobe-source-han-sans-kr-fonts", "adobe-source-han-sans-cn-fonts"]
|
||||
script = """
|
||||
set -eux
|
||||
|
||||
wget -v https://gitlab.com/evlaV/jupiter_steam-jupiter-stable-PKGBUILD/-/raw/5cd60f3cd66527a95f93e6fefd9371fd659a5aea/steam_jupiter_stable_bootstrapped_20230316.1.tar.xz -O /usr/lib/steam/bootstraplinux_ubuntu12_32.tar.xz
|
||||
|
||||
mkdir -p /etc/sddm.conf.d
|
||||
echo "# Created by Manjaro ALMA" > /etc/sddm.conf.d/99-autologin.conf
|
||||
echo "[Autologin]" >> /etc/sddm.conf.d/99-autologin.conf
|
||||
echo "User=${ALMA_USER}" >> /etc/sddm.conf.d/99-autologin.conf
|
||||
echo "Session=gamescope-session.desktop" >> /etc/sddm.conf.d/99-autologin.conf
|
||||
"""
|
||||
environment_variables = ["ALMA_USER"]
|
@ -2,8 +2,8 @@ packages = ["sudo"]
|
||||
script = """
|
||||
set -eux
|
||||
|
||||
useradd -m ${ALMA_USER}
|
||||
passwd ${ALMA_USER}
|
||||
usermod -G wheel -a ${ALMA_USER}
|
||||
useradd -m ${ALMA_USER} -p $(openssl passwd -6 ${ALMA_USER_PASSWORD})
|
||||
usermod -G users,lp,video,network,storage,wheel,audio -a ${ALMA_USER}
|
||||
echo "%wheel ALL=(ALL) ALL" > /etc/sudoers.d/wheel
|
||||
"""
|
||||
environment_variables = ["ALMA_USER"]
|
||||
environment_variables = ["ALMA_USER", "ALMA_USER_PASSWORD"]
|
||||
|
5
presets/xfce-branding.toml
Normal file
5
presets/xfce-branding.toml
Normal file
@ -0,0 +1,5 @@
|
||||
packages = ["manjaro-xfce-settings", "manjaro-hello", "manjaro-application-utility", "manjaro-settings-manager-notifier", "manjaro-documentation-en", "manjaro-browser-settings", "manjaro-release", "manjaro-firmware", "manjaro-system"]
|
||||
script = """
|
||||
cp /shared_dirs/xfce-branding/lightdm-gtk-greeter.conf /etc/lightdm/lightdm-gtk-greeter.conf
|
||||
"""
|
||||
shared_directories = ["xfce-branding"]
|
16
presets/xfce-branding/lightdm-gtk-greeter.conf
Normal file
16
presets/xfce-branding/lightdm-gtk-greeter.conf
Normal file
@ -0,0 +1,16 @@
|
||||
[greeter]
|
||||
background = /usr/share/backgrounds/illyria-default-lockscreen.jpg
|
||||
user-background = false
|
||||
font-name = Cantarell Bold 12
|
||||
xft-antialias = true
|
||||
icon-theme-name = Adapta-Papirus-Maia
|
||||
screensaver-timeout = 60
|
||||
theme-name = Matcha-sea
|
||||
cursor-theme-name = xcursor-breeze
|
||||
show-clock = false
|
||||
default-user-image = #manjaro
|
||||
xft-hintstyle = hintfull
|
||||
position = 50%,center 57%,center
|
||||
clock-format =
|
||||
panel-position = bottom
|
||||
indicators = ~host;~spacer;~clock;~spacer;~language;~session;~a11y;~power
|
2
presets/xfce-goodies.toml
Normal file
2
presets/xfce-goodies.toml
Normal file
@ -0,0 +1,2 @@
|
||||
packages = ["xfce4-goodies", "xfce4-pulseaudio-plugin", "pulseaudio", "pavucontrol", "mugshot", "engrampa", "catfish", "firefox", "screenfetch", "thunderbird", "network-manager-applet"]
|
||||
|
1
presets/xfce-manjaro/00-base.toml
Symbolic link
1
presets/xfce-manjaro/00-base.toml
Symbolic link
@ -0,0 +1 @@
|
||||
../base.toml
|
1
presets/xfce-manjaro/01-xorg.toml
Symbolic link
1
presets/xfce-manjaro/01-xorg.toml
Symbolic link
@ -0,0 +1 @@
|
||||
../xorg.toml
|
1
presets/xfce-manjaro/02-user.toml
Symbolic link
1
presets/xfce-manjaro/02-user.toml
Symbolic link
@ -0,0 +1 @@
|
||||
../user.toml
|
1
presets/xfce-manjaro/03-xfce.toml
Symbolic link
1
presets/xfce-manjaro/03-xfce.toml
Symbolic link
@ -0,0 +1 @@
|
||||
../xfce.toml
|
1
presets/xfce-manjaro/04-xfce-goodies.toml
Symbolic link
1
presets/xfce-manjaro/04-xfce-goodies.toml
Symbolic link
@ -0,0 +1 @@
|
||||
../xfce-goodies.toml
|
1
presets/xfce-manjaro/05-xfce-branding.toml
Symbolic link
1
presets/xfce-manjaro/05-xfce-branding.toml
Symbolic link
@ -0,0 +1 @@
|
||||
../xfce-branding.toml
|
1
presets/xfce-manjaro/06-pamac.toml
Symbolic link
1
presets/xfce-manjaro/06-pamac.toml
Symbolic link
@ -0,0 +1 @@
|
||||
../pamac.toml
|
1
presets/xfce-manjaro/07-audio.toml
Symbolic link
1
presets/xfce-manjaro/07-audio.toml
Symbolic link
@ -0,0 +1 @@
|
||||
../audio.toml
|
1
presets/xfce-manjaro/xfce-branding
Symbolic link
1
presets/xfce-manjaro/xfce-branding
Symbolic link
@ -0,0 +1 @@
|
||||
../xfce-branding
|
6
presets/xfce.toml
Normal file
6
presets/xfce.toml
Normal file
@ -0,0 +1,6 @@
|
||||
packages = ["xfce4", "ttf-dejavu", "lightdm-gtk-greeter-settings", "accountsservice"]
|
||||
script = """
|
||||
set -exu
|
||||
|
||||
systemctl enable lightdm
|
||||
"""
|
1
presets/xfce/00-base.toml
Symbolic link
1
presets/xfce/00-base.toml
Symbolic link
@ -0,0 +1 @@
|
||||
../base.toml
|
1
presets/xfce/01-xorg.toml
Symbolic link
1
presets/xfce/01-xorg.toml
Symbolic link
@ -0,0 +1 @@
|
||||
../xorg.toml
|
1
presets/xfce/02-user.toml
Symbolic link
1
presets/xfce/02-user.toml
Symbolic link
@ -0,0 +1 @@
|
||||
../user.toml
|
1
presets/xfce/03-xfce.toml
Symbolic link
1
presets/xfce/03-xfce.toml
Symbolic link
@ -0,0 +1 @@
|
||||
../xfce.toml
|
1
presets/xfce/04-audio.toml
Symbolic link
1
presets/xfce/04-audio.toml
Symbolic link
@ -0,0 +1 @@
|
||||
../audio.toml
|
1
presets/xorg.toml
Normal file
1
presets/xorg.toml
Normal file
@ -0,0 +1 @@
|
||||
packages = ["xf86-input-libinput", "xf86-video-amdgpu", "xf86-video-ati", "xf86-video-nouveau", "xorg-server", "xterm"]
|
@ -1,13 +0,0 @@
|
||||
script = """
|
||||
set -exu
|
||||
|
||||
cat << 'EOF' >> /etc/pacman.conf
|
||||
[archzfs]
|
||||
Server = https://archzfs.com/$repo/$arch
|
||||
EOF
|
||||
|
||||
pacman-key -r F75D9D76
|
||||
pacman-key --lsign-key F75D9D76
|
||||
|
||||
pacman -Sy archzfs-linux
|
||||
"""
|
37
src/args.rs
37
src/args.rs
@ -1,7 +1,10 @@
|
||||
use super::aur::AurHelper;
|
||||
use byte_unit::Byte;
|
||||
use std::path::PathBuf;
|
||||
use structopt::StructOpt;
|
||||
|
||||
/// Parse size argument as bytes
|
||||
/// e.g. 10GB, 10GiB, etc.
|
||||
fn parse_bytes(src: &str) -> Result<Byte, &'static str> {
|
||||
Byte::from_str(src).map_err(|_| "Invalid image size")
|
||||
}
|
||||
@ -35,10 +38,24 @@ pub struct CreateCommand {
|
||||
#[structopt(parse(from_os_str))]
|
||||
pub path: Option<PathBuf>,
|
||||
|
||||
/// Additional pacakges to install
|
||||
/// Path to a pacman.conf file which will be used to pacstrap packages into the image.
|
||||
///
|
||||
/// This pacman.conf will also be copied into the resulting Arch Linux image.
|
||||
#[structopt(short = "c", long = "pacman-conf", value_name = "pacman_conf")]
|
||||
pub pacman_conf: Option<PathBuf>,
|
||||
|
||||
/// Additional packages to install
|
||||
#[structopt(short = "p", long = "extra-packages", value_name = "package")]
|
||||
pub extra_packages: Vec<String>,
|
||||
|
||||
/// Additional packages to install
|
||||
#[structopt(long = "aur-packages", value_name = "aurpackage")]
|
||||
pub aur_packages: Vec<String>,
|
||||
|
||||
/// Boot partition size in megabytes
|
||||
#[structopt(long = "boot-size")]
|
||||
pub boot_size: Option<u32>,
|
||||
|
||||
/// Enter interactive chroot before unmounting the drive
|
||||
#[structopt(short = "i", long = "interactive")]
|
||||
pub interactive: bool,
|
||||
@ -54,15 +71,25 @@ pub struct CreateCommand {
|
||||
/// Create an image with a certain size in the given path instead of using an actual block device
|
||||
#[structopt(
|
||||
long = "image",
|
||||
parse(try_from_str = "parse_bytes"),
|
||||
parse(try_from_str = parse_bytes),
|
||||
value_name = "size",
|
||||
requires = "path"
|
||||
)]
|
||||
pub image: Option<Byte>,
|
||||
|
||||
/// Overwrite existing image files. Use with caution
|
||||
/// Overwrite existing image files. Use with caution!
|
||||
#[structopt(long = "overwrite")]
|
||||
pub overwrite: bool,
|
||||
|
||||
/// Allow installation on non-removable devices. Use with extreme caution!
|
||||
///
|
||||
/// If no device is specified in the command line, the device selection menu will
|
||||
/// show non-removable devices
|
||||
#[structopt(long = "allow-non-removable")]
|
||||
pub allow_non_removable: bool,
|
||||
|
||||
#[structopt(long = "aur-helper", possible_values=&["paru", "yay"], default_value="paru")]
|
||||
pub aur_helper: AurHelper,
|
||||
}
|
||||
|
||||
#[derive(StructOpt)]
|
||||
@ -71,6 +98,10 @@ pub struct ChrootCommand {
|
||||
#[structopt(parse(from_os_str))]
|
||||
pub block_device: PathBuf,
|
||||
|
||||
/// Allow installation on non-removable devices. Use with extreme caution!
|
||||
#[structopt(long = "allow-non-removable")]
|
||||
pub allow_non_removable: bool,
|
||||
|
||||
/// Optional command to run
|
||||
#[structopt()]
|
||||
pub command: Vec<String>,
|
||||
|
60
src/aur.rs
Normal file
60
src/aur.rs
Normal file
@ -0,0 +1,60 @@
|
||||
use anyhow::anyhow;
|
||||
use std::str::FromStr;
|
||||
|
||||
pub struct AurHelper {
|
||||
pub name: String,
|
||||
pub package_name: String,
|
||||
pub install_command: Vec<String>,
|
||||
}
|
||||
|
||||
impl FromStr for AurHelper {
|
||||
type Err = anyhow::Error;
|
||||
|
||||
fn from_str(s: &str) -> anyhow::Result<Self> {
|
||||
match s {
|
||||
"paru" => Ok(Self {
|
||||
name: String::from("paru"),
|
||||
package_name: String::from("paru-bin"),
|
||||
install_command: vec![
|
||||
String::from("paru"),
|
||||
String::from("-S"),
|
||||
String::from("--skipreview"),
|
||||
String::from("--noupgrademenu"),
|
||||
String::from("--useask"),
|
||||
String::from("--removemake"),
|
||||
String::from("--norebuild"),
|
||||
String::from("--nocleanafter"),
|
||||
String::from("--noredownload"),
|
||||
String::from("--mflags"),
|
||||
String::from(""),
|
||||
String::from("--noconfirm"),
|
||||
String::from("--batchinstall"),
|
||||
],
|
||||
}),
|
||||
"yay" => Ok(Self {
|
||||
name: String::from("yay"),
|
||||
package_name: String::from("yay-bin"),
|
||||
install_command: vec![
|
||||
String::from("yay"),
|
||||
String::from("-S"),
|
||||
String::from("--nocleanmenu"),
|
||||
String::from("--nodiffmenu"),
|
||||
String::from("--noeditmenu"),
|
||||
String::from("--noupgrademenu"),
|
||||
String::from("--useask"),
|
||||
String::from("--removemake"),
|
||||
String::from("--norebuild"),
|
||||
String::from("--answerdiff"),
|
||||
String::from("None"),
|
||||
String::from("--answeredit"),
|
||||
String::from("None"),
|
||||
String::from("--answerclean"),
|
||||
String::from("None"),
|
||||
String::from("--mflags"),
|
||||
String::from("--noconfirm"),
|
||||
],
|
||||
}),
|
||||
_ => Err(anyhow!("Error parsing AUR helper string: {}", s)),
|
||||
}
|
||||
}
|
||||
}
|
12
src/constants.rs
Normal file
12
src/constants.rs
Normal file
@ -0,0 +1,12 @@
|
||||
pub const BOOT_PARTITION_INDEX: u8 = 1;
|
||||
pub const ROOT_PARTITION_INDEX: u8 = 3;
|
||||
|
||||
pub static JOURNALD_CONF: &str = "
|
||||
[Journal]
|
||||
Storage=volatile
|
||||
SystemMaxUse=16M
|
||||
";
|
||||
|
||||
pub const BASE_PACKAGES: [&str; 1] = ["base"];
|
||||
|
||||
pub const AUR_DEPENDENCIES: [&str; 3] = ["base-devel", "git", "sudo"];
|
130
src/error.rs
130
src/error.rs
@ -1,130 +0,0 @@
|
||||
use failure::{Backtrace, Context, Fail};
|
||||
use std::fmt::{self, Display};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Error {
|
||||
inner: Context<ErrorKind>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Eq, PartialEq, Debug, Fail)]
|
||||
pub enum ErrorKind {
|
||||
#[fail(display = "Error quering information about the block device")]
|
||||
DeviceQuery,
|
||||
|
||||
#[fail(display = "Invalid device name")]
|
||||
InvalidDeviceName,
|
||||
|
||||
#[fail(display = "The given block device is neither removable nor a loop device")]
|
||||
DangerousDevice,
|
||||
|
||||
#[fail(display = "Partition {} does not exist", _0)]
|
||||
NoSuchPartition(u8),
|
||||
|
||||
#[fail(display = "Could not find {}", _0)]
|
||||
NoTool(&'static str),
|
||||
|
||||
#[fail(display = "Error creating a temporary directory")]
|
||||
TmpDirError,
|
||||
|
||||
#[fail(display = "Partitioning error")]
|
||||
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 caused by the interactive mode")]
|
||||
Interactive,
|
||||
|
||||
#[fail(display = "Failed umounting filesystems")]
|
||||
UmountFailure,
|
||||
|
||||
#[fail(display = "Error setting up an encrypted device")]
|
||||
LuksSetup,
|
||||
|
||||
#[fail(display = "Error opening the encrypted device")]
|
||||
LuksOpen,
|
||||
|
||||
#[fail(display = "Error closing the encrypted device")]
|
||||
LuksClose,
|
||||
|
||||
#[fail(display = "Error detecting whether the root partition is an encrypted device")]
|
||||
LuksDetection,
|
||||
|
||||
#[fail(display = "Error setting the locale")]
|
||||
Locale,
|
||||
|
||||
#[fail(display = "Failed launching Qemu")]
|
||||
Qemu,
|
||||
|
||||
#[fail(display = "Error loading preset \"{}\"", _0)]
|
||||
Preset(String),
|
||||
|
||||
#[fail(display = "Missing environment variables \"{:?}\"", _0)]
|
||||
MissingEnvironmentVariables(Vec<String>),
|
||||
|
||||
#[fail(display = "Error executing preset script")]
|
||||
PresetScript,
|
||||
|
||||
#[fail(display = "Error creating the image")]
|
||||
Image,
|
||||
|
||||
#[fail(display = "Error setting up a loop device: {}", _0)]
|
||||
Losetup(String),
|
||||
|
||||
#[fail(display = "Error querying removeable devices")]
|
||||
RemoveableDevicesQuery,
|
||||
|
||||
#[fail(display = "There are no removable devices")]
|
||||
NoRemovableDevices,
|
||||
}
|
||||
|
||||
impl Fail for Error {
|
||||
fn cause(&self) -> Option<&Fail> {
|
||||
self.inner.cause()
|
||||
}
|
||||
|
||||
fn backtrace(&self) -> Option<&Backtrace> {
|
||||
self.inner.backtrace()
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
Display::fmt(&self.inner, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ErrorKind> for Error {
|
||||
fn from(kind: ErrorKind) -> Error {
|
||||
Error {
|
||||
inner: Context::new(kind),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Context<ErrorKind>> for Error {
|
||||
fn from(inner: Context<ErrorKind>) -> Error {
|
||||
Error { inner }
|
||||
}
|
||||
}
|
@ -2,27 +2,35 @@ use std::fmt::Write;
|
||||
|
||||
pub struct Initcpio {
|
||||
encrypted: bool,
|
||||
plymouth: bool,
|
||||
}
|
||||
|
||||
impl Initcpio {
|
||||
pub fn new(encrypted: bool) -> Self {
|
||||
Self { encrypted }
|
||||
pub fn new(encrypted: bool, plymouth: bool) -> Self {
|
||||
Self {
|
||||
encrypted,
|
||||
plymouth,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_config(&self) -> String {
|
||||
pub fn to_config(&self) -> anyhow::Result<String> {
|
||||
let mut output = String::from(
|
||||
"MODULES=()
|
||||
BINARIES=()
|
||||
FILES=()
|
||||
HOOKS=(base udev keyboard consolefont block ",
|
||||
HOOKS=(base udev autodetect modconf kms keyboard keymap consolefont block ",
|
||||
);
|
||||
|
||||
if self.encrypted {
|
||||
output.write_str("encrypt ").unwrap();
|
||||
output.write_str("encrypt ")?;
|
||||
}
|
||||
|
||||
output.write_str("filesystems keyboard fsck)\n").unwrap();
|
||||
if self.plymouth {
|
||||
output.write_str("filesystems plymouth)\n")?;
|
||||
} else {
|
||||
output.write_str("filesystems fsck)\n")?;
|
||||
}
|
||||
|
||||
output
|
||||
Ok(output)
|
||||
}
|
||||
}
|
||||
|
523
src/main.rs
523
src/main.rs
@ -1,69 +1,62 @@
|
||||
mod args;
|
||||
mod error;
|
||||
mod aur;
|
||||
mod constants;
|
||||
mod initcpio;
|
||||
mod presets;
|
||||
mod process;
|
||||
mod storage;
|
||||
mod tool;
|
||||
|
||||
use crate::args::*;
|
||||
use crate::error::*;
|
||||
use crate::process::CommandExt;
|
||||
use crate::storage::*;
|
||||
use crate::tool::Tool;
|
||||
use anyhow::{anyhow, Context};
|
||||
use args::Command;
|
||||
use byte_unit::Byte;
|
||||
use console::style;
|
||||
use dialoguer::{theme::ColorfulTheme, Select};
|
||||
use failure::{Fail, ResultExt};
|
||||
use log::{debug, error, info, log_enabled, Level, LevelFilter};
|
||||
use pretty_env_logger;
|
||||
use process::CommandExt;
|
||||
use std::collections::HashSet;
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
use std::os::unix::{fs::PermissionsExt, process::CommandExt as UnixCommandExt};
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::{exit, Command as ProcessCommand};
|
||||
use std::process::Command as ProcessCommand;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use storage::EncryptedDevice;
|
||||
use storage::{BlockDevice, Filesystem, FilesystemType, LoopDevice, MountStack};
|
||||
use structopt::StructOpt;
|
||||
use tempfile::tempdir;
|
||||
use tool::Tool;
|
||||
|
||||
const BOOT_PARTITION_INDEX: u8 = 1;
|
||||
const ROOT_PARTITION_INDEX: u8 = 3;
|
||||
fn main() -> anyhow::Result<()> {
|
||||
// Get struct of args using structopt
|
||||
let app = args::App::from_args();
|
||||
|
||||
static JOURNALD_CONF: &'static str = "
|
||||
[Journal]
|
||||
Storage=volatile
|
||||
SystemMaxUse=16M
|
||||
";
|
||||
// Set up logging
|
||||
let mut builder = pretty_env_logger::formatted_timed_builder();
|
||||
let log_level = if app.verbose {
|
||||
LevelFilter::Debug
|
||||
} else {
|
||||
LevelFilter::Info
|
||||
};
|
||||
builder.filter_level(log_level);
|
||||
builder.init();
|
||||
|
||||
fn mount<'a>(
|
||||
mount_path: &Path,
|
||||
boot_filesystem: &'a Filesystem,
|
||||
root_filesystem: &'a Filesystem,
|
||||
) -> Result<MountStack<'a>, Error> {
|
||||
let mut mount_stack = MountStack::new();
|
||||
debug!(
|
||||
"Root partition: {}",
|
||||
root_filesystem.block().path().display()
|
||||
);
|
||||
// Match command from arguments and run relevant code
|
||||
match app.cmd {
|
||||
Command::Create(command) => create(command),
|
||||
Command::Chroot(command) => tool::chroot(command),
|
||||
Command::Qemu(command) => tool::qemu(command),
|
||||
}?;
|
||||
|
||||
info!("Mounting filesystems to {}", mount_path.display());
|
||||
mount_stack
|
||||
.mount(&root_filesystem, mount_path.into(), None)
|
||||
.context(ErrorKind::Mounting)?;
|
||||
|
||||
let boot_point = mount_path.join("boot");
|
||||
if !boot_point.exists() {
|
||||
fs::create_dir(&boot_point).context(ErrorKind::CreateBoot)?;
|
||||
}
|
||||
|
||||
mount_stack
|
||||
.mount(&boot_filesystem, boot_point, None)
|
||||
.context(ErrorKind::Mounting)?;
|
||||
|
||||
Ok(mount_stack)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Remove swap entry from fstab and any commented lines
|
||||
/// Returns an owned String
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `fstab` - A string slice holding the contents of the fstab file
|
||||
fn fix_fstab(fstab: &str) -> String {
|
||||
fstab
|
||||
.lines()
|
||||
@ -72,7 +65,8 @@ fn fix_fstab(fstab: &str) -> String {
|
||||
.join("\n")
|
||||
}
|
||||
|
||||
fn create_image(path: &Path, size: Byte, overwrite: bool) -> Result<LoopDevice, Error> {
|
||||
/// Creates a file at the path provided, and mounts it to a loop device
|
||||
fn create_image(path: &Path, size: Byte, overwrite: bool) -> anyhow::Result<LoopDevice> {
|
||||
{
|
||||
let mut options = fs::OpenOptions::new();
|
||||
|
||||
@ -82,35 +76,45 @@ fn create_image(path: &Path, size: Byte, overwrite: bool) -> Result<LoopDevice,
|
||||
} else {
|
||||
options.create_new(true);
|
||||
}
|
||||
let file = options.open(path).context(ErrorKind::Image)?;
|
||||
let file = options.open(path).context("Error creating the image")?;
|
||||
|
||||
file.set_len(size.get_bytes() as u64)
|
||||
.context(ErrorKind::Image)?;
|
||||
.context("Error creating the image")?;
|
||||
}
|
||||
|
||||
LoopDevice::create(path)
|
||||
}
|
||||
|
||||
fn select_block_device() -> Result<PathBuf, Error> {
|
||||
let devices = get_removable_devices()?;
|
||||
/// Requests selection of block device (no device was given in the arguments)
|
||||
fn select_block_device(allow_non_removable: bool) -> anyhow::Result<PathBuf> {
|
||||
let devices = storage::get_storage_devices(allow_non_removable)?;
|
||||
|
||||
if devices.is_empty() {
|
||||
Err(ErrorKind::NoRemovableDevices)?
|
||||
return Err(anyhow!("There are no removable devices"));
|
||||
}
|
||||
|
||||
if allow_non_removable {
|
||||
println!(
|
||||
"{}\n",
|
||||
style("Showing non-removable devices. Make sure you select the correct device.")
|
||||
.red()
|
||||
.bold()
|
||||
);
|
||||
}
|
||||
|
||||
let selection = Select::with_theme(&ColorfulTheme::default())
|
||||
.with_prompt("Select a removable device")
|
||||
.default(0)
|
||||
.items(&devices)
|
||||
.interact()
|
||||
.unwrap();
|
||||
.interact()?;
|
||||
|
||||
Ok(PathBuf::from("/dev").join(&devices[selection].name))
|
||||
}
|
||||
|
||||
#[allow(clippy::cognitive_complexity)]
|
||||
fn create(command: CreateCommand) -> Result<(), Error> {
|
||||
let presets = presets::Presets::load(&command.presets)?;
|
||||
/// Creates the installation
|
||||
#[allow(clippy::cognitive_complexity)] // TODO: Split steps into functions and remove this
|
||||
fn create(command: args::CreateCommand) -> anyhow::Result<()> {
|
||||
let presets = presets::PresetsCollection::load(&command.presets)?;
|
||||
|
||||
let sgdisk = Tool::find("sgdisk")?;
|
||||
let pacstrap = Tool::find("pacstrap")?;
|
||||
@ -132,7 +136,7 @@ fn create(command: CreateCommand) -> Result<(), Error> {
|
||||
let storage_device_path = if let Some(path) = command.path {
|
||||
path
|
||||
} else {
|
||||
select_block_device()?
|
||||
select_block_device(command.allow_non_removable)?
|
||||
};
|
||||
|
||||
let image_loop = if let Some(size) = command.image {
|
||||
@ -149,38 +153,42 @@ fn create(command: CreateCommand) -> Result<(), Error> {
|
||||
loop_dev.path()
|
||||
})
|
||||
.unwrap_or(&storage_device_path),
|
||||
command.allow_non_removable,
|
||||
)?;
|
||||
|
||||
let mount_point = tempdir().context(ErrorKind::TmpDirError)?;
|
||||
let mount_point = tempdir().context("Error creating a temporary directory")?;
|
||||
let disk_path = storage_device.path();
|
||||
|
||||
info!("Partitioning the block device");
|
||||
debug!("{:?}", disk_path);
|
||||
|
||||
let boot_size = command.boot_size.unwrap_or(300);
|
||||
|
||||
sgdisk
|
||||
.execute()
|
||||
.args(&[
|
||||
.args([
|
||||
"-Z",
|
||||
"-o",
|
||||
"--new=1::+100M",
|
||||
&format!("--new=1::+{}M", boot_size),
|
||||
"--new=2::+1M",
|
||||
"--largest-new=3",
|
||||
"--typecode=1:EF00",
|
||||
"--typecode=2:EF02",
|
||||
])
|
||||
.arg(&disk_path)
|
||||
.run(ErrorKind::Partitioning)?;
|
||||
.arg(disk_path)
|
||||
.run()
|
||||
.context("Partitioning error")?;
|
||||
|
||||
thread::sleep(Duration::from_millis(1000));
|
||||
|
||||
info!("Formatting filesystems");
|
||||
let boot_partition = storage_device.get_partition(BOOT_PARTITION_INDEX)?;
|
||||
let boot_partition = storage_device.get_partition(constants::BOOT_PARTITION_INDEX)?;
|
||||
let boot_filesystem = Filesystem::format(&boot_partition, FilesystemType::Vfat, &mkfat)?;
|
||||
|
||||
let root_partition_base = storage_device.get_partition(ROOT_PARTITION_INDEX)?;
|
||||
let root_partition_base = storage_device.get_partition(constants::ROOT_PARTITION_INDEX)?;
|
||||
let encrypted_root = if let Some(cryptsetup) = &cryptsetup {
|
||||
info!("Encrypting the root filesystem");
|
||||
EncryptedDevice::prepare(&cryptsetup, &root_partition_base)?;
|
||||
EncryptedDevice::prepare(cryptsetup, &root_partition_base)?;
|
||||
Some(EncryptedDevice::open(
|
||||
cryptsetup,
|
||||
&root_partition_base,
|
||||
@ -191,14 +199,14 @@ fn create(command: CreateCommand) -> Result<(), Error> {
|
||||
};
|
||||
|
||||
let root_partition = if let Some(e) = encrypted_root.as_ref() {
|
||||
e as &BlockDevice
|
||||
e as &dyn BlockDevice
|
||||
} else {
|
||||
&root_partition_base as &BlockDevice
|
||||
&root_partition_base as &dyn BlockDevice
|
||||
};
|
||||
|
||||
let root_filesystem = Filesystem::format(root_partition, FilesystemType::Ext4, &mkext4)?;
|
||||
|
||||
let mount_stack = mount(mount_point.path(), &boot_filesystem, &root_filesystem)?;
|
||||
let mount_stack = tool::mount(mount_point.path(), &boot_filesystem, &root_filesystem)?;
|
||||
|
||||
if log_enabled!(Level::Debug) {
|
||||
debug!("lsblk:");
|
||||
@ -212,78 +220,59 @@ fn create(command: CreateCommand) -> Result<(), Error> {
|
||||
.ok();
|
||||
}
|
||||
|
||||
let mut packages: HashSet<String> = [
|
||||
"base",
|
||||
"grub",
|
||||
"efibootmgr",
|
||||
"intel-ucode",
|
||||
"networkmanager",
|
||||
"broadcom-wl",
|
||||
]
|
||||
.iter()
|
||||
.map(|s| String::from(*s))
|
||||
.collect();
|
||||
let mut packages: HashSet<String> = constants::BASE_PACKAGES
|
||||
.iter()
|
||||
.map(|s| String::from(*s))
|
||||
.collect();
|
||||
|
||||
packages.extend(presets.packages);
|
||||
|
||||
let aur_packages = {
|
||||
let mut p = vec![String::from("shim-signed")];
|
||||
p.extend(presets.aur_packages);
|
||||
p.extend(command.aur_packages);
|
||||
p
|
||||
};
|
||||
|
||||
packages.extend(constants::AUR_DEPENDENCIES.iter().map(|s| String::from(*s)));
|
||||
|
||||
let pacman_conf_path = command
|
||||
.pacman_conf
|
||||
.unwrap_or_else(|| "/etc/pacman.conf".into());
|
||||
|
||||
info!("Bootstrapping system");
|
||||
pacstrap
|
||||
.execute()
|
||||
.arg("-C")
|
||||
.arg(&pacman_conf_path)
|
||||
.arg("-c")
|
||||
.arg(mount_point.path())
|
||||
.args(packages)
|
||||
.args(&command.extra_packages)
|
||||
.run(ErrorKind::Pacstrap)?;
|
||||
.run()
|
||||
.context("Pacstrap error")?;
|
||||
|
||||
// Copy pacman.conf to the image.
|
||||
fs::copy(pacman_conf_path, mount_point.path().join("etc/pacman.conf"))
|
||||
.context("Failed copying pacman.conf")?;
|
||||
|
||||
let fstab = fix_fstab(
|
||||
&genfstab
|
||||
.execute()
|
||||
.arg("-U")
|
||||
.arg(mount_point.path())
|
||||
.run_text_output(ErrorKind::Fstab)?,
|
||||
.run_text_output()
|
||||
.context("fstab error")?,
|
||||
);
|
||||
debug!("fstab:\n{}", fstab);
|
||||
fs::write(mount_point.path().join("etc/fstab"), fstab).context(ErrorKind::Fstab)?;
|
||||
|
||||
if !presets.scripts.is_empty() {
|
||||
info!("Running custom scripts");
|
||||
}
|
||||
|
||||
for script in presets.scripts {
|
||||
let mut script_file =
|
||||
tempfile::NamedTempFile::new_in(mount_point.path()).context(ErrorKind::PresetScript)?;
|
||||
script_file
|
||||
.write_all(script.as_bytes())
|
||||
.and_then(|_| script_file.as_file_mut().metadata())
|
||||
.and_then(|metadata| {
|
||||
let mut permissions = metadata.permissions();
|
||||
permissions.set_mode(0o755);
|
||||
fs::set_permissions(script_file.path(), permissions)
|
||||
})
|
||||
.context(ErrorKind::PresetScript)?;
|
||||
|
||||
let script_path = script_file.into_temp_path();
|
||||
arch_chroot
|
||||
.execute()
|
||||
.arg(mount_point.path())
|
||||
.arg(Path::new("/").join(script_path.file_name().unwrap()))
|
||||
.run(ErrorKind::PostInstallation)?;
|
||||
}
|
||||
|
||||
info!("Performing post installation tasks");
|
||||
fs::write(mount_point.path().join("etc/fstab"), fstab).context("fstab error")?;
|
||||
|
||||
arch_chroot
|
||||
.execute()
|
||||
.arg(mount_point.path())
|
||||
.args(&["systemctl", "enable", "NetworkManager"])
|
||||
.run(ErrorKind::PostInstallation)?;
|
||||
|
||||
info!("Configuring journald");
|
||||
fs::write(
|
||||
mount_point.path().join("etc/systemd/journald.conf"),
|
||||
JOURNALD_CONF,
|
||||
)
|
||||
.context(ErrorKind::PostInstallation)?;
|
||||
.args(["passwd", "-d", "root"])
|
||||
.run()
|
||||
.context("Failed to delete the root password")?;
|
||||
|
||||
info!("Setting locale");
|
||||
fs::OpenOptions::new()
|
||||
@ -291,62 +280,214 @@ fn create(command: CreateCommand) -> Result<(), Error> {
|
||||
.write(true)
|
||||
.open(mount_point.path().join("etc/locale.gen"))
|
||||
.and_then(|mut locale_gen| locale_gen.write_all(b"en_US.UTF-8 UTF-8\n"))
|
||||
.context(ErrorKind::Locale)?;
|
||||
.context("Failed to create locale.gen")?;
|
||||
fs::write(
|
||||
mount_point.path().join("etc/locale.conf"),
|
||||
"LANG=en_US.UTF-8",
|
||||
)
|
||||
.context(ErrorKind::Locale)?;
|
||||
.context("Failed to write to locale.conf")?;
|
||||
arch_chroot
|
||||
.execute()
|
||||
.arg(mount_point.path())
|
||||
.arg("locale-gen")
|
||||
.run(ErrorKind::Locale)?;
|
||||
.run()
|
||||
.context("locale-gen failed")?;
|
||||
|
||||
info!("Installing AUR packages");
|
||||
|
||||
info!("Generating initramfs");
|
||||
fs::write(
|
||||
mount_point.path().join("etc/mkinitcpio.conf"),
|
||||
initcpio::Initcpio::new(encrypted_root.is_some()).to_config(),
|
||||
)
|
||||
.context(ErrorKind::Initramfs)?;
|
||||
arch_chroot
|
||||
.execute()
|
||||
.arg(mount_point.path())
|
||||
.args(&["mkinitcpio", "-p", "linux"])
|
||||
.run(ErrorKind::Initramfs)?;
|
||||
.args(["useradd", "-m", "aur"])
|
||||
.run()
|
||||
.context("Failed to create temporary user to install AUR packages")?;
|
||||
|
||||
let aur_sudoers = mount_point.path().join("etc/sudoers.d/aur");
|
||||
fs::write(&aur_sudoers, "aur ALL=(ALL) NOPASSWD: ALL")
|
||||
.context("Failed to modify sudoers file for AUR packages")?;
|
||||
|
||||
arch_chroot
|
||||
.execute()
|
||||
.arg(mount_point.path())
|
||||
.args(["sudo", "-u", "aur"])
|
||||
.arg("git")
|
||||
.arg("clone")
|
||||
.arg(format!(
|
||||
"https://aur.archlinux.org/{}.git",
|
||||
&command.aur_helper.package_name
|
||||
))
|
||||
.arg(format!("/home/aur/{}", &command.aur_helper.name))
|
||||
.run()
|
||||
.context("Failed to clone AUR helper package")?;
|
||||
|
||||
arch_chroot
|
||||
.execute()
|
||||
.arg(mount_point.path())
|
||||
.args([
|
||||
"bash",
|
||||
"-c",
|
||||
&format!(
|
||||
"cd /home/aur/{} && sudo -u aur makepkg -s -i --noconfirm",
|
||||
&command.aur_helper.name
|
||||
),
|
||||
])
|
||||
.run()
|
||||
.context("Failed to build AUR helper")?;
|
||||
|
||||
arch_chroot
|
||||
.execute()
|
||||
.arg(mount_point.path())
|
||||
.args(["sudo", "-u", "aur"])
|
||||
.args(command.aur_helper.install_command)
|
||||
.args(aur_packages)
|
||||
.run()
|
||||
.context("Failed to install AUR packages")?;
|
||||
|
||||
// Clean up aur user:
|
||||
arch_chroot
|
||||
.execute()
|
||||
.arg(mount_point.path())
|
||||
.args(["userdel", "-r", "aur"])
|
||||
.run()
|
||||
.context("Failed to delete temporary aur user")?;
|
||||
|
||||
fs::remove_file(&aur_sudoers).context("Cannot delete the AUR sudoers temporary file")?;
|
||||
|
||||
if !presets.scripts.is_empty() {
|
||||
info!("Running custom scripts");
|
||||
}
|
||||
|
||||
for script in presets.scripts {
|
||||
let mut bind_mount_stack = MountStack::new();
|
||||
if let Some(shared_dirs) = &script.shared_dirs {
|
||||
for dir in shared_dirs {
|
||||
// Create shared directories mount points inside chroot
|
||||
std::fs::create_dir_all(
|
||||
mount_point
|
||||
.path()
|
||||
.join(PathBuf::from("shared_dirs/"))
|
||||
.join(dir.file_name().expect("Dir had no filename")),
|
||||
)
|
||||
.context("Failed mounting shared directories in preset")?;
|
||||
|
||||
// Bind mount shared directories
|
||||
let target = mount_point
|
||||
.path()
|
||||
.join(PathBuf::from("shared_dirs/"))
|
||||
.join(dir.file_name().expect("Dir had no filename"));
|
||||
bind_mount_stack
|
||||
.bind_mount(dir.clone(), target, None)
|
||||
.context("Failed mounting shared directories in preset")?;
|
||||
}
|
||||
}
|
||||
|
||||
let mut script_file = tempfile::NamedTempFile::new_in(mount_point.path())
|
||||
.context("Failed creating temporary preset script")?;
|
||||
script_file
|
||||
.write_all(script.script_text.as_bytes())
|
||||
.and_then(|_| script_file.as_file_mut().metadata())
|
||||
.and_then(|metadata| {
|
||||
let mut permissions = metadata.permissions();
|
||||
permissions.set_mode(0o755);
|
||||
fs::set_permissions(script_file.path(), permissions)
|
||||
})
|
||||
.context("Failed creating temporary preset script")?;
|
||||
|
||||
let script_path = script_file.into_temp_path();
|
||||
arch_chroot
|
||||
.execute()
|
||||
.arg(mount_point.path())
|
||||
.arg(
|
||||
Path::new("/").join(
|
||||
script_path
|
||||
.file_name()
|
||||
.expect("Script path had no file name"),
|
||||
),
|
||||
)
|
||||
.run()
|
||||
.with_context(|| format!("Failed running preset script:\n{}", script.script_text))?;
|
||||
}
|
||||
|
||||
info!("Performing post installation tasks");
|
||||
|
||||
arch_chroot
|
||||
.execute()
|
||||
.arg(mount_point.path())
|
||||
.args(["systemctl", "enable", "NetworkManager"])
|
||||
.run()
|
||||
.context("Failed to enable NetworkManager")?;
|
||||
|
||||
info!("Configuring journald");
|
||||
fs::write(
|
||||
mount_point.path().join("etc/systemd/journald.conf"),
|
||||
constants::JOURNALD_CONF,
|
||||
)
|
||||
.context("Failed to write to journald.conf")?;
|
||||
|
||||
info!("Generating initramfs");
|
||||
let plymouth_exists = Path::new(&mount_point.path().join("usr/bin/plymouth")).exists();
|
||||
fs::write(
|
||||
mount_point.path().join("etc/mkinitcpio.conf"),
|
||||
initcpio::Initcpio::new(encrypted_root.is_some(), plymouth_exists).to_config()?,
|
||||
)
|
||||
.context("Failed to write to mkinitcpio.conf")?;
|
||||
arch_chroot
|
||||
.execute()
|
||||
.arg(mount_point.path())
|
||||
.args(["mkinitcpio", "-P"])
|
||||
.run()
|
||||
.context("Failed to run mkinitcpio - do you have the base and linux packages installed?")?;
|
||||
|
||||
if encrypted_root.is_some() {
|
||||
debug!("Setting up GRUB for an encrypted root partition");
|
||||
|
||||
let uuid = blkid
|
||||
.unwrap()
|
||||
.expect("No tool for blkid")
|
||||
.execute()
|
||||
.arg(root_partition_base.path())
|
||||
.args(&["-o", "value", "-s", "UUID"])
|
||||
.run_text_output(ErrorKind::Partitioning)?;
|
||||
.args(["-o", "value", "-s", "UUID"])
|
||||
.run_text_output()
|
||||
.context("Failed to run blkid")?;
|
||||
let trimmed = uuid.trim();
|
||||
debug!("Root partition UUID: {}", trimmed);
|
||||
|
||||
let mut grub_file = fs::OpenOptions::new()
|
||||
.append(true)
|
||||
.open(mount_point.path().join("etc/default/grub"))
|
||||
.context(ErrorKind::Bootloader)?;
|
||||
.context("Failed to create /etc/default/grub")?;
|
||||
|
||||
write!(
|
||||
&mut grub_file,
|
||||
"GRUB_CMDLINE_LINUX=\"cryptdevice=UUID={}:luks_root\"",
|
||||
trimmed
|
||||
)
|
||||
.context(ErrorKind::Bootloader)?;
|
||||
.context("Failed to write to /etc/default/grub")?;
|
||||
}
|
||||
|
||||
info!("Installing the Bootloader");
|
||||
arch_chroot
|
||||
.execute()
|
||||
.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_path.display()))
|
||||
.run(ErrorKind::Bootloader)?;
|
||||
.run().context("Failed to install grub")?;
|
||||
|
||||
let bootloader = mount_point.path().join("boot/EFI/BOOT/BOOTX64.efi");
|
||||
fs::rename(
|
||||
&bootloader,
|
||||
mount_point.path().join("boot/EFI/BOOT/grubx64.efi"),
|
||||
)
|
||||
.context("Cannot move out grub")?;
|
||||
fs::copy(
|
||||
mount_point.path().join("usr/share/shim-signed/mmx64.efi"),
|
||||
mount_point.path().join("boot/EFI/BOOT/mmx64.efi"),
|
||||
)
|
||||
.context("Failed copying mmx64")?;
|
||||
fs::copy(
|
||||
mount_point.path().join("usr/share/shim-signed/shimx64.efi"),
|
||||
bootloader,
|
||||
)
|
||||
.context("Failed copying shim")?;
|
||||
|
||||
debug!(
|
||||
"GRUB configuration: {}",
|
||||
@ -359,7 +500,8 @@ fn create(command: CreateCommand) -> Result<(), Error> {
|
||||
arch_chroot
|
||||
.execute()
|
||||
.arg(mount_point.path())
|
||||
.run(ErrorKind::Interactive)?;
|
||||
.run()
|
||||
.context("Failed to enter interactive chroot")?;
|
||||
}
|
||||
|
||||
info!("Unmounting filesystems");
|
||||
@ -367,116 +509,3 @@ fn create(command: CreateCommand) -> Result<(), Error> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn chroot(command: ChrootCommand) -> Result<(), Error> {
|
||||
let arch_chroot = Tool::find("arch-chroot")?;
|
||||
let mut cryptsetup;
|
||||
|
||||
let mut loop_device: Option<LoopDevice>;
|
||||
let storage_device = match storage::StorageDevice::from_path(&command.block_device) {
|
||||
Ok(b) => b,
|
||||
Err(_) => {
|
||||
loop_device = Some(LoopDevice::create(&command.block_device)?);
|
||||
storage::StorageDevice::from_path(loop_device.as_ref().unwrap().path())?
|
||||
}
|
||||
};
|
||||
let mount_point = tempdir().context(ErrorKind::TmpDirError)?;
|
||||
|
||||
let boot_partition = storage_device.get_partition(BOOT_PARTITION_INDEX)?;
|
||||
let boot_filesystem = Filesystem::from_partition(&boot_partition, FilesystemType::Vfat);
|
||||
|
||||
let root_partition_base = storage_device.get_partition(ROOT_PARTITION_INDEX)?;
|
||||
let encrypted_root = if is_encrypted_device(&root_partition_base)? {
|
||||
cryptsetup = Some(Tool::find("cryptsetup")?);
|
||||
Some(EncryptedDevice::open(
|
||||
cryptsetup.as_ref().unwrap(),
|
||||
&root_partition_base,
|
||||
"alma_root".into(),
|
||||
)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let root_partition = if let Some(e) = encrypted_root.as_ref() {
|
||||
e as &BlockDevice
|
||||
} else {
|
||||
&root_partition_base as &BlockDevice
|
||||
};
|
||||
let root_filesystem = Filesystem::from_partition(root_partition, FilesystemType::Ext4);
|
||||
|
||||
let mount_stack = mount(mount_point.path(), &boot_filesystem, &root_filesystem)?;
|
||||
|
||||
arch_chroot
|
||||
.execute()
|
||||
.arg(mount_point.path())
|
||||
.args(&command.command)
|
||||
.run(ErrorKind::Interactive)?;
|
||||
|
||||
info!("Unmounting filesystems");
|
||||
mount_stack.umount()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn qemu(command: QemuCommand) -> Result<(), Error> {
|
||||
let qemu = Tool::find("qemu-system-x86_64")?;
|
||||
|
||||
let err = qemu
|
||||
.execute()
|
||||
.args(&[
|
||||
"-enable-kvm",
|
||||
"-cpu",
|
||||
"host",
|
||||
"-m",
|
||||
"4G",
|
||||
"-netdev",
|
||||
"user,id=user.0",
|
||||
"-device",
|
||||
"virtio-net-pci,netdev=user.0",
|
||||
"-device",
|
||||
"qemu-xhci,id=xhci",
|
||||
"-device",
|
||||
"usb-tablet,bus=xhci.0",
|
||||
"-drive",
|
||||
])
|
||||
.arg(format!(
|
||||
"file={},if=virtio,format=raw",
|
||||
command.block_device.display()
|
||||
))
|
||||
.args(command.args)
|
||||
.exec();
|
||||
|
||||
Err(err).context(ErrorKind::Qemu)?
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let app = App::from_args();
|
||||
|
||||
let mut builder = pretty_env_logger::formatted_timed_builder();
|
||||
let log_level = if app.verbose {
|
||||
LevelFilter::Debug
|
||||
} else {
|
||||
LevelFilter::Info
|
||||
};
|
||||
builder.filter_level(log_level);
|
||||
builder.init();
|
||||
|
||||
let result = match app.cmd {
|
||||
Command::Create(command) => create(command),
|
||||
Command::Chroot(command) => chroot(command),
|
||||
Command::Qemu(command) => qemu(command),
|
||||
};
|
||||
|
||||
match result {
|
||||
Ok(()) => {
|
||||
exit(0);
|
||||
}
|
||||
Err(error) => {
|
||||
error!("{}", error);
|
||||
for cause in (&error as &Fail).iter_causes() {
|
||||
error!("Caused by: {}", cause);
|
||||
}
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
153
src/presets.rs
153
src/presets.rs
@ -1,67 +1,158 @@
|
||||
use crate::error::{Error, ErrorKind};
|
||||
use failure::ResultExt;
|
||||
use anyhow::{anyhow, Context};
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashSet;
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
use toml;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
||||
struct Preset {
|
||||
packages: Option<Vec<String>>,
|
||||
script: Option<String>,
|
||||
environment_variables: Option<Vec<String>>,
|
||||
shared_directories: Option<Vec<PathBuf>>,
|
||||
aur_packages: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
fn visit_dirs(dir: &Path, filevec: &mut Vec<PathBuf>) -> Result<(), io::Error> {
|
||||
if dir.is_dir() {
|
||||
for entry in fs::read_dir(dir)? {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
if path.is_dir() {
|
||||
visit_dirs(&path, filevec)?;
|
||||
} else if entry.path().extension() == Some(&std::ffi::OsString::from("toml")) {
|
||||
filevec.push(entry.path());
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl Preset {
|
||||
fn load(path: &Path) -> Result<Self, Error> {
|
||||
let data = fs::read_to_string(path)
|
||||
.with_context(|_| ErrorKind::Preset(format!("{}", path.display())))?;
|
||||
Ok(toml::from_str(&data)
|
||||
.with_context(|_| ErrorKind::Preset(format!("{}", path.display())))?)
|
||||
fn load(path: &Path) -> anyhow::Result<Self> {
|
||||
let data = fs::read_to_string(path).with_context(|| format!("{}", path.display()))?;
|
||||
toml::from_str(&data).with_context(|| format!("{}", path.display()))
|
||||
}
|
||||
|
||||
fn process(
|
||||
&self,
|
||||
packages: &mut HashSet<String>,
|
||||
scripts: &mut Vec<Script>,
|
||||
environment_variables: &mut HashSet<String>,
|
||||
path: &Path,
|
||||
aur_packages: &mut HashSet<String>,
|
||||
) -> anyhow::Result<()> {
|
||||
if let Some(preset_packages) = &self.packages {
|
||||
packages.extend(preset_packages.clone());
|
||||
}
|
||||
|
||||
if let Some(preset_aur_packages) = &self.aur_packages {
|
||||
aur_packages.extend(preset_aur_packages.clone());
|
||||
}
|
||||
|
||||
if let Some(preset_environment_variables) = &self.environment_variables {
|
||||
environment_variables.extend(preset_environment_variables.clone());
|
||||
}
|
||||
|
||||
if let Some(script_text) = &self.script {
|
||||
scripts.push(Script {
|
||||
script_text: script_text.clone(),
|
||||
shared_dirs: self
|
||||
.shared_directories
|
||||
.clone()
|
||||
.map(|x| {
|
||||
// Convert directories to absolute paths
|
||||
// If any shared directory is not a directory then throw an error
|
||||
x.iter()
|
||||
.cloned()
|
||||
.map(|y| {
|
||||
let full_path = path.parent().expect("Path has no parent").join(&y);
|
||||
if full_path.is_dir() {
|
||||
Ok(full_path)
|
||||
} else {
|
||||
Err(anyhow!(
|
||||
"Preset: {} - shared directory: {} is not directory",
|
||||
path.display(),
|
||||
y.display()
|
||||
))
|
||||
}
|
||||
})
|
||||
.collect::<anyhow::Result<Vec<_>>>()
|
||||
})
|
||||
.map_or(Ok(None), |r| r.map(Some))?,
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Presets {
|
||||
pub packages: HashSet<String>,
|
||||
pub scripts: Vec<String>,
|
||||
pub struct Script {
|
||||
pub script_text: String,
|
||||
pub shared_dirs: Option<Vec<PathBuf>>,
|
||||
}
|
||||
|
||||
impl Presets {
|
||||
pub fn load(list: &[PathBuf]) -> Result<Self, Error> {
|
||||
pub struct PresetsCollection {
|
||||
pub packages: HashSet<String>,
|
||||
pub aur_packages: HashSet<String>,
|
||||
pub scripts: Vec<Script>,
|
||||
}
|
||||
|
||||
impl PresetsCollection {
|
||||
pub fn load(list: &[PathBuf]) -> anyhow::Result<Self> {
|
||||
let mut packages = HashSet::new();
|
||||
let mut scripts = Vec::new();
|
||||
let mut aur_packages = HashSet::new();
|
||||
let mut scripts: Vec<Script> = Vec::new();
|
||||
let mut environment_variables = HashSet::new();
|
||||
|
||||
for preset in list {
|
||||
let Preset {
|
||||
script,
|
||||
packages: preset_packages,
|
||||
environment_variables: preset_environment_variables,
|
||||
} = Preset::load(&preset)?;
|
||||
if preset.is_dir() {
|
||||
// Build vector of paths to files, then sort by path name
|
||||
// Recursively load directories of preset files
|
||||
let mut dir_paths: Vec<PathBuf> = Vec::new();
|
||||
visit_dirs(preset, &mut dir_paths)
|
||||
.with_context(|| format!("{}", preset.display()))?;
|
||||
|
||||
if let Some(preset_packages) = preset_packages {
|
||||
packages.extend(preset_packages);
|
||||
// Order not guaranteed so we sort
|
||||
// In the future may want to support numerical sort i.e. 15_... < 100_...
|
||||
dir_paths.sort();
|
||||
|
||||
for path in dir_paths {
|
||||
Preset::load(&path)?.process(
|
||||
&mut packages,
|
||||
&mut scripts,
|
||||
&mut environment_variables,
|
||||
&path,
|
||||
&mut aur_packages,
|
||||
)?;
|
||||
}
|
||||
} else {
|
||||
Preset::load(preset)?.process(
|
||||
&mut packages,
|
||||
&mut scripts,
|
||||
&mut environment_variables,
|
||||
preset,
|
||||
&mut aur_packages,
|
||||
)?;
|
||||
}
|
||||
|
||||
if let Some(preset_environment_variables) = preset_environment_variables {
|
||||
environment_variables.extend(preset_environment_variables);
|
||||
}
|
||||
|
||||
scripts.extend(script);
|
||||
}
|
||||
|
||||
let missing_envrionments: Vec<String> = environment_variables
|
||||
.into_iter()
|
||||
.filter(|var| env::var(var).is_err())
|
||||
.collect();
|
||||
|
||||
if !missing_envrionments.is_empty() {
|
||||
Err(ErrorKind::MissingEnvironmentVariables(missing_envrionments))?
|
||||
return Err(anyhow!(
|
||||
"Missing environment variables {:?}",
|
||||
missing_envrionments
|
||||
));
|
||||
}
|
||||
|
||||
Ok(Self { packages, scripts })
|
||||
Ok(Self {
|
||||
packages,
|
||||
aur_packages,
|
||||
scripts,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -1,51 +1,35 @@
|
||||
use super::error::*;
|
||||
use failure::{Fail, ResultExt};
|
||||
use anyhow::anyhow;
|
||||
use log::error;
|
||||
use std::process::{Command, ExitStatus};
|
||||
use std::process::Command;
|
||||
use std::str;
|
||||
|
||||
#[derive(Debug, Fail)]
|
||||
enum ProcessError {
|
||||
#[fail(display = "{}", _0)]
|
||||
BadExitCode(ExitStatus),
|
||||
|
||||
#[fail(display = "Process output isn't valid UTF-8")]
|
||||
InvalidUtf8,
|
||||
}
|
||||
|
||||
pub trait CommandExt {
|
||||
fn run(&mut self, context: ErrorKind) -> Result<(), Error>;
|
||||
fn run_text_output(&mut self, context: ErrorKind) -> Result<String, Error>;
|
||||
fn run(&mut self) -> anyhow::Result<()>;
|
||||
fn run_text_output(&mut self) -> anyhow::Result<String>;
|
||||
}
|
||||
|
||||
impl CommandExt for Command {
|
||||
fn run(&mut self, context: ErrorKind) -> Result<(), Error> {
|
||||
let exit_status = self
|
||||
.spawn()
|
||||
.with_context(|_| context.clone())?
|
||||
.wait()
|
||||
.with_context(|_| context.clone())?;
|
||||
fn run(&mut self) -> anyhow::Result<()> {
|
||||
let exit_status = self.spawn()?.wait()?;
|
||||
|
||||
if !exit_status.success() {
|
||||
Err(ProcessError::BadExitCode(exit_status)).with_context(|_| context.clone())?;
|
||||
return Err(anyhow!("Bad exit code: {}", exit_status));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn run_text_output(&mut self, context: ErrorKind) -> Result<String, Error> {
|
||||
let output = self.output().with_context(|_| context.clone())?;
|
||||
fn run_text_output(&mut self) -> anyhow::Result<String> {
|
||||
let output = self.output()?;
|
||||
|
||||
if !output.status.success() {
|
||||
let error = str::from_utf8(&output.stderr).unwrap_or("[INVALID UTF8]");
|
||||
error!("{}", error);
|
||||
Err(ProcessError::BadExitCode(output.status)).with_context(|_| context.clone())?;
|
||||
return Err(anyhow!("Bad exit code: {}", output.status));
|
||||
}
|
||||
|
||||
Ok(String::from(
|
||||
str::from_utf8(&output.stdout)
|
||||
.map_err(|_| ProcessError::InvalidUtf8)
|
||||
.with_context(|_| context.clone())?,
|
||||
))
|
||||
Ok(String::from(str::from_utf8(&output.stdout).map_err(
|
||||
|_| anyhow!("Process output is not valid UTF-8"),
|
||||
)?))
|
||||
}
|
||||
}
|
||||
|
@ -1,43 +1,43 @@
|
||||
use super::markers::BlockDevice;
|
||||
use crate::error::{Error, ErrorKind};
|
||||
use crate::process::CommandExt;
|
||||
use crate::tool::Tool;
|
||||
use failure::ResultExt;
|
||||
use anyhow::Context;
|
||||
use log::{debug, warn};
|
||||
use std::fs;
|
||||
use std::io::Read;
|
||||
use std::marker::PhantomData;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
static LUKS_MAGIC_1: &'static [u8] = &[0x4c, 0x55, 0x4b, 0x53, 0xba, 0xbe];
|
||||
static LUKS_MAGIC_2: &'static [u8] = &[0x53, 0x4b, 0x55, 0x4c, 0xba, 0xbe];
|
||||
static LUKS_MAGIC_1: &[u8] = &[0x4c, 0x55, 0x4b, 0x53, 0xba, 0xbe];
|
||||
static LUKS_MAGIC_2: &[u8] = &[0x53, 0x4b, 0x55, 0x4c, 0xba, 0xbe];
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct EncryptedDevice<'t, 'o> {
|
||||
cryptsetup: &'t Tool,
|
||||
name: String,
|
||||
path: PathBuf,
|
||||
origin: PhantomData<&'o BlockDevice>,
|
||||
origin: PhantomData<&'o dyn BlockDevice>,
|
||||
}
|
||||
|
||||
impl<'t, 'o> EncryptedDevice<'t, 'o> {
|
||||
pub fn prepare(cryptsetup: &Tool, device: &BlockDevice) -> Result<(), Error> {
|
||||
pub fn prepare(cryptsetup: &Tool, device: &dyn BlockDevice) -> anyhow::Result<()> {
|
||||
debug!("Preparing encrypted device in {}", device.path().display());
|
||||
cryptsetup
|
||||
.execute()
|
||||
.arg("luksFormat")
|
||||
.arg("-q")
|
||||
.arg(device.path())
|
||||
.run(ErrorKind::LuksSetup)?;
|
||||
.run()
|
||||
.context("Error setting up an encrypted device")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn open(
|
||||
cryptsetup: &'t Tool,
|
||||
device: &'o BlockDevice,
|
||||
device: &'o dyn BlockDevice,
|
||||
name: String,
|
||||
) -> Result<EncryptedDevice<'t, 'o>, Error> {
|
||||
) -> anyhow::Result<EncryptedDevice<'t, 'o>> {
|
||||
debug!(
|
||||
"Opening encrypted device {} as {}",
|
||||
device.path().display(),
|
||||
@ -48,7 +48,8 @@ impl<'t, 'o> EncryptedDevice<'t, 'o> {
|
||||
.arg("open")
|
||||
.arg(device.path())
|
||||
.arg(&name)
|
||||
.run(ErrorKind::LuksOpen)?;
|
||||
.run()
|
||||
.context("Error opening the encrypted device")?;
|
||||
|
||||
let path = PathBuf::from("/dev/mapper").join(&name);
|
||||
Ok(Self {
|
||||
@ -59,13 +60,14 @@ impl<'t, 'o> EncryptedDevice<'t, 'o> {
|
||||
})
|
||||
}
|
||||
|
||||
fn _close(&mut self) -> Result<(), Error> {
|
||||
fn _close(&mut self) -> anyhow::Result<()> {
|
||||
debug!("Closing encrypted device {}", self.name);
|
||||
self.cryptsetup
|
||||
.execute()
|
||||
.arg("close")
|
||||
.arg(&self.name)
|
||||
.run(ErrorKind::LuksClose)?;
|
||||
.run()
|
||||
.context("Error closing the encrypted device")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -85,16 +87,16 @@ impl<'t, 'o> BlockDevice for EncryptedDevice<'t, 'o> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_encrypted_device(device: &BlockDevice) -> Result<bool, Error> {
|
||||
pub fn is_encrypted_device(device: &dyn BlockDevice) -> anyhow::Result<bool> {
|
||||
let mut f = fs::OpenOptions::new()
|
||||
.read(true)
|
||||
.write(false)
|
||||
.open(device.path())
|
||||
.context(ErrorKind::LuksDetection)?;
|
||||
.context("Error detecting whether the root partition is an encrypted device")?;
|
||||
|
||||
let mut buffer = [0; 6];
|
||||
f.read_exact(&mut buffer)
|
||||
.context(ErrorKind::LuksDetection)?;
|
||||
.context("Error detecting whether the root partition is an encrypted device")?;
|
||||
|
||||
Ok(buffer == LUKS_MAGIC_1 || buffer == LUKS_MAGIC_2)
|
||||
}
|
||||
|
@ -1,9 +1,6 @@
|
||||
use super::markers::BlockDevice;
|
||||
use crate::{
|
||||
error::{Error, ErrorKind},
|
||||
process::CommandExt,
|
||||
tool::Tool,
|
||||
};
|
||||
use crate::{process::CommandExt, tool::Tool};
|
||||
use anyhow::Context;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum FilesystemType {
|
||||
@ -23,31 +20,31 @@ impl FilesystemType {
|
||||
#[derive(Debug)]
|
||||
pub struct Filesystem<'a> {
|
||||
fs_type: FilesystemType,
|
||||
block: &'a BlockDevice,
|
||||
block: &'a dyn BlockDevice,
|
||||
}
|
||||
|
||||
impl<'a> Filesystem<'a> {
|
||||
pub fn format(
|
||||
block: &'a BlockDevice,
|
||||
block: &'a dyn BlockDevice,
|
||||
fs_type: FilesystemType,
|
||||
mkfs: &Tool,
|
||||
) -> Result<Self, Error> {
|
||||
) -> anyhow::Result<Self> {
|
||||
let mut command = mkfs.execute();
|
||||
match fs_type {
|
||||
FilesystemType::Ext4 => command.arg("-F").arg(block.path()),
|
||||
FilesystemType::Vfat => command.arg("-F32").arg(block.path()),
|
||||
};
|
||||
|
||||
command.run(ErrorKind::Formatting)?;
|
||||
command.run().context("Error formatting filesystem")?;
|
||||
|
||||
Ok(Self { fs_type, block })
|
||||
}
|
||||
|
||||
pub fn from_partition(block: &'a BlockDevice, fs_type: FilesystemType) -> Self {
|
||||
pub fn from_partition(block: &'a dyn BlockDevice, fs_type: FilesystemType) -> Self {
|
||||
Self { fs_type, block }
|
||||
}
|
||||
|
||||
pub fn block(&self) -> &BlockDevice {
|
||||
pub fn block(&self) -> &dyn BlockDevice {
|
||||
self.block
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
use crate::error::{Error, ErrorKind};
|
||||
use crate::tool::Tool;
|
||||
use failure::ResultExt;
|
||||
use anyhow::{anyhow, Context};
|
||||
use log::info;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
@ -11,25 +10,27 @@ pub struct LoopDevice {
|
||||
}
|
||||
|
||||
impl LoopDevice {
|
||||
pub fn create(file: &Path) -> Result<Self, Error> {
|
||||
pub fn create(file: &Path) -> anyhow::Result<Self> {
|
||||
let losetup = Tool::find("losetup")?;
|
||||
let output = losetup
|
||||
.execute()
|
||||
.args(&["--find", "-P", "--show"])
|
||||
.args(["--find", "-P", "--show"])
|
||||
.arg(file)
|
||||
.output()
|
||||
.context(ErrorKind::Image)?;
|
||||
.context("Error creating the image")?;
|
||||
|
||||
if !output.status.success() {
|
||||
Err(ErrorKind::Losetup(
|
||||
String::from_utf8(output.stderr).unwrap(),
|
||||
))?
|
||||
return Err(anyhow!(String::from_utf8(output.stderr)?));
|
||||
}
|
||||
|
||||
let path = PathBuf::from(String::from_utf8(output.stdout).unwrap().trim());
|
||||
let path = PathBuf::from(
|
||||
String::from_utf8(output.stdout)
|
||||
.context("Output not valid UTF-8")?
|
||||
.trim(),
|
||||
);
|
||||
info!("Mounted {} to {}", file.display(), path.display());
|
||||
|
||||
Ok(LoopDevice { path, losetup })
|
||||
Ok(Self { path, losetup })
|
||||
}
|
||||
|
||||
pub fn path(&self) -> &Path {
|
||||
@ -45,7 +46,7 @@ impl Drop for LoopDevice {
|
||||
.arg("-d")
|
||||
.arg(&self.path)
|
||||
.spawn()
|
||||
.unwrap()
|
||||
.expect("Failed to spawn command to detach loop device")
|
||||
.wait()
|
||||
.ok();
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
use std::path::Path;
|
||||
|
||||
// Marker traits
|
||||
pub trait BlockDevice: std::fmt::Debug {
|
||||
fn path(&self) -> &Path;
|
||||
}
|
||||
|
@ -12,5 +12,5 @@ pub use filesystem::{Filesystem, FilesystemType};
|
||||
pub use loop_device::LoopDevice;
|
||||
pub use markers::BlockDevice;
|
||||
pub use mount_stack::MountStack;
|
||||
pub use removeable_devices::get_removable_devices;
|
||||
pub use removeable_devices::get_storage_devices;
|
||||
pub use storage_device::StorageDevice;
|
||||
|
@ -1,8 +1,6 @@
|
||||
use super::Filesystem;
|
||||
use crate::error::{Error, ErrorKind};
|
||||
use failure::Fail;
|
||||
use anyhow::anyhow;
|
||||
use log::{debug, warn};
|
||||
use nix;
|
||||
use nix::mount::{mount, umount, MsFlags};
|
||||
use std::marker::PhantomData;
|
||||
use std::path::PathBuf;
|
||||
@ -20,7 +18,6 @@ impl<'a> MountStack<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn mount(
|
||||
&mut self,
|
||||
filesystem: &'a Filesystem,
|
||||
@ -40,21 +37,43 @@ impl<'a> MountStack<'a> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn _umount(&mut self) -> Result<(), Error> {
|
||||
pub fn bind_mount(
|
||||
&mut self,
|
||||
source: PathBuf,
|
||||
target: PathBuf,
|
||||
options: Option<&str>,
|
||||
) -> nix::Result<()> {
|
||||
debug!("Mounting {:?} to {:?}", source, target);
|
||||
mount::<_, _, str, _>(
|
||||
Some(&source),
|
||||
&target,
|
||||
None,
|
||||
MsFlags::MS_BIND | MsFlags::MS_NOATIME, // Read-only flag has no effect for bind mounts
|
||||
options,
|
||||
)?;
|
||||
self.targets.push(target);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn _umount(&mut self) -> anyhow::Result<()> {
|
||||
let mut result = Ok(());
|
||||
|
||||
while let Some(target) = self.targets.pop() {
|
||||
debug!("Unmounting {}", target.display());
|
||||
if let Err(e) = umount(&target) {
|
||||
warn!("Unable to umount {}: {}", target.display(), e);
|
||||
result = Err(Error::from(e.context(ErrorKind::UmountFailure)));
|
||||
result = Err(anyhow!(
|
||||
"Failed unmounting filesystem: {}, {}",
|
||||
target.display(),
|
||||
e
|
||||
));
|
||||
};
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
pub fn umount(mut self) -> Result<(), Error> {
|
||||
pub fn umount(mut self) -> anyhow::Result<()> {
|
||||
self._umount()
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ use std::path::{Path, PathBuf};
|
||||
#[derive(Debug)]
|
||||
pub struct Partition<'a> {
|
||||
path: PathBuf,
|
||||
origin: PhantomData<&'a Origin>,
|
||||
origin: PhantomData<&'a dyn Origin>,
|
||||
}
|
||||
|
||||
impl<'a> Partition<'a> {
|
||||
|
@ -1,6 +1,5 @@
|
||||
use crate::error::{Error, ErrorKind};
|
||||
use anyhow::Context;
|
||||
use byte_unit::Byte;
|
||||
use failure::ResultExt;
|
||||
use std::{fmt, fs};
|
||||
|
||||
#[derive(Debug)]
|
||||
@ -27,22 +26,24 @@ fn trimmed(source: String) -> String {
|
||||
String::from(source.trim_end())
|
||||
}
|
||||
|
||||
pub fn get_removable_devices() -> Result<Vec<Device>, Error> {
|
||||
pub fn get_storage_devices(allow_non_removable: bool) -> anyhow::Result<Vec<Device>> {
|
||||
let mut result = Vec::new();
|
||||
|
||||
for entry in fs::read_dir("/sys/block").context(ErrorKind::RemoveableDevicesQuery)? {
|
||||
let entry = entry.context(ErrorKind::RemoveableDevicesQuery)?;
|
||||
for entry in fs::read_dir("/sys/block").context("Error querying storage devices")? {
|
||||
let entry = entry.context("Error querying storage devices")?;
|
||||
|
||||
let removable = fs::read_to_string(entry.path().join("removable"))
|
||||
.context(ErrorKind::RemoveableDevicesQuery)?;
|
||||
let removable = allow_non_removable
|
||||
|| fs::read_to_string(entry.path().join("removable"))
|
||||
.map(|v| v == "1\n")
|
||||
.context("Error querying storage devices")?;
|
||||
|
||||
if removable != "1\n" {
|
||||
if !removable {
|
||||
continue;
|
||||
}
|
||||
|
||||
let model = fs::read_to_string(entry.path().join("device/model"))
|
||||
.map(trimmed)
|
||||
.context(ErrorKind::RemoveableDevicesQuery)?;
|
||||
.context("Error querying storage devices")?;
|
||||
|
||||
if model == "CD-ROM" {
|
||||
continue;
|
||||
@ -52,22 +53,22 @@ pub fn get_removable_devices() -> Result<Vec<Device>, Error> {
|
||||
name: entry
|
||||
.path()
|
||||
.file_name()
|
||||
.unwrap()
|
||||
.expect("Could not get file name for dir entry /sys/block")
|
||||
.to_string_lossy()
|
||||
.into_owned(),
|
||||
model,
|
||||
vendor: fs::read_to_string(entry.path().join("device/vendor"))
|
||||
.map(trimmed)
|
||||
.context(ErrorKind::RemoveableDevicesQuery)?,
|
||||
.context("Error querying storage devices")?,
|
||||
size: Byte::from_bytes(
|
||||
fs::read_to_string(entry.path().join("size"))
|
||||
.context(ErrorKind::RemoveableDevicesQuery)?
|
||||
.context("Error querying storage devices")?
|
||||
.trim()
|
||||
.parse::<u128>()
|
||||
.unwrap()
|
||||
.context("Could not parse block size to unsigned integer (u128)")?
|
||||
* 512,
|
||||
),
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
@ -79,7 +80,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn sanity() {
|
||||
let devices = get_removable_devices().unwrap();
|
||||
let devices = get_storage_devices(false).expect("No devices");
|
||||
println!("{:?}", devices);
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
use super::markers::{BlockDevice, Origin};
|
||||
use super::partition::Partition;
|
||||
use crate::error::{Error, ErrorKind};
|
||||
use failure::ResultExt;
|
||||
use anyhow::{anyhow, Context};
|
||||
use log::debug;
|
||||
use std::fs::read_to_string;
|
||||
use std::marker::PhantomData;
|
||||
@ -11,18 +10,20 @@ use std::path::{Path, PathBuf};
|
||||
pub struct StorageDevice<'a> {
|
||||
name: String,
|
||||
path: PathBuf,
|
||||
origin: PhantomData<&'a Origin>,
|
||||
origin: PhantomData<&'a dyn Origin>,
|
||||
}
|
||||
|
||||
impl<'a> StorageDevice<'a> {
|
||||
pub fn from_path(path: &'a Path) -> Result<Self, Error> {
|
||||
pub fn from_path(path: &'a Path, allow_non_removable: bool) -> anyhow::Result<Self> {
|
||||
debug!("path: {:?}", path);
|
||||
let path = path.canonicalize().context(ErrorKind::DeviceQuery)?;
|
||||
let path = path
|
||||
.canonicalize()
|
||||
.context("Error querying information about the block device")?;
|
||||
let device_name = path
|
||||
.file_name()
|
||||
.and_then(|s| s.to_str())
|
||||
.and_then(std::ffi::OsStr::to_str)
|
||||
.map(String::from)
|
||||
.ok_or_else(|| Error::from(ErrorKind::InvalidDeviceName))?;
|
||||
.ok_or_else(|| anyhow!("Invalid device name: {}", path.display()))?;
|
||||
|
||||
debug!("real path: {:?}, device name: {:?}", path, device_name);
|
||||
|
||||
@ -31,8 +32,14 @@ impl<'a> StorageDevice<'a> {
|
||||
path,
|
||||
origin: PhantomData,
|
||||
};
|
||||
if !(_self.is_removable_device()? || _self.is_loop_device()) {
|
||||
return Err(ErrorKind::DangerousDevice)?;
|
||||
|
||||
// If we only allow removable/loop devices, and the device is neither removable or a loop
|
||||
// device then throw a DangerousDevice error
|
||||
if !(allow_non_removable || _self.is_removable_device()? || _self.is_loop_device()) {
|
||||
return Err(anyhow!(
|
||||
"The given block device is neither removable nor a loop device: {}",
|
||||
_self.name
|
||||
));
|
||||
}
|
||||
|
||||
Ok(_self)
|
||||
@ -44,12 +51,13 @@ impl<'a> StorageDevice<'a> {
|
||||
path
|
||||
}
|
||||
|
||||
fn is_removable_device(&self) -> Result<bool, Error> {
|
||||
fn is_removable_device(&self) -> anyhow::Result<bool> {
|
||||
let mut path = self.sys_path();
|
||||
path.push("removable");
|
||||
|
||||
debug!("Reading: {:?}", path);
|
||||
let result = read_to_string(&path).context(ErrorKind::DeviceQuery)?;
|
||||
let result =
|
||||
read_to_string(&path).context("Error querying information about the block device")?;
|
||||
debug!("{:?} -> {}", path, result);
|
||||
|
||||
Ok(result == "1\n")
|
||||
@ -61,8 +69,15 @@ impl<'a> StorageDevice<'a> {
|
||||
path.exists()
|
||||
}
|
||||
|
||||
pub fn get_partition(&self, index: u8) -> Result<Partition, Error> {
|
||||
let name = if self.name.chars().rev().next().unwrap().is_digit(10) {
|
||||
pub fn get_partition(&self, index: u8) -> anyhow::Result<Partition> {
|
||||
let name = if self
|
||||
.name
|
||||
.chars()
|
||||
.rev()
|
||||
.next()
|
||||
.expect("Storage device name is empty")
|
||||
.is_ascii_digit()
|
||||
{
|
||||
format!("{}p{}", self.name, index)
|
||||
} else {
|
||||
format!("{}{}", self.name, index)
|
||||
@ -72,7 +87,7 @@ impl<'a> StorageDevice<'a> {
|
||||
|
||||
debug!("Partition {} for {} is in {:?}", index, self.name, path);
|
||||
if !path.exists() {
|
||||
return Err(ErrorKind::NoSuchPartition(index).into());
|
||||
return Err(anyhow!("Partition {} does not exist", index));
|
||||
}
|
||||
Ok(Partition::new::<Self>(path))
|
||||
}
|
||||
|
75
src/tool/chroot.rs
Normal file
75
src/tool/chroot.rs
Normal file
@ -0,0 +1,75 @@
|
||||
use super::mount;
|
||||
use super::Tool;
|
||||
use crate::args;
|
||||
use crate::constants::{BOOT_PARTITION_INDEX, ROOT_PARTITION_INDEX};
|
||||
use crate::process::CommandExt;
|
||||
use crate::storage;
|
||||
use crate::storage::{is_encrypted_device, EncryptedDevice};
|
||||
use crate::storage::{BlockDevice, Filesystem, FilesystemType, LoopDevice};
|
||||
use anyhow::Context;
|
||||
use log::info;
|
||||
|
||||
use tempfile::tempdir;
|
||||
|
||||
/// Use arch-chroot to chroot to the given device
|
||||
/// Also handles encrypted root partitions (detected by checking for the LUKS magic header)
|
||||
pub fn chroot(command: args::ChrootCommand) -> anyhow::Result<()> {
|
||||
let arch_chroot = Tool::find("arch-chroot")?;
|
||||
let cryptsetup;
|
||||
|
||||
let loop_device: Option<LoopDevice>;
|
||||
let storage_device =
|
||||
match storage::StorageDevice::from_path(&command.block_device, command.allow_non_removable)
|
||||
{
|
||||
Ok(b) => b,
|
||||
Err(_) => {
|
||||
loop_device = Some(LoopDevice::create(&command.block_device)?);
|
||||
storage::StorageDevice::from_path(
|
||||
loop_device.as_ref().expect("loop device not found").path(),
|
||||
command.allow_non_removable,
|
||||
)?
|
||||
}
|
||||
};
|
||||
let mount_point = tempdir().context("Error creating a temporary directory")?;
|
||||
|
||||
let boot_partition = storage_device.get_partition(BOOT_PARTITION_INDEX)?;
|
||||
let boot_filesystem = Filesystem::from_partition(&boot_partition, FilesystemType::Vfat);
|
||||
|
||||
let root_partition_base = storage_device.get_partition(ROOT_PARTITION_INDEX)?;
|
||||
let encrypted_root = if is_encrypted_device(&root_partition_base)? {
|
||||
cryptsetup = Some(Tool::find("cryptsetup")?);
|
||||
Some(EncryptedDevice::open(
|
||||
cryptsetup.as_ref().expect("cryptsetup not found"),
|
||||
&root_partition_base,
|
||||
"alma_root".into(),
|
||||
)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let root_partition = if let Some(e) = encrypted_root.as_ref() {
|
||||
e as &dyn BlockDevice
|
||||
} else {
|
||||
&root_partition_base as &dyn BlockDevice
|
||||
};
|
||||
let root_filesystem = Filesystem::from_partition(root_partition, FilesystemType::Ext4);
|
||||
|
||||
let mount_stack = mount(mount_point.path(), &boot_filesystem, &root_filesystem)?;
|
||||
|
||||
arch_chroot
|
||||
.execute()
|
||||
.arg(mount_point.path())
|
||||
.args(&command.command)
|
||||
.run()
|
||||
.with_context(|| {
|
||||
format!(
|
||||
"Error running command in chroot: {}",
|
||||
command.command.join(" "),
|
||||
)
|
||||
})?;
|
||||
|
||||
info!("Unmounting filesystems");
|
||||
mount_stack.umount()?;
|
||||
|
||||
Ok(())
|
||||
}
|
@ -1,5 +1,12 @@
|
||||
use super::error::*;
|
||||
use failure::ResultExt;
|
||||
mod chroot;
|
||||
mod mount;
|
||||
mod qemu;
|
||||
|
||||
use anyhow::Context;
|
||||
pub use chroot::chroot;
|
||||
pub use mount::mount;
|
||||
pub use qemu::qemu;
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
use which::which;
|
||||
@ -10,9 +17,9 @@ pub struct Tool {
|
||||
}
|
||||
|
||||
impl Tool {
|
||||
pub fn find(name: &'static str) -> Result<Self, Error> {
|
||||
pub fn find(name: &'static str) -> anyhow::Result<Self> {
|
||||
Ok(Self {
|
||||
exec: which(name).context(ErrorKind::NoTool(name))?,
|
||||
exec: which(name).context(format!("Cannot find {}", name))?,
|
||||
})
|
||||
}
|
||||
|
36
src/tool/mount.rs
Normal file
36
src/tool/mount.rs
Normal file
@ -0,0 +1,36 @@
|
||||
use crate::storage::{Filesystem, MountStack};
|
||||
use anyhow::Context;
|
||||
use log::{debug, info};
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
/// Mounts root filesystem to given mount_path
|
||||
/// Mounts boot filesystem to mount_path/boot
|
||||
/// Note we mount with noatime to reduce disk writes by not recording file access times
|
||||
pub fn mount<'a>(
|
||||
mount_path: &Path,
|
||||
boot_filesystem: &'a Filesystem,
|
||||
root_filesystem: &'a Filesystem,
|
||||
) -> anyhow::Result<MountStack<'a>> {
|
||||
let mut mount_stack = MountStack::new();
|
||||
debug!(
|
||||
"Root partition: {}",
|
||||
root_filesystem.block().path().display()
|
||||
);
|
||||
|
||||
info!("Mounting filesystems to {}", mount_path.display());
|
||||
mount_stack
|
||||
.mount(root_filesystem, mount_path.into(), None)
|
||||
.with_context(|| format!("Error mounting filesystem to {}", mount_path.display()))?;
|
||||
|
||||
let boot_point = mount_path.join("boot");
|
||||
if !boot_point.exists() {
|
||||
fs::create_dir(&boot_point).context("Error creating the boot directory")?;
|
||||
}
|
||||
|
||||
mount_stack
|
||||
.mount(boot_filesystem, boot_point, None)
|
||||
.context("Error mounting the boot point")?;
|
||||
|
||||
Ok(mount_stack)
|
||||
}
|
42
src/tool/qemu.rs
Normal file
42
src/tool/qemu.rs
Normal file
@ -0,0 +1,42 @@
|
||||
use super::Tool;
|
||||
use crate::args;
|
||||
use anyhow::Context;
|
||||
use log::debug;
|
||||
|
||||
use std::os::unix::process::CommandExt as UnixCommandExt;
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// Loads given block device in qemu
|
||||
/// Uses kvm if it is enabled
|
||||
pub fn qemu(command: args::QemuCommand) -> anyhow::Result<()> {
|
||||
let qemu = Tool::find("qemu-system-x86_64")?;
|
||||
|
||||
let mut run = qemu.execute();
|
||||
run.args([
|
||||
"-m",
|
||||
"4G",
|
||||
"-netdev",
|
||||
"user,id=user.0",
|
||||
"-device",
|
||||
"virtio-net-pci,netdev=user.0",
|
||||
"-device",
|
||||
"qemu-xhci,id=xhci",
|
||||
"-device",
|
||||
"usb-tablet,bus=xhci.0",
|
||||
"-drive",
|
||||
])
|
||||
.arg(format!(
|
||||
"file={},if=virtio,format=raw",
|
||||
command.block_device.display()
|
||||
))
|
||||
.args(command.args);
|
||||
|
||||
if PathBuf::from("/dev/kvm").exists() {
|
||||
debug!("KVM is enabled");
|
||||
run.args(["-enable-kvm", "-cpu", "host"]);
|
||||
}
|
||||
|
||||
let err = run.exec();
|
||||
|
||||
Err(err).context("Failed launching Qemu")?
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user