-
Notifications
You must be signed in to change notification settings - Fork 5
/
10-vhdmount.sh
186 lines (153 loc) · 5.13 KB
/
10-vhdmount.sh
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#! /bin/bash
# This script is for dracut, and boot from kloop or vloop method
KLOOP=$(getarg kloop=)
KROOT=$(getarg kroot=)
KLOOPFSTYPE=$(getarg kloopfstype=)
KLVM=$(getarg klvm=)
HOSTFSTYPE=$(getarg hostfstype=)
HOSTHIDDEN=$(getarg hosthidden=)
SQUASHFS=$(getarg squashfs=)
UPPERDIR=$(getarg upperdir=)
WORKDIR=$(getarg workdir=)
VLOOP=$(getarg vloop=)
VLOOPPART=$(getarg vlooppart=)
VLOOPFSTYPE=$(getarg vloopfstype=)
HOSTFSTYPE=$(getarg hostfstype=)
export KLOOP
export KROOT
export KLOOPFSTYPE
export KLVM
export HOSTFSTYPE
export HOSTHIDDEN
export SQUASHFS
export UPPERDIR
export WORKIDR
export VLOOP
export VLOOPPART
export VLOOPFSTYPE
export HOSTFSTYPE
if [ -n "$UPPERDIR" ] && [ -n $"WORKDIR" ]; then
### reset the value of the root variable
HOSTDEV="${root#block:}"
if ismounted "$NEWROOT"; then
umount "$NEWROOT"
fi
### auto probe the fs-type of the partition in which vhd-file live and mount it /host
mkdir -p /host
if [ -z "${HOSTFSTYPE}" ]; then
HOSTFSTYPE="$(blkid -s TYPE -o value "$HOSTDEV")"
[ -z "${HOSTFSTYPE}" -o "${HOSTFSTYPE}" = "ntfs" ] && HOSTFSTYPE="ntfs-3g"
fi
[ "${HOSTFSTYPE}" = "ntfs-3g" ] || modprobe ${HOSTFSTYPE}
mount -t "${HOSTFSTYPE}" -o rw $HOSTDEV /host
###try to boot from upperdir
mkdir /run/tmpreadroot
mount -t overlay overlay -o lowerdir=/run/tmpreadroot,upperdir=/host$UPPERDIR,workdir=/host$WORKDIR $NEWROOT
### mount /host in initrd to /host of the realrootfs
if [ "${HOSTHIDDEN}" != "y" ] ; then
[ -d "${NEWROOT}"/host ] || mkdir -p ${NEWROOT}/host
mount -R /host ${NEWROOT}/host
fi
fi
if [ -n "$SQUASHFS" ]; then
### reset the value of the root variable
HOSTDEV="${root#block:}"
if ismounted "$NEWROOT"; then
umount "$NEWROOT"
fi
### auto probe the fs-type of the partition in which vhd-file live and mount it /host
mkdir -p /host
if [ -z "${HOSTFSTYPE}" ]; then
HOSTFSTYPE="$(blkid -s TYPE -o value "$HOSTDEV")"
[ -z "${HOSTFSTYPE}" -o "${HOSTFSTYPE}" = "ntfs" ] && HOSTFSTYPE="ntfs-3g"
fi
[ "${HOSTFSTYPE}" = "ntfs-3g" ] || modprobe ${HOSTFSTYPE}
mount -t "${HOSTFSTYPE}" -o rw $HOSTDEV /host
###try to boot from squashfs
mkdir /run/upperdir /run/lowerdir /run/workdir
mount /host$SQUASHFS /run/lowerdir
mount -t overlay overlay -o lowerdir=/run/lowerdir,upperdir=/run/upperdir,workdir=/run/workdir $NEWROOT
### mount /host in initrd to /host of the realrootfs
if [ "${HOSTHIDDEN}" != "y" ] ; then
[ -d "${NEWROOT}"/host ] || mkdir -p ${NEWROOT}/host
mount -R /host ${NEWROOT}/host
fi
fi
if [ -n "$KLOOP" ]; then
### reset the value of the root variable
HOSTDEV="${root#block:}"
[ -n "$KROOT" ] || root="/dev/loop0"
[ -n "$KROOT" ] && root="$KROOT"
realroot="$root"
export root
if ismounted "$NEWROOT"; then
umount "$NEWROOT"
fi
### auto probe the fs-type of the partition in which vhd-file live and mount it /host
mkdir -p /host
if [ -z "${HOSTFSTYPE}" ]; then
HOSTFSTYPE="$(blkid -s TYPE -o value "$HOSTDEV")"
[ -z "${HOSTFSTYPE}" -o "${HOSTFSTYPE}" = "ntfs" ] && HOSTFSTYPE="ntfs-3g"
fi
[ "${HOSTFSTYPE}" = "ntfs-3g" ] || modprobe ${HOSTFSTYPE}
mount -t "${HOSTFSTYPE}" -o rw $HOSTDEV /host
### mount the vhd-file on a loop-device
if [ "${KLOOP#/}" != "${KLOOP}" ]; then
modprobe loop
kpartx -av /host$KLOOP
[ -e "$realroot" ] || sleep 3
fi
### probe lvm on vhd-file and active them
if [ -n "$KLVM" ]; then
modprobe dm-mod
vgscan
vgchange -ay "$KLVM"
[ -e "$realroot" ] || sleep 3
fi
### mount the realroot / in vhd-file on $NEWROOT
if [ -z "${KLOOPFSTYPE}" ]; then
KLOOPFSTYPE="$(blkid -s TYPE -o value "$realroot")"
[ -z "${KLOOPFSTYPE}" ] && KLOOPFSTYPE="ext4"
fi
[ -e "$realroot" ] || sleep 3
mount -t "${KLOOPFSTYPE}" -o rw $realroot $NEWROOT
### mount /host in initrd to /host of the realrootfs
if [ "${HOSTHIDDEN}" != "y" ] ; then
[ -d "${NEWROOT}"/host ] || mkdir -p ${NEWROOT}/host
mount -R /host ${NEWROOT}/host
fi
fi
if [ -n "$VLOOP" ]; then
### reset the value of the root variable
HOSTDEV="${root#block:}"
[ -n "$VLOOPPART" ] || root=/dev/loop0
[ -n "$VLOOPPART" ] && root=/dev/mapper/loop0$VLOOPPART
export root
realroot="$root"
if ismounted "$NEWROOT"; then
umount "$NEWROOT"
fi
### auto probe the fs-type of the partition in which vhd-file live and mount it /host
mkdir -p /host
if [ -z "${HOSTFSTYPE}" ]; then
HOSTFSTYPE="$(blkid -s TYPE -o value "$HOSTDEV")"
[ -z "${HOSTFSTYPE}" -o "${HOSTFSTYPE}" = "ntfs" ] && HOSTFSTYPE="ntfs-3g"
fi
mount -t "${HOSTFSTYPE}" -o rw "${HOSTDEV}" /host
### mount the vhd-file on a loop-device
if [ "${VLOOP#/}" != "${VLOOP}" ]; then
modprobe loop
kpartx -av "/host$VLOOP"
[ -e "$realroot" ] || sleep 3
fi
### mount the realroot / in vhd-file on $NEWROOT
[ -e "$realroot" ] || sleep 3
if [ -z "${VLOOPFSTYPE}" ]; then
VLOOPFSTYPE="$(blkid -s TYPE -o value "$realroot")"
[ -z "${VLOOPFSTYPE}" ] && VLOOPFSTYPE="ext4"
fi
mount -t "${VLOOPFSTYPE}" -o rw $realroot $NEWROOT
### mount /host in initrd to /host of the realrootfs
[ -d $NEWROOT/host ] || mkdir -p $NEWROOT/host
mount -R /host $NEWROOT/host
fi