aboutsummaryrefslogtreecommitdiffstats
path: root/main/mkinitfs/git.patch
blob: 6e812b7d2fee6f0d11d9d36515570b0fb52bfd01 (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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
diff --git a/Makefile b/Makefile
index aaa553e..a62c294 100644
--- a/Makefile
+++ b/Makefile
@@ -5,7 +5,7 @@ sysconfdir	?= /etc/mkinitfs
 datarootdir	?= /usr/share
 datadir		?= $(datarootdir)/mkinitfs
 
-SBIN_FILES	:= mkinitfs bootchartd
+SBIN_FILES	:= mkinitfs bootchartd nlplug-findfs
 SHARE_FILES	:= initramfs-init fstab passwd group
 CONF_FILES	:= mkinitfs.conf \
 		features.d/ata.modules \
@@ -32,6 +32,7 @@ CONF_FILES	:= mkinitfs.conf \
 		features.d/network.files \
 		features.d/network.modules \
 		features.d/ocfs2.modules \
+		features.d/raid.files\
 		features.d/raid.modules \
 		features.d/reiserfs.modules \
 		features.d/scsi.modules \
@@ -41,7 +42,7 @@ CONF_FILES	:= mkinitfs.conf \
 		features.d/virtio.modules \
 		features.d/xfs.modules
 
-SCRIPTS		:= $(SBIN_FILES) initramfs-init
+SCRIPTS		:= mkinitfs bootchartd initramfs-init
 IN_FILES	:= $(addsuffix .in,$(SCRIPTS))
 
 GIT_REV := $(shell test -d .git && git describe || echo exported)
@@ -62,7 +63,8 @@ SED_REPLACE	:= -e 's:@VERSION@:$(FULL_VERSION):g' \
 		-e 's:@datadir@:$(datadir):g'
 
 
-all:	$(SCRIPTS)
+
+all:	$(SBIN_FILES) $(SCRIPTS)
 
 clean:
 	rm -f $(SCRIPTS)
@@ -71,6 +73,24 @@ help:
 	@echo mkinitfs $(VERSION)
 	@echo "usage: make install [DESTDIR=]"
 
+CFLAGS ?= -Wall -Werror -g
+CFLAGS += -D_GNU_SOURCE -DDEBUG
+
+PKGCONF		?= pkg-config
+BLKID_CFLAGS	:= $(shell $(PKGCONF) --cflags blkid)
+BLKID_LIBS	:= $(shell $(PKGCONF) --libs blkid)
+LIBKMOD_CFLAGS	:= $(shell $(PKGCONF) --cflags libkmod)
+LIBKMOD_LIBS	:= $(shell $(PKGCONF) --libs libkmod)
+
+CFLAGS		+= $(BLKID_CFLAGS) $(LIBKMOD_CFLAGS)
+LIBS		= $(BLKID_LIBS) $(LIBKMOD_LIBS)
+
+%.o: %.c
+	$(CC) $(CFLAGS) -o $@ -c $<
+
+nlplug-findfs: nlplug-findfs.o
+	$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
+
 .SUFFIXES:	.in
 .in:
 	${SED} ${SED_REPLACE} ${SED_EXTRA} $< > $@
diff --git a/arg.h b/arg.h
new file mode 100644
index 0000000..92a313f
--- /dev/null
+++ b/arg.h
@@ -0,0 +1,39 @@
+/*
+ * Copy me if you can.
+ * by 20h
+ */
+
+#ifndef __ARG_H__
+#define __ARG_H__
+
+#define USED(x) ((void)(x))
+
+#define ARGBEGIN	for (argv++, argc--;\
+					argv[0] && argv[0][1]\
+					&& argv[0][0] == '-';\
+					argc--, argv++) {\
+				char _argc;\
+				char **_argv;\
+				if (argv[0][1] == '-' && argv[0][2] == '\0') {\
+					argv++;\
+					argc--;\
+					break;\
+				}\
+				for (argv[0]++, _argv = argv; argv[0][0];\
+						argv[0]++) {\
+					if (_argv != argv)\
+						break;\
+					_argc = argv[0][0];\
+					switch (_argc)
+
+#define ARGEND			}\
+				USED(_argc);\
+			}\
+			USED(argv);\
+			USED(argc);
+
+#define EARGF(x)	((argv[1] == NULL)? ((x), abort(), (char *)0) :\
+			(argc--, argv++, argv[0]))
+
+#endif
+
diff --git a/bootchartd.in b/bootchartd.in
index c4daa35..55a0883 100755
--- a/bootchartd.in
+++ b/bootchartd.in
@@ -46,7 +46,7 @@ do_logging()
 	# Enable process accounting if configured
 	if [ "$PROCESS_ACCOUNTING" = "yes" ]; then
 		[ -e kernel_pacct ] || : > kernel_pacct
-		accton kernel_pacct
+		accton kernel_pacct > /dev/null
 	fi
 
 	# open file descriptors
@@ -98,7 +98,7 @@ do_logging()
 		i=$(($i + 1))
 	done
 
-	[ -e kernel_pacct ] && accton
+	[ -e kernel_pacct ] && accton off > /dev/null
 }
 
 # Stop the boot logger.  The lock file is removed to force the loggers in
@@ -151,19 +151,18 @@ finalize()
 
 	# Package log files
 	tar -zcf "$BOOTLOG_DEST" header $pacct *.log
-	rm "$LOGDIR"/*
-	rmdir "$LOGDIR"
+	rm -rf "$LOGDIR"
 }
 
 case "$1" in
 start-initfs)
 	NEWROOT="$2"
+	mkdir -p "$LOGDIR"
 	(
 		cleanup=true
 		trap "not_stop_logging=false" USR1
 		trap "cleanup=false; not_stop_logging=false" USR2
 
-		mkdir "$LOGDIR"
 		cd "$LOGDIR"
 		do_logging
 		if $cleanup; then
@@ -171,18 +170,16 @@ start-initfs)
 			finalize
 		fi
 	) &
-	echo $! > $LOGDIR/bootchart.pid
+	echo $! > "$LOGDIR"/bootchart.pid
 	;;
 stop-initfs)
 	NEWROOT="$2"
-
-	cd "$LOGDIR"
-	mkdir "$NEWROOT$LOGDIR"
-	cp /sbin/bootchartd $NEWROOT/sbin
-	PID=`cat bootchart.pid`
+	[ -x "$NEWROOT"/sbin/bootchartd ] || cp -a /sbin/bootchartd "$NEWROOT"/sbin
+	rm -rf "$NEWROOT/$LOGDIR"
+	PID=$(cat "$LOGDIR"/bootchart.pid)
 	kill -USR2 $PID
 	wait $PID
-	mv * "$NEWROOT$LOGDIR"
+	mv "$LOGDIR" "$NEWROOT"
 	;;
 start-rootfs)
 	(
diff --git a/features.d/base.files b/features.d/base.files
index 7dd36e5..23718cb 100644
--- a/features.d/base.files
+++ b/features.d/base.files
@@ -4,3 +4,4 @@
 /sbin/apk
 /etc/modprobe.d/*.conf
 /etc/mdev.conf
+/sbin/nlplug-findfs
diff --git a/features.d/bootchart.files b/features.d/bootchart.files
index 8adf4df..6f28294 100644
--- a/features.d/bootchart.files
+++ b/features.d/bootchart.files
@@ -1,3 +1,4 @@
+/sbin/bootchartd
 /usr/bin/ac
 /usr/bin/last
 /usr/bin/lastcomm
@@ -5,4 +6,3 @@
 /usr/sbin/dump-acct
 /usr/sbin/accton
 /usr/sbin/sa
-
diff --git a/features.d/lvm.modules b/features.d/lvm.modules
index 92f0721..3af8ef9 100644
--- a/features.d/lvm.modules
+++ b/features.d/lvm.modules
@@ -1 +1,2 @@
 kernel/drivers/md/dm-mod.ko
+kernel/drivers/md/dm-snapshot.ko
diff --git a/features.d/raid.files b/features.d/raid.files
new file mode 100644
index 0000000..c393dc4
--- /dev/null
+++ b/features.d/raid.files
@@ -0,0 +1,2 @@
+/etc/mdadm.conf
+/sbin/mdadm
diff --git a/features.d/virtio.modules b/features.d/virtio.modules
index fa74c10..b0e6c22 100644
--- a/features.d/virtio.modules
+++ b/features.d/virtio.modules
@@ -1,2 +1,3 @@
 kernel/drivers/block/virtio*
 kernel/drivers/virtio
+kernel/drivers/net/virtio_net*
diff --git a/initramfs-init.in b/initramfs-init.in
index 934fe92..0834175 100755
--- a/initramfs-init.in
+++ b/initramfs-init.in
@@ -7,7 +7,7 @@ sysroot=/sysroot
 splashfile=/.splash.ctrl
 
 /bin/busybox mkdir -p /usr/bin /usr/sbin /proc /sys /dev $sysroot \
-	/media/cdrom /media/usb /tmp
+	/media/cdrom /media/usb /tmp /run
 /bin/busybox --install -s
 
 # basic environment
@@ -42,30 +42,6 @@ eend() {
 	fi
 }
 
-scan_drivers() {
-	if [ "$KOPT_autodetect" != no ] ; then
-		find /sys -name modalias | xargs sort -u | xargs modprobe -a 2> /dev/null
-	fi
-}
-
-find_ovl() {
-	local mnt="$1"
-
-	if [ -n "$APKOVL" ]; then
-		[ -f "$mnt/$APKOVL" ] && echo "$mnt/$APKOVL"
-		return
-	fi
-
-	# look for apkovl's on mounted media
-	set -- "$mnt"/*.apkovl.tar.gz*
-
-	if [ $# -gt 1 ] ; then
-		echo "ERROR: More than one apkovl file was found on $(basename $mnt). None will be read." >&2
-		return 1
-	fi
-	echo "$1"
-}
-
 retry_mount() {
 	# usb might need some time to settle so we retry a few times
 	for i in $(seq 0 9); do
@@ -87,7 +63,6 @@ unpack_apkovl() {
 	fi
 
 	# we need openssl. let apk handle deps
-	find_boot_repositories > /tmp/repositories
 	apk add --quiet --initdb --repositories-file /tmp/repositories openssl\
 		|| return 1
 
@@ -136,66 +111,12 @@ find_mnt() {
 	MNTOPTS=
 }
 
-# Wait for usb to settle
-wait_usb() {
-	if [ -n "$USB_DONE" ] || ! dmesg | grep '^usb-storage: waiting' >/dev/null; then
-		return 0
-	fi
-	ebegin "Waiting for USB device to settle"
-	while ! dmesg | grep 'usb-storage: device scan complete' >/dev/null; do
-		sleep 1
-	done
-	USB_DONE=yes
-	eend 0
-}
-
 #  add a boot service to $sysroot
 rc_add() {
 	mkdir -p $sysroot/etc/runlevels/$2
 	ln -sf /etc/init.d/$1 $sysroot/etc/runlevels/$2/$1
 }
 
-find_ovl_blkdev() {
-	local fsmoddir=/lib/modules/$(uname -r)/kernel/fs
-	blkid | while read line; do
-		mod=
-		UUID=
-		TYPE=
-		dev=${line%%: *}
-		eval ${line#$dev:}
-		for i in $fsmoddir/$TYPE.ko $fsmoddir/*/$TYPE.ko; do
-			[ -f $fsmoddir/*/$TYPE.ko ] && mod=$i && break
-		done
-		[ -n "$mod" ] || continue
-		mnt=/media/${UUID:-${dev##*/}}
-		mkdir -p "$mnt"
-		mount -o ro -t "$TYPE" "$dev" "$mnt" 2>/dev/null || continue
-		ovl=$(find_ovl "$mnt")
-		if [ -f "$ovl" ]; then
-			echo "$ovl"
-			break
-		fi
-		umount "$mnt"
-	done
-}
-
-# we have issues with some slow usb 1 hosts so we add 1 second delay
-# with possibility to increase delay at boot prompt with usbdelay=<sec>
-find_ovl_delayed_usb() {
-	local n i
-	# look for apkovl
-	for n in $(seq 0 ${KOPT_usbdelay:-1}); do
-		# wait for usb to settle if needed
-		wait_usb
-		ovl=$(find_ovl_blkdev)
-		if [ -f "$ovl" ]; then
-			ovl_unmount="${ovl%/*}"
-			return
-		fi
-		sleep 1
-	done
-}
-
 setup_inittab_console(){
 	while [ $# -gt 0 ]; do
 		local tty=${1%,*}
@@ -225,44 +146,6 @@ setup_inittab_console(){
 	done
 }
 
-start_raid() {
-	local n= i=
-	case "$KOPT_root" in
-		/dev/md*) n=${KOPT_root#/dev/md} ;;
-	esac
-	case "$KOPT_autoraid" in
-		[0-9]*) n="$n $(echo $KOPT_autoraid | tr ',' ' ')" ;;
-	esac
-	# if kernel can autostart the raid he will
-	for i in $n; do
-		mknod /dev/md$i b 9 $i
-		raidautorun /dev/md$i
-	done
-	# kernel cannot autostart newer versions of mdadm metadata
-	# so we also check if mdadm binary is there
-	if [ -x /sbin/mdadm ]; then
-		mdadm --assemble --scan
-	fi
-}
-
-# start cryptsetup if exists
-start_cryptsetup() {
-	[ -x /sbin/cryptsetup ] || return
-	modprobe dm-crypt
-	if [ -n "$KOPT_cryptroot" ]; then
-		modprobe dm-crypt
-		cryptsetup luksOpen $(resolve_dev "$KOPT_cryptroot") "$KOPT_cryptdm"
-	fi
-}
-
-# start lvm if exists
-start_lvm() {
-	[ -x /sbin/lvm ] || return
-	modprobe dm-mod
-	lvm vgscan --mknodes --ignorelockingfailure >/dev/null 2>&1
-	lvm vgchange --ignorelockingfailure -a y >/dev/null 2>&1
-}
-
 # determine the default interface to use if ip=dhcp is set
 # uses the first "eth" interface.
 ip_choose_if() {
@@ -332,10 +215,6 @@ configure_ip() {
 		ip_set "$device" "$client_ip" "$netmask" "$gw_ip"
 		eend $?
 	fi
-	MAC_ADDRESS=$(cat /sys/class/net/$device/address)
-	MACHINE_UUID=$(cat /sys/class/dmi/id/product_uuid)
-	OVL_DEV="${OVL_DEV/{MAC\}/$MAC_ADDRESS}"
-	OVL_DEV="${OVL_DEV/{UUID\}/$MACHINE_UUID}"
 }
 
 # resolve an uuid or symlink to the real device
@@ -346,37 +225,23 @@ resolve_dev() {
 	esac
 }
 
-# relocate ALPINE_MNT according given fstab
-relocate_alpine_mnt() {
-	local fstab="$1"
-	local mnt=$(find_mnt $ALPINE_DEV $fstab)
-	if [ -n "$mnt" ] && [ "$ALPINE_MNT" != "$mnt" ]; then
+# relocate mountpoint according given fstab
+relocate_mount() {
+	local dir="${1%%/}"
+	local fstab="$2"
+	local mnt=$(find_mnt $dir $fstab)
+	if [ -n "$mnt" ] && [ "$dir" != "$mnt" ]; then
 		mkdir -p "$mnt"
-		mount -o move $ALPINE_MNT $mnt
-		ALPINE_MNT=$mnt
+		mount -o move "$dir" "$mnt"
 	fi
 }
 
-# detect filesystem type on given device/UUID
-find_fs_type() {
-	local dev=$(findfs $1)
-	local i=
-	for i in $(blkid $dev); do
-		case $i in
-		TYPE=*) eval "$i"
-			echo $TYPE
-			return
-			;;
-		esac
-	done
-}
-
 # find the dirs under ALPINE_MNT that are boot repositories
 find_boot_repositories() {
 	if [ -n "$ALPINE_REPO" ]; then
 		echo "$ALPINE_REPO"
 	else
-		find $ALPINE_MNT -name .boot_repository -type f -maxdepth 3 \
+		find /media/* -name .boot_repository -type f -maxdepth 3 \
 			| sed 's:/.boot_repository$::'
 	fi
 }
@@ -438,63 +303,14 @@ if [ "$KOPT_dma" = no ]; then
 	modprobe libata dma=0
 fi
 
-ALPINE_DEV=${KOPT_alpine_dev%%:*}
-ALPINE_DEV_FS=${KOPT_alpine_dev##*:}
-if [ "$ALPINE_DEV_FS" = "$ALPINE_DEV" ]; then
-	unset ALPINE_DEV_FS
-fi
-
-# /dev/blah:ext3
-if [ -n "$KOPT_ovl_dev" ] ; then
-	OVL_DEV=${KOPT_ovl_dev%%:*}
-	OVL_DEV_FS=${KOPT_ovl_dev##*:}
-	if [ "$OVL_DEV_FS" = "$OVL_DEV" ]; then
-		unset OVL_DEV_FS
-	fi
-fi
-
-# http://.../blah.apkovl.tar.gz
-case "$KOPT_apkovl" in
-	http://*|https://|ftp://*)
-		OVL_DEV="$KOPT_apkovl";;
-	*:*:*)	# apkovl=sda1:ext4:/subdir/host.apkovl.tar.gz
-		OVL_DEV="${KOPT_apkovl%%:*}"
-		OVL_DEV_FS="${KOPT_apkovl%:*}"
-		OVL_DEV_FS="${OVL_DEV_FS#*:}"
-		APKOVL="${KOPT_apkovl##*:}"
-		;;
-	*:*)	# apkovl=sda1:/subdir/host.apkovl.tar.gz
-		OVL_DEV=${KOPT_apkovl%%:*}
-		APKOVL=${KOPT_apkovl##*:}
-		;;
-	*)	# apkovl=subdir/host.apkovl.tar.gz
-		APKOVL="${KOPT_apkovl}"
-		;;
-esac
-
-case "$ALPINE_DEV" in
-	UUID=*|LABEL=*) ;;
-	nfs)
-		# nfs:IP:EXPORT
-		ALPINE_DEV_FS="$ALPINE_DEV"
-		ALPINE_DEV="${KOPT_alpine_dev:4}"
-		;;
-	*) ALPINE_DEV=/dev/$ALPINE_DEV ;;
-esac
-
 # The following values are supported:
 #   alpine_repo=auto         -- default, search for .boot_repository
 #   alpine_repo=http://...   -- network repository
 ALPINE_REPO=${KOPT_alpine_repo}
 [ "$ALPINE_REPO" = "auto" ] && ALPINE_REPO=
 
-# look for standard mountpoint locations
-ALPINE_MNT=$(find_mnt $ALPINE_DEV /etc/fstab)
-[ -z "$ALPINE_MNT" ] && [ "$ALPINE_DEV_FS" = nfs ] && ALPINE_MNT=/media/alpine
-[ -z "$ALPINE_MNT" ] && ALPINE_MNT=/media/${ALPINE_DEV##*/}
-
 # hide kernel messages
-[ "$KOPT_quiet" = yes ] && dmesg -n 1
+#[ "$KOPT_quiet" = yes ] && dmesg -n 1
 
 # optional blacklist
 for i in ${KOPT_blacklist/,/ }; do
@@ -502,12 +318,8 @@ for i in ${KOPT_blacklist/,/ }; do
 done
 
 # setup /dev
-ebegin "Starting mdev"
 mount -t devtmpfs -o exec,nosuid,mode=0755,size=2M devtmpfs /dev 2>/dev/null \
 	|| mount -t tmpfs -o exec,nosuid,mode=0755,size=2M tmpfs /dev
-echo "/sbin/mdev" > /proc/sys/kernel/hotplug
-mdev -s
-RC=$?
 [ -d /dev/pts ] || mkdir -m 755 /dev/pts
 [ -c /dev/ptmx ] || mknod -m 666 /dev/ptmx c 5 2
 # make sure /dev/null is setup correctly
@@ -516,7 +328,6 @@ RC=$?
 mount -t devpts -o gid=5,mode=0620,noexec,nosuid devpts /dev/pts
 [ -d /dev/shm ] || mkdir /dev/shm
 mount -t tmpfs -o nodev,nosuid,noexec shm /dev/shm
-eend $RC
 
 # load available drivers to get access to modloop media
 ebegin "Loading boot drivers"
@@ -528,34 +339,36 @@ if [ -f /etc/modules ] ; then
 		modprobe -q $module $args
 	done
 fi
-scan_drivers
-scan_drivers
 eend 0
 
+if [ -n "$KOPT_cryptroot" ]; then
+	cryptopts="-c ${KOPT_cryptroot}"
+	if [ -n "$KOPT_cryptdm" ]; then
+		cryptopts="$cryptopts -m ${KOPT_cryptdm}"
+	fi
+fi
+
 # check if root=... was set
 if [ -n "$KOPT_root" ]; then
 	if [ "$SINGLEMODE" = "yes" ]; then
 		echo "Entering single mode. Type 'exit' to continue booting."
 		sh
 	fi
-	# let usb settle in case we boot from usb disks
-	[ -n "$KOPT_usbdelay" ] && sleep "$KOPT_usbdelay"
-	wait_usb
-	start=${KOPT_alpine_start:-raid,cryptsetup,lvm}
-	for i in ${start//,/ }; do
-		start_$i
-	done
+
 	ebegin "Mounting root"
 	if [ "$KOPT_overlaytmpfs" = "yes" ]; then
-		mkdir -p /media/root-ro /media/root-rw $sysroot/media/root-ro $sysroot/media/root-rw
-		retry_mount -o ro $KOPT_root /media/root-ro 2>/dev/null
+		mkdir -p /media/root-ro /media/root-rw $sysroot/media/root-ro \
+			$sysroot/media/root-rw
+		nlplug-findfs $cryptopts -p /sbin/mdev $KOPT_root \
+			&& mount -o ro $KOPT_root /media/root-ro
 		mount -t tmpfs root-tmpfs /media/root-rw
 		mkdir -p /media/root-rw/work /media/root-rw/root
 		mount -t overlay -o lowerdir=/media/root-ro,upperdir=/media/root-rw/root,workdir=/media/root-rw/work overlayfs $sysroot
 	else
-		retry_mount ${KOPT_rootfstype:+-t} ${KOPT_rootfstype} \
+		nlplug-findfs $cryptopts -p /sbin/mdev $KOPT_root
+		mount ${KOPT_rootfstype:+-t} ${KOPT_rootfstype} \
 			-o ${KOPT_rootflags:-ro} \
-			$KOPT_root $sysroot 2>/dev/null
+			$KOPT_root $sysroot
 	fi
 
 	eend $?
@@ -571,35 +384,11 @@ if [ -n "$KOPT_root" ]; then
 	exec /bin/busybox sh
 fi
 
-# we only want to wait for usb if really needed at this point
-if [ -z "${ALPINE_DEV##*usb*}" ]; then
-	wait_usb
-fi
-
-# IP. This shouldn't be needed if root= is set.
-configure_ip
-
-# incase we have alpine_dev on raid device...
-start=${KOPT_alpine_start:-raid,cryptsetup,lvm}
-for i in ${start//,/ }; do
-	start_$i
-done
-
 # locate boot media and mount it
 ebegin "Mounting boot media"
-mkdir -p $ALPINE_MNT
-
-# try detect the filesystem
-if [ -z "$ALPINE_DEV_FS" ]; then
-	ALPINE_DEV_FS=$(find_fs_type $ALPINE_DEV)
-fi
-
-if [ -n "$ALPINE_DEV_FS" ]; then
-	mount_opts="-t $ALPINE_DEV_FS"
-	[ "$ALPINE_DEV_FS" = "nfs" ] && mount_opts="$mount_opts -o nolock"
-fi
-
-retry_mount -o ro $mount_opts $ALPINE_DEV $ALPINE_MNT >/dev/null 2>&1
+nlplug-findfs $cryptopts -p /sbin/mdev ${KOPT_debug_init:+-d} \
+	${KOPT_usbdelay:+-t $(( $KOPT_usbdelay * 1000 ))} \
+	-b /tmp/repositories -a /tmp/apkovls
 eend $?
 
 # early console?
@@ -620,27 +409,24 @@ fi
 
 mount -t tmpfs -o $rootflags tmpfs $sysroot
 
-case "$OVL_DEV" in
+case "$KOPT_apkovl" in
 	'')
-		ovl=$(find_ovl $ALPINE_MNT)
-		if ! [ -f "$ovl" ]; then
-			find_ovl_delayed_usb
+		if [ -e /tmp/apkovls ]; then
+			ovl=$(head -n 1 /tmp/apkovls)
 		fi
 		;;
 	http://*|https://*|ftp://*)
-		ovl=/tmp/boot.apkovl.tar.gz
-		wget -O "$ovl" "$OVL_DEV" || ovl=
+		configure_ip
+
+		MAC_ADDRESS=$(cat /sys/class/net/$device/address)
+		MACHINE_UUID=$(cat /sys/class/dmi/id/product_uuid)
+		url="${KOPT_apkovl/{MAC\}/$MAC_ADDRESS}"
+		url="${url/{UUID\}/$MACHINE_UUID}"
+		ovl=/tmp/${url##*/}
+		wget -O "$ovl" "$url" || ovl=
 		;;
 	*)
-		mkdir -p /media/$OVL_DEV
-		unset mount_opts
-		if [ -n "$OVL_DEV_FS" ]; then
-			mount_opts="-t $OVL_DEV_FS"
-		fi
-
-		retry_mount -o ro $mount_opts /dev/$OVL_DEV /media/$OVL_DEV \
-			>/dev/null 2>&1
-		ovl=$(find_ovl /media/$OVL_DEV)
+		ovl="$KOPT_apkovl"
 		;;
 esac
 
@@ -660,26 +446,16 @@ if [ -f "$ovl" ]; then
 	eend $? $errstr || ovlfiles=
 	# hack, incase /root/.ssh was included in apkovl
 	[ -d "$sysroot/root" ] && chmod 700 "$sysroot/root"
-	pkgs="$pkgs $(sed 's/\#.*//' $sysroot/etc/lbu/packages.list 2>/dev/null)"
-	pkgs="$pkgs $(cat $sysroot/var/lib/apk/world \
-		$sysroot/etc/apk/world 2>/dev/null)"
-	# clean up after upgrade
-	rm -f $sysroot/etc/lbu/packages.list \
-		$sysroot/var/lib/apk/world
-
-	# fix up inittab from pre openrc times (alpine v1.8)
-	if [ -f "$sysroot"/etc/inittab ]; then
-		sed -i -e 's|:/etc/init.d/rcS|:/sbin/rc sysinit|' \
-			-e 's|:/etc/init.d/rcL|:/sbin/rc default|' \
-			-e 's|:/etc/init.d/rcK|:/sbin/rc shutdown|' \
-			"$sysroot"/etc/inittab
-	fi
+	pkgs="$pkgs $(cat $sysroot/etc/apk/world 2>/dev/null)"
 fi
+
 if [ -f "$sysroot/etc/.default_boot_services" -o ! -f "$ovl" ]; then
 	# add some boot services by default
 	rc_add devfs sysinit
 	rc_add dmesg sysinit
 	rc_add mdev sysinit
+	rc_add hwdrivers sysinit
+	rc_add modloop sysinit
 
 	rc_add hwclock boot
 	rc_add modules boot
@@ -695,17 +471,24 @@ if [ -f "$sysroot/etc/.default_boot_services" -o ! -f "$ovl" ]; then
 	rm -f "$sysroot/etc/.default_boot_services"
 fi
 
-if [ "$KOPT_splash" != "no" -a -e $ALPINE_MNT/fbsplash.ppm ]; then
+if [ "$KOPT_splash" != "no" ]; then
+	for fbsplash in /media/*/fbsplash.ppm; do
+		if [ -e "$fbsplash" ]; then
+			break;
+		fi
+	done
+fi
+
+if [ -n "$fbsplash" ] && [ -e "$fbsplash" ]; then
 	local config
-	ebegin "Starting bootsplash (from $ALPINE_MNT)"
+	ebegin "Starting bootsplash"
 	mkfifo $sysroot/$splashfile
-	if [ -e $ALPINE_MNT/fbsplash.cfg ]; then
-		config=$ALPINE_MNT/fbsplash.cfg
-	else
+	config="${fbsplash%.*}.cfg"
+	if ! [ -e "$config" ]; then
 		config=/tmp/fbsplash.cfg
 		echo "IMAGE_ALIGN=CM" > $config
 	fi
-	setsid fbsplash -T 16 -s $ALPINE_MNT/fbsplash.ppm -i $config -f $sysroot/$splashfile &
+	setsid fbsplash -T 16 -s "$fbsplash" -i $config -f $sysroot/$splashfile &
 	eend 0
 else
 	KOPT_splash="no"
@@ -723,13 +506,13 @@ if [ -f $sysroot/etc/fstab ]; then
 	# move the ALPINE_MNT if ALPINE_DEV is specified in users fstab
 	# this is so a generated /etc/apk/repositories will use correct
 	# mount dir
-	relocate_alpine_mnt "$sysroot"/etc/fstab
-fi
-
-# in case we upgrade we might need those:
-rc_add hwdrivers sysinit
-rc_add modloop sysinit
 
+	if [ -e /tmp/repositores ]; then
+		cut -d/ -f1-3 < /tmp/repositories | while read dir; do
+			relocate_mount "$dir" "$sysroot"/etc/fstab
+		done
+	fi
+fi
 
 # hack so we get openrc
 pkgs="$pkgs alpine-base"
@@ -776,11 +559,16 @@ if [ "$KOPT_keep_apk_new" != yes ]; then
 	apkflags="$apkflags --clean-protected"
 	[ -n "$ovlfiles" ] && apkflags="$apkflags --overlay-from-stdin"
 fi
+mkdir -p $sysroot/sys $sysroot/proc $sysroot/dev
+mount -o bind /sys $sysroot/sys
+mount -o bind /proc $sysroot/proc
+mount -o bind /dev $sysroot/dev
 if [ -n "$ovlfiles" ]; then
 	apk add --root $sysroot $repo_opt $apkflags $pkgs <$ovlfiles
 else
 	apk add --root $sysroot $repo_opt $apkflags $pkgs
 fi
+umount $sysroot/sys $sysroot/proc $sysroot/dev
 eend $?
 
 # unmount ovl mount if needed
@@ -788,9 +576,11 @@ if [ -n "$ovl_unmount" ]; then
 	umount $ovl_unmount 2>/dev/null
 fi
 
-# remount ALPINE_MNT according default fstab from package
-if [ -z "$has_fstab" ] && [ -f "$sysroot"/etc/fstab ]; then
-	relocate_alpine_mnt "$sysroot"/etc/fstab
+# remount according default fstab from package
+if [ -z "$has_fstab" ] && [ -f "$sysroot"/etc/fstab ] && [ -f /tmp/repositories ]; then
+	cut -d/ -f1-3 < /tmp/repositories | while read dir; do
+		relocate_mount "$dir" "$sysroot"/etc/fstab
+	done
 fi
 
 # generate repositories if none exists. this needs to be done after relocation
@@ -810,10 +600,10 @@ fi
 setup_inittab_console $CONSOLE
 
 # copy alpine release info
-if ! [ -f "$sysroot"/etc/alpine-release ] && [ -f $ALPINE_MNT/.alpine-release ]; then
-	cp $ALPINE_MNT/.alpine-release $sysroot/
-	ln -sf /.alpine-release $sysroot/etc/alpine-release
-fi
+#if ! [ -f "$sysroot"/etc/alpine-release ] && [ -f $ALPINE_MNT/.alpine-release ]; then
+#	cp $ALPINE_MNT/.alpine-release $sysroot/
+#	ln -sf /.alpine-release $sysroot/etc/alpine-release
+#fi
 
 ! [ -f "$sysroot"/etc/resolv.conf ] && [ -f /etc/resolv.conf ] && \
   cp /etc/resolv.conf "$sysroot"/etc
diff --git a/mkinitfs.conf b/mkinitfs.conf
index 35cc96a..67a5444 100644
--- a/mkinitfs.conf
+++ b/mkinitfs.conf
@@ -1,2 +1,2 @@
 # run mkinitfs -L for a list of available features
-features="ata base bootchart cdrom cramfs ext2 ext3 ext4 floppy keymap kms raid scsi usb virtio"
+features="ata base cdrom ext2 ext3 ext4 keymap kms mmc raid scsi usb virtio"
diff --git a/mkinitfs.in b/mkinitfs.in
index 04dc99c..14c728f 100755
--- a/mkinitfs.in
+++ b/mkinitfs.in
@@ -36,7 +36,7 @@ feature_files() {
 
 initfs_base() {
 	local i= dirs= glob= file=
-	for i in dev proc sys sbin bin .modloop lib/modules media/cdrom \
+	for i in dev proc sys sbin bin run .modloop lib/modules media/cdrom \
 	    etc/apk media/floppy media/usb newroot; do
 		dirs="$dirs $tmpdir/$i"
 	done
@@ -115,6 +115,11 @@ initfs_kmods() {
 	for file in $(find_kmods); do
 		echo "${file#/}"
 	done | sort -u | cpio -pdm "$tmpdir" || return 1
+	for file in modules.order modules.builtin; do
+		if [ -f "$kerneldir"/$file ]; then
+			cp "$kerneldir"/$file "$tmpdir"/lib/modules/$kernel/
+		fi
+	done
 	depmod $kernel -b "$tmpdir"
 	cd "$oldpwd"
 }
@@ -179,7 +184,7 @@ done
 shift $(( $OPTIND - 1 ))
 
 . $(readlink -f "$config")
-features_dir=${features_dir:-"$sysconfdir/features.d"}
+features_dir=${features_dir:-"${basedir%/:-}/${sysconfdir#/}/features.d"}
 [ -n "$myfeatures" ] && features="$myfeatures"
 
 if [ -n "$list_features" ]; then
diff --git a/nlplug-findfs.c b/nlplug-findfs.c
new file mode 100644
index 0000000..f1a58fd
--- /dev/null
+++ b/nlplug-findfs.c
@@ -0,0 +1,779 @@
+
+/*
+ * Copy me if you can.
+ * by 20h
+ *
+ * Copyright (c) 2015 Natanael Copa <ncopa@alpinelinux.org>
+ */
+
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
+#include <dirent.h>
+#include <err.h>
+#include <errno.h>
+#include <glob.h>
+#include <limits.h>
+#include <poll.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <sys/eventfd.h>
+#include <sys/mount.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/uio.h>
+#include <sys/wait.h>
+
+#include <linux/netlink.h>
+
+#include <libkmod.h>
+#include <blkid.h>
+
+#include "arg.h"
+
+#define DEFAULT_EVENT_TIMEOUT	250
+#define USB_STORAGE_TIMEOUT	2000
+
+#define FOUND_DEVICE	0x1
+#define FOUND_BOOTREPO	0x2
+#define FOUND_APKOVL	0x4
+
+static int dodebug;
+char *argv0;
+
+#if defined(DEBUG)
+#include <stdarg.h>
+static void dbg(const char *fmt, ...)
+{
+	va_list fmtargs;
+	if (!dodebug)
+		return;
+
+	fprintf(stderr, "%s: ", argv0);
+	va_start(fmtargs, fmt);
+	vfprintf(stderr, fmt, fmtargs);
+	va_end(fmtargs);
+	fprintf(stderr, "\n");
+}
+#else
+#define dbg(...)
+#endif
+
+struct uevent {
+	char *buf;
+	size_t bufsize;
+	char *message;
+	char *subsystem;
+	char *action;
+	char *modalias;
+	char *devname;
+	char *major;
+	char *minor;
+	char *driver;
+	char devnode[256];
+};
+
+struct ueventconf {
+	char **program_argv;
+	char *search_device;
+	char *crypt_device;
+	char *crypt_name;
+	char *subsystem_filter;
+	int modalias_count;
+	int fork_count;
+	char *bootrepos;
+	char *apkovls;
+	int timeout;
+};
+
+
+static void sighandler(int sig)
+{
+	switch(sig) {
+	case SIGHUP:
+	case SIGINT:
+	case SIGQUIT:
+	case SIGABRT:
+	case SIGTERM:
+		exit(0);
+		break;
+	default:
+		break;
+	}
+}
+
+static void initsignals(void)
+{
+	signal(SIGHUP, sighandler);
+	signal(SIGINT, sighandler);
+	signal(SIGQUIT, sighandler);
+	signal(SIGABRT, sighandler);
+	signal(SIGTERM, sighandler);
+	signal(SIGPIPE, SIG_IGN);
+}
+
+static int init_netlink_socket(void)
+{
+	struct sockaddr_nl nls;
+	int fd, slen;
+
+	memset(&nls, 0, sizeof(nls));
+	nls.nl_family = AF_NETLINK;
+	nls.nl_pid = getpid();
+	nls.nl_groups = -1;
+
+	fd = socket(PF_NETLINK, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
+		    NETLINK_KOBJECT_UEVENT);
+	if (fd < 0)
+		err(1, "socket");
+
+	/* kernel will not create events bigger than 16kb, but we need
+	   buffer up all events during coldplug */
+	slen = 512*1024;
+	if (setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &slen,
+				sizeof(slen)) < 0) {
+		err(1, "setsockopt");
+	}
+	slen = 1;
+	if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &slen,
+				sizeof(slen)) < 0) {
+		err(1, "setsockopt");
+	}
+
+	if (bind(fd, (void *)&nls, sizeof(nls)))
+		err(1, "bind");
+
+	return fd;
+}
+
+void run_child(char **argv)
+{
+	pid_t pid;
+
+	if (!(pid = fork())) {
+		dbg("running %s", argv[0]);
+		if (execv(argv[0], argv) < 0)
+			err(1, argv[0]);
+		exit(0);
+	}
+	if (pid < 0)
+		err(1,"fork");
+
+	waitpid(pid, NULL, 0);
+}
+
+
+int load_kmod(const char *modalias)
+{
+	static struct kmod_ctx *ctx = NULL;
+	struct kmod_list *list = NULL;
+	struct kmod_list *node;
+	int r, count=0;
+
+	if (ctx == NULL) {
+		dbg("initializing kmod");
+		ctx = kmod_new(NULL, NULL);
+		if (ctx == NULL)
+			return -1;
+		kmod_set_log_fn(ctx, NULL, NULL);
+		r = kmod_load_resources(ctx);
+	}
+
+	r = kmod_module_new_from_lookup(ctx, modalias, &list);
+	if (r < 0) {
+		dbg("alias '%s' lookup failure", modalias);
+		return r;
+	}
+
+	kmod_list_foreach(node, list) {
+		struct kmod_module *mod = kmod_module_get_module(node);
+		const char *fmt;
+		r = kmod_module_probe_insert_module(mod,
+						    KMOD_PROBE_APPLY_BLACKLIST,
+						    NULL, NULL, NULL, NULL);
+		if (r == 0) {
+			fmt = "module '%s' inserted";
+			count++;
+		} else if (r == KMOD_PROBE_APPLY_BLACKLIST) {
+			fmt = "module '%s' is blacklisted";
+		} else {
+			fmt = "module '%s' failed";
+		}
+		dbg(fmt, kmod_module_get_name(mod));
+		kmod_module_unref(mod);
+	}
+	kmod_module_unref_list(list);
+	return count;
+}
+
+void start_mdadm(char *devnode)
+{
+	char *mdadm_argv[] = {
+		"/sbin/mdadm",
+		"--incremental",
+		"--quiet",
+		devnode,
+		NULL
+	};
+	run_child(mdadm_argv);
+}
+
+void start_lvm2(char *devnode)
+{
+	char *lvm2_argv[] = {
+		"/sbin/lvm", "vgchange",
+		"--activate" , "ay", "--noudevsync", "--sysinit",
+		NULL
+	};
+	run_child(lvm2_argv);
+}
+
+void start_cryptsetup(char *devnode, char *cryptdm)
+{
+	char *cryptsetup_argv[] = {
+		"/sbin/cryptsetup", "luksOpen",
+		devnode, cryptdm ? cryptdm : "crypdm", NULL
+	};
+	load_kmod("dm-crypt");
+	run_child(cryptsetup_argv);
+}
+
+static int is_mounted(const char *devnode) {
+	char line[PATH_MAX];
+	FILE *f = fopen("/proc/mounts", "r");
+	int r = 0;
+	if (f == NULL)
+		return 0;
+	while (fgets(line, sizeof(line), f) != NULL) {
+		strtok(line, " ");
+		if (strcmp(devnode, line) == 0) {
+			r = 1;
+			break;
+		}
+	}
+	fclose(f);
+	return r;
+}
+
+struct recurse_opts {
+	const char *searchname;
+	void (*callback)(const char *, const void *);
+	void *userdata;
+};
+
+/* pathbuf needs hold PATH_MAX chars */
+void recurse_dir(char *pathbuf, struct recurse_opts *opts)
+{
+	DIR *d = opendir(pathbuf);
+	struct dirent *entry;
+
+	if (d == NULL)
+		return;
+
+	while ((entry = readdir(d)) != NULL) {
+		size_t pathlen = strlen(pathbuf);
+		size_t namelen = strlen(entry->d_name);
+		int is_dir;
+
+		/* d_type is not supported by all filesystems so we need
+		   lstat */
+		if (pathlen + 2 + namelen > PATH_MAX) {
+			dbg("path length overflow");
+			continue;
+		}
+
+		pathbuf[pathlen] = '/';
+		strcpy(&pathbuf[pathlen+1], entry->d_name);
+
+		if (entry->d_type == DT_UNKNOWN) {
+			/* some filesystems like iso9660 does not support
+			   the d_type so we use lstat */
+			struct stat st;
+			if (lstat(pathbuf, &st) < 0) {
+				dbg("%s: %s", pathbuf, strerror(errno));
+				goto next;
+			}
+			is_dir = S_ISDIR(st.st_mode);
+		} else
+			is_dir = entry->d_type & DT_DIR;
+
+		if (is_dir) {
+			if (entry->d_name[0] == '.')
+				goto next;
+		} else if (opts->searchname
+			   && strcmp(entry->d_name, opts->searchname) != 0) {
+			goto next;
+		}
+
+		if (is_dir)
+			recurse_dir(pathbuf, opts);
+		else
+			opts->callback(pathbuf, opts->userdata);
+next:
+		pathbuf[pathlen] = '\0';
+	}
+	closedir(d);
+}
+
+struct bootrepos {
+	char *outfile;
+	int count;
+};
+
+void bootrepo_cb(const char *path, const void *data)
+{
+	struct bootrepos *repos = (struct bootrepos *)data;
+	int fd = open(repos->outfile, O_WRONLY | O_CREAT | O_APPEND);
+	if (fd == -1)
+		err(1, "%s", repos->outfile);
+
+	write(fd, path, strlen(path) - strlen("/.boot_repository"));
+	write(fd, "\n", 1);
+	close(fd);
+	dbg("added boot repository %s to %s\n", path, repos->outfile);
+	repos->count++;
+}
+
+static int find_apkovl(const char *dir, const char *outfile)
+{
+	char pattern[PATH_MAX];
+	glob_t gl;
+	int r, fd;
+
+	if (outfile == NULL)
+		return 0;
+
+	snprintf(pattern, sizeof(pattern), "%s/*.apkovl.tar.gz*", dir);
+
+	r = glob(pattern, 0, NULL, &gl);
+	if (r != 0)
+		return 0;
+
+	fd = open(outfile, O_WRONLY | O_CREAT | O_APPEND);
+	if (fd == -1)
+		err(1, "%s", outfile);
+
+	for (r = 0; r < gl.gl_pathc; r++) {
+		dbg("Found apkovl: %s", gl.gl_pathv[r]);
+		write(fd, gl.gl_pathv[r], strlen(gl.gl_pathv[r]));
+		write(fd, "\n", 1);
+	}
+	close(fd);
+	globfree(&gl);
+	return FOUND_APKOVL;
+}
+
+static int find_bootrepos(const char *devnode, const char *type,
+			 char *bootrepos, const char *apkovls)
+{
+	char mountdir[PATH_MAX] = "";
+	char *devname;
+	int r, rc = 0;
+	struct bootrepos repos = {
+		.outfile = bootrepos,
+		.count = 0,
+	};
+	struct recurse_opts opts = {
+		.searchname = ".boot_repository",
+		.callback = bootrepo_cb,
+		.userdata = &repos,
+	};
+
+
+	/* skip already mounted devices */
+	if (is_mounted(devnode)) {
+		dbg("%s is mounted (%s). skipping", devnode, type);
+		return 0;
+	}
+	devname = strrchr(devnode, '/');
+
+	if (devname)
+		snprintf(mountdir, sizeof(mountdir), "/media%s", devname);
+
+	dbg("mounting %s on %s. (%s)", devnode, mountdir, type);
+	mkdir(mountdir, 0755);
+
+	r = mount(devnode, mountdir, type, MS_RDONLY, NULL);
+	if (r < 0) {
+		dbg("Failed to mount %s on %s: %s",
+		    devnode, mountdir, strerror(errno));
+		return 0;
+	}
+
+	recurse_dir(mountdir, &opts);
+	if (repos.count > 0)
+		rc |= FOUND_BOOTREPO;
+
+	if (find_apkovl(mountdir, apkovls))
+		rc |= FOUND_APKOVL;
+
+	if (rc == 0)
+		umount(mountdir);
+
+	return rc;
+}
+
+int searchdev(char *devname, const char *searchdev, char *bootrepos,
+	      const char *apkovls)
+{
+	static blkid_cache cache = NULL;
+	char *type = NULL, *label = NULL, *uuid = NULL;
+	char devnode[256];
+	int rc = 0;
+
+	if (searchdev == NULL && bootrepos == NULL && apkovls == NULL)
+		return 0;
+
+	snprintf(devnode, sizeof(devnode), "/dev/%s", devname);
+	if (searchdev && (strcmp(devname, searchdev) == 0
+	                  || strcmp(devnode, searchdev) == 0)) {
+		return FOUND_DEVICE;
+	}
+
+	if (cache == NULL)
+		blkid_get_cache(&cache, NULL);
+
+	type = blkid_get_tag_value(cache, "TYPE", devnode);
+
+	if (searchdev != NULL) {
+		if (strncmp("LABEL=", searchdev, 6) == 0) {
+			label = blkid_get_tag_value(cache, "LABEL", devnode);
+			if (label && strcmp(label, searchdev+6) == 0)
+				rc = FOUND_DEVICE;
+		} else if (strncmp("UUID=", searchdev, 5) == 0) {
+			uuid = blkid_get_tag_value(cache, "UUID", devnode);
+			if (uuid && strcmp(uuid, searchdev+5) == 0)
+				rc = FOUND_DEVICE;
+		}
+	}
+
+	if (type || label || uuid) {
+		dbg("%s:\n"
+			"\ttype='%s'\n"
+			"\tlabel='%s'\n"
+			"\tuuid='%s'\n", devnode,
+			type ? type : NULL,
+			label ? label : NULL,
+			uuid ? uuid : NULL);
+	}
+
+	if (!rc && type) {
+		if (strcmp("linux_raid_member", type) == 0) {
+			start_mdadm(devnode);
+		} else if (strcmp("LVM2_member", type) == 0) {
+			start_lvm2(devnode);
+		} else if (bootrepos) {
+			rc = find_bootrepos(devnode, type, bootrepos, apkovls);
+		}
+	}
+
+	if (type)
+		free(type);
+	if (label)
+		free(label);
+	if (uuid)
+		free(uuid);
+
+	return rc;
+}
+
+int dispatch_uevent(struct uevent *ev, struct ueventconf *conf)
+{
+	static int timeout_increment = USB_STORAGE_TIMEOUT;
+
+	if (conf->subsystem_filter && ev->subsystem
+	    && strcmp(ev->subsystem, conf->subsystem_filter) != 0) {
+		dbg("subsystem '%s' filtered out (by '%s').",
+		    ev->subsystem, conf->subsystem_filter);
+		return 0;
+	}
+
+	if (ev->action == NULL)
+		return 0;
+
+	if (ev->modalias != NULL && strcmp(ev->action, "add") == 0) {
+		load_kmod(ev->modalias);
+		conf->modalias_count++;
+
+	} else if (ev->driver != NULL && strcmp(ev->driver, "usb-storage") == 0) {
+		conf->timeout += timeout_increment;
+		timeout_increment = 0;
+
+	} else if (ev->devname != NULL) {
+		if (conf->program_argv[0] != NULL) {
+			run_child(conf->program_argv);
+			conf->fork_count++;
+		}
+
+		if (ev->subsystem && strcmp(ev->subsystem, "block") == 0
+		    && strcmp(ev->action, "add") == 0) {
+			int rc;
+
+			snprintf(ev->devnode, sizeof(ev->devnode), "/dev/%s",
+				 ev->devname);
+			rc = searchdev(ev->devname, conf->search_device,
+				       conf->bootrepos, conf->apkovls);
+			if (rc)
+				return rc;
+
+			if (searchdev(ev->devname, conf->crypt_device, NULL,
+				      NULL))
+				start_cryptsetup(ev->devnode, conf->crypt_name);
+		}
+	}
+	return 0;
+}
+
+int process_uevent(char *buf, const size_t len, struct ueventconf *conf)
+{
+	struct uevent ev;
+
+	int i, slen = 0;
+	char *key, *value;
+
+	memset(&ev, 0, sizeof(ev));
+	ev.buf = buf;
+	ev.bufsize = len;
+	clearenv();
+	setenv("PATH", "/sbin:/bin", 1);
+
+	for (i = 0; i < len; i += slen + 1) {
+
+		key = buf + i;
+		value = strchr(key, '=');
+		slen = strlen(buf+i);
+
+		if (i == 0 && slen != 0) {
+			/* first line, the message */
+			ev.message = key;
+			continue;
+		}
+
+		if (!slen)
+			continue;
+
+		value[0] = '\0';
+		value++;
+
+		if (strcmp(key, "MODALIAS") == 0) {
+			ev.modalias = value;
+		} else if (strcmp(key, "ACTION") == 0) {
+			ev.action = value;
+		} else if (strcmp(key, "SUBSYSTEM") == 0) {
+			ev.subsystem = value;
+		} else if (strcmp(key, "DEVNAME") == 0) {
+			ev.devname = value;
+		} else if (strcmp(key, "MAJOR") == 0) {
+			ev.major = value;
+		} else if (strcmp(key, "MINOR") == 0) {
+			ev.minor = value;
+		} else if (strcmp(key, "DRIVER") == 0) {
+			ev.driver = value;
+		}
+
+		if (strcmp(key, "PATH")) {
+			setenv(key, value, 1);
+		}
+	}
+	return dispatch_uevent(&ev, conf);
+}
+
+void trigger_uevent_cb(const char *path, const void *data)
+{
+	int fd = open(path, O_WRONLY);
+	write(fd, "add", 3);
+	close(fd);
+}
+
+void *trigger_thread(void *data)
+{
+	int fd = *(int *)data;
+	uint64_t ok = 1;
+	struct recurse_opts opts = {
+		.searchname = "uevent",
+		.callback = trigger_uevent_cb,
+		.userdata = NULL,
+	};
+	char path[PATH_MAX] = "/sys/bus";
+
+	recurse_dir(path, &opts);
+	strcpy(path, "/sys/devices");
+	recurse_dir(path, &opts);
+	write(fd, &ok, sizeof(ok));
+	return NULL;
+}
+
+void usage(int rc)
+{
+	printf("coldplug system til given device is found\n"
+	"usage: %s [options] DEVICE\n"
+	"\n"
+	"options:\n"
+	" -a OUTFILE      add paths to found apkovls to OUTFILE\n"
+	" -b OUTFILE      add found boot repositories to OUTFILE\n"
+	" -c CRYPTDEVICE  run cryptsetup luksOpen when CRYPTDEVICE is found\n"
+	" -h              show this help\n"
+	" -m CRYPTNAME    use CRYPTNAME name for crypto device mapping\n"
+	" -d              enable debugging ouput\n"
+	" -f SUBSYSTEM    filter subsystem\n"
+	" -p PROGRAM      use PROGRAM as handler for every event with DEVNAME\n"
+	" -t TIMEOUT      timeout after TIMEOUT milliseconds without uevents\n"
+	"\n", argv0);
+
+	exit(rc);
+}
+
+int main(int argc, char *argv[])
+{
+	struct pollfd fds[2];
+	int numfds = 2;
+	int r;
+	struct ueventconf conf;
+	int event_count = 0;
+	size_t total_bytes;
+	int found = 0, trigger_running = 0;
+	char *program_argv[2] = {0,0};
+	pthread_t tid;
+
+	memset(&conf, 0, sizeof(conf));
+	conf.program_argv = program_argv;
+	conf.timeout = DEFAULT_EVENT_TIMEOUT;
+	argv0 = strrchr(argv[0], '/');
+	if (argv0++ == NULL)
+		argv0 = argv[0];
+
+	ARGBEGIN {
+	case 'a':
+		conf.apkovls = EARGF(usage(1));;
+		break;
+	case 'b':
+		conf.bootrepos = EARGF(usage(1));
+		break;
+	case 'c':
+		conf.crypt_device = EARGF(usage(1));
+		break;
+	case 'h':
+		usage(0);
+		break;
+	case 'm':
+		conf.crypt_name = EARGF(usage(1));
+		break;
+	case 'd':
+		dodebug = 1;
+		break;
+	case 'f':
+		conf.subsystem_filter = EARGF(usage(1));
+		break;
+	case 'p':
+		conf.program_argv[0] = EARGF(usage(1));
+		break;
+	case 't':
+		conf.timeout = atoi(EARGF(usage(1)));
+		break;
+	default:
+		usage(1);
+	} ARGEND;
+
+	if (argc > 0)
+		conf.search_device = argv[0];
+
+	initsignals();
+
+	fds[0].fd = init_netlink_socket();
+	fds[0].events = POLLIN;
+
+	fds[1].fd = eventfd(0, EFD_CLOEXEC);
+	fds[1].events = POLLIN;
+
+	pthread_create(&tid, NULL, trigger_thread, &fds[1].fd);
+	trigger_running = 1;
+
+	while (1) {
+		size_t len;
+		struct iovec iov;
+		char cbuf[CMSG_SPACE(sizeof(struct ucred))];
+		char buf[16384];
+		struct cmsghdr *chdr;
+		struct ucred *cred;
+		struct msghdr hdr;
+		struct sockaddr_nl cnls;
+
+		r = poll(fds, numfds, trigger_running ? -1 : conf.timeout);
+		if (r == -1)
+			err(1, "poll");
+
+		if (r == 0) {
+			dbg("exit due to timeout");
+			break;
+		}
+
+		if (numfds > 1 && fds[1].revents & POLLIN) {
+			close(fds[1].fd);
+			fds[1].fd = -1;
+			numfds--;
+			trigger_running = 0;
+			pthread_join(tid, NULL);
+		}
+
+		if (!(fds[0].revents & POLLIN))
+			continue;
+
+		iov.iov_base = &buf;
+		iov.iov_len = sizeof(buf);
+		memset(&hdr, 0, sizeof(hdr));
+		hdr.msg_iov = &iov;
+		hdr.msg_iovlen = 1;
+		hdr.msg_control = cbuf;
+		hdr.msg_controllen = sizeof(cbuf);
+		hdr.msg_name = &cnls;
+		hdr.msg_namelen = sizeof(cnls);
+
+		len = recvmsg(fds[0].fd, &hdr, 0);
+		if (len < 0) {
+			if (errno == EINTR)
+				continue;
+			err(1, "recvmsg");
+		}
+		if (len < 32 || len >= sizeof(buf))
+			continue;
+
+		total_bytes += len;
+		chdr = CMSG_FIRSTHDR(&hdr);
+		if (chdr == NULL || chdr->cmsg_type != SCM_CREDENTIALS)
+			continue;
+
+		/* filter out messages that are not from root or kernel */
+		cred = (struct ucred *)CMSG_DATA(chdr);
+		if (cred->uid != 0 || cnls.nl_pid > 0)
+			continue;
+
+		event_count++;
+		found |= process_uevent(buf, len, &conf);
+
+		if ((found & FOUND_DEVICE)
+		    || ((found & FOUND_BOOTREPO) && (found & FOUND_APKOVL))) {
+			dbg("setting timeout to 0");
+			conf.timeout = 0;
+		}
+
+		if (fds[0].revents & POLLHUP) {
+			dbg("parent hung up\n");
+			break;
+		}
+	}
+
+	dbg("modaliases: %i, forks: %i, events: %i, total bufsize: %zu",
+		conf.modalias_count,
+		conf.fork_count,
+		event_count, total_bytes);
+
+	return found ? 0 : 1;
+}
+
+