52c55a68df5aa60819c5a00eb7c84754c2543dbb
[zfs.git] / dracut / 90zfs / mount-zfs.sh
1 #!/bin/sh
2
3 . /lib/dracut-lib.sh
4
5 ZPOOL_FORCE=""
6
7 if getargbool 0 zfs_force -y zfs.force -y zfsforce ; then
8         warn "ZFS: Will force-import pools if necessary."
9         ZPOOL_FORCE="-f"
10 fi
11
12 case "$root" in
13         zfs:*)
14                 # We have ZFS modules loaded, so we're able to import pools now.
15                 if [ "$root" = "zfs:AUTO" ] ; then
16                         # Need to parse bootfs attribute
17                         info "ZFS: Attempting to detect root from imported ZFS pools."
18
19                         # Might be imported by the kernel module, so try searching before
20                         # we import anything.
21                         zfsbootfs=`zpool list -H -o bootfs | sed 'q'`
22                         if [ "$?" != "0" ] || [ "$zfsbootfs" = "" ] || [ "$zfsbootfs" = "no pools available" ] ; then
23                                 # Not there, so we need to import everything.
24                                 info "ZFS: Attempting to import additional pools."
25                                 zpool import -N -a ${ZPOOL_FORCE}
26                                 zfsbootfs=`zpool list -H -o bootfs | sed 'q'`
27                                 if [ "$?" != "0" ] || [ "$zfsbootfs" = "" ] || [ "$zfsbootfs" = "no pools available" ] ; then
28                                         rootok=0
29                                         pool=""
30
31                                         warn "ZFS: No bootfs attribute found in importable pools."
32
33                                         # Re-export everything since we're not prepared to take
34                                         # responsibility for them.
35                                         zpool list -H | while read fs rest ; do
36                                                 zpool export "$fs"
37                                         done
38
39                                         return 1
40                                 fi
41                         fi
42                         info "ZFS: Using ${zfsbootfs} as root."
43                 else
44                         # Should have an explicit pool set, so just import it and we're done.
45                         zfsbootfs="${root#zfs:}"
46                         pool="${zfsbootfs%%/*}"
47                         if ! zpool list -H $pool > /dev/null ; then
48                                 # pool wasn't imported automatically by the kernel module, so
49                                 # try it manually.
50                                 info "ZFS: Importing pool ${pool}..."
51                                 if ! zpool import -N ${ZPOOL_FORCE} $pool ; then
52                                         warn "ZFS: Unable to import pool ${pool}."
53                                         rootok=0
54
55                                         return 1
56                                 fi
57                         fi
58                 fi
59
60                 # Above should have left our rpool imported and pool/dataset in $root.
61                 # We need zfsutil for non-legacy mounts and not for legacy mounts.
62                 mountpoint=`zfs get -H -o value mountpoint $zfsbootfs`
63                 if [ "$mountpoint" = "legacy" ] ; then
64                         mount -t zfs "$zfsbootfs" "$NEWROOT" && ROOTFS_MOUNTED=yes
65                 else
66                         mount -o zfsutil -t zfs "$zfsbootfs" "$NEWROOT" && ROOTFS_MOUNTED=yes
67                 fi
68                 ;;
69 esac