Browse code

Add support for rpm-ostree host creation

Change-Id: I1538ea24c3348cd3d33b3ca0cc022dac7acc2ccd
Reviewed-on: http://photon-jenkins.eng.vmware.com:8082/3861
Reviewed-by: Anish Swaminathan <anishs@vmware.com>
Tested-by: Anish Swaminathan <anishs@vmware.com>

DheerajSShetty authored on 2017/09/26 03:34:42
Showing 4 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,42 @@
0
+fail() {	local _red="\\033[1;31m"
1
+	local _normal="\\033[0;39m"
2
+	[ -n "$*" ] && printf "${_red}$*${_normal}\n"
3
+	exit 1
4
+}
5
+print_message() {
6
+	printf "%s" "${1}"
7
+}
8
+print_failed_in_red() {
9
+	local _red="\\033[1;31m"
10
+	local _normal="\\033[0;39m"
11
+	printf "${_red}%s${_normal}\n" "FAILURE"
12
+	exit 2
13
+}
14
+print_succeeded_in_green() {
15
+	local _green="\\033[1;32m"
16
+	local _normal="\\033[0;39m"
17
+	printf "${_green}%s${_normal}\n" "SUCCESS"
18
+	return 0
19
+}
20
+run_command() {
21
+	# $1 = message 
22
+	# $2 = command
23
+	# $3 = log file
24
+	local _start=$(date +%s)
25
+	local _msg="${1}"
26
+	local _cmd="${2}"
27
+	local _logfile="${3}"
28
+	if [ "/dev/null" == "${_logfile}" ]; then
29
+		print_message "${_msg}: "
30
+		eval ${_cmd} >> ${_logfile} 2>&1 && print_succeeded_in_green || print_failed_in_red 
31
+	else
32
+		print_message "${_msg}: "
33
+		printf "\n%s\n\n" "###       ${_msg}       ###" >> ${_logfile} 2>&1
34
+		eval ${_cmd} >> ${_logfile} 2>&1 && print_succeeded_in_green || print_failed_in_red 
35
+		fi
36
+
37
+	local _end=$(date +%s)
38
+	local _elapsed=$((_end-_start))
39
+	echo "Elapsed time: ${_elapsed} seconds" >> ${_logfile} 2>&1
40
+	return 0
41
+}
0 42
new file mode 100755
... ...
@@ -0,0 +1,186 @@
0
+#!/bin/bash
1
+#################################################
2
+#	Title:	mk-ostree-host			#
3
+#        Date:	2017-09-22	 		#
4
+#     Version:	1.0				#
5
+#      Author:	dheerajs@vmware.com		#
6
+#     Options:					#
7
+#################################################
8
+#	Overview
9
+#		Creating rpm-ostree host raw file
10
+#	End
11
+#
12
+set -o errexit    # exit if any command returns non true return value
13
+set -o nounset    # exit if you try to use uninitialised variable
14
+PRGNAME=${0##*/}  # script name minus the path
15
+LOGFILE=/var/log/"${PRGNAME}-$(date +%Y-%m-%d).log"
16
+source function.inc
17
+NARGS=10
18
+ARGS_PASSED=$#
19
+SDA3=/dev/sda3
20
+
21
+while [[ $# > 0 ]]
22
+do
23
+        key="$1"
24
+        shift
25
+
26
+        case $key in
27
+                -s|--FILE_SIZE)
28
+                FILE_SIZE="$1"
29
+                shift
30
+        ;;
31
+                -n|--IMG_NAME)
32
+                RAW_IMAGE_NAME="$1".raw
33
+                shift
34
+        ;;
35
+                -i|--IP_ADDR)
36
+                IP_ADDR="$1"
37
+                shift
38
+        ;;
39
+                -r|--REPO_REF)
40
+                REPO_REF="$1"
41
+                shift
42
+        ;;
43
+                -m|--MOUNT_POINT)
44
+                MOUNT_POINT="$1"
45
+                shift
46
+        ;;
47
+                -h|--help)
48
+                echo 'Usage:'
49
+                echo '-s|--FILE_SIZE           :Total Size in GB. Make sure you have this space in your disk'
50
+                echo '-n|--RAW_IMAGE_NAME      :Name of the Raw file'
51
+                echo '-i|--IP_ADDR             :rpm-ostree server IP address'
52
+                echo '-r|--REPO_REF            :rpm-ostree ref ex. photon/2.0/x86_64/base'
53
+                echo '-m|--MOUNT_POINT         :mount point ex. /mnt/photon-root'
54
+                exit 0
55
+        ;;
56
+        *)
57
+                # unknown option
58
+        ;;
59
+        esac
60
+done
61
+
62
+if [ $ARGS_PASSED -ne $NARGS ]; then
63
+   echo "Error in the arguments passed. Try ./mk-ostree-host.sh -h for help"
64
+   exit 1
65
+fi
66
+
67
+function mount_devices {
68
+    run_command "Mount devices in deployment" "mount -t bind -o bind,defaults /dev  ${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0/dev" "${LOGFILE}"
69
+    run_command "Mount devices in deployment" "mount -t devpts -o gid=5,mode=620 devpts  ${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0/dev/pts" "${LOGFILE}"
70
+    run_command "Mount devices in deployment" "mount -t tmpfs -o defaults tmpfs  ${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0/dev/shm" "${LOGFILE}"
71
+    run_command "Mount devices in deployment" "mount -t proc -o defaults proc  ${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0/proc" "${LOGFILE}"
72
+    run_command "Mount devices in deployment" "mount -t bind -o bind,defaults /run  ${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0/run" "${LOGFILE}"
73
+    run_command "Mount devices in deployment" "mount -t sysfs -o defaults sysfs ${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0/sys" "${LOGFILE}"
74
+}
75
+function unmount_devices {
76
+    run_command "Unmount devices in deployment" "umount ${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0/sys" "${LOGFILE}"
77
+    run_command "Unmount devices in deployment" "umount ${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0/run" "${LOGFILE}"
78
+    run_command "Unmount devices in deployment" "umount ${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0/proc" "${LOGFILE}"
79
+    run_command "Unmount devices in deployment" "umount ${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0/dev/shm" "${LOGFILE}"
80
+    run_command "Unmount devices in deployment" "umount ${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0/dev/pts" "${LOGFILE}"
81
+    run_command "Unmount devices in deployment" "umount ${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0/dev" "${LOGFILE}"
82
+}
83
+
84
+function create_systemd_tmpfile {
85
+    run_command "Create systemd-tmpfiles" "systemd-tmpfiles --create --boot --root=${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0 --prefix=/var/home" "${LOGFILE}"
86
+    run_command "Create systemd-tmpfiles" "systemd-tmpfiles --create --boot --root=${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0 --prefix=/var/roothome" "${LOGFILE}"
87
+    run_command "Create systemd-tmpfiles" "systemd-tmpfiles --create --boot --root=${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0 --prefix=/var/lib/rpm" "${LOGFILE}"
88
+    run_command "Create systemd-tmpfiles" "systemd-tmpfiles --create --boot --root=${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0 --prefix=/var/opt" "${LOGFILE}"
89
+    run_command "Create systemd-tmpfiles" "systemd-tmpfiles --create --boot --root=${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0 --prefix=/var/srv" "${LOGFILE}"
90
+    run_command "Create systemd-tmpfiles" "systemd-tmpfiles --create --boot --root=${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0 --prefix=/var/userlocal" "${LOGFILE}"
91
+    run_command "Create systemd-tmpfiles" "systemd-tmpfiles --create --boot --root=${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0 --prefix=/var/mnt" "${LOGFILE}"
92
+    run_command "Create systemd-tmpfiles" "systemd-tmpfiles --create --boot --root=${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0 --prefix=/var/spool/mail" "${LOGFILE}"
93
+}
94
+
95
+function unmount_loop_devices {
96
+    run_command "Unmounting p2" "umount ${ROOT_PARTITION}" "${LOGFILE}"
97
+    run_command "Unmounting p3" "umount ${SYSROOT_BOOT_PARTITION}" "${LOGFILE}"
98
+    run_command "kpartx loop0" "kpartx -d ${DISK_DEVICE}" "${LOGFILE}"
99
+    run_command "delete loop0" "losetup -d ${DISK_DEVICE}" "${LOGFILE}"
100
+}
101
+
102
+function rpm_ostree_init_deploy {
103
+    run_command "Init the Ostree Repo" "ostree --repo=${MOUNT_POINT}/repo init --mode=archive-z2" "${LOGFILE}"
104
+    run_command "Ostree Init FS" "ostree admin --sysroot=${MOUNT_POINT} init-fs ${MOUNT_POINT}" "${LOGFILE}"
105
+    run_command "Add Remote" "ostree remote add --repo=${MOUNT_POINT}/ostree/repo --set=gpg-verify=false photon http://${IP_ADDR}" "${LOGFILE}"
106
+    run_command "Pull Repo" "ostree pull --repo=${MOUNT_POINT}/ostree/repo photon ${REPO_REF}" "${LOGFILE}"
107
+    run_command "Init-OS" "ostree admin --sysroot=${MOUNT_POINT} os-init photon" "${LOGFILE}"
108
+    run_command "Deploy Ostree" "ostree admin --sysroot=${MOUNT_POINT} deploy --os=photon photon:${REPO_REF}" "${LOGFILE}"
109
+}
110
+
111
+function install_generate_grub {
112
+    run_command "Install grub" "chroot ${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0 bash -c \"grub2-install /dev/loop0\"" "${LOGFILE}"
113
+    run_command "Generate grub.cfg file" "chroot ${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0 bash -c \"grub2-mkconfig -o /boot/grub2/grub.cfg\"" "${LOGFILE}"
114
+    run_command "Set boot volume in grub config file" "chroot ${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0 bash -c \"ostree admin instutil set-kargs root=${ROOT_PARTITION}\"" "${LOGFILE}"
115
+    run_command "Replace ${ROOT_PARTITION} with ${SDA3} in cfg file" "sed -i 's:${ROOT_PARTITION}:${SDA3}:' ${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0/boot/loader/grub.cfg" "${LOGFILE}"
116
+    run_command "Replace ${ROOT_PARTITION} with ${SDA3} in loader conf file" "sed -i 's:${ROOT_PARTITION}:${SDA3}:' ${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0/boot/loader/entries/ostree-photon-0.conf" "${LOGFILE}"
117
+    run_command "Remove grub config from /boot/grub2" "chroot ${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0 bash -c \"rm /boot/grub2/grub.cfg\"" "${LOGFILE}"
118
+    run_command "Create a link file to /boot/loader/grub.cfg from /boot/grub2" "chroot ${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0 bash -c \"cd /boot/grub2/ && ln -sf ../loader/grub.cfg ./grub.cfg\"" "${LOGFILE}"
119
+}
120
+
121
+function update_fstab {
122
+    run_command "Update /etc/fstab in chroot for /dev/sda3" "echo \"/dev/sda3    /        ext4   defaults   1 1  \" >> ${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0/etc/fstab" "${LOGFILE}"
123
+    run_command "Update /etc/fstab in chroot for /dev/sda2" "echo \"/dev/sda2    /boot    ext4   defaults   1 2  \" >> ${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0/etc/fstab" "${LOGFILE}"
124
+}
125
+
126
+function create_password {
127
+    run_command "Echo password to a file. Change this password" "echo \"root:changeme\" >> ${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0/root/mypwdfile" "${LOGFILE}"
128
+    run_command "Change Password of root to changeme" "chroot ${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0 bash -c \"cat root/mypwdfile | chpasswd\"" "${LOGFILE}"
129
+    run_command "Delete the temporary mypwdfile" "rm ${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0/root/mypwdfile" "${LOGFILE}"
130
+}
131
+
132
+run_command "Install gptfdisk kpartx and device-mapper-devel" "tdnf -y install gptfdisk kpartx device-mapper-devel device-mapper-libs" "${LOGFILE}"
133
+
134
+echo "Creating raw disk file " $RAW_IMAGE_NAME " of size " $FILE_SIZE
135
+dd if=/dev/zero of=$RAW_IMAGE_NAME bs=1 seek=$(($FILE_SIZE * 1024 * 1024 * 1024)) count=0
136
+chmod 755 $RAW_IMAGE_NAME
137
+
138
+echo "Associating loopdevice to raw disk"
139
+DISK_DEVICE=`losetup --show -f $RAW_IMAGE_NAME`
140
+
141
+echo "Creating partition on raw disk"
142
+sgdisk -n 1::+2M -n 2::+300M -n 3: -p $DISK_DEVICE 
143
+sgdisk -t1:ef02 $DISK_DEVICE
144
+
145
+echo "Mapping device partition to loop device"
146
+kpartx -avs $DISK_DEVICE
147
+
148
+DEVICE_NAME=`echo $DISK_DEVICE|cut -c6- `
149
+
150
+echo "Adding file system to device partition"
151
+mkfs -t ext4 /dev/mapper/${DEVICE_NAME}p2
152
+mkfs -t ext4 /dev/mapper/${DEVICE_NAME}p3
153
+
154
+ROOT_PARTITION=/dev/mapper/${DEVICE_NAME}p3
155
+SYSROOT_BOOT_PARTITION=/dev/mapper/${DEVICE_NAME}p2
156
+
157
+SYSROOT_BOOT=${MOUNT_POINT}/boot
158
+SYSROOT_OSTREE=${MOUNT_POINT}/ostree
159
+
160
+run_command "Making Mount Point Directory" "mkdir -p ${MOUNT_POINT}" "${LOGFILE}"
161
+run_command "Mount Root" "mount ${ROOT_PARTITION} ${MOUNT_POINT}" "$LOGFILE"
162
+run_command "Making Sysroot Boot Directory" "mkdir -p ${SYSROOT_BOOT}" "${LOGFILE}"
163
+run_command "Mount Sysroot Boot" "mount ${SYSROOT_BOOT_PARTITION} ${SYSROOT_BOOT}" "${LOGFILE}"
164
+run_command "Make repo directory for ostree" "mkdir -p ${MOUNT_POINT}/repo" "${LOGFILE}"
165
+
166
+rpm_ostree_init_deploy
167
+
168
+id=$(cat ${MOUNT_POINT}/ostree/repo/refs/remotes/photon/${REPO_REF})
169
+
170
+create_systemd_tmpfile
171
+
172
+mount_devices
173
+
174
+run_command "Mount Boot" "mount --bind ${MOUNT_POINT}/boot ${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0/boot" "${LOGFILE}"
175
+run_command "Mount Sysroot" "mount --bind ${MOUNT_POINT} ${MOUNT_POINT}/ostree/deploy/photon/deploy/${id}.0/sysroot" "${LOGFILE}"
176
+
177
+install_generate_grub
178
+
179
+update_fstab
180
+
181
+create_password
182
+
183
+unmount_devices
184
+
185
+unmount_loop_devices
... ...
@@ -1,13 +1,15 @@
1 1
 Summary:        Commit RPMs to an OSTree repository
2 2
 Name:           rpm-ostree
3 3
 Version:        2017.4
4
-Release:        4%{?dist}
4
+Release:        5%{?dist}
5 5
 Source0:        rpm-ostree-%{version}.tar.gz
6 6
 %define sha1    rpm-ostree=d34882a455afbf0b57617c0962725276967e838a
7 7
 Source1:        libglnx-0c52d85.tar.gz
8 8
 %define sha1    libglnx=137767ad957f37d6210aaa6b28e4333a42aa9fad
9 9
 Source2:        libdnf-2086268.tar.gz
10 10
 %define sha1    libdnf=4e913da416c61a5525f94ef09f38c658179e3e25
11
+Source3:        mk-ostree-host.sh
12
+Source4:        function.inc
11 13
 Patch0:         rpm-ostree-libdnf-build.patch
12 14
 Patch1:         Set-arch.patch
13 15
 Patch2:         Makefile-test.patch
... ...
@@ -72,6 +74,14 @@ Requires: %{name} = %{version}-%{release}
72 72
 %description devel
73 73
 Includes the header files for the rpm-ostree library.
74 74
 
75
+%package host
76
+Summary: File for rpm-ostree-host creation
77
+Group: Development/Libraries
78
+Requires: %{name} = %{version}-%{release}
79
+
80
+%description host
81
+Includes the scripts for rpm-ostree host creation
82
+
75 83
 %prep
76 84
 %setup -q
77 85
 tar xf /usr/src/photon/SOURCES/libglnx-0c52d85.tar.gz --no-same-owner
... ...
@@ -89,7 +99,9 @@ make %{?_smp_mflags}
89 89
 %install
90 90
 make install DESTDIR=%{buildroot} INSTALL="install -p -c"
91 91
 find %{buildroot} -name '*.la' -delete
92
-
92
+install -d %{buildroot}%{_bindir}/rpm-ostree-host
93
+install -p -m 755 -D %{SOURCE3} %{buildroot}%{_bindir}/rpm-ostree-host
94
+install -p -m 644 -D %{SOURCE4} %{buildroot}%{_bindir}/rpm-ostree-host
93 95
 %check
94 96
 make check
95 97
 
... ...
@@ -111,7 +123,13 @@ make check
111 111
 %{_datadir}/gtk-doc/html/*
112 112
 %{_datadir}/gir-1.0/*-1.0.gir
113 113
 
114
+%files host
115
+%{_bindir}/rpm-ostree-host/mk-ostree-host.sh
116
+%{_bindir}/rpm-ostree-host/function.inc
117
+
114 118
 %changelog
119
+*   Fri Sep 22 2017 Dheeraj Shetty <dheerajs@vmware.com> 2017.4-5
120
+-   Add support to generate rpm-ostree host
115 121
 *   Fri Sep 15 2017 Dheeraj Shetty <dheerajs@vmware.com> 2017.4-4
116 122
 -   Changes for Makecheck
117 123
 *   Thu Aug 03 2017 Xiaolin Li <xiaolinl@vmware.com> 2017.4-3
... ...
@@ -23,7 +23,7 @@
23 23
                 "dbus", "docker", "dracut", "dracut-tools",
24 24
                 "expat",
25 25
                 "file", "findutils",
26
-                "glibc", "gmp", "grep", "grub2", "gzip",
26
+                "glibc", "gmp", "grep", "grub2", "grub2-pc", "gzip",
27 27
                 "iana-etc", "iproute2", "iptables",
28 28
                 "kmod",
29 29
                 "libcap", "libgcc", "libstdc++", "libtool", "linux",