libdevuansdk/zlibs/imaging

153 lines
3.5 KiB
Bash

#!/usr/bin/env zsh
#
# Copyright (c) 2016 Dyne.org Foundation
# libdevuansdk is written and maintained by
# Jaromil <jaromil@dyne.org>
# KatolaZ <katolaz@freaknet.org>
# parazyd <parazyd@dyne.org>
#
# This file is part of libdevuansdk
#
# This source code is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this source code. If not, see <http://www.gnu.org/licenses/>.
## Imaging
vars+=(imgname imgpath)
img_mkimage() {
fn img_mkimage $@
imgpath=${strapdir}.img
local mbrtype="$1"
req=(imgpath imgsize mbrtype)
ckreq || return 1
imgname=`basename ${imgpath}`
notice "Creating raw image..."
silly
dd if=/dev/zero \
of="${imgpath}" \
bs=1M count=${imgsize}
if [[ $mbrtype == "dos" ]]; then
img_partition_dos
elif [[ $mbrtype == "gpt" ]]; then
img_partition_gpt
else
error "No valid MBR type specified..."
zerr; zshexit
fi
img_mount
img_rsync_strapdir
img_install_bootloader
img_umount
}
img_partition_dos() {
fn img_partition_dos
req=(imgname imgpath)
ckreq || return 1
notice "Partitioning with dos"
silly
parted_boot=${parted_boot:-ext2 0 64}
parted_root=${parted_root:-ext4 64 -1}
pushd ${workdir}
parted ${imgname} --script -- mklabel msdos
parted ${imgname} --script -- mkpart primary ${dos_boot_size}
parted ${imgname} --script -- mkpart primary ${dos_root_size}
# setup loopdevice and mappdevice (zlibs/helpers)
findloopmapp
notice "Formatting partitions..."
sudo mkfs.ext2 ${bootpart}
sudo mkfs.ext4 ${rootpart}
popd
}
img_partition_gpt() {
fn img_partition_gpt
req=(imgname imgpath)
ckreq || return 1
notice "Partitioning with gpt"
silly
pushd ${workdir}
parted ${imgname} --script -- mklabel gpt
cgpt create -z ${imgname}
cgpt create ${imgname}
cgpt add -i 1 -t kernel -b 8192 -s 32768 -l kernel -S 1 -T 5 -P 10 ${imgname}
cgpt add -i 2 -t data -b 40960 -s `expr $(cgpt show ${imgname} \
| awk '/Sec GPT table/ {print \$1}') - 40960` -l Root ${imgname}
# setup loopdevice and mappdevice (zlibs/helpers)
findloopmapp
notice "Formatting partitions..."
sudo mkfs.ext2 -L bootfs $bootpart
sudo mkfs.ext4 -L rootfs $rootpart
popd
}
img_rsync_strapdir() {
fn img_rsync_strapdir
req=(workdir strapdir)
ckreq || return 1
notice "Rsyncing strapdir to raw image..."
silly
sudo rsync -HPavz -q ${strapdir}/* ${workdir}/rootp
}
img_install_bootloader() {
fn img_install_bootloader
conf_install_kernel ${workdir}/rootp
conf_install_grub ${workdir}/rootp
}
img_mount() {
fn img_mount
req=(bootpart rootpart workdir)
ckreq || return 1
mkdir -p ${workdir}/rootp
sudo mount ${rootpart} ${workdir}/rootp && act "mounted root partition"
sudo mkdir -p ${workdir}/rootp/{boot,dev,proc,sys}
sudo mount ${bootpart} ${workdir}/rootp/boot && act "mounted boot partition"
mountdevprocsys ${workdir}/rootp
}
img_umount() {
fn img_umount
req=(bootpart rootpart workdir)
ckreq || return 1
umountdevprocsys ${workdir}/rootp
sudo umount ${workdir}/rootp/boot && act "umounted boot partition"
sudo umount ${workdir}/rootp && act "umounted root partition"
}