Compare commits

...

70 Commits

Author SHA1 Message Date
Philip Mueller
42af023b54 [presets] steam - fix Intel
- remove amdvlk
2023-06-23 20:43:45 +02:00
Philip Mueller
6f5e2dd748 [presets] update kde
- add falkon and discover
2023-06-23 20:06:49 +02:00
Philip Mueller
979d30162f [src] update initcpio
- count in plymouth
2023-06-23 19:05:40 +02:00
Philip Mueller
af66a89067 [src] set paru as default 2023-06-23 15:09:23 +02:00
Philip Mueller
5259bb6105 [presets] kde: enable sddm theme 2023-06-23 14:51:16 +02:00
Philip Mueller
b3629c3120 [aur] set '--answerdiff=None' for yay 2023-06-23 14:05:16 +02:00
Philip Mueller
7510cce532 [presets] add audio module 2023-06-23 13:05:03 +02:00
Philip Mueller
3f15f87a4a [presets] misc fixes
- update user module not to ask for a password
- ALMA_USER_PASSWORD env variable added
- update steam module
- update base module
2023-06-23 12:57:35 +02:00
Philip Mueller
88df8af4ed [presets] improve kde 2023-06-23 09:38:52 +02:00
Philip Mueller
4b263f2f33 [presets] steam fix asian fonts 2023-06-22 23:47:19 +02:00
Philip Mueller
0c12f01685 [code] try to make clippy happy 2023-06-22 23:03:17 +02:00
Philip Mueller
7fefb5fde2 [code] try to make clippy happy 2023-06-22 23:02:14 +02:00
Philip Mueller
fb3808335e [code] try to make clippy happy 2023-06-22 22:59:04 +02:00
Philip Mueller
ba18f68f8f [doc] update readme 2023-06-22 22:43:55 +02:00
Philip Mueller
0bd7be2304 [presets] fix some files 2023-06-22 22:38:45 +02:00
Philip Müller
19d26cf0bf [presets] fix user toml 2023-06-22 22:35:48 +02:00
Roey Darwish Dror
20a80c73e2 Update README.md 2023-06-22 22:35:17 +02:00
Roey Darwish Dror
3f89061369 Add a command line flag to specify the boot partition size (fix #79) 2023-06-22 22:35:17 +02:00
Roey Darwish Dror
60438bac5c Be more informative about missing binaries 2023-06-22 22:35:17 +02:00
EdJoPaTo
21e11151c8 feat: support paru as AurHelper (#75) 2023-06-22 22:35:17 +02:00
EdJoPaTo
dc127ed87a Fix Clippy lints & GitHub Action improvements (#74)
* ci: improve

- run on all branches to test things
- no cargo color, it destroys actions-rs/cargo features
- update versions

* refactor(lint): fix clippy issues

* refactor(lint): fix more clippy issues
2023-06-22 22:35:17 +02:00
Hans Gaiser
a6984b0b84 Add pacman_conf argument for alma create. (#68) 2023-06-22 22:35:17 +02:00
Roey Darwish Dror
0680de7c42 Add a note about Arch Linux derivatives 2023-06-22 22:35:17 +02:00
Roey Darwish Dror
86a7b8f43f Update issue templates 2023-06-22 22:35:17 +02:00
Roey Darwish Dror
67f7266ef9 Migrate to Github actions (fix #56) (#66) 2023-06-22 22:35:17 +02:00
Roey Darwish Dror
f594fc2ffc Enable secure boot (fix #44) (#65) 2023-06-22 22:35:17 +02:00
Roey Darwish Dror
38d0085ba1 AUR fixes
The commit switches to using yay-bin instead of yay, avoiding the need to install Go.
In addition, it fixes a bug where aur packages
aren't build when specified only in the command line
2023-06-22 22:35:17 +02:00
Laurențiu Nicola
fdbe285855 Bump deps (#63) 2023-06-22 22:35:17 +02:00
Laurențiu Nicola
b4ecfd3c2a Allow sudo for wheel, otherwise you're locked out (#62) 2023-06-22 22:35:10 +02:00
James McMurray
7bba0c8c5a Move locale generation before user scripts (#59)
So user scripts can change locale if they wish
2023-06-22 22:33:26 +02:00
James McMurray
38e2cfbf5b Cache go when installing yay (#57)
Added common issues to README.md
2023-06-22 22:33:26 +02:00
Philip Mueller
f8bb291db4 [presets] add kate for kde 2023-06-22 22:26:01 +02:00
Philip Mueller
3bc7d478ec [presets] fix steam script 2023-06-22 22:08:41 +02:00
Philip Mueller
ef3d5ef01b [presets] fix steam script 2023-06-22 22:02:46 +02:00
Philip Mueller
433b3300dc [presets] update kde 2023-06-22 21:51:52 +02:00
Philip Mueller
841704b39c [presets] add steam 2023-06-22 21:51:09 +02:00
Philip Mueller
0405b4022d [presets] fix base.toml 2023-06-22 21:04:11 +02:00
Philip Mueller
7922e34634 [presets] add kde 2023-06-22 20:47:36 +02:00
Philip Mueller
2580f02a6a [PKGBUILD] add 2023-06-22 09:42:47 +02:00
Philip Mueller
98a475feb9 [src] fix BASE_PACKAGES 2023-06-22 09:38:37 +02:00
Philip Mueller
153c408c87 [presets] try to make base adjustable 2023-06-22 09:24:36 +02:00
Philip Müller
493caad5b5
Update constants.rs
- use Linux61
2023-06-21 20:11:20 +02:00
Philip Müller
ac97608fe7
Update constants.rs
- use Linux515 LTS
2022-04-19 13:56:17 +02:00
Philip Müller
5610aa0335 [presets] remove vesa as it creates issues 2020-07-07 12:34:11 +02:00
Philip Müller
315f2c5a62 [presets] update xfce 2020-07-07 08:54:37 +02:00
Philip Müller
b42f2f41b2 [presets] add xfce-manjaro 2020-07-04 09:38:28 +02:00
Philip Müller
eec0bf1628 [presets] remove unneeded examples 2020-07-03 18:30:19 +02:00
Philip Müller
4df5629d43 [presets] add XFCE 2020-07-03 18:27:56 +02:00
Philip Müller
5b2e5d75ec
Update constants.rs
- use correct extramodule for LTS kernel
2020-07-02 13:47:21 +02:00
Philip Müller
922f9cccfe
Update main.rs
- use mkinitcpio -P to have options to use more than one kernel.
2020-07-02 13:40:30 +02:00
Philip Müller
b2dc7e628a
Update constants.rs
- use latest LTS kernel
2020-07-02 13:38:52 +02:00
James McMurray
3ca2e01f1f
Bump which to version 4 removing failure dependency (#55)
We are now completely free of failure dependencies!
2020-06-18 08:56:37 +03:00
Roey Darwish Dror
460752adfa Large boot partition (fix #50) 2020-06-18 05:48:31 +00:00
Roey Darwish Dror
b92219af41 Clear the root password 2020-06-18 05:47:01 +00:00
Roey Darwish Dror
b3449b6b3d Make clippy happy 2020-06-17 19:39:58 +00:00
James McMurray
c04b5f5559
Add amd-ucode to default packages (#52) 2020-05-31 08:39:13 +03:00
James McMurray
7152901820
Migrate ALMA to anyhow (#54) 2020-05-31 08:38:20 +03:00
James McMurray
c8b151fe5f
Add support for installing AUR packages (#48) 2020-05-10 19:31:40 +03:00
James McMurray
bc9969a0db
Add shared_directories preset parameter (#35) 2020-03-26 20:57:46 +02:00
James McMurray
14347710ce
Add linux package to defaults (#36)
Should fix mkinitcpio issues due to missing "linux" preset now it's no
longer included in "base".

Note this issue only affected newer Arch Linux installs (since the
change).
2020-03-21 20:38:25 +02:00
James McMurray
1f5b28c065
Recursively import presets from provided directory (#33)
Allow a directory to be passed as a preset, in which case all files
inside the directory (recursively) are treated as presets, loaded in
lexicographical order.

This allows one to compose a system by mixing in different presets, and
easily change their order of execution.
2020-03-21 07:16:15 +02:00
Roey Darwish Dror
6624f05d1e
Merge pull request #29 from jamesmcm/refactor 2020-03-14 20:54:37 +02:00
James McMurray
490ab30f4c Refactor main.rs and fix clippy lints
Status: WIP

Completed:
* Fixed flagged clippy lints
* Moved qemu(), main.rs::mount() and chroot() to the tools module.
* Moved constants in main.rs to constants.rs (including base packages
  array)
* Renamed Presets struct to PresetsCollection to avoid confusion with
  Preset struct
* Moved main() to the top of main.rs to highlight general logic path
* Added comments and docstrings to some functions
* Removed some uses of `use foo::*` to make the source of imported functions
  and structs clearer

TODO:
* Move remaining code in main.rs to modules (except main())
* Break up create() function in to separate steps
* Log every command run (with arguments) to debug! when verbose flag is used
* Add docstrings for remaining functions and document constants (e.g.
  why noatime is used)
* Remove remaining uses of `use foo::*`
* Consider renaming/moving tools module to address tool:: vs. Tool::
  confusion
2020-03-06 23:11:56 +01:00
Roey Darwish Dror
19eef3a0e1
Merge pull request #28 from jamesmcm/expand_readme
Add more comprehensive examples to README.md
2020-03-06 06:36:20 +02:00
James McMurray
1de58314e4 Add more comprehensive examples to README.md 2020-03-05 22:38:18 +01:00
Roey Darwish Dror
7c88f4527d Dependencies bump and compilation fixes 2020-03-01 21:25:26 +02:00
Roey Darwish Dror
11c5b04677 Version bump 2019-08-13 21:44:33 +03:00
Roey Darwish Dror
750653c222 Add a flag for non-removable devices (fix #24) 2019-08-13 21:43:55 +03:00
Roey Darwish Dror
25cdc44c7c Dependencies bump 2019-08-13 21:21:23 +03:00
Roey Darwish Dror
34020614e1 Enable KVM only if available 2019-08-11 15:46:19 +03:00
70 changed files with 1481 additions and 1026 deletions

27
.github/ISSUE_TEMPLATE/bug_report.md vendored Normal file
View 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.

View 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
View 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

File diff suppressed because it is too large Load Diff

View File

@ -1,19 +1,20 @@
[package] [package]
name = "alma" name = "alma"
version = "0.9.0" version = "0.10.0"
authors = ["Roey Darwish Dror"] authors = ["Roey Darwish Dror"]
edition = "2018" edition = "2018"
[dependencies] [dependencies]
which = "2.0.1" which = "4"
failure = "0.1.5" log = "0.4"
log = "0.4.6" structopt = "0.3"
structopt = "0.2.14" tempfile = "3"
tempfile = "3.0.5" serde = { version = "1", features = ["derive"] }
serde = { version = "1.0.92", features = ["derive"] } toml = "0.5"
toml = "0.5.1" byte-unit = "4.0"
byte-unit = "3.0.1" nix = "0.19"
nix = "0.14.1" env_logger = "0.8"
env_logger = "0.6.2" pretty_env_logger = "0.4"
pretty_env_logger = "0.3.0" dialoguer = "0.7"
dialoguer = "0.4.0" console = "0.13"
anyhow = "1"

36
PKGBUILD Normal file
View 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
View File

@ -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. 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 ## Usage
### Image creation on removable device
``` shell ``` shell
sudo alma create /dev/disk/by-id/usb-Generic_USB_Flash_Disk-0:0 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 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. 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. 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 ## Presets
Reproducing a build can be easily done using a preset file. Presets file are simple TOML file which Reproducing a build can be easily done using a preset file.
contain a list of packages to install, a post-installation script and environment variables required
by the preset. See the presets directory for examples. 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 ## Similar projects
* [NomadBSD](http://nomadbsd.org/) * [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)

View File

@ -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
View File

@ -0,0 +1 @@
packages = ["alsa-firmware", "alsa-utils", "pavucontrol", "pulseaudio-bluetooth", "pulseaudio-ctl", "pulseaudio-zeroconf"]

2
presets/aur_example.toml Normal file
View File

@ -0,0 +1,2 @@
packages = ["clang"]
aur_packages = ["bat-cat-git"]

1
presets/base.toml Normal file
View 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
View 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"]

View File

@ -0,0 +1 @@
test

View File

@ -1 +0,0 @@
packages = ['arch-install-scripts']

View File

@ -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 = """ script = """
set -exu set -exu
systemctl enable sddm 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
View 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
"""

View File

@ -0,0 +1 @@
../base.toml

View File

@ -0,0 +1 @@
../xorg.toml

View File

@ -0,0 +1 @@
../user.toml

View File

@ -0,0 +1 @@
../kde.toml

View File

@ -0,0 +1 @@
../steam.toml

View File

@ -0,0 +1 @@
../audio.toml

1
presets/plasma/00-base.toml Symbolic link
View File

@ -0,0 +1 @@
../base.toml

1
presets/plasma/01-xorg.toml Symbolic link
View File

@ -0,0 +1 @@
../xorg.toml

1
presets/plasma/02-user.toml Symbolic link
View File

@ -0,0 +1 @@
../user.toml

1
presets/plasma/03-kde.toml Symbolic link
View File

@ -0,0 +1 @@
../kde.toml

View File

@ -0,0 +1 @@
../audio.toml

13
presets/steam.toml Normal file
View 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"]

View File

@ -2,8 +2,8 @@ packages = ["sudo"]
script = """ script = """
set -eux set -eux
useradd -m ${ALMA_USER} useradd -m ${ALMA_USER} -p $(openssl passwd -6 ${ALMA_USER_PASSWORD})
passwd ${ALMA_USER} usermod -G users,lp,video,network,storage,wheel,audio -a ${ALMA_USER}
usermod -G wheel -a ${ALMA_USER} echo "%wheel ALL=(ALL) ALL" > /etc/sudoers.d/wheel
""" """
environment_variables = ["ALMA_USER"] environment_variables = ["ALMA_USER", "ALMA_USER_PASSWORD"]

View 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"]

View 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

View File

@ -0,0 +1,2 @@
packages = ["xfce4-goodies", "xfce4-pulseaudio-plugin", "pulseaudio", "pavucontrol", "mugshot", "engrampa", "catfish", "firefox", "screenfetch", "thunderbird", "network-manager-applet"]

View File

@ -0,0 +1 @@
../base.toml

View File

@ -0,0 +1 @@
../xorg.toml

View File

@ -0,0 +1 @@
../user.toml

View File

@ -0,0 +1 @@
../xfce.toml

View File

@ -0,0 +1 @@
../xfce-goodies.toml

View File

@ -0,0 +1 @@
../xfce-branding.toml

View File

@ -0,0 +1 @@
../pamac.toml

View File

@ -0,0 +1 @@
../audio.toml

View File

@ -0,0 +1 @@
../xfce-branding

6
presets/xfce.toml Normal file
View 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
View File

@ -0,0 +1 @@
../base.toml

1
presets/xfce/01-xorg.toml Symbolic link
View File

@ -0,0 +1 @@
../xorg.toml

1
presets/xfce/02-user.toml Symbolic link
View File

@ -0,0 +1 @@
../user.toml

1
presets/xfce/03-xfce.toml Symbolic link
View File

@ -0,0 +1 @@
../xfce.toml

1
presets/xfce/04-audio.toml Symbolic link
View File

@ -0,0 +1 @@
../audio.toml

1
presets/xorg.toml Normal file
View File

@ -0,0 +1 @@
packages = ["xf86-input-libinput", "xf86-video-amdgpu", "xf86-video-ati", "xf86-video-nouveau", "xorg-server", "xterm"]

View File

@ -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
"""

View File

@ -1,7 +1,10 @@
use super::aur::AurHelper;
use byte_unit::Byte; use byte_unit::Byte;
use std::path::PathBuf; use std::path::PathBuf;
use structopt::StructOpt; use structopt::StructOpt;
/// Parse size argument as bytes
/// e.g. 10GB, 10GiB, etc.
fn parse_bytes(src: &str) -> Result<Byte, &'static str> { fn parse_bytes(src: &str) -> Result<Byte, &'static str> {
Byte::from_str(src).map_err(|_| "Invalid image size") Byte::from_str(src).map_err(|_| "Invalid image size")
} }
@ -35,10 +38,24 @@ pub struct CreateCommand {
#[structopt(parse(from_os_str))] #[structopt(parse(from_os_str))]
pub path: Option<PathBuf>, 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")] #[structopt(short = "p", long = "extra-packages", value_name = "package")]
pub extra_packages: Vec<String>, 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 /// Enter interactive chroot before unmounting the drive
#[structopt(short = "i", long = "interactive")] #[structopt(short = "i", long = "interactive")]
pub interactive: bool, 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 /// Create an image with a certain size in the given path instead of using an actual block device
#[structopt( #[structopt(
long = "image", long = "image",
parse(try_from_str = "parse_bytes"), parse(try_from_str = parse_bytes),
value_name = "size", value_name = "size",
requires = "path" requires = "path"
)] )]
pub image: Option<Byte>, pub image: Option<Byte>,
/// Overwrite existing image files. Use with caution /// Overwrite existing image files. Use with caution!
#[structopt(long = "overwrite")] #[structopt(long = "overwrite")]
pub overwrite: bool, 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)] #[derive(StructOpt)]
@ -71,6 +98,10 @@ pub struct ChrootCommand {
#[structopt(parse(from_os_str))] #[structopt(parse(from_os_str))]
pub block_device: PathBuf, 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 /// Optional command to run
#[structopt()] #[structopt()]
pub command: Vec<String>, pub command: Vec<String>,

60
src/aur.rs Normal file
View 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
View 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"];

View File

@ -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 }
}
}

View File

@ -2,27 +2,35 @@ use std::fmt::Write;
pub struct Initcpio { pub struct Initcpio {
encrypted: bool, encrypted: bool,
plymouth: bool,
} }
impl Initcpio { impl Initcpio {
pub fn new(encrypted: bool) -> Self { pub fn new(encrypted: bool, plymouth: bool) -> Self {
Self { encrypted } Self {
encrypted,
plymouth,
}
} }
pub fn to_config(&self) -> String { pub fn to_config(&self) -> anyhow::Result<String> {
let mut output = String::from( let mut output = String::from(
"MODULES=() "MODULES=()
BINARIES=() BINARIES=()
FILES=() FILES=()
HOOKS=(base udev keyboard consolefont block ", HOOKS=(base udev autodetect modconf kms keyboard keymap consolefont block ",
); );
if self.encrypted { 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)
} }
} }

View File

@ -1,69 +1,62 @@
mod args; mod args;
mod error; mod aur;
mod constants;
mod initcpio; mod initcpio;
mod presets; mod presets;
mod process; mod process;
mod storage; mod storage;
mod tool; mod tool;
use crate::args::*; use anyhow::{anyhow, Context};
use crate::error::*; use args::Command;
use crate::process::CommandExt;
use crate::storage::*;
use crate::tool::Tool;
use byte_unit::Byte; use byte_unit::Byte;
use console::style;
use dialoguer::{theme::ColorfulTheme, Select}; use dialoguer::{theme::ColorfulTheme, Select};
use failure::{Fail, ResultExt};
use log::{debug, error, info, log_enabled, Level, LevelFilter}; use log::{debug, error, info, log_enabled, Level, LevelFilter};
use pretty_env_logger; use process::CommandExt;
use std::collections::HashSet; use std::collections::HashSet;
use std::fs; use std::fs;
use std::io::Write; 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::path::{Path, PathBuf};
use std::process::{exit, Command as ProcessCommand}; use std::process::Command as ProcessCommand;
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
use storage::EncryptedDevice;
use storage::{BlockDevice, Filesystem, FilesystemType, LoopDevice, MountStack};
use structopt::StructOpt; use structopt::StructOpt;
use tempfile::tempdir; use tempfile::tempdir;
use tool::Tool;
const BOOT_PARTITION_INDEX: u8 = 1; fn main() -> anyhow::Result<()> {
const ROOT_PARTITION_INDEX: u8 = 3; // Get struct of args using structopt
let app = args::App::from_args();
static JOURNALD_CONF: &'static str = " // Set up logging
[Journal] let mut builder = pretty_env_logger::formatted_timed_builder();
Storage=volatile let log_level = if app.verbose {
SystemMaxUse=16M LevelFilter::Debug
"; } else {
LevelFilter::Info
};
builder.filter_level(log_level);
builder.init();
fn mount<'a>( // Match command from arguments and run relevant code
mount_path: &Path, match app.cmd {
boot_filesystem: &'a Filesystem, Command::Create(command) => create(command),
root_filesystem: &'a Filesystem, Command::Chroot(command) => tool::chroot(command),
) -> Result<MountStack<'a>, Error> { Command::Qemu(command) => tool::qemu(command),
let mut mount_stack = MountStack::new(); }?;
debug!(
"Root partition: {}",
root_filesystem.block().path().display()
);
info!("Mounting filesystems to {}", mount_path.display()); Ok(())
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)
} }
/// 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 { fn fix_fstab(fstab: &str) -> String {
fstab fstab
.lines() .lines()
@ -72,7 +65,8 @@ fn fix_fstab(fstab: &str) -> String {
.join("\n") .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(); let mut options = fs::OpenOptions::new();
@ -82,35 +76,45 @@ fn create_image(path: &Path, size: Byte, overwrite: bool) -> Result<LoopDevice,
} else { } else {
options.create_new(true); 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) file.set_len(size.get_bytes() as u64)
.context(ErrorKind::Image)?; .context("Error creating the image")?;
} }
LoopDevice::create(path) LoopDevice::create(path)
} }
fn select_block_device() -> Result<PathBuf, Error> { /// Requests selection of block device (no device was given in the arguments)
let devices = get_removable_devices()?; fn select_block_device(allow_non_removable: bool) -> anyhow::Result<PathBuf> {
let devices = storage::get_storage_devices(allow_non_removable)?;
if devices.is_empty() { 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()) let selection = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Select a removable device") .with_prompt("Select a removable device")
.default(0) .default(0)
.items(&devices) .items(&devices)
.interact() .interact()?;
.unwrap();
Ok(PathBuf::from("/dev").join(&devices[selection].name)) Ok(PathBuf::from("/dev").join(&devices[selection].name))
} }
#[allow(clippy::cognitive_complexity)] /// Creates the installation
fn create(command: CreateCommand) -> Result<(), Error> { #[allow(clippy::cognitive_complexity)] // TODO: Split steps into functions and remove this
let presets = presets::Presets::load(&command.presets)?; fn create(command: args::CreateCommand) -> anyhow::Result<()> {
let presets = presets::PresetsCollection::load(&command.presets)?;
let sgdisk = Tool::find("sgdisk")?; let sgdisk = Tool::find("sgdisk")?;
let pacstrap = Tool::find("pacstrap")?; 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 { let storage_device_path = if let Some(path) = command.path {
path path
} else { } else {
select_block_device()? select_block_device(command.allow_non_removable)?
}; };
let image_loop = if let Some(size) = command.image { let image_loop = if let Some(size) = command.image {
@ -149,38 +153,42 @@ fn create(command: CreateCommand) -> Result<(), Error> {
loop_dev.path() loop_dev.path()
}) })
.unwrap_or(&storage_device_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(); let disk_path = storage_device.path();
info!("Partitioning the block device"); info!("Partitioning the block device");
debug!("{:?}", disk_path); debug!("{:?}", disk_path);
let boot_size = command.boot_size.unwrap_or(300);
sgdisk sgdisk
.execute() .execute()
.args(&[ .args([
"-Z", "-Z",
"-o", "-o",
"--new=1::+100M", &format!("--new=1::+{}M", boot_size),
"--new=2::+1M", "--new=2::+1M",
"--largest-new=3", "--largest-new=3",
"--typecode=1:EF00", "--typecode=1:EF00",
"--typecode=2:EF02", "--typecode=2:EF02",
]) ])
.arg(&disk_path) .arg(disk_path)
.run(ErrorKind::Partitioning)?; .run()
.context("Partitioning error")?;
thread::sleep(Duration::from_millis(1000)); thread::sleep(Duration::from_millis(1000));
info!("Formatting filesystems"); 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 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 { let encrypted_root = if let Some(cryptsetup) = &cryptsetup {
info!("Encrypting the root filesystem"); info!("Encrypting the root filesystem");
EncryptedDevice::prepare(&cryptsetup, &root_partition_base)?; EncryptedDevice::prepare(cryptsetup, &root_partition_base)?;
Some(EncryptedDevice::open( Some(EncryptedDevice::open(
cryptsetup, cryptsetup,
&root_partition_base, &root_partition_base,
@ -191,14 +199,14 @@ fn create(command: CreateCommand) -> Result<(), Error> {
}; };
let root_partition = if let Some(e) = encrypted_root.as_ref() { let root_partition = if let Some(e) = encrypted_root.as_ref() {
e as &BlockDevice e as &dyn BlockDevice
} else { } else {
&root_partition_base as &BlockDevice &root_partition_base as &dyn BlockDevice
}; };
let root_filesystem = Filesystem::format(root_partition, FilesystemType::Ext4, &mkext4)?; 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) { if log_enabled!(Level::Debug) {
debug!("lsblk:"); debug!("lsblk:");
@ -212,78 +220,59 @@ fn create(command: CreateCommand) -> Result<(), Error> {
.ok(); .ok();
} }
let mut packages: HashSet<String> = [ let mut packages: HashSet<String> = constants::BASE_PACKAGES
"base",
"grub",
"efibootmgr",
"intel-ucode",
"networkmanager",
"broadcom-wl",
]
.iter() .iter()
.map(|s| String::from(*s)) .map(|s| String::from(*s))
.collect(); .collect();
packages.extend(presets.packages); 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"); info!("Bootstrapping system");
pacstrap pacstrap
.execute() .execute()
.arg("-C")
.arg(&pacman_conf_path)
.arg("-c") .arg("-c")
.arg(mount_point.path()) .arg(mount_point.path())
.args(packages) .args(packages)
.args(&command.extra_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( let fstab = fix_fstab(
&genfstab &genfstab
.execute() .execute()
.arg("-U") .arg("-U")
.arg(mount_point.path()) .arg(mount_point.path())
.run_text_output(ErrorKind::Fstab)?, .run_text_output()
.context("fstab error")?,
); );
debug!("fstab:\n{}", fstab); debug!("fstab:\n{}", fstab);
fs::write(mount_point.path().join("etc/fstab"), fstab).context(ErrorKind::Fstab)?; fs::write(mount_point.path().join("etc/fstab"), fstab).context("fstab error")?;
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");
arch_chroot arch_chroot
.execute() .execute()
.arg(mount_point.path()) .arg(mount_point.path())
.args(&["systemctl", "enable", "NetworkManager"]) .args(["passwd", "-d", "root"])
.run(ErrorKind::PostInstallation)?; .run()
.context("Failed to delete the root password")?;
info!("Configuring journald");
fs::write(
mount_point.path().join("etc/systemd/journald.conf"),
JOURNALD_CONF,
)
.context(ErrorKind::PostInstallation)?;
info!("Setting locale"); info!("Setting locale");
fs::OpenOptions::new() fs::OpenOptions::new()
@ -291,62 +280,214 @@ fn create(command: CreateCommand) -> Result<(), Error> {
.write(true) .write(true)
.open(mount_point.path().join("etc/locale.gen")) .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")) .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( fs::write(
mount_point.path().join("etc/locale.conf"), mount_point.path().join("etc/locale.conf"),
"LANG=en_US.UTF-8", "LANG=en_US.UTF-8",
) )
.context(ErrorKind::Locale)?; .context("Failed to write to locale.conf")?;
arch_chroot arch_chroot
.execute() .execute()
.arg(mount_point.path()) .arg(mount_point.path())
.arg("locale-gen") .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 arch_chroot
.execute() .execute()
.arg(mount_point.path()) .arg(mount_point.path())
.args(&["mkinitcpio", "-p", "linux"]) .args(["useradd", "-m", "aur"])
.run(ErrorKind::Initramfs)?; .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() { if encrypted_root.is_some() {
debug!("Setting up GRUB for an encrypted root partition"); debug!("Setting up GRUB for an encrypted root partition");
let uuid = blkid let uuid = blkid
.unwrap() .expect("No tool for blkid")
.execute() .execute()
.arg(root_partition_base.path()) .arg(root_partition_base.path())
.args(&["-o", "value", "-s", "UUID"]) .args(["-o", "value", "-s", "UUID"])
.run_text_output(ErrorKind::Partitioning)?; .run_text_output()
.context("Failed to run blkid")?;
let trimmed = uuid.trim(); let trimmed = uuid.trim();
debug!("Root partition UUID: {}", trimmed); debug!("Root partition UUID: {}", trimmed);
let mut grub_file = fs::OpenOptions::new() let mut grub_file = fs::OpenOptions::new()
.append(true) .append(true)
.open(mount_point.path().join("etc/default/grub")) .open(mount_point.path().join("etc/default/grub"))
.context(ErrorKind::Bootloader)?; .context("Failed to create /etc/default/grub")?;
write!( write!(
&mut grub_file, &mut grub_file,
"GRUB_CMDLINE_LINUX=\"cryptdevice=UUID={}:luks_root\"", "GRUB_CMDLINE_LINUX=\"cryptdevice=UUID={}:luks_root\"",
trimmed trimmed
) )
.context(ErrorKind::Bootloader)?; .context("Failed to write to /etc/default/grub")?;
} }
info!("Installing the Bootloader"); info!("Installing the Bootloader");
arch_chroot arch_chroot
.execute() .execute()
.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_path.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_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!( debug!(
"GRUB configuration: {}", "GRUB configuration: {}",
@ -359,7 +500,8 @@ fn create(command: CreateCommand) -> Result<(), Error> {
arch_chroot arch_chroot
.execute() .execute()
.arg(mount_point.path()) .arg(mount_point.path())
.run(ErrorKind::Interactive)?; .run()
.context("Failed to enter interactive chroot")?;
} }
info!("Unmounting filesystems"); info!("Unmounting filesystems");
@ -367,116 +509,3 @@ fn create(command: CreateCommand) -> Result<(), Error> {
Ok(()) 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);
}
}
}

View File

@ -1,67 +1,158 @@
use crate::error::{Error, ErrorKind}; use anyhow::{anyhow, Context};
use failure::ResultExt;
use serde::Deserialize; use serde::Deserialize;
use std::collections::HashSet; use std::collections::HashSet;
use std::env; use std::env;
use std::fs; use std::fs;
use std::io;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use toml;
#[derive(Deserialize)] #[derive(Deserialize)]
struct Preset { struct Preset {
packages: Option<Vec<String>>, packages: Option<Vec<String>>,
script: Option<String>, script: Option<String>,
environment_variables: Option<Vec<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 { impl Preset {
fn load(path: &Path) -> Result<Self, Error> { fn load(path: &Path) -> anyhow::Result<Self> {
let data = fs::read_to_string(path) let data = fs::read_to_string(path).with_context(|| format!("{}", path.display()))?;
.with_context(|_| ErrorKind::Preset(format!("{}", path.display())))?; toml::from_str(&data).with_context(|| format!("{}", path.display()))
Ok(toml::from_str(&data) }
.with_context(|_| ErrorKind::Preset(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 struct Script {
pub packages: HashSet<String>, pub script_text: String,
pub scripts: Vec<String>, pub shared_dirs: Option<Vec<PathBuf>>,
} }
impl Presets { pub struct PresetsCollection {
pub fn load(list: &[PathBuf]) -> Result<Self, Error> { 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 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(); let mut environment_variables = HashSet::new();
for preset in list { for preset in list {
let Preset { if preset.is_dir() {
script, // Build vector of paths to files, then sort by path name
packages: preset_packages, // Recursively load directories of preset files
environment_variables: preset_environment_variables, let mut dir_paths: Vec<PathBuf> = Vec::new();
} = Preset::load(&preset)?; visit_dirs(preset, &mut dir_paths)
.with_context(|| format!("{}", preset.display()))?;
if let Some(preset_packages) = preset_packages { // Order not guaranteed so we sort
packages.extend(preset_packages); // 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 {
if let Some(preset_environment_variables) = preset_environment_variables { Preset::load(preset)?.process(
environment_variables.extend(preset_environment_variables); &mut packages,
&mut scripts,
&mut environment_variables,
preset,
&mut aur_packages,
)?;
} }
scripts.extend(script);
} }
let missing_envrionments: Vec<String> = environment_variables let missing_envrionments: Vec<String> = environment_variables
.into_iter() .into_iter()
.filter(|var| env::var(var).is_err()) .filter(|var| env::var(var).is_err())
.collect(); .collect();
if !missing_envrionments.is_empty() { 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,
})
} }
} }

View File

@ -1,51 +1,35 @@
use super::error::*; use anyhow::anyhow;
use failure::{Fail, ResultExt};
use log::error; use log::error;
use std::process::{Command, ExitStatus}; use std::process::Command;
use std::str; 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 { pub trait CommandExt {
fn run(&mut self, context: ErrorKind) -> Result<(), Error>; fn run(&mut self) -> anyhow::Result<()>;
fn run_text_output(&mut self, context: ErrorKind) -> Result<String, Error>; fn run_text_output(&mut self) -> anyhow::Result<String>;
} }
impl CommandExt for Command { impl CommandExt for Command {
fn run(&mut self, context: ErrorKind) -> Result<(), Error> { fn run(&mut self) -> anyhow::Result<()> {
let exit_status = self let exit_status = self.spawn()?.wait()?;
.spawn()
.with_context(|_| context.clone())?
.wait()
.with_context(|_| context.clone())?;
if !exit_status.success() { if !exit_status.success() {
Err(ProcessError::BadExitCode(exit_status)).with_context(|_| context.clone())?; return Err(anyhow!("Bad exit code: {}", exit_status));
} }
Ok(()) Ok(())
} }
fn run_text_output(&mut self, context: ErrorKind) -> Result<String, Error> { fn run_text_output(&mut self) -> anyhow::Result<String> {
let output = self.output().with_context(|_| context.clone())?; let output = self.output()?;
if !output.status.success() { if !output.status.success() {
let error = str::from_utf8(&output.stderr).unwrap_or("[INVALID UTF8]"); let error = str::from_utf8(&output.stderr).unwrap_or("[INVALID UTF8]");
error!("{}", error); error!("{}", error);
Err(ProcessError::BadExitCode(output.status)).with_context(|_| context.clone())?; return Err(anyhow!("Bad exit code: {}", output.status));
} }
Ok(String::from( Ok(String::from(str::from_utf8(&output.stdout).map_err(
str::from_utf8(&output.stdout) |_| anyhow!("Process output is not valid UTF-8"),
.map_err(|_| ProcessError::InvalidUtf8) )?))
.with_context(|_| context.clone())?,
))
} }
} }

View File

@ -1,43 +1,43 @@
use super::markers::BlockDevice; use super::markers::BlockDevice;
use crate::error::{Error, ErrorKind};
use crate::process::CommandExt; use crate::process::CommandExt;
use crate::tool::Tool; use crate::tool::Tool;
use failure::ResultExt; use anyhow::Context;
use log::{debug, warn}; use log::{debug, warn};
use std::fs; use std::fs;
use std::io::Read; use std::io::Read;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
static LUKS_MAGIC_1: &'static [u8] = &[0x4c, 0x55, 0x4b, 0x53, 0xba, 0xbe]; static LUKS_MAGIC_1: &[u8] = &[0x4c, 0x55, 0x4b, 0x53, 0xba, 0xbe];
static LUKS_MAGIC_2: &'static [u8] = &[0x53, 0x4b, 0x55, 0x4c, 0xba, 0xbe]; static LUKS_MAGIC_2: &[u8] = &[0x53, 0x4b, 0x55, 0x4c, 0xba, 0xbe];
#[derive(Debug)] #[derive(Debug)]
pub struct EncryptedDevice<'t, 'o> { pub struct EncryptedDevice<'t, 'o> {
cryptsetup: &'t Tool, cryptsetup: &'t Tool,
name: String, name: String,
path: PathBuf, path: PathBuf,
origin: PhantomData<&'o BlockDevice>, origin: PhantomData<&'o dyn BlockDevice>,
} }
impl<'t, 'o> EncryptedDevice<'t, 'o> { 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()); debug!("Preparing encrypted device in {}", device.path().display());
cryptsetup cryptsetup
.execute() .execute()
.arg("luksFormat") .arg("luksFormat")
.arg("-q") .arg("-q")
.arg(device.path()) .arg(device.path())
.run(ErrorKind::LuksSetup)?; .run()
.context("Error setting up an encrypted device")?;
Ok(()) Ok(())
} }
pub fn open( pub fn open(
cryptsetup: &'t Tool, cryptsetup: &'t Tool,
device: &'o BlockDevice, device: &'o dyn BlockDevice,
name: String, name: String,
) -> Result<EncryptedDevice<'t, 'o>, Error> { ) -> anyhow::Result<EncryptedDevice<'t, 'o>> {
debug!( debug!(
"Opening encrypted device {} as {}", "Opening encrypted device {} as {}",
device.path().display(), device.path().display(),
@ -48,7 +48,8 @@ impl<'t, 'o> EncryptedDevice<'t, 'o> {
.arg("open") .arg("open")
.arg(device.path()) .arg(device.path())
.arg(&name) .arg(&name)
.run(ErrorKind::LuksOpen)?; .run()
.context("Error opening the encrypted device")?;
let path = PathBuf::from("/dev/mapper").join(&name); let path = PathBuf::from("/dev/mapper").join(&name);
Ok(Self { 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); debug!("Closing encrypted device {}", self.name);
self.cryptsetup self.cryptsetup
.execute() .execute()
.arg("close") .arg("close")
.arg(&self.name) .arg(&self.name)
.run(ErrorKind::LuksClose)?; .run()
.context("Error closing the encrypted device")?;
Ok(()) 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() let mut f = fs::OpenOptions::new()
.read(true) .read(true)
.write(false) .write(false)
.open(device.path()) .open(device.path())
.context(ErrorKind::LuksDetection)?; .context("Error detecting whether the root partition is an encrypted device")?;
let mut buffer = [0; 6]; let mut buffer = [0; 6];
f.read_exact(&mut buffer) 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) Ok(buffer == LUKS_MAGIC_1 || buffer == LUKS_MAGIC_2)
} }

View File

@ -1,9 +1,6 @@
use super::markers::BlockDevice; use super::markers::BlockDevice;
use crate::{ use crate::{process::CommandExt, tool::Tool};
error::{Error, ErrorKind}, use anyhow::Context;
process::CommandExt,
tool::Tool,
};
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum FilesystemType { pub enum FilesystemType {
@ -23,31 +20,31 @@ impl FilesystemType {
#[derive(Debug)] #[derive(Debug)]
pub struct Filesystem<'a> { pub struct Filesystem<'a> {
fs_type: FilesystemType, fs_type: FilesystemType,
block: &'a BlockDevice, block: &'a dyn BlockDevice,
} }
impl<'a> Filesystem<'a> { impl<'a> Filesystem<'a> {
pub fn format( pub fn format(
block: &'a BlockDevice, block: &'a dyn BlockDevice,
fs_type: FilesystemType, fs_type: FilesystemType,
mkfs: &Tool, mkfs: &Tool,
) -> Result<Self, Error> { ) -> anyhow::Result<Self> {
let mut command = mkfs.execute(); let mut command = mkfs.execute();
match fs_type { match fs_type {
FilesystemType::Ext4 => command.arg("-F").arg(block.path()), FilesystemType::Ext4 => command.arg("-F").arg(block.path()),
FilesystemType::Vfat => command.arg("-F32").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 }) 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 } Self { fs_type, block }
} }
pub fn block(&self) -> &BlockDevice { pub fn block(&self) -> &dyn BlockDevice {
self.block self.block
} }

View File

@ -1,6 +1,5 @@
use crate::error::{Error, ErrorKind};
use crate::tool::Tool; use crate::tool::Tool;
use failure::ResultExt; use anyhow::{anyhow, Context};
use log::info; use log::info;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@ -11,25 +10,27 @@ pub struct LoopDevice {
} }
impl 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 losetup = Tool::find("losetup")?;
let output = losetup let output = losetup
.execute() .execute()
.args(&["--find", "-P", "--show"]) .args(["--find", "-P", "--show"])
.arg(file) .arg(file)
.output() .output()
.context(ErrorKind::Image)?; .context("Error creating the image")?;
if !output.status.success() { if !output.status.success() {
Err(ErrorKind::Losetup( return Err(anyhow!(String::from_utf8(output.stderr)?));
String::from_utf8(output.stderr).unwrap(),
))?
} }
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()); info!("Mounted {} to {}", file.display(), path.display());
Ok(LoopDevice { path, losetup }) Ok(Self { path, losetup })
} }
pub fn path(&self) -> &Path { pub fn path(&self) -> &Path {
@ -45,7 +46,7 @@ impl Drop for LoopDevice {
.arg("-d") .arg("-d")
.arg(&self.path) .arg(&self.path)
.spawn() .spawn()
.unwrap() .expect("Failed to spawn command to detach loop device")
.wait() .wait()
.ok(); .ok();
} }

View File

@ -1,5 +1,5 @@
use std::path::Path; use std::path::Path;
// Marker traits
pub trait BlockDevice: std::fmt::Debug { pub trait BlockDevice: std::fmt::Debug {
fn path(&self) -> &Path; fn path(&self) -> &Path;
} }

View File

@ -12,5 +12,5 @@ pub use filesystem::{Filesystem, FilesystemType};
pub use loop_device::LoopDevice; pub use loop_device::LoopDevice;
pub use markers::BlockDevice; pub use markers::BlockDevice;
pub use mount_stack::MountStack; pub use mount_stack::MountStack;
pub use removeable_devices::get_removable_devices; pub use removeable_devices::get_storage_devices;
pub use storage_device::StorageDevice; pub use storage_device::StorageDevice;

View File

@ -1,8 +1,6 @@
use super::Filesystem; use super::Filesystem;
use crate::error::{Error, ErrorKind}; use anyhow::anyhow;
use failure::Fail;
use log::{debug, warn}; use log::{debug, warn};
use nix;
use nix::mount::{mount, umount, MsFlags}; use nix::mount::{mount, umount, MsFlags};
use std::marker::PhantomData; use std::marker::PhantomData;
use std::path::PathBuf; use std::path::PathBuf;
@ -20,7 +18,6 @@ impl<'a> MountStack<'a> {
} }
} }
#[must_use]
pub fn mount( pub fn mount(
&mut self, &mut self,
filesystem: &'a Filesystem, filesystem: &'a Filesystem,
@ -40,21 +37,43 @@ impl<'a> MountStack<'a> {
Ok(()) 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(()); let mut result = Ok(());
while let Some(target) = self.targets.pop() { while let Some(target) = self.targets.pop() {
debug!("Unmounting {}", target.display()); debug!("Unmounting {}", target.display());
if let Err(e) = umount(&target) { if let Err(e) = umount(&target) {
warn!("Unable to umount {}: {}", target.display(), e); 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 result
} }
pub fn umount(mut self) -> Result<(), Error> { pub fn umount(mut self) -> anyhow::Result<()> {
self._umount() self._umount()
} }
} }

View File

@ -5,7 +5,7 @@ use std::path::{Path, PathBuf};
#[derive(Debug)] #[derive(Debug)]
pub struct Partition<'a> { pub struct Partition<'a> {
path: PathBuf, path: PathBuf,
origin: PhantomData<&'a Origin>, origin: PhantomData<&'a dyn Origin>,
} }
impl<'a> Partition<'a> { impl<'a> Partition<'a> {

View File

@ -1,6 +1,5 @@
use crate::error::{Error, ErrorKind}; use anyhow::Context;
use byte_unit::Byte; use byte_unit::Byte;
use failure::ResultExt;
use std::{fmt, fs}; use std::{fmt, fs};
#[derive(Debug)] #[derive(Debug)]
@ -27,22 +26,24 @@ fn trimmed(source: String) -> String {
String::from(source.trim_end()) 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(); let mut result = Vec::new();
for entry in fs::read_dir("/sys/block").context(ErrorKind::RemoveableDevicesQuery)? { for entry in fs::read_dir("/sys/block").context("Error querying storage devices")? {
let entry = entry.context(ErrorKind::RemoveableDevicesQuery)?; let entry = entry.context("Error querying storage devices")?;
let removable = fs::read_to_string(entry.path().join("removable")) let removable = allow_non_removable
.context(ErrorKind::RemoveableDevicesQuery)?; || 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; continue;
} }
let model = fs::read_to_string(entry.path().join("device/model")) let model = fs::read_to_string(entry.path().join("device/model"))
.map(trimmed) .map(trimmed)
.context(ErrorKind::RemoveableDevicesQuery)?; .context("Error querying storage devices")?;
if model == "CD-ROM" { if model == "CD-ROM" {
continue; continue;
@ -52,22 +53,22 @@ pub fn get_removable_devices() -> Result<Vec<Device>, Error> {
name: entry name: entry
.path() .path()
.file_name() .file_name()
.unwrap() .expect("Could not get file name for dir entry /sys/block")
.to_string_lossy() .to_string_lossy()
.into_owned(), .into_owned(),
model, model,
vendor: fs::read_to_string(entry.path().join("device/vendor")) vendor: fs::read_to_string(entry.path().join("device/vendor"))
.map(trimmed) .map(trimmed)
.context(ErrorKind::RemoveableDevicesQuery)?, .context("Error querying storage devices")?,
size: Byte::from_bytes( size: Byte::from_bytes(
fs::read_to_string(entry.path().join("size")) fs::read_to_string(entry.path().join("size"))
.context(ErrorKind::RemoveableDevicesQuery)? .context("Error querying storage devices")?
.trim() .trim()
.parse::<u128>() .parse::<u128>()
.unwrap() .context("Could not parse block size to unsigned integer (u128)")?
* 512, * 512,
), ),
}) });
} }
Ok(result) Ok(result)
@ -79,7 +80,7 @@ mod tests {
#[test] #[test]
fn sanity() { fn sanity() {
let devices = get_removable_devices().unwrap(); let devices = get_storage_devices(false).expect("No devices");
println!("{:?}", devices); println!("{:?}", devices);
} }
} }

View File

@ -1,7 +1,6 @@
use super::markers::{BlockDevice, Origin}; use super::markers::{BlockDevice, Origin};
use super::partition::Partition; use super::partition::Partition;
use crate::error::{Error, ErrorKind}; use anyhow::{anyhow, Context};
use failure::ResultExt;
use log::debug; use log::debug;
use std::fs::read_to_string; use std::fs::read_to_string;
use std::marker::PhantomData; use std::marker::PhantomData;
@ -11,18 +10,20 @@ use std::path::{Path, PathBuf};
pub struct StorageDevice<'a> { pub struct StorageDevice<'a> {
name: String, name: String,
path: PathBuf, path: PathBuf,
origin: PhantomData<&'a Origin>, origin: PhantomData<&'a dyn Origin>,
} }
impl<'a> StorageDevice<'a> { 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); 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 let device_name = path
.file_name() .file_name()
.and_then(|s| s.to_str()) .and_then(std::ffi::OsStr::to_str)
.map(String::from) .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); debug!("real path: {:?}, device name: {:?}", path, device_name);
@ -31,8 +32,14 @@ impl<'a> StorageDevice<'a> {
path, path,
origin: PhantomData, 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) Ok(_self)
@ -44,12 +51,13 @@ impl<'a> StorageDevice<'a> {
path path
} }
fn is_removable_device(&self) -> Result<bool, Error> { fn is_removable_device(&self) -> anyhow::Result<bool> {
let mut path = self.sys_path(); let mut path = self.sys_path();
path.push("removable"); path.push("removable");
debug!("Reading: {:?}", path); 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); debug!("{:?} -> {}", path, result);
Ok(result == "1\n") Ok(result == "1\n")
@ -61,8 +69,15 @@ impl<'a> StorageDevice<'a> {
path.exists() path.exists()
} }
pub fn get_partition(&self, index: u8) -> Result<Partition, Error> { pub fn get_partition(&self, index: u8) -> anyhow::Result<Partition> {
let name = if self.name.chars().rev().next().unwrap().is_digit(10) { let name = if self
.name
.chars()
.rev()
.next()
.expect("Storage device name is empty")
.is_ascii_digit()
{
format!("{}p{}", self.name, index) format!("{}p{}", self.name, index)
} else { } else {
format!("{}{}", self.name, index) format!("{}{}", self.name, index)
@ -72,7 +87,7 @@ impl<'a> StorageDevice<'a> {
debug!("Partition {} for {} is in {:?}", index, self.name, path); debug!("Partition {} for {} is in {:?}", index, self.name, path);
if !path.exists() { if !path.exists() {
return Err(ErrorKind::NoSuchPartition(index).into()); return Err(anyhow!("Partition {} does not exist", index));
} }
Ok(Partition::new::<Self>(path)) Ok(Partition::new::<Self>(path))
} }

75
src/tool/chroot.rs Normal file
View 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(())
}

View File

@ -1,5 +1,12 @@
use super::error::*; mod chroot;
use failure::ResultExt; 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::path::PathBuf;
use std::process::Command; use std::process::Command;
use which::which; use which::which;
@ -10,9 +17,9 @@ pub struct Tool {
} }
impl Tool { impl Tool {
pub fn find(name: &'static str) -> Result<Self, Error> { pub fn find(name: &'static str) -> anyhow::Result<Self> {
Ok(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
View 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
View 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")?
}