commit f614a4ee87967bf3cfa8ed59619b1d3a94e1e606 Author: Dave Reisner Date: Sun Jun 17 15:17:10 2012 -0400 initial commit diff --git a/common b/common new file mode 100644 index 0000000..4afbbb2 --- /dev/null +++ b/common @@ -0,0 +1,71 @@ +#!/bin/bash + +out() { printf "$1 $2\n" "${@:3}"; } +error() { out "==> ERROR:" "$@"; } +msg() { out "==>" "$@"; } +msg2() { out " ->" "$@";} +die() { error "$@"; exit 1; } + +in_array() { + local i + for i in "${@:2}"; do + [[ $1 = "$i" ]] && return + done +} + +api_fs_mount() { + mount -t proc proc "$1/proc" -o nosuid,noexec,nodev || return 1 + mount -t sysfs sys "$1/sys" -o nosuid,noexec,nodev || return 1 + mount -t devtmpfs udev "$1/dev" -o mode=0755,nosuid || return 1 + mount -t devpts devpts "$1/dev/pts" -o mode=0620,gid=5,nosuid,noexec || return 1 + mount -t tmpfs shm "$1/dev/shm" -o mode=1777,nosuid,nodev || return 1 + mount -t tmpfs run "$1/run" -o nosuid,nodev,mode=0755 || return 1 + mount -t tmpfs tmp "$1/tmp" -o mode=1777,strictatime,nodev,nosuid,size=50M || return 1 +} + +api_fs_umount() { + umount \ + "$1/tmp" \ + "$1/run" \ + "$1/dev/shm" \ + "$1/dev/pts" \ + "$1/dev" \ + "$1/sys" \ + "$1/proc" +} + +fstype_is_pseudofs() { + # list taken from util-linux source: libmount/src/utils.c + local pseudofs_types=('anon_inodefs' + 'autofs' + 'bdev' + 'binfmt_misc' + 'cgroup' + 'configfs' + 'cpuset' + 'debugfs' + 'devfs' + 'devpts' + 'devtmpfs' + 'dlmfs' + 'fuse.gvfs-fuse-daemon' + 'fusectl' + 'hugetlbfs' + 'mqueue' + 'nfsd' + 'none' + 'pipefs' + 'proc' + 'pstore' + 'ramfs' + 'rootfs' + 'rpc_pipefs' + 'securityfs' + 'sockfs' + 'spufs' + 'sysfs' + 'tmpfs') + + in_array "$1" "${pseudofs_types[@]}" +} + diff --git a/genfstab b/genfstab new file mode 100755 index 0000000..7e78735 --- /dev/null +++ b/genfstab @@ -0,0 +1,94 @@ +#!/bin/bash + +. ./common + +shopt -s extglob + +write_source() { + local tag= spec= + + if (( bylabel )); then + tag=LABEL + spec=$(lsblk -rno LABEL "$1" 2>/dev/null) + elif (( byuuid )); then + tag=UUID + spec=$(lsblk -rno UUID "$1" 2>/dev/null) + fi + + if [[ $spec ]]; then + printf '# %s\n' "$source" + printf '%-20s' "$tag=$spec" + else + printf '%-20s' "$source" + fi +} + +root=/mnt + +while getopts ':LUr:' flag; do + case $flag in + L) + bylabel=1 + ;; + U) + byuuid=1 + ;; + r) + # trim trailing slashes + root=${OPTARG%%+(/)} + ;; + ?) + die '%s: invalid option -- '\''%s'\' "${0##*/}" "$OPTARG" + ;; + esac +done +shift $(( OPTIND - 1 )) + +if (( bylabel && byuuid )); then + die "cannot specify both -U and -L" +fi + +# handle block devices +findmnt -Recvruno SOURCE,TARGET,FSTYPE,VFS-OPTIONS "$root" | + while read -r source target fstype opts; do + # default 5th and 6th columns + dump=0 pass=2 + + # this is root + target=${target#$root} + if [[ $target = ?(/) ]]; then + target=/ + pass=1 + fi + + # we don't fsck pseudofs + if fstype_is_pseudofs "$fstype"; then + continue + pass=0 + fi + + # write one line + write_source "$source" + printf '\t%-10s' "$target" "$fstype" "$opts" + printf '\t%s %s' "$dump" "$pass" + printf '\n\n' +done + +# handle swaps devices +{ + # ignore header + read + + while read -r device _ _ _ prio; do + options=defaults + if [[ $prio != -1 ]]; then + options+=,pri=$prio + fi + + printf -v device '%b' "$device" + + printf '%-20s\t%-10s\t%-10s\t%-10s\t0 0\n' "$device" 'none' 'swap' "$options" + done +}