aboutsummaryrefslogtreecommitdiff
path: root/shell/inity.sh
blob: 970e8a18e525c22807a971525ff813c99af3256a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/bash

##############################################
# inity.sh
#
# Extract initrd.gz
#     inity.sh -x /full/path/of/initrd.gz
#     Creates initrd-root directory (in same path as initrd.gz) containing content of initrd
#
# Recreate initrd.gz
#     inity.sh -s /full/path/of/initrd-root
#     Takes contents of path and puts it in /path/of/initrd.gz
##############################################

getargs() { 
	echo "$@" | sed 's/\(-[a-zA-Z0-9] \)/\n\1/g' | awk '/^-/ { printf("ARG_%s=\"%s\"\n",gensub(/^-/,"","g",$1),gensub(/[ \t]*$/,"","g",gensub(/^-[a-zA-Z0-9][ \t]*(.*)$/,"\\1","g",$0))) }'
}

usage() {
	echo "usage: inity [-x  <path-of-compressed-initrd> | -s <path-of-virtual-root>]"
	echo "    WARNING:  the -s option will overwrite existing initrd files"
}

[[ -n "$@" ]] && eval "$(getargs "$@")"

if [ -z "$ARG_x" ] && [ -z "$ARG_s" ]; then
	usage
else
	if [ -n "$ARG_x" ] && [ -f $ARG_x ]; then
		INITRDDIR="$(sed 's/\..*$/-root/g' <<< "$ARG_x")"

		pushd . 2>&1 > /dev/null
		mkdir $INITRDDIR
		cd $INITRDDIR
		gzip -cd $ARG_x | cpio -i 2>&1 > /dev/null
		echo "$ARG_x -> $INITRDDIR"
		popd 2>&1 > /dev/null
	elif [ -n "$ARG_s" ] && [ -d $ARG_s ]; then
		INITRDGZFILE="$(sed 's/-root[\/]*$//g' <<< "$ARG_s")"
		pushd . 2>&1 > /dev/null
		cd $ARG_s
		find . | cpio -H newc -o > $INITRDGZFILE 2>&1 > /dev/null
		gzip -f $INITRDGZFILE
		echo "$ARG_s -> $INITRDGZFILE.gz"
		popd 2>&1 > /dev/null
	else
		echo "Argument error"
		usage
	fi
fi