aboutsummaryrefslogtreecommitdiff
path: root/mkrootfs
blob: e8941b064c6ab0f50cbebb83fdf25929b9a46e43 (plain) (blame)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/bash
set -e

# Directory in which the file system will be built
#
ROOTFS=$(pwd)/cb5-rootfs

# Debian release
#
RELEASE=stretch

# Host name to be used by machine
#
HOSTNAME=cb5

# SSH to log in as root
#
SSH_KEY="$HOME/.ssh/id_rsa.pub"

print_usage() {
    cat 1>&2 <<EOF
Usage: $0 [options] rootfs-directory
Create a debian root filesystem for the cubieboard5.
The password for the root account will be set to 'guest', unless an ssh key is
specified, in which case the account will be locked.

Options:
  --hostname=<$HOSTNAME>   Use a given hostname for the device.
  --ssh-key=<$SSH_KEY>	   Add the given SSH key to root's authorized_keys file. When this option is provided, root's account will be locked.
  --release=<$RELEASE>     Create a root filesystem for the given debian release.
EOF
}

# Process options
#
while [ $# -gt 1 ]
do
    case "$1" in
	--help|-h)
	    print_usage
	    exit 0
	    ;;	
	--hostname=*)
	    HOSTNAME="${1#*=}"
	    ;;
	--release=*)
	    RELEASE="${1#*=}"
	    ;;
	--ssh-key=*)
	    SSH_KEY="${1#*=}"
	    ;;
	*)
	    echo "Unknown option '$1'" 1>&2
	    exit 1
    esac
    shift
done

# Process last argument, the root file system location
if [ -z "$1" ] || [ "$1" = -h ] || [ "$1" = --help ]; then
    print_usage
    exit 1
fi
if [ "$EUID" -ne 0 ]; then
    echo "This must be run as root." 1>&2
    exit 1
fi
export ROOTFS="$1"

export RELEASE
export HOSTNAME
export SSH_KEY

# Run a command in the root file system
#
chroot_exec() {
    # Exec command in chroot
    LANG=C LC_ALL=C DEBIAN_FRONTEND=noninteractive chroot ${ROOTFS} /bin/sh -c "$*"
}
export -f chroot_exec

# Unmount and remove any temporary files
#
cleanup() {
    set +e

    # Identify and kill all processes still using files
    echo "Killing processes using mount point" 1>&2
    fuser -k "${ROOTFS}"
    sleep 3
    fuser -9 -k -v "${ROOTFS}"

    # Clean up all temporary mount points
    echo "Removing temporary mount points" 1>&2
    umount -l "${ROOTFS}/proc" 2> /dev/null
    umount -l "${ROOTFS}/sys" 2> /dev/null
    umount -l "${ROOTFS}/dev/pts" 2> /dev/null
    umount -l "${ROOTFS}/dev" 2> /dev/null
    trap - 0 1 2 3 6
}
trap cleanup 0 1 2 3 6

# Bootstrap initial file system
#
echo "Creating rootfs" 1>&2
# proxy is set to use apt-cacher-ng
http_proxy="localhost:3142" qemu-debootstrap \
 	  --verbose \
      	  --include=linux-image-armmp-lpae,locales,flash-kernel,sunxi-tools,firmware-linux-free,u-boot-tools \
      	  --arch=armhf \
      	  "$RELEASE" \
      	  "$ROOTFS" \
      	  http://httpredir.debian.org/debian

# Prepare filesystem to be chrooted
#
echo "Creating temporary system mount points in rootfs" 1>&2
mount -t proc chproc "$ROOTFS/proc"
mount -t sysfs chsys "$ROOTFS/sys"
mount -t devtmpfs chdev "$ROOTFS/dev"
mount -t devpts chpts "$ROOTFS/dev/pts"
echo "Configuring rootfs" 1>&2
chroot_exec dpkg --configure -a

# Run setup scripts
#
echo "Running setup scripts" 1>&2
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
for script in "$dir"/mkrootfs.d/*.sh; do
    echo "Running $script" 1>&2
    bash "$script"
done
echo "Completed setup scripts" 1>&2

cleanup
echo "Successfully created rootfs" 1>&2