traw image operations - libdevuansdk - common library for devuan's simple distro kits
 (HTM) git clone https://git.parazyd.org/libdevuansdk
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Submodules
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 4e4f10ef42735c252d7ffc7fee7f3fb5b46863ed
 (DIR) parent 8e1d14f13f2b0ab69401437f52b8f22c66548be4
 (HTM) Author: parazyd <parazyd@dyne.org>
       Date:   Wed,  8 Jun 2016 18:29:00 +0200
       
       raw image operations
       
       Diffstat:
         M config                              |       6 +++++-
         M doc/README-functions.md             |      26 ++++++++++++++++++++++++--
         M libdevuansdk                        |       1 +
         M zlibs/helpers                       |      58 ++++++++++++++++++++++++++++++
         A zlibs/imaging                       |     109 +++++++++++++++++++++++++++++++
       
       5 files changed, 197 insertions(+), 3 deletions(-)
       ---
 (DIR) diff --git a/config b/config
       t@@ -49,10 +49,14 @@ base_packages=(bzip2 dialog apt-utils fakeroot e2fsprogs parted)
        # other system packages
        system_packages=(sysvinit ssh)
        
       +# size of raw image file in MB
       +imgsize=1337
       +
        ## libdevuansdk-specific settings
        workdir="$H/workdir"
       -strapdir="${workdir}/${os}-${release}-${version}-${arch}"
       +strapdir="${workdir}/${os}-${release}-${version}"
        
        # declare zuper globals
        vars+=(os release version mirror section)
        vars+=(workdir strapdir)
       +vars+=(imgsize)
 (DIR) diff --git a/doc/README-functions.md b/doc/README-functions.md
       t@@ -7,8 +7,6 @@ libdevuansdk functions
        
        Main function, goes through stages 1, 2, and 3 of debootstrap.
        
       -Other internal functions are prefixed with `config_` and `tar_`
       -
        ### bootstrap_config_cleanup() ###
        
        Final cleanup of the rootfs.
       t@@ -25,6 +23,23 @@ Make a tarball of a base working system, ready to be worked on later.
        
        Unpack the tarball of a base working system to the strapdir.
        
       +# zlibs/imaging
       +
       +## img_mkimage() ##
       +Uses dd to dump zeroes into a raw .img of the preconfigured size.
       +
       +## img_partimage_dos() ##
       +Partitions the raw image into dos format and formats (boot=ext2; root=ext4)
       +
       +## img_partimage_gpt() ##
       +Partitions the raw image into gpt format and formats (boot=ext2; root=ext4)
       +
       +## img_mountimage() ##
       +Mounts the root and boot partitions in `$workdir/rootp` in order to work on it.
       +
       +## img_umountimage() ##
       +Undoes the above function.
       +
        # zlibs/helpers
        
        ## escalate()
       t@@ -35,6 +50,10 @@ arguments, user and command in the style of:
        escalate root "chroot /somewhere/where/i/want/to"
        ```
        
       +## findloopmapp()
       +For the raw image. Finds a free loopdevice and makes a /dev/mapper device which
       +is then kpartx-ed to give us partitions we can mount.
       +
        ## mountdevproc()
        Mounts `/dev`, `/dev/pts`, and `/proc` where needed. Takes one argument, which
        is the path containing those. ex:
       t@@ -46,6 +65,9 @@ mountdevproc /path/to/bootstrapped/chroot
        ## umountdevproc()
        Does the opposite of `mountdevproc`.
        
       +## silly()
       +Because NSA
       +
        # zlibs/sysconf
        NOTE: everything is printed to stdout. Pipe or redirect if you want to write on
        storage.
 (DIR) diff --git a/libdevuansdk b/libdevuansdk
       t@@ -57,6 +57,7 @@ vars+=(R H E)
        source $E/config
        source $R/zlibs/debootstrap
        source $R/zlibs/helpers
       +source $R/zlibs/imaging
        source $R/zlibs/sysconf
        
        # conclude initialization
 (DIR) diff --git a/zlibs/helpers b/zlibs/helpers
       t@@ -37,6 +37,23 @@ escalate() {
                fi
        }
        
       +findloopmapp() {
       +        fn findloopmapp
       +        req=(imgpath)
       +        ckreq || return 1
       +
       +        loopdevice=`sudo losetup -f --show ${imgpath}`
       +        mappdevice=`sudo kpartx -va $loopdevice | sed -E 's/.*(loop[0-9])p.*/\1/g' | head -1`
       +        func "Loop device:   ::1 loopdev::" $loopdevice
       +        func "Mapper device: ::1 mappdev::" $mappdevice
       +
       +        silly sleep
       +
       +        mappdevice="/dev/mapper/${mappdevice}"
       +        bootpart=${mappdevice}p1
       +        rootpart=${mappdevice}p2
       +}
       +
        mountdevproc() {
                fn mountdevproc $@
                local mntdir="$1"
       t@@ -58,3 +75,44 @@ umountdevproc() {
                escalate "root" "umount ${mntdir}/dev"  && act "unmounted /dev" && sleep 2
                escalate "root" "umount ${mntdir}/proc" && act "unmounted /proc" && sleep 2
        }
       +
       +silly() {
       +        fn silly
       +
       +        # cheers mailpile!
       +        funneh=("do not think of purple hippos"
       +                "increasing entropy & scrambling bits"
       +                "indexing kittens..."
       +                "patching bugs..."
       +                "spinning violently around the y-axis"
       +                "warming up particle accelerator"
       +                "this server is powered by a lemon and two electrodes"
       +                "becoming self-aware"
       +                "BCC-ing ALL THE SPIES!"
       +                "all of your config settings & passwords are encrypted with AES-256"
       +                "the most common e-mail password is 123456, hopefully yours is different"
       +                "good things come to those who wait"
       +                "Make Free Software and be happy"
       +                "We like volcanos, do you like volcanos?"
       +                "Crossing out swear words..."
       +                "Informing David Cameron of suspicious ac^H^H^H ... naaah :)"
       +                "Abandon all hope, ye who enter here"
       +                "Compiling bullshit bingo grid..."
       +                "Estimating chance of asteroid hitting Earth"
       +                "Applying coupons..."
       +                "Backing up the entire Internet..."
       +                "Making you wait for no reason"
       +                "Doing nothing"
       +                "Pay no attention to the man behind the curtain"
       +                "You are great just the way you are"
       +                "Supplying monkeys with typewriters"
       +                "Swapping time and space"
       +                "Self potato"
       +                "Hey, there is some Nigerian prince here who wants to give you twenty million dollars..."
       +                "A million hamsters are spinning their wheels right now")
       +
       +        local rnd=`shuf -i1-30 -n 1`
       +        act "${funneh[$rnd]}"
       +
       +        [[ $1 == "sleep" ]] && sleep 4
       +}
 (DIR) diff --git a/zlibs/imaging b/zlibs/imaging
       t@@ -0,0 +1,109 @@
       +#!/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
       +        req=(imgpath imgsize)
       +        ckreq || return 1
       +
       +        imgname=`basename ${imgpath}`
       +
       +        notice "Creating raw image..."
       +        dd if=/dev/zero \
       +                of="${imgpath}" \
       +                bs=1M count=${imgsize}
       +}
       +
       +img_partimage_dos() {
       +        fn img_partimage_dos
       +        req=(imgname imgpath)
       +        ckreq || return 1
       +
       +        notice "Partitioning with dos"
       +
       +        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_partimage_gpt() {
       +        fn img_partimage_gpt
       +        req=(imgname imgpath)
       +        ckreq || return 1
       +
       +        notice "Partitioning with gpt"
       +
       +        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_mountimage() {
       +        fn img_mountimage
       +        req=(bootpart rootpart workdir)
       +        ckreq || return 1
       +
       +        mkdir -p ${workdir}/rootp
       +
       +        escalate root "mount ${rootpart} ${workdir}/rootp"      && act "mounted root partition"
       +        escalate root "mount ${bootpart} ${workdir}/rootp/boot" && act "mounted root partition"
       +}
       +
       +img_umountimage() {
       +        fn img_umountimage
       +        req=(bootpart rootpart workdir)
       +        ckreq || return 1
       +
       +        escalate root "umount ${workdir}/rootp/boot" && act "umounted boot partition"
       +        escalate root "umount ${workdir}/rootp"      && act "umounted root partition"
       +}