changelog tags changeset manifest revisions annotate raw

disks_detect.rb

changeset 28: e5d4b97d6db5
parent:3ebbab371962
author: frsyuki@vcore
date: Tue Mar 27 00:12:24 2007 +0900 (14 years ago)
permissions: -rw-r--r--
1require "log"
2
3
4class DiskManager
5 FSMODULES = ["ntfs", "vfat", "nls_cp437", "nls_iso8859_1", "ext3", "hfsplus", "xfs", "reiserfs", "reiser4", "hfs", "squashfs", "hfs", "ufs", "jfs", "ext2", "iso9660"]
6
7 def loadFilesystemModules
8 @modules_loaded == true && return
9 FSMODULES.each {|m|
10 @kmodule.load(m)
11 }
12 @modules_loaded = true
13 end
14 private :loadFilesystemModules
15
16
17 def detectIMPL(disk_dir, disk_name)
18 # disk_dirは保持しないので、DynamicPathでなくて良い
19
20 if disk_name =~ /^\.|^loop|^ram|^fd|^nbd/
21 # dot-fileとループバックとRAMディスクとFDとnbdはスキップ
22 return
23 end
24
25 major = 0
26 minor = 0
27 size = 0
28 removable = false
29 partitions = Array.new
30
31 File.open( disk_dir + "dev" ) {|file|
32 major, minor = file.gets.split(":", 2).map! { |s| s.to_i }
33 }
34 if major == 0 # majro == 0 は読み取り失敗
35 raise "read error - #{disk_dir}dev"
36 end
37
38 ## XXX: マウントされていないと全部0になる?
39 #File.open( disk_dir + "stat") {|file|
40 # if file.gets.split.delete_if {|x| x == "0" }.empty?
41 # # statの中身が全部0なのは、メディアが入っていない光学ドライブやカードリーダ
42 # raise "No medium inserted (optical/card)"
43 # end
44 #}
45
46 File.open( disk_dir + "size") {|file|
47 size = file.gets.to_i
48 }
49 if size == 0
50 # size == 0はメディアが入っていないscsi系ドライバ制御のデバイス
51 raise "No medium inserted (scsi/sata)"
52 end
53
54 File.open( disk_dir + "removable" ) {|file|
55 if file.read(1) == "1"
56 removable = true
57 end
58 }
59
60 for i in 1..255
61 File.open( disk_dir + disk_name + i.to_s ) {|file|
62 partitions.push(disk_name + i.to_s)
63 } rescue break # 開けなかったら終わり
64 end
65
66
67 if !partitions.empty?
68 # パーティションを含んでいる BlockEntryに加えるのは個々のパーティション
69 partitions.each {|part_name|
70 part_dir = disk_dir + part_name + "/"
71
72 part_major = 0
73 part_minor = 0
74 part_size = 0
75
76 File.open( part_dir + "dev" ) {|file|
77 part_major, part_minor = file.gets.split(":", 2).map! { |s| s.to_i }
78 }
79 if part_major == 0 # 読み取り失敗
80 raise "read error - #{part_dir}dev"
81 end
82
83 File.open( part_dir + "size") {|file|
84 part_size = file.gets.to_i
85 }
86 if part_size == 0 # 読み取り失敗
87 raise "read error - #{part_dir}size"
88 end
89
90 type = guessPartition(disk_name, size, part_name, part_size, removable)
91 if type.include?("extend_partition")
92 # block_entriesに入れない
93 $log.debug "found extend partition: #{part_name} (#{type.join(",")})"
94 else
95 @block_entries.push( BlockEntry.new(part_major, part_minor, part_size, part_name, removable, type) )
96 $log.debug "found partition: #{part_name} (#{type.join(",")})"
97 end
98 }
99 else
100 # パーティションを含んでいない
101 # 光学ドライブかDeviceMapper(dm-*)
102 type = guessDisk(disk_name, size, removable)
103 @block_entries.push( BlockEntry.new(major, minor, size, disk_name, removable, type) )
104 $log.debug "found disk: #{disk_name} (#{type.join(",")})"
105 end
106 end
107 private :detectIMPL
108
109
110
111 def detect(force = false)
112 if force == false && @detected == true
113 return
114 end
115
116 loadFilesystemModules
117
118 Dir.open(@sysfs.block) {|blocks|
119 @block_entries.clear
120 blocks.each {|disk_name|
121 begin
122 disk_dir_cache = @sysfs.block.to_s + "/#{disk_name}/"
123 detectIMPL(disk_dir_cache, disk_name)
124 rescue
125 $log.debug "ignored volume #{disk_name}: #{$!}"
126 end
127 }
128 }
129 probe_threads = Array.new
130 @block_entries.each {|b|
131 $log.debug0 "start filesystem type probing thread for #{b.name}"
132 t = Thread.new {
133 b.probeFSType
134 }
135 # XXX: 例外は投げない
136 probe_threads.push(t)
137 }
138 probe_threads.each { |t| t.join }
139
140 @detected = true
141 end
142
143end
144
145
146
147$log.debug "#{File.basename(__FILE__, ".*")} loaded"